Browse Source

* TcpListener.cs: Renamed LocalEndPoint to LocalEndpoint
* NetworkStream.cs, UdpClient.cs and TcpClient.cs: modified disposable
routines, added checks for disposed state.

svn path=/trunk/mcs/; revision=4721

Lawrence Pit 23 years ago
parent
commit
b43bd5c9a0

+ 3 - 2
mcs/class/System/System.Net.Sockets/ChangeLog

@@ -1,6 +1,7 @@
 2002-05-17  Lawrence Pit <[email protected]>
-	* UdpClient.cs and TcpClient.cs: modified disposable routines, added
-	checks for disposed state.
+ 	* TcpListener.cs: Renamed LocalEndPoint to LocalEndpoint
+	* NetworkStream.cs, UdpClient.cs and TcpClient.cs: modified disposable 
+	routines, added	checks for disposed state.
 	* UdpClient.cs: commented out GetHashCode and Equals as it's not
 	overriden in ms.net implementation.
 

+ 108 - 73
mcs/class/System/System.Net.Sockets/NetworkStream.cs

@@ -16,6 +16,7 @@ namespace System.Net.Sockets
 		Socket socket;
 		bool owns_socket;
 		bool readable, writeable;
+		bool disposed = false;
 		
 		public NetworkStream (Socket socket)
 			: this (socket, FileAccess.ReadWrite, false)
@@ -72,7 +73,11 @@ namespace System.Net.Sockets
 
 		public virtual bool DataAvailable {
 			get {
-				return socket.Available > 0;
+				try {
+					return socket.Available > 0;
+				} finally {
+					CheckDisposed ();
+				}
 			}
 		}
 
@@ -124,45 +129,50 @@ namespace System.Net.Sockets
 		public override IAsyncResult BeginRead (byte [] buffer, int offset, int size,
 							AsyncCallback callback, object state)
 		{
-			IAsyncResult retval;
-			
-			if (buffer == null)
-				throw new ArgumentNullException ();
-			if (socket == null)
-				throw new ObjectDisposedException ("socket");
-			int len = buffer.Length;
-			if (offset >= len || size != len)
-				throw new ArgumentOutOfRangeException ();
-
 			try {
-				retval = socket.BeginReceive (buffer, offset, size, 0, callback, state);
-			} catch {
-				throw new IOException ("BeginReceive failure");
+				IAsyncResult retval;
+
+				if (buffer == null)
+					throw new ArgumentNullException ();
+				int len = buffer.Length;
+				if (offset >= len || size != len)
+					throw new ArgumentOutOfRangeException ();
+
+				try {
+					retval = socket.BeginReceive (buffer, offset, size, 0, callback, state);
+				} catch {
+					throw new IOException ("BeginReceive failure");
+				}
+
+				return retval;
+			} finally {		
+				CheckDisposed ();				
 			}
-
-			return retval;
 		}
 
 		public override IAsyncResult BeginWrite (byte [] buffer, int offset, int size,
 							AsyncCallback callback, object state)
 		{
-			IAsyncResult retval;
-			
-			if (buffer == null)
-				throw new ArgumentNullException ();
-			if (socket == null)
-				throw new ObjectDisposedException ("socket");
-			int len = buffer.Length;
-			if (len < size)
-				throw new ArgumentException ();
-
 			try {
-				retval = socket.BeginSend (buffer, offset, size, 0, callback, state);
-			} catch {
-				throw new IOException ("BeginWrite failure");
-			}
+				IAsyncResult retval;
+
+				if (buffer == null)
+					throw new ArgumentNullException ();
+
+				int len = buffer.Length;
+				if (len < size)
+					throw new ArgumentException ();
 
-			return retval;
+				try {
+					retval = socket.BeginSend (buffer, offset, size, 0, callback, state);
+				} catch {
+					throw new IOException ("BeginWrite failure");
+				}
+
+				return retval;
+			} finally {
+				CheckDisposed ();
+			}
 		}
 
 		~NetworkStream ()
