From c726c88f325d22516f91a89e2c33a55ad48855ef Mon Sep 17 00:00:00 2001 From: Bassam Tabbara Date: Sun, 7 Aug 2011 11:17:27 -0700 Subject: [PATCH] Networkstream now throws IOException('connection closed') if the socket was closed/disposed --- .../System.Net.Sockets/NetworkStream.cs | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/mcs/class/System/System.Net.Sockets/NetworkStream.cs b/mcs/class/System/System.Net.Sockets/NetworkStream.cs index 29c5b8bb51c8a..95d3b4dd51a03 100644 --- a/mcs/class/System/System.Net.Sockets/NetworkStream.cs +++ b/mcs/class/System/System.Net.Sockets/NetworkStream.cs @@ -214,8 +214,14 @@ public override int WriteTimeout throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer"); } + Socket s = socket; + + if (s == null) { + throw new IOException("Connection closed"); + } + try { - retval = socket.BeginReceive (buffer, offset, size, 0, callback, state); + retval = s.BeginReceive (buffer, offset, size, 0, callback, state); } catch (Exception e) { throw new IOException ("BeginReceive failure", e); } @@ -240,8 +246,14 @@ public override int WriteTimeout throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer"); } + Socket s = socket; + + if (s == null) { + throw new IOException("Connection closed"); + } + try { - retval = socket.BeginSend (buffer, offset, size, 0, callback, state); + retval = s.BeginSend (buffer, offset, size, 0, callback, state); } catch { throw new IOException ("BeginWrite failure"); } @@ -316,8 +328,14 @@ public override int EndRead (IAsyncResult ar) if (ar == null) throw new ArgumentNullException ("async result is null"); + Socket s = socket; + + if (s == null) { + throw new IOException("Connection closed"); + } + try { - res = socket.EndReceive (ar); + res = s.EndReceive (ar); } catch (Exception e) { throw new IOException ("EndRead failure", e); } @@ -330,8 +348,14 @@ public override void EndWrite (IAsyncResult ar) if (ar == null) throw new ArgumentNullException ("async result is null"); + Socket s = socket; + + if (s == null) { + throw new IOException("Connection closed"); + } + try { - socket.EndSend (ar); + s.EndSend (ar); } catch (Exception e) { throw new IOException ("EndWrite failure", e); } @@ -363,8 +387,14 @@ public override int Read ([In,Out] byte [] buffer, int offset, int size) throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer"); } + Socket s = socket; + + if (s == null) { + throw new IOException("Connection closed"); + } + try { - res = socket.Receive (buffer, offset, size, 0); + res = s.Receive (buffer, offset, size, 0); } catch (Exception e) { throw new IOException ("Read failure", e); } @@ -398,10 +428,16 @@ public override void Write (byte [] buffer, int offset, int size) if (size < 0 || size > buffer.Length - offset) throw new ArgumentOutOfRangeException("offset+size exceeds the size of buffer"); + Socket s = socket; + + if (s == null) { + throw new IOException("Connection closed"); + } + try { int count = 0; while (size - count > 0) { - count += socket.Send (buffer, offset + count, size - count, 0); + count += s.Send (buffer, offset + count, size - count, 0); } } catch (Exception e) { throw new IOException ("Write failure", e);