using System.IO;
using System.Runtime.InteropServices;
-#if NET_2_0
+#if !NET_2_1 || MOBILE
using System.Timers;
using System.Threading;
#endif
{
}
- public NetworkStream (Socket socket, bool owns_socket)
- : this (socket, FileAccess.ReadWrite, owns_socket)
+ public NetworkStream (Socket socket, bool ownsSocket)
+ : this (socket, FileAccess.ReadWrite, ownsSocket)
{
}
{
}
- public NetworkStream (Socket socket, FileAccess access, bool owns_socket)
+ public NetworkStream (Socket socket, FileAccess access, bool ownsSocket)
{
if (socket == null)
throw new ArgumentNullException ("socket is null");
throw new IOException ("Operation not allowed on a non-blocking socket.");
this.socket = socket;
- this.owns_socket = owns_socket;
+ this.owns_socket = ownsSocket;
this.access = access;
readable = CanRead;
}
}
-#if NET_2_0
public override bool CanTimeout
{
get {
return(true);
}
}
-#endif
public override bool CanWrite {
get {
}
}
-#if NET_2_0
+#if !NET_2_1 || MOBILE
#if TARGET_JVM
[MonoNotSupported ("Not supported since Socket.ReceiveTimeout is not supported")]
#endif
public override int ReadTimeout
{
get {
- return(socket.ReceiveTimeout);
+ int r = socket.ReceiveTimeout;
+ return (r <= 0) ? Timeout.Infinite : r;
}
set {
if (value <= 0 && value != Timeout.Infinite) {
}
}
-#if NET_2_0
+#if !NET_2_1 || MOBILE
#if TARGET_JVM
[MonoNotSupported ("Not supported since Socket.SendTimeout is not supported")]
#endif
public override int WriteTimeout
{
get {
- return(socket.SendTimeout);
+ int r = socket.SendTimeout;
+ return (r <= 0) ? Timeout.Infinite : r;
}
set {
if (value <= 0 && value != Timeout.Infinite) {
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);
}
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");
}
Dispose (false);
}
-#if !NET_2_0
- public override void Close ()
- {
- ((IDisposable) this).Dispose ();
- }
-#endif
-#if NET_2_0
+#if !NET_2_1 || MOBILE
public void Close (int timeout)
{
if (timeout < -1) {
}
#endif
- protected
-#if NET_2_0
- override
-#else
- virtual
-#endif
- void Dispose (bool disposing)
+ protected override void Dispose (bool disposing)
{
if (disposed)
return;
s.Close ();
}
socket = null;
+ access = 0;
+
+ if (disposing)
+ GC.SuppressFinalize (this);
}
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);
}
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);
}
// network streams are non-buffered, this is a no-op
}
- void IDisposable.Dispose ()
- {
- Dispose (true);
- GC.SuppressFinalize (this);
- }
-
public override int Read ([In,Out] byte [] buffer, int offset, int size)
{
CheckDisposed ();
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);
}
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);