Skip to content

Commit 664e4c0

Browse files
committed
fix delimiter of framedelimiter
1 parent adaef5a commit 664e4c0

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

src/NosCore.Networking/Encoding/FrameDelimiter.cs

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

77
using System.Collections.Generic;
8+
using System.Linq;
89
using DotNetty.Buffers;
910
using DotNetty.Codecs;
1011
using DotNetty.Transport.Channels;
@@ -15,20 +16,18 @@ namespace NosCore.Networking.Encoding;
1516
public class FrameDelimiter : ByteToMessageDecoder
1617
{
1718
private readonly ISessionRefHolder _sessionRefHolder;
18-
private readonly IPipelineConfiguration _pipelineConfiguration;
19-
20-
public FrameDelimiter(ISessionRefHolder sessionRefHolder, IPipelineConfiguration pipelineConfiguration)
19+
public FrameDelimiter(ISessionRefHolder sessionRefHolder)
2120
{
2221
_sessionRefHolder = sessionRefHolder;
23-
_pipelineConfiguration = pipelineConfiguration;
2422
}
2523

24+
2625
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
2726
{
2827
var sessionId = context.Channel.Id.AsLongText();
2928
var mapper = _sessionRefHolder[sessionId];
3029

31-
var currentDelimiter = mapper.SessionId == 0 ? (byte)0xE : unchecked((byte)((_pipelineConfiguration.Delimiter ?? 0) + mapper.SessionId));
30+
var currentDelimiter = GetDelimiter(mapper.SessionId, mapper.SessionId == 0);
3231

3332
var startReaderIndex = input.ReaderIndex;
3433
var endReaderIndex = startReaderIndex + input.ReadableBytes;
@@ -45,4 +44,20 @@ protected override void Decode(IChannelHandlerContext context, IByteBuffer input
4544
}
4645
}
4746
}
47+
48+
public static byte GetDelimiter(int session, bool isFirstPacket = false)
49+
{
50+
int stype = !isFirstPacket ? (session >> 6) & 3 : -1;
51+
52+
var key = session & 0xFF;
53+
54+
return (byte)(stype switch
55+
{
56+
0 => (0xff + key + 0x40) & 0xFF,
57+
1 => (0xff - key - 0x40) & 0xFF,
58+
2 => ((0xff ^ 0xC3) + key + 0x40) & 0xFF,
59+
3 => ((0xff ^ 0xC3) - key - 0x40) & 0xFF,
60+
_ => (0xff + 0xF) & 0xFF
61+
});
62+
}
4863
}

src/NosCore.Networking/IPipelineConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ namespace NosCore.Networking;
88

99
public interface IPipelineConfiguration
1010
{
11-
public byte? Delimiter { get; set; }
11+
public bool UseDelimiter { get; set; }
1212
}

src/NosCore.Networking/NosCore.Networking.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<RepositoryUrl>https://github.com/NosCoreIO/NosCore.Networking.git</RepositoryUrl>
1313
<PackageIconUrl></PackageIconUrl>
1414
<PackageTags>nostale, noscore, nostale private server source, nostale emulator</PackageTags>
15-
<Version>5.0.0</Version>
15+
<Version>6.0.0</Version>
1616
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
1717
<Description>NosCore Networking</Description>
1818
<PackageLicenseExpression>MIT</PackageLicenseExpression>

src/NosCore.Networking/PipelineFactory.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ public void CreatePipeline()
5252
pipeline.AddLast(filter);
5353
}
5454

55-
if (_pipelineConfiguration.Delimiter != null)
55+
if (_pipelineConfiguration.UseDelimiter)
5656
{
57-
58-
pipeline.AddLast(new FrameDelimiter(_sessionRefHolder, _pipelineConfiguration));
57+
pipeline.AddLast(new FrameDelimiter(_sessionRefHolder));
5958
}
6059

6160
pipeline.AddLast(_decoder);

0 commit comments

Comments
 (0)