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+ }
0 commit comments