11using Microsoft . Extensions . ObjectPool ;
2+ using MineCase . Buffers ;
23using MineCase . Protocol ;
34using MineCase . Server . Network ;
45using Orleans ;
@@ -23,12 +24,14 @@ class ClientSession : IDisposable
2324 private readonly OutcomingPacketObserver _outcomingPacketObserver ;
2425 private readonly ActionBlock < UncompressedPacket > _outcomingPacketDispatcher ;
2526 private readonly ObjectPool < UncompressedPacket > _uncompressedPacketObjectPool ;
27+ private readonly IBufferPool < byte > _bufferPool ;
2628
27- public ClientSession ( TcpClient tcpClient , IGrainFactory grainFactory , ObjectPool < UncompressedPacket > uncompressedPacketObjectPool )
29+ public ClientSession ( TcpClient tcpClient , IGrainFactory grainFactory , IBufferPool < byte > bufferPool , ObjectPool < UncompressedPacket > uncompressedPacketObjectPool )
2830 {
2931 _sessionId = Guid . NewGuid ( ) ;
3032 _tcpClient = tcpClient ;
3133 _grainFactory = grainFactory ;
34+ _bufferPool = bufferPool ;
3235 _uncompressedPacketObjectPool = uncompressedPacketObjectPool ;
3336 _outcomingPacketObserver = new OutcomingPacketObserver ( this ) ;
3437 _outcomingPacketDispatcher = new ActionBlock < UncompressedPacket > ( SendOutcomingPacket ) ;
@@ -57,35 +60,43 @@ public async Task Startup(CancellationToken cancellationToken)
5760
5861 private void OnClosed ( )
5962 {
60- _outcomingPacketDispatcher . Complete ( ) ;
61- _tcpClient . Client . Shutdown ( SocketShutdown . Send ) ;
63+ _outcomingPacketDispatcher . Post ( null ) ;
6264 }
6365
6466 private async Task DispatchIncomingPacket ( )
6567 {
66- var packet = _uncompressedPacketObjectPool . Get ( ) ;
67- try
68+ using ( var bufferScope = _bufferPool . CreateScope ( ) )
6869 {
69- if ( _useCompression )
70+ var packet = _uncompressedPacketObjectPool . Get ( ) ;
71+ try
7072 {
71- var compressedPacket = await CompressedPacket . DeserializeAsync ( _remoteStream , null ) ;
72- packet = PacketCompress . Decompress ( ref compressedPacket ) ;
73+ if ( _useCompression )
74+ {
75+ var compressedPacket = await CompressedPacket . DeserializeAsync ( _remoteStream , null ) ;
76+ packet = PacketCompress . Decompress ( ref compressedPacket ) ;
77+ }
78+ else
79+ {
80+ packet = await UncompressedPacket . DeserializeAsync ( _remoteStream , bufferScope , packet ) ;
81+ }
82+ await DispatchIncomingPacket ( packet ) ;
7383 }
74- else
84+ finally
7585 {
76- packet = await UncompressedPacket . DeserializeAsync ( _remoteStream , packet ) ;
86+ _uncompressedPacketObjectPool . Return ( packet ) ;
7787 }
78- await DispatchIncomingPacket ( packet ) ;
79- }
80- finally
81- {
82- _uncompressedPacketObjectPool . Return ( packet ) ;
8388 }
8489 }
8590
8691 private async Task SendOutcomingPacket ( UncompressedPacket packet )
8792 {
88- if ( _useCompression )
93+ // Close
94+ if ( packet == null )
95+ {
96+ _tcpClient . Client . Shutdown ( SocketShutdown . Send ) ;
97+ _outcomingPacketDispatcher . Complete ( ) ;
98+ }
99+ else if ( _useCompression )
89100 {
90101 var newPacket = PacketCompress . Compress ( ref packet ) ;
91102 await newPacket . SerializeAsync ( _remoteStream ) ;
0 commit comments