Skip to content

Commit 058f2a9

Browse files
committed
migrate to supersocket
1 parent cff65cb commit 058f2a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+873
-439
lines changed

NosCore.Networking.sln.DotSettings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
-----------------------------------</s:String>
99
<s:Boolean x:Key="/Default/CodeStyle/Generate/=Implementations/@KeyIndexDefined">True</s:Boolean>
1010
<s:String x:Key="/Default/CodeStyle/Generate/=Implementations/Options/=Async/@EntryIndexedValue">False</s:String>
11-
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpAutoNaming/IsNotificationDisabled/@EntryValue">True</s:Boolean></wpf:ResourceDictionary>
11+
<s:Boolean x:Key="/Default/CodeStyle/Naming/CSharpAutoNaming/IsNotificationDisabled/@EntryValue">True</s:Boolean>
12+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EFeature_002EServices_002ECodeCleanup_002EFileHeader_002EFileHeaderSettingsMigrate/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/NosCore.Networking/BroadcastableExtension.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Collections.Generic;
88
using System.Linq;
99
using System.Threading.Tasks;
10-
using DotNetty.Transport.Channels.Groups;
1110
using NosCore.Networking.SessionGroup;
1211
using NosCore.Packets.Interfaces;
1312

@@ -34,9 +33,9 @@ public static Task SendPacketAsync(this IBroadcastable channelGroup, IPacket pac
3433
/// </summary>
3534
/// <param name="channelGroup">The broadcastable group to send the packet to.</param>
3635
/// <param name="packet">The packet to send.</param>
37-
/// <param name="matcher">The channel matcher to filter recipients.</param>
36+
/// <param name="matcher">The session matcher to filter recipients.</param>
3837
/// <returns>A task representing the asynchronous send operation.</returns>
39-
public static Task SendPacketAsync(this IBroadcastable channelGroup, IPacket packet, IChannelMatcher matcher)
38+
public static Task SendPacketAsync(this IBroadcastable channelGroup, IPacket packet, ISessionMatcher matcher)
4039
{
4140
return channelGroup.SendPacketsAsync(new[] { packet }, matcher);
4241
}
@@ -46,10 +45,10 @@ public static Task SendPacketAsync(this IBroadcastable channelGroup, IPacket pac
4645
/// </summary>
4746
/// <param name="channelGroup">The broadcastable group to send the packets to.</param>
4847
/// <param name="packets">The collection of packets to send.</param>
49-
/// <param name="matcher">The optional channel matcher to filter recipients.</param>
48+
/// <param name="matcher">The optional session matcher to filter recipients.</param>
5049
/// <returns>A task representing the asynchronous send operation.</returns>
5150
public static async Task SendPacketsAsync(this IBroadcastable channelGroup, IEnumerable<IPacket> packets,
52-
IChannelMatcher? matcher)
51+
ISessionMatcher? matcher)
5352
{
5453
var packetDefinitions = (packets as IPacket[] ?? packets).Where(c => c != null).ToArray();
5554
if (packetDefinitions.Any())
Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,25 @@
1-
// __ _ __ __ ___ __ ___ ___
1+
// __ _ __ __ ___ __ ___ ___
22
// | \| |/__\ /' _/ / _//__\| _ \ __|
33
// | | ' | \/ |`._`.| \_| \/ | v / _|
44
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
55
// -----------------------------------
66

77
using System;
8-
using System.Collections.Generic;
9-
using DotNetty.Buffers;
10-
using DotNetty.Codecs;
11-
using DotNetty.Transport.Channels;
8+
using System.Net;
129

1310
namespace NosCore.Networking.Encoding.Filter
1411
{
1512
/// <summary>
16-
/// Abstract base class for request filters that process incoming byte data.
13+
/// Defines a request filter that processes incoming byte data.
1714
/// </summary>
18-
public abstract class RequestFilter : MessageToMessageDecoder<IByteBuffer>
15+
public interface IRequestFilter
1916
{
2017
/// <summary>
2118
/// Filters incoming request data.
2219
/// </summary>
23-
/// <param name="context">The channel handler context.</param>
20+
/// <param name="remoteEndPoint">The remote endpoint of the connection.</param>
2421
/// <param name="message">The incoming message bytes.</param>
2522
/// <returns>The filtered byte array, or null if the request should be blocked.</returns>
26-
public abstract byte[]? Filter(IChannelHandlerContext context, Span<byte> message);
27-
28-
/// <summary>
29-
/// Decodes the incoming byte buffer through the filter.
30-
/// </summary>
31-
/// <param name="context">The channel handler context.</param>
32-
/// <param name="message">The byte buffer to decode.</param>
33-
/// <param name="output">The output list to add filtered results to.</param>
34-
protected override void Decode(IChannelHandlerContext context, IByteBuffer message, List<object> output)
35-
{
36-
var result = Filter(context, ((Span<byte>)message.Array).Slice(message.ArrayOffset, message.ReadableBytes));
37-
if (result != null)
38-
{
39-
output.Add(Unpooled.WrappedBuffer(result));
40-
}
41-
}
23+
byte[]? Filter(EndPoint remoteEndPoint, Span<byte> message);
4224
}
4325
}

src/NosCore.Networking/Encoding/Filter/SpamRequestFilter.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// __ _ __ __ ___ __ ___ ___
1+
// __ _ __ __ ___ __ ___ ___
22
// | \| |/__\ /' _/ / _//__\| _ \ __|
33
// | | ' | \/ |`._`.| \_| \/ | v / _|
44
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
@@ -7,7 +7,6 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Net;
10-
using DotNetty.Transport.Channels;
1110
using Microsoft.Extensions.Logging;
1211
using NodaTime;
1312
using NosCore.Networking.Resource;
@@ -18,19 +17,14 @@ namespace NosCore.Networking.Encoding.Filter
1817
/// <summary>
1918
/// Filters spam requests by rate-limiting connections from the same IP address.
2019
/// </summary>
21-
public class SpamRequestFilter : RequestFilter
20+
public class SpamRequestFilter : IRequestFilter
2221
{
2322
private readonly Dictionary<EndPoint, Instant> _connectionsByIp = new();
2423
private readonly TimeSpan _timeBetweenConnection = TimeSpan.FromMilliseconds(1000);
2524
private readonly IClock _clock;
2625
private readonly ILogger<SpamRequestFilter> _logger;
2726
private readonly ILogLanguageLocalizer<LogLanguageKey> _logLanguage;
2827

29-
/// <summary>
30-
/// Gets a value indicating whether this handler can be shared across multiple channels.
31-
/// </summary>
32-
public override bool IsSharable => true;
33-
3428
/// <summary>
3529
/// Initializes a new instance of the <see cref="SpamRequestFilter"/> class.
3630
/// </summary>
@@ -47,21 +41,21 @@ public SpamRequestFilter(IClock clock, ILogger<SpamRequestFilter> logger, ILogLa
4741
/// <summary>
4842
/// Filters incoming requests based on connection rate from the same IP address.
4943
/// </summary>
50-
/// <param name="context">The channel handler context.</param>
44+
/// <param name="remoteEndPoint">The remote endpoint of the connection.</param>
5145
/// <param name="message">The incoming message bytes.</param>
5246
/// <returns>The message bytes if allowed, or null if blocked by the spam filter.</returns>
53-
public override byte[]? Filter(IChannelHandlerContext context, Span<byte> message)
47+
public byte[]? Filter(EndPoint remoteEndPoint, Span<byte> message)
5448
{
55-
if (_connectionsByIp.TryGetValue(context.Channel.RemoteAddress, out var date))
49+
if (_connectionsByIp.TryGetValue(remoteEndPoint, out var date))
5650
{
5751
if (date.Plus(Duration.FromTimeSpan(_timeBetweenConnection)) > _clock.GetCurrentInstant())
5852
{
59-
_logger.LogWarning(_logLanguage[LogLanguageKey.BLOCKED_BY_SPAM_FILTER], context.Channel.RemoteAddress);
53+
_logger.LogWarning(_logLanguage[LogLanguageKey.BLOCKED_BY_SPAM_FILTER], remoteEndPoint);
6054
return null;
6155
}
6256
}
6357

64-
_connectionsByIp[context.Channel.RemoteAddress] = _clock.GetCurrentInstant();
58+
_connectionsByIp[remoteEndPoint] = _clock.GetCurrentInstant();
6559
return message.ToArray();
6660
}
6761
}
Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,16 @@
1-
// __ _ __ __ ___ __ ___ ___
1+
// __ _ __ __ ___ __ ___ ___
22
// | \| |/__\ /' _/ / _//__\| _ \ __|
33
// | | ' | \/ |`._`.| \_| \/ | v / _|
44
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
55
// -----------------------------------
66

7-
using System.Collections.Generic;
8-
using System.Linq;
9-
using DotNetty.Buffers;
10-
using DotNetty.Codecs;
11-
using DotNetty.Transport.Channels;
12-
using NosCore.Networking.SessionRef;
13-
147
namespace NosCore.Networking.Encoding;
158

169
/// <summary>
17-
/// Delimits incoming byte streams into frames based on session-specific delimiters.
10+
/// Provides delimiter calculation for session-specific frame detection.
1811
/// </summary>
19-
public class FrameDelimiter : ByteToMessageDecoder
12+
public static class FrameDelimiter
2013
{
21-
private readonly ISessionRefHolder _sessionRefHolder;
22-
23-
/// <summary>
24-
/// Initializes a new instance of the <see cref="FrameDelimiter"/> class.
25-
/// </summary>
26-
/// <param name="sessionRefHolder">The session reference holder.</param>
27-
public FrameDelimiter(ISessionRefHolder sessionRefHolder)
28-
{
29-
_sessionRefHolder = sessionRefHolder;
30-
}
31-
32-
/// <summary>
33-
/// Decodes the incoming byte buffer into frames based on delimiters.
34-
/// </summary>
35-
/// <param name="context">The channel handler context.</param>
36-
/// <param name="input">The input byte buffer.</param>
37-
/// <param name="output">The output list to add decoded frames to.</param>
38-
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
39-
{
40-
var sessionId = context.Channel.Id.AsLongText();
41-
var mapper = _sessionRefHolder[sessionId];
42-
43-
var currentDelimiter = GetDelimiter(mapper.SessionId, mapper.SessionId == 0);
44-
45-
var startReaderIndex = input.ReaderIndex;
46-
var endReaderIndex = startReaderIndex + input.ReadableBytes;
47-
48-
for (var i = startReaderIndex; i < endReaderIndex; i++)
49-
{
50-
if (input.GetByte(i) == currentDelimiter)
51-
{
52-
var frameLength = i - startReaderIndex + 1;
53-
var frame = input.Copy(startReaderIndex, frameLength);
54-
output.Add(frame);
55-
input.SetReaderIndex(i + 1);
56-
break;
57-
}
58-
}
59-
}
60-
6114
/// <summary>
6215
/// Gets the delimiter byte for a specific session.
6316
/// </summary>
@@ -79,4 +32,4 @@ public static byte GetDelimiter(int session, bool isFirstPacket = false)
7932
_ => (0xff + 0xF) & 0xFF
8033
});
8134
}
82-
}
35+
}

