Skip to content

Commit

Permalink
Networkstream now throws IOException('connection closed') if the sock…
Browse files Browse the repository at this point in the history
…et was closed/disposed
  • Loading branch information
Bassam Tabbara committed Aug 7, 2011
1 parent 3cb9ce9 commit c726c88
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions mcs/class/System/System.Net.Sockets/NetworkStream.cs
Expand Up @@ -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);
}
Expand All @@ -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");
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit c726c88

Please sign in to comment.