Forráskód Böngészése

Add async methods to System.Net.Sockets

Marek Safar 14 éve
szülő
commit
c68fa87950

+ 21 - 1
mcs/class/System/System.Net.Sockets/TcpClient.cs

@@ -4,6 +4,7 @@
 // 	Phillip Pearson ([email protected])
 //	Gonzalo Paniagua Javier ([email protected])
 //	Sridhar Kulkarni ([email protected])
+//	Marek Safar ([email protected])
 //
 // Copyright (C) 2001, Phillip Pearson http://www.myelin.co.nz
 // Copyright (c) 2006 Novell, Inc. (http://www.novell.com)
@@ -33,6 +34,9 @@
 
 using System;
 using System.Net;
+#if NET_4_5
+using System.Threading.Tasks;
+#endif
 
 namespace System.Net.Sockets
 {
@@ -435,7 +439,23 @@ namespace System.Net.Sockets
 			}
 			finally { CheckDisposed (); }
 		}
-		
+
+#if NET_4_5
+		public Task ConnectAsync (IPAddress address, int port)
+		{
+			return Task.Factory.FromAsync (BeginConnect, EndConnect, address, port, null);
+		}
+
+		public Task ConnectAsync (IPAddress[] addresses, int port)
+		{
+			return Task.Factory.FromAsync (BeginConnect, EndConnect, addresses, port, null);
+		}
+
+		public Task ConnectAsync (string host, int port)
+		{
+			return Task.Factory.FromAsync (BeginConnect, EndConnect, host, port, null);
+		}
+#endif
 		private void CheckDisposed ()
 		{
 			if (disposed)

+ 16 - 0
mcs/class/System/System.Net.Sockets/TcpListener.cs

@@ -5,6 +5,8 @@
 //    Gonzalo Paniagua Javier ([email protected])
 //	  Patrik Torstensson
 //    Sridhar Kulkarni ([email protected])
+//    Marek Safar ([email protected])
+
 //
 // Copyright (C) 2001, Phillip Pearson http://www.myelin.co.nz
 //
@@ -36,6 +38,9 @@
 
 using System;
 using System.Net;
+#if NET_4_5
+using System.Threading.Tasks;
+#endif
 
 namespace System.Net.Sockets
 {
@@ -305,5 +310,16 @@ namespace System.Net.Sockets
 			Init (AddressFamily.InterNetwork, savedEP);
 		}
 
+#if NET_4_5
+		public Task<Socket> AcceptSocketAsync ()
+		{
+			return Task<Socket>.Factory.FromAsync (BeginAcceptSocket, EndAcceptSocket, null);
+		}
+
+		public Task<TcpClient> AcceptTcpClientAsync ()
+		{
+			return Task<TcpClient>.Factory.FromAsync (BeginAcceptTcpClient, EndAcceptTcpClient, null);
+		}
+#endif
 	}
 }

+ 37 - 4
mcs/class/System/System.Net.Sockets/UdpClient.cs

@@ -4,6 +4,7 @@
 // Author:
 //    Gonzalo Paniagua Javier <[email protected]>
 //    Sridhar Kulkarni ([email protected])
+//    Marek Safar ([email protected])
 //
 // Copyright (C) Ximian, Inc. http://www.ximian.com
 // Copyright 2011 Xamarin Inc.
@@ -32,6 +33,9 @@
 
 using System;
 using System.Net;
+#if NET_4_5
+using System.Threading.Tasks;
+#endif
 
 namespace System.Net.Sockets
 {
@@ -123,7 +127,6 @@ namespace System.Net.Sockets
 				socket.Bind (localEP);
 		}
 
-#region Public methods
 #region Close
 		public void Close ()
 		{
@@ -469,8 +472,7 @@ namespace System.Net.Sockets
 							requestCallback, state));
 		}
 		
