From 603c906f1eeeba5184019b2d185531b3aa862179 Mon Sep 17 00:00:00 2001 From: Stanislav Yablonskiy Date: Wed, 26 May 2021 18:58:14 +0300 Subject: [PATCH] F! Close socket on SubSock.Send exceptions --- Pixockets/SmartSock.cs | 47 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/Pixockets/SmartSock.cs b/Pixockets/SmartSock.cs index d6e1259..d56e992 100644 --- a/Pixockets/SmartSock.cs +++ b/Pixockets/SmartSock.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.Sockets; using Pixockets.Pools; namespace Pixockets @@ -165,14 +166,28 @@ public void Send(IPEndPoint endPoint, byte[] buffer, int offset, int length, boo // It should be done after using fragmentOffset to cut fragment fragmentOffset += fragmentSize; - SubSock.Send(endPoint, fullBuffer.Array, fullBuffer.Offset, fullBuffer.Count, putBufferToPool); + try + { + SubSock.Send(endPoint, fullBuffer.Array, fullBuffer.Offset, fullBuffer.Count, putBufferToPool); + } + catch (SocketException) + { + Close(endPoint, seqState); + break; + } } } else { var fullBuffer = Wrap(seqState, buffer, offset, length, reliable); - - SubSock.Send(endPoint, fullBuffer.Array, fullBuffer.Offset, fullBuffer.Count, putBufferToPool); + try + { + SubSock.Send(endPoint, fullBuffer.Array, fullBuffer.Offset, fullBuffer.Count, putBufferToPool); + } + catch (SocketException) + { + Close(endPoint, seqState); + } } } @@ -209,14 +224,29 @@ public void Send(byte[] buffer, int offset, int length, bool reliable) // It should be done after using fragmentOffset to cut fragment fragmentOffset += fragmentSize; - SubSock.Send(fullBuffer.Array, fullBuffer.Offset, fullBuffer.Count, putBufferToPool); + try + { + SubSock.Send(fullBuffer.Array, fullBuffer.Offset, fullBuffer.Count, putBufferToPool); + } + catch (SocketException) + { + Close(endPoint, seqState); + break; + } } } else { var fullBuffer = Wrap(seqState, buffer, offset, length, reliable); - SubSock.Send(fullBuffer.Array, fullBuffer.Offset, fullBuffer.Count, putBufferToPool); + try + { + SubSock.Send(fullBuffer.Array, fullBuffer.Offset, fullBuffer.Count, putBufferToPool); + } + catch (SocketException) + { + Close(endPoint, seqState); + } } } @@ -572,5 +602,12 @@ private void SendDisconnectPacket(IPEndPoint endPoint, SequenceState seqState) _headersPool.Put(header); } + + private void Close(IPEndPoint endPoint, SequenceState seqState) + { + _seqStates.Remove(endPoint); + _callbacks.OnDisconnect(endPoint, DisconnectReason.SocketClose); + _seqStatesPool.Put(seqState); + } } }