1- using MineCase . Protocol ;
1+ using Microsoft . Extensions . ObjectPool ;
2+ using MineCase . Protocol ;
23using MineCase . Server . Network ;
34using Orleans ;
45using System ;
@@ -21,12 +22,14 @@ class ClientSession : IDisposable
2122 private readonly Guid _sessionId ;
2223 private readonly OutcomingPacketObserver _outcomingPacketObserver ;
2324 private readonly ActionBlock < UncompressedPacket > _outcomingPacketDispatcher ;
25+ private readonly ObjectPool < UncompressedPacket > _uncompressedPacketObjectPool ;
2426
25- public ClientSession ( TcpClient tcpClient , IGrainFactory grainFactory )
27+ public ClientSession ( TcpClient tcpClient , IGrainFactory grainFactory , ObjectPool < UncompressedPacket > uncompressedPacketObjectPool )
2628 {
2729 _sessionId = Guid . NewGuid ( ) ;
2830 _tcpClient = tcpClient ;
2931 _grainFactory = grainFactory ;
32+ _uncompressedPacketObjectPool = uncompressedPacketObjectPool ;
3033 _outcomingPacketObserver = new OutcomingPacketObserver ( this ) ;
3134 _outcomingPacketDispatcher = new ActionBlock < UncompressedPacket > ( SendOutcomingPacket ) ;
3235 }
@@ -60,17 +63,24 @@ private void OnClosed()
6063
6164 private async Task DispatchIncomingPacket ( )
6265 {
63- UncompressedPacket packet ;
64- if ( _useCompression )
66+ var packet = _uncompressedPacketObjectPool . Get ( ) ;
67+ try
6568 {
66- var compressedPacket = await CompressedPacket . DeserializeAsync ( _remoteStream ) ;
67- packet = PacketCompress . Decompress ( ref compressedPacket ) ;
69+ if ( _useCompression )
70+ {
71+ var compressedPacket = await CompressedPacket . DeserializeAsync ( _remoteStream , null ) ;
72+ packet = PacketCompress . Decompress ( ref compressedPacket ) ;
73+ }
74+ else
75+ {
76+ packet = await UncompressedPacket . DeserializeAsync ( _remoteStream , packet ) ;
77+ }
78+ await DispatchIncomingPacket ( packet ) ;
6879 }
69- else
80+ finally
7081 {
71- packet = await UncompressedPacket . DeserializeAsync ( _remoteStream ) ;
82+ _uncompressedPacketObjectPool . Return ( packet ) ;
7283 }
73- await DispatchIncomingPacket ( packet ) ;
7484 }
7585
7686 private async Task SendOutcomingPacket ( UncompressedPacket packet )
0 commit comments