src/NosCore.Networking/Encoding/IDecoder.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
using System;
88
using System.Collections.Generic;
9-
using DotNetty.Transport.Channels;
109
using NosCore.Packets.Interfaces;
1110

1211
namespace NosCore.Networking.Encoding
1312
{
1413
/// <summary>
1514
/// Defines a packet decoder that converts byte data into packets.
1615
/// </summary>
17-
public interface IDecoder : IChannelHandler
16+
public interface IDecoder
1817
{
1918
/// <summary>
2019
/// Decodes a byte span into a collection of packets.

src/NosCore.Networking/Encoding/IEncoder.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
55
// -----------------------------------
66

7-
using System;
87
using System.Collections.Generic;
9-
using DotNetty.Transport.Channels;
108
using NosCore.Packets.Interfaces;
119

1210
namespace NosCore.Networking.Encoding
1311
{
1412
/// <summary>
1513
/// Defines a packet encoder that converts packets to byte arrays for transmission.
1614
/// </summary>
17-
public interface IEncoder : IChannelHandler
15+
public interface IEncoder
1816
{
1917
/// <summary>
2018
/// Encodes a collection of packets into a byte array.

src/NosCore.Networking/Encoding/LoginDecoder.cs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
// __ _ __ __ ___ __ ___ ___
1+
// __ _ __ __ ___ __ ___ ___
22
// | \| |/__\ /' _/ / _//__\| _ \ __|
33
// | | ' | \/ |`._`.| \_| \/ | v / _|
44
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
55
// -----------------------------------
66

77
using System;
88
using System.Collections.Generic;
9-
using System.Linq;
109
using System.Text;
11-
using DotNetty.Buffers;
12-
using DotNetty.Codecs;
13-
using DotNetty.Transport.Channels;
1410
using Microsoft.Extensions.Logging;
1511
using NosCore.Networking.Resource;
1612
using NosCore.Networking.SessionRef;
@@ -22,7 +18,7 @@ namespace NosCore.Networking.Encoding
2218
/// <summary>
2319
/// Decodes packets from login server communication using region-specific decoding.
2420
/// </summary>
25-
public class LoginDecoder : MessageToMessageDecoder<IByteBuffer>, IDecoder
21+
public class LoginDecoder : IDecoder
2622
{
2723
private readonly IDeserializer _deserializer;
2824
private readonly ILogger<LoginDecoder> _logger;
@@ -90,22 +86,5 @@ public IEnumerable<IPacket> Decode(string clientSessionId, Span<byte> message)
9086

9187
return Array.Empty<IPacket>();
9288
}
93-
94-
/// <summary>
95-
/// Decodes a byte buffer into packets.
96-
/// </summary>
97-
/// <param name="context">The channel handler context.</param>
98-
/// <param name="message">The byte buffer containing encoded packet data.</param>
99-
/// <param name="output">The output list to add decoded packets to.</param>
100-
protected override void Decode(IChannelHandlerContext context, IByteBuffer message, List<object> output)
101-
{
102-
var packets = Decode(context.Channel.Id.AsLongText(),
103-
((Span<byte>)message.Array).Slice(message.ArrayOffset, message.ReadableBytes));
104-
105-
if (packets.Any())
106-
{
107-
output.Add(packets);
108-
}
109-
}
11089
}
111-
}
90+
}

src/NosCore.Networking/Encoding/LoginEncoder.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// __ _ __ __ ___ __ ___ ___
1+
// __ _ __ __ ___ __ ___ ___
22
// | \| |/__\ /' _/ / _//__\| _ \ __|
33
// | | ' | \/ |`._`.| \_| \/ | v / _|
44
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
@@ -7,9 +7,6 @@
77
using System;
88
using System.Collections.Generic;
99
using System.Linq;
10-
using DotNetty.Buffers;
11-
using DotNetty.Codecs;
12-
using DotNetty.Transport.Channels;
1310
using Microsoft.Extensions.Logging;
1411
using NosCore.Networking.Extensions;
1512
using NosCore.Networking.Resource;
@@ -22,7 +19,7 @@ namespace NosCore.Networking.Encoding
2219
/// <summary>
2320
/// Encodes packets for login server communication using region-specific encoding.
2421
/// </summary>
25-
public class LoginEncoder : MessageToMessageEncoder<IEnumerable<IPacket>>, IEncoder
22+
public class LoginEncoder : IEncoder
2623
{
2724
private readonly ILogger<LoginEncoder> _logger;
2825
private readonly ISerializer _serializer;
@@ -44,18 +41,6 @@ public LoginEncoder(ILogger<LoginEncoder> logger, ISerializer serializer, ISessi
4441
_logLanguage = logLanguage;
4542
}
4643

47-
/// <summary>
48-
/// Encodes packets into a byte buffer for transmission.
49-
/// </summary>
50-
/// <param name="context">The channel handler context.</param>
51-
/// <param name="message">The packets to encode.</param>
52-
/// <param name="output">The output list to add the encoded buffer to.</param>
53-
protected override void Encode(IChannelHandlerContext context, IEnumerable<IPacket> message,
54-
List<object> output)
55-
{
56-
output.Add(Unpooled.WrappedBuffer(Encode(context.Channel.Id.AsLongText(), message)));
57-
}
58-
5944
/// <summary>
6045
/// Encodes a collection of packets into a byte array using login server encoding.
6146
/// </summary>
@@ -89,4 +74,4 @@ public byte[] Encode(string clientSessionId, IEnumerable<IPacket> packets)
8974
return Array.Empty<byte>();
9075
}
9176
}
92-
}
77+
}

0 commit comments

Comments
 (0)