@@ -172,45 +182,55 @@ namespace System.Net.Sockets
 		
 		public override void Close ()
 		{
-			Dispose (true);
+			((IDisposable) this).Dispose ();
 		}
 
 		protected virtual void Dispose (bool disposing)
 		{
-			if (owns_socket)
-				if (socket != null)
-					socket.Close ();
+			if (disposed) 
+				return;
+			disposed = true;
+			
+			if (owns_socket) {
+				Socket s = socket;
+				if (s != null)
+					s.Close ();
+			}
 			socket = null;
 		}
 
 		public override int EndRead (IAsyncResult ar)
 		{
-			int res;
-			
-			if (ar == null)
-				throw new ArgumentNullException ();
-			if (socket == null)
-				throw new ObjectDisposedException ("socket");
-
 			try {
-				res = socket.EndReceive (ar);
-			} catch {
-				throw new IOException ("EndRead failure");
+				int res;
+
+				if (ar == null)
+					throw new ArgumentNullException ();
+
+				try {
+					res = socket.EndReceive (ar);
+				} catch {
+					throw new IOException ("EndRead failure");
+				}
+				return res;
+			} finally {
+				CheckDisposed ();
 			}
-			return res;
 		}
 
 		public override void EndWrite (IAsyncResult ar)
 		{
-			if (ar == null)
-				throw new ArgumentNullException ();
-			if (socket == null)
-				throw new ObjectDisposedException ("socket");
-
-			try {
-				socket.EndSend (ar);
-			} catch {
-				throw new IOException ("EndWrite failure");
+			try {			
+				if (ar == null)
+					throw new ArgumentNullException ();
+
+				try {
+					socket.EndSend (ar);
+				} catch {
+					throw new IOException ("EndWrite failure");
+				}
+			} finally {
+				CheckDisposed ();
 			}
 		}
 
@@ -222,23 +242,28 @@ namespace System.Net.Sockets
 		void IDisposable.Dispose ()
 		{
 			Dispose (true);
+			GC.SuppressFinalize (this);
 		}
 
 		public override int Read (byte [] buffer, int offset, int size)
 		{
-			int res;
-					
-			if (buffer == null)
-				throw new ArgumentNullException ();
-			if (buffer.Length < size)
-				throw new ArgumentException ();
-
 			try {
-				res = socket.Receive (buffer, offset, size, 0);
-			} catch {
-				throw new IOException ("Read failure");
+				int res;
+
+				if (buffer == null)
+					throw new ArgumentNullException ();
+				if (buffer.Length < size)
+					throw new ArgumentException ();
+
+				try {
+					res = socket.Receive (buffer, offset, size, 0);
+				} catch {
+					throw new IOException ("Read failure");
+				}
+				return res;
+			} finally { 
+				CheckDisposed ();
 			}
-			return res;
 		}
 
 		public override long Seek (long offset, SeekOrigin origin)
@@ -257,16 +282,26 @@ namespace System.Net.Sockets
 
 		public override void Write (byte [] buffer, int offset, int size)
 		{
-			if (buffer == null)
-				throw new ArgumentNullException ();
-			if (buffer.Length < size)
-				throw new ArgumentException ();
 			try {
-				socket.Send (buffer, offset, size, 0);
-			} catch {
-				throw new IOException ("Write failure"); 
+				if (buffer == null)
+					throw new ArgumentNullException ();
+				if (buffer.Length < size)
+					throw new ArgumentException ();
+				try {
+					socket.Send (buffer, offset, size, 0);
+				} catch {
+					throw new IOException ("Write failure"); 
+				}
+			} finally {
+				CheckDisposed ();
 			}
 		}
+		
+		private void CheckDisposed ()
+		{
+			if (disposed)
+				throw new ObjectDisposedException (GetType().FullName);
+		}		
 	     
 	}
 }

+ 11 - 14
mcs/class/System/System.Net.Sockets/TcpClient.cs

@@ -227,7 +227,7 @@ namespace System.Net.Sockets
 		/// </summary>
 		public void Close ()
 		{
-			Dispose();
+			((IDisposable) this).Dispose ();
 		}
 		
 		/// <summary>
