Skip to content

Commit

Permalink
F! Close socket on SubSock.Send exceptions <DINO-4463>
Browse files Browse the repository at this point in the history
  • Loading branch information
s-yablonskiy committed May 26, 2021
1 parent 8b9a7b2 commit 603c906
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions Pixockets/SmartSock.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using Pixockets.Pools;

namespace Pixockets
Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 603c906

Please sign in to comment.