-		public byte[] EndReceive (IAsyncResult asyncResult,
-					  ref IPEndPoint remoteEP)
+		public byte[] EndReceive (IAsyncResult asyncResult, ref IPEndPoint remoteEP)
 		{
 			CheckDisposed ();
 			
@@ -616,7 +618,38 @@ namespace System.Net.Sockets
 				throw new ObjectDisposedException (GetType().FullName);
 		}		
 #endregion
-#endregion
+
+#if NET_4_5
+		
+		public Task<UdpReceiveResult> ReceiveAsync ()
+		{
+			return Task<UdpReceiveResult>.Factory.FromAsync (BeginReceive, r => {
+				IPEndPoint remoteEndPoint = null;
+				return new UdpReceiveResult (EndReceive (r, ref remoteEndPoint), remoteEndPoint);
+			}, null);
+		}
+
+		public Task<int> SendAsync (byte[] datagram, int bytes)
+		{
+			return Task<int>.Factory.FromAsync (BeginSend, EndSend, datagram, bytes, null);
+		}
+
+		public Task<int> SendAsync (byte[] datagram, int bytes, IPEndPoint endPoint)
+		{
+			return Task<int>.Factory.FromAsync (BeginSend, EndSend, datagram, bytes, endPoint, null);
+		}
+
+		public Task<int> SendAsync (byte[] datagram, int bytes, string hostname, int port)
+		{
+			var t = Tuple.Create (datagram, bytes, hostname, port, this);
+
+			return Task<int>.Factory.FromAsync ((callback, state) => {
+				var d = (Tuple<byte[], int, string, int, UdpClient>) state;
+				return d.Item5.BeginSend (d.Item1, d.Item2, d.Item3, d.Item4, callback, null);
+			}, EndSend, t);
+ 
+		}
+#endif
 	}
 }
 

+ 78 - 0
mcs/class/System/System.Net.Sockets/UdpReceiveResult.cs

@@ -0,0 +1,78 @@
+//
+// UdpReceiveResult.cs
+//
+// Authors:
+//	Marek Safar  <[email protected]>
+//
+// Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System.Runtime.CompilerServices;
+
+namespace System.Net.Sockets
+{
+	public struct UdpReceiveResult : IEquatable<UdpReceiveResult>
+	{
+		public UdpReceiveResult (byte[] buffer, IPEndPoint remoteEndPoint)
+			: this ()
+		{
+			if (buffer == null) {
+				throw new ArgumentNullException ("buffer");
+			}
+
+			if (remoteEndPoint == null) {
+				throw new ArgumentNullException ("remoteEndPoint");
+			}
+
+			this.Buffer = buffer;
+			this.RemoteEndPoint = remoteEndPoint;
+		}
+
+		public byte[] Buffer { get; private set; }
+		public IPEndPoint RemoteEndPoint { get; private set; }
+
+		public override int GetHashCode ()
+		{
+			return RuntimeHelpers.GetHashCode (Buffer) ^ RuntimeHelpers.GetHashCode (RemoteEndPoint);
+		}
+
+		public override bool Equals (object obj)
+		{
+			return obj is UdpReceiveResult && Equals ((UdpReceiveResult) obj);
+		}
+
+		public bool Equals (UdpReceiveResult other)
+		{
+			return Buffer == other.Buffer && Equals (RemoteEndPoint, other.RemoteEndPoint);
+		}
+
+		public static bool operator == (UdpReceiveResult left, UdpReceiveResult right)
+		{
+			return left.Equals (right);
+		}
+
+		public static bool operator != (UdpReceiveResult left, UdpReceiveResult right)
+		{
+			return !left.Equals (right);
+		}
+	}
+}

+ 1 - 0
mcs/class/System/System.dll.sources

@@ -862,6 +862,7 @@ System.Net.Sockets/TcpClient.cs
 System.Net.Sockets/TcpListener.cs
 System.Net.Sockets/TransmitFileOptions.cs
 System.Net.Sockets/UdpClient.cs
+System.Net.Sockets/UdpReceiveResult.cs
 System/NetTcpStyleUriParser.cs
 System.Net/TransportType.cs
 System.Net/WebAsyncResult.cs