diff options
author | Dick Porter <dick@acm.org> | 2007-01-24 18:26:47 +0300 |
---|---|---|
committer | Dick Porter <dick@acm.org> | 2007-01-24 18:26:47 +0300 |
commit | 164ee390b71d5b6b54b4be15d7c6d40564ea04c1 (patch) | |
tree | ac0c902d57555e9fe7bf21286147f6b585cc50e3 | |
parent | 8bb4414a5bb0099b8d3444bd40fcc7da0e2a9411 (diff) |
2007-01-24 Dick Porter <dick@ximian.com>
* NetworkStream.cs: 2.0 profile updates, based on a patch by
Sridhar Kulkarni (sridharkulkarni@gmail.com)
2007-01-24 Dick Porter <dick@ximian.com>
* NetworkStreamTest.cs: Test 2.0 ReadTimeout property
svn path=/trunk/mcs/; revision=71598
5 files changed, 147 insertions, 0 deletions
diff --git a/mcs/class/System/System.Net.Sockets/ChangeLog b/mcs/class/System/System.Net.Sockets/ChangeLog index ad1e59c3ce7..1f60e58e4fe 100644 --- a/mcs/class/System/System.Net.Sockets/ChangeLog +++ b/mcs/class/System/System.Net.Sockets/ChangeLog @@ -1,3 +1,8 @@ +2007-01-24 Dick Porter <dick@ximian.com> + + * NetworkStream.cs: 2.0 profile updates, based on a patch by + Sridhar Kulkarni (sridharkulkarni@gmail.com) + 2007-01-23 Dick Porter <dick@ximian.com> * TcpListener.cs: diff --git a/mcs/class/System/System.Net.Sockets/NetworkStream.cs b/mcs/class/System/System.Net.Sockets/NetworkStream.cs index c7ec9b71636..7c7b457c05c 100644 --- a/mcs/class/System/System.Net.Sockets/NetworkStream.cs +++ b/mcs/class/System/System.Net.Sockets/NetworkStream.cs @@ -3,8 +3,10 @@ // // Author: // Miguel de Icaza (miguel@ximian.com) +// Sridhar Kulkarni <sridharkulkarni@gmail.com> // // (C) 2002 Ximian, Inc. http://www.ximian.com +// Copyright (C) 2002-2006 Novell, Inc. http://www.novell.com // // @@ -30,6 +32,10 @@ using System.IO; using System.Runtime.InteropServices; +#if NET_2_0 +using System.Timers; +using System.Threading; +#endif namespace System.Net.Sockets { @@ -87,6 +93,15 @@ namespace System.Net.Sockets } } +#if NET_2_0 + public override bool CanTimeout + { + get { + return(true); + } + } +#endif + public override bool CanWrite { get { return access == FileAccess.ReadWrite || access == FileAccess.Write; @@ -129,6 +144,22 @@ namespace System.Net.Sockets } } +#if NET_2_0 + public override int ReadTimeout + { + get { + return(socket.ReceiveTimeout); + } + set { + if (value <= 0 && value != Timeout.Infinite) { + throw new ArgumentOutOfRangeException ("value", "The value specified is less than or equal to zero and is not Infinite."); + } + + socket.ReceiveTimeout = value; + } + } +#endif + protected Socket Socket { get { return socket; @@ -145,6 +176,22 @@ namespace System.Net.Sockets } } +#if NET_2_0 + public override int WriteTimeout + { + get { + return(socket.SendTimeout); + } + set { + if (value <= 0 && value != Timeout.Infinite) { + throw new ArgumentOutOfRangeException ("value", "The value specified is less than or equal to zero and is not Infinite"); + } + + socket.SendTimeout = value; + } + } +#endif + public override IAsyncResult BeginRead (byte [] buffer, int offset, int size, AsyncCallback callback, object state) { @@ -206,6 +253,29 @@ namespace System.Net.Sockets ((IDisposable) this).Dispose (); } +#if NET_2_0 + public void Close (int timeout) + { + if (timeout < -1) { + throw new ArgumentOutOfRangeException ("timeout", "timeout is less than -1"); + } + + System.Timers.Timer close_timer = new System.Timers.Timer (); + close_timer.Elapsed += new ElapsedEventHandler (OnTimeoutClose); + /* NB timeout is in milliseconds here, cf + * seconds in Socket.Close(int) + */ + close_timer.Interval = timeout; + close_timer.AutoReset = false; + close_timer.Enabled = true; + } + + private void OnTimeoutClose (object source, ElapsedEventArgs e) + { + this.Close (); + } +#endif + protected #if NET_2_0 override diff --git a/mcs/class/System/System_test.dll.sources b/mcs/class/System/System_test.dll.sources index 9c371fb5104..da97b78500d 100644 --- a/mcs/class/System/System_test.dll.sources +++ b/mcs/class/System/System_test.dll.sources @@ -184,6 +184,7 @@ System.Net/WebRequestTest.cs System.Net.Configuration/ConnectionManagementSectionTest.cs System.Net.Configuration/HttpWebRequestElementTest.cs System.Net.Configuration/WebRequestModulesSectionTest.cs +System.Net.Sockets/NetworkStreamTest.cs System.Net.Sockets/TcpClientTest.cs System.Net.Sockets/TcpListenerTest.cs System.Net.Sockets/SocketTest.cs diff --git a/mcs/class/System/Test/System.Net.Sockets/ChangeLog b/mcs/class/System/Test/System.Net.Sockets/ChangeLog index aa34326e942..2bc17a588f7 100644 --- a/mcs/class/System/Test/System.Net.Sockets/ChangeLog +++ b/mcs/class/System/Test/System.Net.Sockets/ChangeLog @@ -1,3 +1,7 @@ +2007-01-24 Dick Porter <dick@ximian.com> + + * NetworkStreamTest.cs: Test 2.0 ReadTimeout property + 2007-01-23 Dick Porter <dick@ximian.com> * TcpListenerTest.cs: Test 2.0 Start(int) overload diff --git a/mcs/class/System/Test/System.Net.Sockets/NetworkStreamTest.cs b/mcs/class/System/Test/System.Net.Sockets/NetworkStreamTest.cs new file mode 100644 index 00000000000..043e023ee57 --- /dev/null +++ b/mcs/class/System/Test/System.Net.Sockets/NetworkStreamTest.cs @@ -0,0 +1,67 @@ +// System.Net.Sockets.NetworkStreamTest.cs +// +// Author: +// Dick Porter (dick@ximian.com) +// +// Copyright (C) 2007 Novell, Inc (http://www.novell.com) +// + +using System.Net.Sockets; +using System.Net; +using System; +using System.IO; +using NUnit.Framework; + + +namespace MonoTests.System.Net.Sockets +{ + [TestFixture] + public class NetworkStreamTest + { +#if NET_2_0 + [Test] + public void ReadTimeout () + { + Socket sock = new Socket (AddressFamily.InterNetwork, + SocketType.Stream, + ProtocolType.Tcp); + Socket listen = new Socket (AddressFamily.InterNetwork, + SocketType.Stream, + ProtocolType.Tcp); + IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 0); + + listen.Bind (ep); + listen.Listen (1); + + sock.Connect (listen.LocalEndPoint); + + NetworkStream stream = new NetworkStream (sock); + stream.ReadTimeout = 1000; + + byte[] buf = new byte[1024]; + + try { + stream.Read (buf, 0, buf.Length); + Assert.Fail ("ReadTimeout #1"); + } catch (IOException ex) { + Exception inner = ex.InnerException; + SocketException sockex = inner as SocketException; + + Assert.IsNotNull (sockex, "ReadTimeout #2"); + +/* Linux gives error 10035 (EWOULDBLOCK) here, whereas windows has 10060 (ETIMEDOUT) + Assertion.AssertEquals ("ReadTimeout #3", + 10060, + sockex.ErrorCode); +*/ + } catch { + Assert.Fail ("ReadTimeout #4"); + } finally { + stream.Close (); + sock.Close (); + listen.Close (); + } + } +#endif + } +} |