Skip to content

Commit 8b37967

Browse files
committed
fixes
1 parent 058f2a9 commit 8b37967

4 files changed

Lines changed: 51 additions & 20 deletions

File tree

src/NosCore.Networking/BroadcastableExtension.cs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,33 @@ public static async Task SendPacketsAsync(this IBroadcastable channelGroup, IEnu
5151
ISessionMatcher? matcher)
5252
{
5353
var packetDefinitions = (packets as IPacket[] ?? packets).Where(c => c != null).ToArray();
54-
if (packetDefinitions.Any())
54+
if (packetDefinitions.Length == 0)
5555
{
56-
Parallel.ForEach(packets, packet => channelGroup.LastPackets.Enqueue(packet));
57-
Parallel.For(0, channelGroup.LastPackets.Count - channelGroup.MaxPacketsBuffer, (_, __) => channelGroup.LastPackets.TryDequeue(out var ___));
58-
if (channelGroup.Sessions == null!)
59-
{
60-
return;
61-
}
56+
return;
57+
}
58+
59+
foreach (var packet in packetDefinitions)
60+
{
61+
channelGroup.LastPackets.Enqueue(packet);
62+
}
63+
64+
while (channelGroup.LastPackets.Count > channelGroup.MaxPacketsBuffer)
65+
{
66+
channelGroup.LastPackets.TryDequeue(out _);
67+
}
6268

63-
if (matcher == null)
64-
{
65-
await channelGroup.Sessions.Broadcast(packetDefinitions).ConfigureAwait(false);
66-
}
67-
else
68-
{
69-
await channelGroup.Sessions.Broadcast(packetDefinitions, matcher).ConfigureAwait(false);
70-
}
69+
if (channelGroup.Sessions == null!)
70+
{
71+
return;
72+
}
73+
74+
if (matcher == null)
75+
{
76+
await channelGroup.Sessions.Broadcast(packetDefinitions).ConfigureAwait(false);
77+
}
78+
else
79+
{
80+
await channelGroup.Sessions.Broadcast(packetDefinitions, matcher).ConfigureAwait(false);
7181
}
7282
}
7383

src/NosCore.Networking/NetworkClient.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,15 @@ public async Task SendPacketsAsync(IEnumerable<IPacket?> packets)
140140
return;
141141
}
142142

143-
var encoded = _encoder.Encode(SessionKey, packetList);
144-
await _channel.SendAsync(new ReadOnlyMemory<byte>(encoded));
143+
try
144+
{
145+
var encoded = _encoder.Encode(SessionKey, packetList);
146+
await _channel.SendAsync(new ReadOnlyMemory<byte>(encoded));
147+
}
148+
catch (Exception ex)
149+
{
150+
_logger.Warning(ex, _logLanguage[LogLanguageKey.ENCODE_ERROR], SessionId);
151+
}
145152
}
146153
}
147154
}

src/NosCore.Networking/PipelineFactory.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
using System;
88
using System.Collections.Concurrent;
99
using System.Collections.Generic;
10+
using Microsoft.Extensions.Logging;
1011
using NosCore.Networking.Encoding;
1112
using NosCore.Networking.Encoding.Filter;
1213
using NosCore.Networking.Filters;
14+
using NosCore.Networking.Resource;
1315
using NosCore.Networking.SessionRef;
1416
using NosCore.Shared.Enumerations;
17+
using NosCore.Shared.I18N;
1518
using SuperSocket.ProtoBase;
1619
using SuperSocket.Server.Abstractions.Session;
1720

@@ -29,6 +32,8 @@ public class PipelineFactory : IPipelineFilterFactory<NosPackageInfo>
2932
private readonly Func<INetworkClient> _clientFactory;
3033
private readonly Action<NosPackageInfo, INetworkClient> _packetHandler;
3134
private readonly Action<INetworkClient>? _disconnectHandler;
35+
private readonly ILogger<PipelineFactory>? _logger;
36+
private readonly ILogLanguageLocalizer<LogLanguageKey>? _logLanguage;
3237

3338
private readonly ConcurrentDictionary<object, PipelineFilter> _filtersByConnection = new();
3439
private readonly ConcurrentDictionary<string, INetworkClient> _clientsBySession = new();
@@ -43,10 +48,13 @@ public class PipelineFactory : IPipelineFilterFactory<NosPackageInfo>
4348
/// <param name="clientFactory">Factory function to create network client instances.</param>
4449
/// <param name="packetHandler">Handler for processing received packets.</param>
4550
/// <param name="disconnectHandler">Handler for processing client disconnections.</param>
51+
/// <param name="logger">The logger instance.</param>
52+
/// <param name="logLanguage">The localized log language provider.</param>
4653
public PipelineFactory(IDecoder decoder, ISessionRefHolder sessionRefHolder,
4754
IEnumerable<IRequestFilter> requestFilters, IPipelineConfiguration pipelineConfiguration,
4855
Func<INetworkClient> clientFactory, Action<NosPackageInfo, INetworkClient> packetHandler,
49-
Action<INetworkClient>? disconnectHandler = null)
56+
Action<INetworkClient>? disconnectHandler = null,
57+
ILogger<PipelineFactory>? logger = null, ILogLanguageLocalizer<LogLanguageKey>? logLanguage = null)
5058
{
5159
_decoder = decoder;
5260
_sessionRefHolder = sessionRefHolder;
@@ -55,6 +63,8 @@ public PipelineFactory(IDecoder decoder, ISessionRefHolder sessionRefHolder,
5563
_clientFactory = clientFactory;
5664
_packetHandler = packetHandler;
5765
_disconnectHandler = disconnectHandler;
66+
_logger = logger;
67+
_logLanguage = logLanguage;
5868
}
5969

6070
/// <summary>
@@ -115,6 +125,10 @@ public void HandlePackage(IAppSession session, NosPackageInfo package)
115125
{
116126
_packetHandler(package, client);
117127
}
128+
else if (_logger != null && _logLanguage != null)
129+
{
130+
_logger.LogWarning(_logLanguage[LogLanguageKey.ERROR_SESSIONID], session.SessionID);
131+
}
118132
}
119133
}
120134
}

src/NosCore.Networking/SessionRef/SessionRefHolder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// -----------------------------------
66

77
using System.Collections.Concurrent;
8+
using System.Threading;
89

910
namespace NosCore.Networking.SessionRef;
1011

@@ -21,8 +22,7 @@ public class SessionRefHolder : ConcurrentDictionary<string, RegionTypeMapping>,
2122
/// <returns>A new session identifier incremented by 2.</returns>
2223
public int GenerateSessionId()
2324
{
24-
_sessionCounter += 2;
25-
return _sessionCounter;
25+
return Interlocked.Add(ref _sessionCounter, 2);
2626
}
2727

2828
/// <summary>

0 commit comments

Comments
 (0)