@@ -273,23 +273,20 @@ namespace System.Net.Sockets
 		[MonoTODO]
 		public void Connect (string hostname, int port)
 		{
-			try {
-				IPHostEntry host = Dns.GetHostByName(hostname);
-				/* TODO: This will connect to the first IP address returned
-				from GetHostByName.  Is that right? */
-				Connect(new IPEndPoint(host.AddressList[0], port));
-			} finally {
-				CheckDisposed ();
-			}
+			CheckDisposed ();
+			IPHostEntry host = Dns.GetHostByName(hostname);
+			/* TODO: This will connect to the first IP address returned
+			from GetHostByName.  Is that right? */
+			Connect(new IPEndPoint(host.AddressList[0], port));
 		}
 		
 		/// <summary>
 		/// Gets rid of all managed resources
 		/// </summary>
-		public void Dispose()
+		void IDisposable.Dispose ()
 		{
-			Dispose(true);
-			GC.SuppressFinalize(this);
+			Dispose (true);
+			GC.SuppressFinalize (this);
 		}
 
 		/// <summary>
@@ -312,8 +309,8 @@ namespace System.Net.Sockets
 				s.Close();
 				active = false;
 				s = null;
-				client = null;
 			}
+			client = null;
 		}
 		
 		/// <summary>
@@ -321,7 +318,7 @@ namespace System.Net.Sockets
 		/// </summary>
 		~TcpClient ()
 		{
-			Dispose(false);
+			Dispose (false);
 		}
 		
 		/// <returns>A NetworkStream object connected to the

+ 1 - 1
mcs/class/System/System.Net.Sockets/TcpListener.cs

@@ -85,7 +85,7 @@ namespace System.Net.Sockets
 		/// <summary>
 		/// The local end point
 		/// </summary>
-		public EndPoint LocalEndPoint
+		public EndPoint LocalEndpoint
 		{
 			get { return server.LocalEndPoint; }
 		}

+ 7 - 9
mcs/class/System/System.Net.Sockets/UdpClient.cs

@@ -28,9 +28,7 @@ namespace System.Net.Sockets
 
 		public UdpClient (int port)
 		{
-			if (port < IPEndPoint.MinPort || port > IPEndPoint.MaxPort)
-				throw new ArgumentException ("Invalid port");
-
+			// IPEndPoint throws ArgumentException when port is invalid
 			localEP = new IPEndPoint (IPAddress.Any, port);
 			InitSocket ();
 		}
@@ -69,7 +67,7 @@ namespace System.Net.Sockets
 #region Close
 		public void Close ()
 		{
-			this.Dispose ();	
+			((IDisposable) this).Dispose ();	
 		}
 #endregion
 #region Connect
@@ -94,7 +92,7 @@ namespace System.Net.Sockets
 		}
 #endregion
 #region Multicast methods
-		public void DropMulticastGroup(IPAddress multicastAddr)
+		public void DropMulticastGroup (IPAddress multicastAddr)
 		{
 			try {
 				socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
@@ -104,7 +102,7 @@ namespace System.Net.Sockets
 			}
 		}
 
-		public void JoinMulticastGroup(IPAddress multicastAddr)
+		public void JoinMulticastGroup (IPAddress multicastAddr)
 		{
 			try {
 				socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
@@ -114,10 +112,10 @@ namespace System.Net.Sockets
 			}
 		}
 
-		public void JoinMulticastGroup(IPAddress multicastAddr, int timeToLive)
+		public void JoinMulticastGroup (IPAddress multicastAddr, int timeToLive)
 		{
+			JoinMulticastGroup (multicastAddr);
 			try {
-				JoinMulticastGroup (multicastAddr);
 				socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive,
 							timeToLive);
 			} finally {
@@ -232,7 +230,7 @@ namespace System.Net.Sockets
 */
 
 #region Disposing
-		public void Dispose()
+		void IDisposable.Dispose ()
 		{
 			Dispose (true);
 			GC.SuppressFinalize (this);