55// -----------------------------------
66
77using System . Collections . Generic ;
8+ using System . Linq ;
89using DotNetty . Buffers ;
910using DotNetty . Codecs ;
1011using DotNetty . Transport . Channels ;
@@ -15,20 +16,18 @@ namespace NosCore.Networking.Encoding;
1516public 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}
0 commit comments