Skip to content

Commit adaef5a

Browse files
committed
add frame delimiter
1 parent 2b53a54 commit adaef5a

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// __ _ __ __ ___ __ ___ ___
2+
// | \| |/__\ /' _/ / _//__\| _ \ __|
3+
// | | ' | \/ |`._`.| \_| \/ | v / _|
4+
// |_|\__|\__/ |___/ \__/\__/|_|_\___|
5+
// -----------------------------------
6+
7+
using System.Collections.Generic;
8+
using DotNetty.Buffers;
9+
using DotNetty.Codecs;
10+
using DotNetty.Transport.Channels;
11+
using NosCore.Networking.SessionRef;
12+
13+
namespace NosCore.Networking.Encoding;
14+
15+
public class FrameDelimiter : ByteToMessageDecoder
16+
{
17+
private readonly ISessionRefHolder _sessionRefHolder;
18+
private readonly IPipelineConfiguration _pipelineConfiguration;
19+
20+
public FrameDelimiter(ISessionRefHolder sessionRefHolder, IPipelineConfiguration pipelineConfiguration)
21+
{
22+
_sessionRefHolder = sessionRefHolder;
23+
_pipelineConfiguration = pipelineConfiguration;
24+
}
25+
26+
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
27+
{
28+
var sessionId = context.Channel.Id.AsLongText();
29+
var mapper = _sessionRefHolder[sessionId];
30+
31+
var currentDelimiter = mapper.SessionId == 0 ? (byte)0xE : unchecked((byte)((_pipelineConfiguration.Delimiter ?? 0) + mapper.SessionId));
32+
33+
var startReaderIndex = input.ReaderIndex;
34+
var endReaderIndex = startReaderIndex + input.ReadableBytes;
35+
36+
for (var i = startReaderIndex; i < endReaderIndex; i++)
37+
{
38+
if (input.GetByte(i) == currentDelimiter)
39+
{
40+
var frameLength = i - startReaderIndex + 1;
41+
var frame = input.Copy(startReaderIndex, frameLength);
42+
output.Add(frame);
43+
input.SetReaderIndex(i + 1);
44+
break;
45+
}
46+
}
47+
}
48+
}
File renamed without changes.

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>4.0.4</Version>
15+
<Version>5.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
@@ -54,9 +54,8 @@ public void CreatePipeline()
5454

5555
if (_pipelineConfiguration.Delimiter != null)
5656
{
57-
pipeline.AddLast(new DelimiterBasedFrameDecoder(8192, new[] {
58-
Unpooled.WrappedBuffer(new[] { (byte)_pipelineConfiguration.Delimiter })
59-
}));
57+
58+
pipeline.AddLast(new FrameDelimiter(_sessionRefHolder, _pipelineConfiguration));
6059
}
6160

6261
pipeline.AddLast(_decoder);

0 commit comments

Comments
 (0)