Skip to content

Commit 37073ce

Browse files
committed
fixed longshine#25 handle SocketException in BeginSend/BeginReceive
1 parent a0d1383 commit 37073ce

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

Mina.NET/Transport/Socket/AsyncSocketSession.NET20.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ protected override void BeginReceive()
4040
{
4141
// do nothing
4242
}
43+
catch (SocketException ex)
44+
{
45+
EndReceive(ex);
46+
}
47+
catch (Exception ex)
48+
{
49+
EndReceive(ex);
50+
}
4351
}
4452

4553
/// <inheritdoc/>
@@ -54,6 +62,10 @@ protected override void BeginSend(IWriteRequest request, IoBuffer buf)
5462
{
5563
// ignore
5664
}
65+
catch (SocketException ex)
66+
{
67+
EndSend(ex);
68+
}
5769
catch (Exception ex)
5870
{
5971
EndSend(ex);
@@ -71,6 +83,10 @@ protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
7183
{
7284
// ignore
7385
}
86+
catch (SocketException ex)
87+
{
88+
EndSend(ex);
89+
}
7490
catch (Exception ex)
7591
{
7692
EndSend(ex);

Mina.NET/Transport/Socket/AsyncSocketSession.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ protected override void BeginSend(IWriteRequest request, IoBuffer buf)
118118
// do nothing
119119
return;
120120
}
121+
catch (SocketException ex)
122+
{
123+
EndSend(ex);
124+
return;
125+
}
121126
catch (Exception ex)
122127
{
123128
EndSend(ex);
@@ -147,6 +152,11 @@ protected override void BeginSendFile(IWriteRequest request, IFileRegion file)
147152
// do nothing
148153
return;
149154
}
155+
catch (SocketException ex)
156+
{
157+
EndSend(ex);
158+
return;
159+
}
150160
catch (Exception ex)
151161
{
152162
EndSend(ex);
@@ -174,16 +184,9 @@ public void ProcessSend(SocketAsyncEventArgs e)
174184
{
175185
EndSend(e.BytesTransferred);
176186
}
177-
else if (e.SocketError != SocketError.OperationAborted
178-
&& e.SocketError != SocketError.Interrupted
179-
&& e.SocketError != SocketError.ConnectionReset)
180-
{
181-
EndSend(new SocketException((Int32)e.SocketError));
182-
}
183187
else
184188
{
185-
// closed
186-
Processor.Remove(this);
189+
EndSend(new SocketException((Int32)e.SocketError));
187190
}
188191
}
189192

@@ -202,6 +205,11 @@ protected override void BeginReceive()
202205
// do nothing
203206
return;
204207
}
208+
catch (SocketException ex)
209+
{
210+
EndReceive(ex);
211+
return;
212+
}
205213
catch (Exception ex)
206214
{
207215
EndReceive(ex);
@@ -247,16 +255,9 @@ public void ProcessReceive(SocketAsyncEventArgs e)
247255
this.FilterChain.FireInputClosed();
248256
}
249257
}
250-
else if (e.SocketError != SocketError.OperationAborted
251-
&& e.SocketError != SocketError.Interrupted
252-
&& e.SocketError != SocketError.ConnectionReset)
253-
{
254-
EndReceive(new SocketException((Int32)e.SocketError));
255-
}
256258
else
257259
{
258-
// closed
259-
Processor.Remove(this);
260+
EndReceive(new SocketException((Int32)e.SocketError));
260261
}
261262
}
262263
}

Mina.NET/Transport/Socket/SocketSession.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.IO;
2+
using System.Net.Sockets;
33
using System.Net;
44
using System.Threading;
55
using Mina.Core.Buffer;
@@ -220,6 +220,23 @@ protected void EndSend(Int32 bytesTransferred)
220220
BeginSend();
221221
}
222222

223+
/// <summary>
224+
/// Ends send operation by a <see cref="SocketException"/>.
225+
/// </summary>
226+
protected void EndSend(SocketException ex)
227+
{
228+
// may be closed
229+
if (ex.SocketErrorCode != SocketError.OperationAborted
230+
&& ex.SocketErrorCode != SocketError.Interrupted
231+
&& ex.SocketErrorCode != SocketError.ConnectionAborted
232+
&& ex.SocketErrorCode != SocketError.ConnectionReset)
233+
{
234+
this.FilterChain.FireExceptionCaught(ex);
235+
}
236+
// TODO should I check the SocketError code?
237+
Processor.Remove(this);
238+
}
239+
223240
/// <summary>
224241
/// Ends send operation.
225242
/// </summary>
@@ -277,6 +294,24 @@ protected void EndReceive(IoBuffer buf)
277294
}
278295
}
279296

297+
/// <summary>
298+
/// Ends receive operation by a <see cref="SocketException"/>.
299+
/// </summary>
300+
/// <param name="ex"></param>
301+
protected void EndReceive(SocketException ex)
302+
{
303+
// may be closed
304+
if (ex.SocketErrorCode != SocketError.OperationAborted
305+
&& ex.SocketErrorCode != SocketError.Interrupted
306+
&& ex.SocketErrorCode != SocketError.ConnectionAborted
307+
&& ex.SocketErrorCode != SocketError.ConnectionReset)
308+
{
309+
this.FilterChain.FireExceptionCaught(ex);
310+
}
311+
// TODO should I check the SocketError code?
312+
Processor.Remove(this);
313+
}
314+
280315
/// <summary>
281316
/// Ends receive operation.
282317
/// </summary>

0 commit comments

Comments
 (0)