Skip to content

Commit

Permalink
fixed #25 handle SocketException in BeginSend/BeginReceive
Browse files Browse the repository at this point in the history
  • Loading branch information
longshine committed Apr 1, 2018
1 parent a0d1383 commit 37073ce
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
16 changes: 16 additions & 0 deletions Mina.NET/Transport/Socket/AsyncSocketSession.NET20.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ protected override void BeginReceive()
{
// do nothing
}
catch (SocketException ex)
{
EndReceive(ex);
}
catch (Exception ex)
{
EndReceive(ex);
}
}

/// <inheritdoc/>
Expand All @@ -54,6 +62,10 @@ protected override void BeginSend(IWriteRequest request, IoBuffer buf)
{
// ignore
}
catch (SocketException ex)
{
EndSend(ex);
}
catch (Exception ex)
{
EndSend(ex);
Expand All @@ -71,6 +83,10 @@ protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
{
// ignore
}
catch (SocketException ex)
{
EndSend(ex);
}
catch (Exception ex)
{
EndSend(ex);
Expand Down
33 changes: 17 additions & 16 deletions Mina.NET/Transport/Socket/AsyncSocketSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ protected override void BeginSend(IWriteRequest request, IoBuffer buf)
// do nothing
return;
}
catch (SocketException ex)
{
EndSend(ex);
return;
}
catch (Exception ex)
{
EndSend(ex);
Expand Down Expand Up @@ -147,6 +152,11 @@ protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
// do nothing
return;
}
catch (SocketException ex)
{
EndSend(ex);
return;
}
catch (Exception ex)
{
EndSend(ex);
Expand Down Expand Up @@ -174,16 +184,9 @@ public void ProcessSend(SocketAsyncEventArgs e)
{
EndSend(e.BytesTransferred);
}
else if (e.SocketError != SocketError.OperationAborted
&& e.SocketError != SocketError.Interrupted
&& e.SocketError != SocketError.ConnectionReset)
{
EndSend(new SocketException((Int32)e.SocketError));
}
else
{
// closed
Processor.Remove(this);
EndSend(new SocketException((Int32)e.SocketError));
}
}

Expand All @@ -202,6 +205,11 @@ protected override void BeginReceive()
// do nothing
return;
}
catch (SocketException ex)
{
EndReceive(ex);
return;
}
catch (Exception ex)
{
EndReceive(ex);
Expand Down Expand Up @@ -247,16 +255,9 @@ public void ProcessReceive(SocketAsyncEventArgs e)
this.FilterChain.FireInputClosed();
}
}
else if (e.SocketError != SocketError.OperationAborted
&& e.SocketError != SocketError.Interrupted
&& e.SocketError != SocketError.ConnectionReset)
{
EndReceive(new SocketException((Int32)e.SocketError));
}
else
{
// closed
Processor.Remove(this);
EndReceive(new SocketException((Int32)e.SocketError));
}
}
}
Expand Down
37 changes: 36 additions & 1 deletion Mina.NET/Transport/Socket/SocketSession.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using Mina.Core.Buffer;
Expand Down Expand Up @@ -220,6 +220,23 @@ protected void EndSend(Int32 bytesTransferred)
BeginSend();
}

/// <summary>
/// Ends send operation by a <see cref="SocketException"/>.
/// </summary>
protected void EndSend(SocketException ex)
{
// may be closed
if (ex.SocketErrorCode != SocketError.OperationAborted
&& ex.SocketErrorCode != SocketError.Interrupted
&& ex.SocketErrorCode != SocketError.ConnectionAborted
&& ex.SocketErrorCode != SocketError.ConnectionReset)
{
this.FilterChain.FireExceptionCaught(ex);
}
// TODO should I check the SocketError code?
Processor.Remove(this);
}

/// <summary>
/// Ends send operation.
/// </summary>
Expand Down Expand Up @@ -277,6 +294,24 @@ protected void EndReceive(IoBuffer buf)
}
}

/// <summary>
/// Ends receive operation by a <see cref="SocketException"/>.
/// </summary>
/// <param name="ex"></param>
protected void EndReceive(SocketException ex)
{
// may be closed
if (ex.SocketErrorCode != SocketError.OperationAborted
&& ex.SocketErrorCode != SocketError.Interrupted
&& ex.SocketErrorCode != SocketError.ConnectionAborted
&& ex.SocketErrorCode != SocketError.ConnectionReset)
{
this.FilterChain.FireExceptionCaught(ex);
}
// TODO should I check the SocketError code?
Processor.Remove(this);
}

/// <summary>
/// Ends receive operation.
/// </summary>
Expand Down

0 comments on commit 37073ce

Please sign in to comment.