1 // System.Net.Sockets.Socket.cs
4 // Phillip Pearson (pp@myelin.co.nz)
5 // Dick Porter <dick@ximian.com>
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // Sridhar Kulkarni (sridharkulkarni@gmail.com)
8 // Brian Nickel (brian.nickel@gmail.com)
9 // Ludovic Henry (ludovic@xamarin.com)
11 // Copyright (C) 2001, 2002 Phillip Pearson and Ximian, Inc.
12 // http://www.myelin.co.nz
13 // (c) 2004-2011 Novell, Inc. (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 using System.Collections;
39 using System.Collections.Generic;
40 using System.Runtime.CompilerServices;
41 using System.Runtime.InteropServices;
42 using System.Threading;
43 using System.Reflection;
45 using System.Net.Configuration;
48 using System.Net.NetworkInformation;
50 namespace System.Net.Sockets
52 public partial class Socket : IDisposable
54 const int SOCKET_CLOSED_CODE = 10004;
55 const string TIMEOUT_EXCEPTION_MSG = "A connection attempt failed because the connected party did not properly respond" +
56 "after a period of time, or established connection failed because connected host has failed to respond";
58 /* true if we called Close_internal */
66 AddressFamily addressFamily;
67 SocketType socketType;
68 ProtocolType protocolType;
70 /* the field "m_Handle" is looked up by name by the runtime */
71 internal SafeSocketHandle m_Handle;
74 * This EndPoint is used when creating new endpoints. Because
75 * there are many types of EndPoints possible,
76 * seed_endpoint.Create(addr) is used for creating new ones.
77 * As such, this value is set on Bind, SentTo, ReceiveFrom,
80 internal EndPoint seed_endpoint = null;
82 internal SemaphoreSlim ReadSem = new SemaphoreSlim (1, 1);
83 internal SemaphoreSlim WriteSem = new SemaphoreSlim (1, 1);
85 internal bool is_blocking = true;
86 internal bool is_bound;
88 /* When true, the socket was connected at the time of the last IO operation */
89 internal bool is_connected;
92 internal bool connect_in_progress;
97 public Socket (SocketInformation socketInformation)
99 this.is_listening = (socketInformation.Options & SocketInformationOptions.Listening) != 0;
100 this.is_connected = (socketInformation.Options & SocketInformationOptions.Connected) != 0;
101 this.is_blocking = (socketInformation.Options & SocketInformationOptions.NonBlocking) == 0;
102 this.useOverlappedIO = (socketInformation.Options & SocketInformationOptions.UseOnlyOverlappedIO) != 0;
104 var result = Mono.DataConverter.Unpack ("iiiil", socketInformation.ProtocolInformation, 0);
106 this.addressFamily = (AddressFamily) (int) result [0];
107 this.socketType = (SocketType) (int) result [1];
108 this.protocolType = (ProtocolType) (int) result [2];
109 this.is_bound = (ProtocolType) (int) result [3] != 0;
110 this.m_Handle = new SafeSocketHandle ((IntPtr) (long) result [4], true);
112 InitializeSockets ();
117 /* private constructor used by Accept, which already has a socket handle to use */
118 internal Socket(AddressFamily family, SocketType type, ProtocolType proto, SafeSocketHandle safe_handle)
120 this.addressFamily = family;
121 this.socketType = type;
122 this.protocolType = proto;
124 this.m_Handle = safe_handle;
125 this.is_connected = true;
127 InitializeSockets ();
130 void SocketDefaults ()
133 /* Need to test IPv6 further */
134 if (addressFamily == AddressFamily.InterNetwork
135 // || addressFamily == AddressFamily.InterNetworkV6
137 /* This is the default, but it probably has nasty side
138 * effects on Linux, as the socket option is kludged by
139 * turning on or off PMTU discovery... */
140 this.DontFragment = false;
141 if (protocolType == ProtocolType.Tcp)
142 this.NoDelay = false;
143 } else if (addressFamily == AddressFamily.InterNetworkV6) {
144 this.DualMode = true;
147 /* Microsoft sets these to 8192, but we are going to keep them
148 * both to the OS defaults as these have a big performance impact.
149 * on WebClient performance. */
150 // this.ReceiveBufferSize = 8192;
151 // this.SendBufferSize = 8192;
152 } catch (SocketException) {
156 /* Creates a new system socket, returning the handle */
157 [MethodImplAttribute(MethodImplOptions.InternalCall)]
158 extern IntPtr Socket_internal (AddressFamily family, SocketType type, ProtocolType proto, out int error);
164 public int Available {
166 ThrowIfDisposedAndClosed ();
169 ret = Available_internal (m_Handle, out error);
172 throw new SocketException (error);
178 static int Available_internal (SafeSocketHandle safeHandle, out int error)
180 bool release = false;
182 safeHandle.DangerousAddRef (ref release);
183 return Available_internal (safeHandle.DangerousGetHandle (), out error);
186 safeHandle.DangerousRelease ();
190 /* Returns the amount of data waiting to be read on socket */
191 [MethodImplAttribute(MethodImplOptions.InternalCall)]
192 extern static int Available_internal (IntPtr socket, out int error);
194 // FIXME: import from referencesource
195 public bool EnableBroadcast {
197 ThrowIfDisposedAndClosed ();
199 if (protocolType != ProtocolType.Udp)
200 throw new SocketException ((int) SocketError.ProtocolOption);
202 return ((int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast)) != 0;
205 ThrowIfDisposedAndClosed ();
207 if (protocolType != ProtocolType.Udp)
208 throw new SocketException ((int) SocketError.ProtocolOption);
210 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, value ? 1 : 0);
214 public bool IsBound {
220 // FIXME: import from referencesource
221 public bool MulticastLoopback {
223 ThrowIfDisposedAndClosed ();
225 /* Even though this option can be set for TCP sockets on Linux, throw
226 * this exception anyway to be compatible (the MSDN docs say
227 * "Setting this property on a Transmission Control Protocol (TCP)
228 * socket will have no effect." but the MS runtime throws the
230 if (protocolType == ProtocolType.Tcp)
231 throw new SocketException ((int)SocketError.ProtocolOption);
233 switch (addressFamily) {
234 case AddressFamily.InterNetwork:
235 return ((int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback)) != 0;
236 case AddressFamily.InterNetworkV6:
237 return ((int) GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback)) != 0;
239 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
243 ThrowIfDisposedAndClosed ();
245 /* Even though this option can be set for TCP sockets on Linux, throw
246 * this exception anyway to be compatible (the MSDN docs say
247 * "Setting this property on a Transmission Control Protocol (TCP)
248 * socket will have no effect." but the MS runtime throws the
250 if (protocolType == ProtocolType.Tcp)
251 throw new SocketException ((int)SocketError.ProtocolOption);
253 switch (addressFamily) {
254 case AddressFamily.InterNetwork:
255 SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, value ? 1 : 0);
257 case AddressFamily.InterNetworkV6:
258 SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback, value ? 1 : 0);
261 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
266 // Wish: support non-IP endpoints.
267 public EndPoint LocalEndPoint {
269 ThrowIfDisposedAndClosed ();
271 /* If the seed EndPoint is null, Connect, Bind, etc has not yet
272 * been called. MS returns null in this case. */
273 if (seed_endpoint == null)
277 SocketAddress sa = LocalEndPoint_internal (m_Handle, (int) addressFamily, out error);
280 throw new SocketException (error);
282 return seed_endpoint.Create (sa);
286 static SocketAddress LocalEndPoint_internal (SafeSocketHandle safeHandle, int family, out int error)
288 bool release = false;
290 safeHandle.DangerousAddRef (ref release);
291 return LocalEndPoint_internal (safeHandle.DangerousGetHandle (), family, out error);
294 safeHandle.DangerousRelease ();
298 /* Returns the local endpoint details in addr and port */
299 [MethodImplAttribute(MethodImplOptions.InternalCall)]
300 extern static SocketAddress LocalEndPoint_internal (IntPtr socket, int family, out int error);
302 public bool Blocking {
303 get { return is_blocking; }
305 ThrowIfDisposedAndClosed ();
308 Blocking_internal (m_Handle, value, out error);
311 throw new SocketException (error);
317 static void Blocking_internal (SafeSocketHandle safeHandle, bool block, out int error)
319 bool release = false;
321 safeHandle.DangerousAddRef (ref release);
322 Blocking_internal (safeHandle.DangerousGetHandle (), block, out error);
325 safeHandle.DangerousRelease ();
329 [MethodImplAttribute(MethodImplOptions.InternalCall)]
330 internal extern static void Blocking_internal(IntPtr socket, bool block, out int error);
332 public bool Connected {
333 get { return is_connected; }
334 internal set { is_connected = value; }
337 // FIXME: import from referencesource
338 public bool NoDelay {
340 ThrowIfDisposedAndClosed ();
343 return ((int) GetSocketOption (SocketOptionLevel.Tcp, SocketOptionName.NoDelay)) != 0;
347 ThrowIfDisposedAndClosed ();
349 SetSocketOption (SocketOptionLevel.Tcp, SocketOptionName.NoDelay, value ? 1 : 0);
353 public EndPoint RemoteEndPoint {
355 ThrowIfDisposedAndClosed ();
357 /* If the seed EndPoint is null, Connect, Bind, etc has
358 * not yet been called. MS returns null in this case. */
359 if (!is_connected || seed_endpoint == null)
363 SocketAddress sa = RemoteEndPoint_internal (m_Handle, (int) addressFamily, out error);
366 throw new SocketException (error);
368 return seed_endpoint.Create (sa);
372 static SocketAddress RemoteEndPoint_internal (SafeSocketHandle safeHandle, int family, out int error)
374 bool release = false;
376 safeHandle.DangerousAddRef (ref release);
377 return RemoteEndPoint_internal (safeHandle.DangerousGetHandle (), family, out error);
380 safeHandle.DangerousRelease ();
384 /* Returns the remote endpoint details in addr and port */
385 [MethodImplAttribute(MethodImplOptions.InternalCall)]
386 extern static SocketAddress RemoteEndPoint_internal (IntPtr socket, int family, out int error);
392 public static void Select (IList checkRead, IList checkWrite, IList checkError, int microSeconds)
394 var list = new List<Socket> ();
395 AddSockets (list, checkRead, "checkRead");
396 AddSockets (list, checkWrite, "checkWrite");
397 AddSockets (list, checkError, "checkError");
400 throw new ArgumentNullException ("checkRead, checkWrite, checkError", "All the lists are null or empty.");
402 /* The 'sockets' array contains:
403 * - READ socket 0-n, null,
404 * - WRITE socket 0-n, null,
405 * - ERROR socket 0-n, null */
406 Socket [] sockets = list.ToArray ();
409 Select_internal (ref sockets, microSeconds, out error);
412 throw new SocketException (error);
414 if (sockets == null) {
415 if (checkRead != null)
417 if (checkWrite != null)
419 if (checkError != null)
425 int count = sockets.Length;
426 IList currentList = checkRead;
428 for (int i = 0; i < count; i++) {
429 Socket sock = sockets [i];
430 if (sock == null) { // separator
431 if (currentList != null) {
432 // Remove non-signaled sockets after the current one
433 int to_remove = currentList.Count - currentIdx;
434 for (int k = 0; k < to_remove; k++)
435 currentList.RemoveAt (currentIdx);
437 currentList = (mode == 0) ? checkWrite : checkError;
443 if (mode == 1 && currentList == checkWrite && !sock.is_connected) {
444 if ((int) sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error) == 0)
445 sock.is_connected = true;
448 /* Remove non-signaled sockets before the current one */
449 while (((Socket) currentList [currentIdx]) != sock)
450 currentList.RemoveAt (currentIdx);
456 static void AddSockets (List<Socket> sockets, IList list, string name)
459 foreach (Socket sock in list) {
460 if (sock == null) // MS throws a NullRef
461 throw new ArgumentNullException (name, "Contains a null element");
469 [MethodImplAttribute(MethodImplOptions.InternalCall)]
470 extern static void Select_internal (ref Socket [] sockets, int microSeconds, out int error);
476 public bool Poll (int microSeconds, SelectMode mode)
478 ThrowIfDisposedAndClosed ();
480 if (mode != SelectMode.SelectRead && mode != SelectMode.SelectWrite && mode != SelectMode.SelectError)
481 throw new NotSupportedException ("'mode' parameter is not valid.");
484 bool result = Poll_internal (m_Handle, mode, microSeconds, out error);
487 throw new SocketException (error);
489 if (mode == SelectMode.SelectWrite && result && !is_connected) {
490 /* Update the is_connected state; for non-blocking Connect()
491 * this is when we can find out that the connect succeeded. */
492 if ((int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error) == 0)
499 static bool Poll_internal (SafeSocketHandle safeHandle, SelectMode mode, int timeout, out int error)
501 bool release = false;
503 safeHandle.DangerousAddRef (ref release);
504 return Poll_internal (safeHandle.DangerousGetHandle (), mode, timeout, out error);
507 safeHandle.DangerousRelease ();
511 [MethodImplAttribute(MethodImplOptions.InternalCall)]
512 extern static bool Poll_internal (IntPtr socket, SelectMode mode, int timeout, out int error);
518 public Socket Accept()
520 ThrowIfDisposedAndClosed ();
523 SafeSocketHandle safe_handle = Accept_internal (this.m_Handle, out error, is_blocking);
527 error = SOCKET_CLOSED_CODE;
528 throw new SocketException(error);
531 Socket accepted = new Socket (this.AddressFamily, this.SocketType, this.ProtocolType, safe_handle) {
532 seed_endpoint = this.seed_endpoint,
533 Blocking = this.Blocking,
539 internal void Accept (Socket acceptSocket)
541 ThrowIfDisposedAndClosed ();
544 SafeSocketHandle safe_handle = Accept_internal (this.m_Handle, out error, is_blocking);
548 error = SOCKET_CLOSED_CODE;
549 throw new SocketException (error);
552 acceptSocket.addressFamily = this.AddressFamily;
553 acceptSocket.socketType = this.SocketType;
554 acceptSocket.protocolType = this.ProtocolType;
555 acceptSocket.m_Handle = safe_handle;
556 acceptSocket.is_connected = true;
557 acceptSocket.seed_endpoint = this.seed_endpoint;
558 acceptSocket.Blocking = this.Blocking;
560 // FIXME: figure out what if anything else needs to be reset
563 public bool AcceptAsync (SocketAsyncEventArgs e)
565 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
567 ThrowIfDisposedAndClosed ();
570 throw new InvalidOperationException ("You must call the Bind method before performing this operation.");
572 throw new InvalidOperationException ("You must call the Listen method before performing this operation.");
573 if (e.BufferList != null)
574 throw new ArgumentException ("Multiple buffers cannot be used with this method.");
576 throw new ArgumentOutOfRangeException ("e.Count");
578 Socket acceptSocket = e.AcceptSocket;
579 if (acceptSocket != null) {
580 if (acceptSocket.is_bound || acceptSocket.is_connected)
581 throw new InvalidOperationException ("AcceptSocket: The socket must not be bound or connected.");
584 InitSocketAsyncEventArgs (e, AcceptAsyncCallback, e, SocketOperation.Accept);
586 QueueIOSelectorJob (ReadSem, e.socket_async_result.Handle, new IOSelectorJob (IOOperation.Read, BeginAcceptCallback, e.socket_async_result));
591 static AsyncCallback AcceptAsyncCallback = new AsyncCallback (ares => {
592 SocketAsyncEventArgs e = (SocketAsyncEventArgs) ((SocketAsyncResult) ares).AsyncState;
594 if (Interlocked.Exchange (ref e.in_progress, 0) != 1)
595 throw new InvalidOperationException ("No operation in progress");
598 e.AcceptSocket = e.current_socket.EndAccept (ares);
599 } catch (SocketException ex) {
600 e.SocketError = ex.SocketErrorCode;
601 } catch (ObjectDisposedException) {
602 e.SocketError = SocketError.OperationAborted;
604 if (e.AcceptSocket == null)
605 e.AcceptSocket = new Socket (e.current_socket.AddressFamily, e.current_socket.SocketType, e.current_socket.ProtocolType, null);
610 public IAsyncResult BeginAccept(AsyncCallback callback, object state)
612 ThrowIfDisposedAndClosed ();
614 if (!is_bound || !is_listening)
615 throw new InvalidOperationException ();
617 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Accept);
619 QueueIOSelectorJob (ReadSem, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginAcceptCallback, sockares));
624 static IOAsyncCallback BeginAcceptCallback = new IOAsyncCallback (ares => {
625 SocketAsyncResult sockares = (SocketAsyncResult) ares;
626 Socket acc_socket = null;
628 if (sockares.AcceptSocket == null) {
629 acc_socket = sockares.socket.Accept ();
631 acc_socket = sockares.AcceptSocket;
632 sockares.socket.Accept (acc_socket);
635 } catch (Exception e) {
636 sockares.Complete (e);
639 sockares.Complete (acc_socket);
642 public IAsyncResult BeginAccept (Socket acceptSocket, int receiveSize, AsyncCallback callback, object state)
644 ThrowIfDisposedAndClosed ();
647 throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero");
649 if (acceptSocket != null) {
650 ThrowIfDisposedAndClosed (acceptSocket);
652 if (acceptSocket.IsBound)
653 throw new InvalidOperationException ();
655 /* For some reason the MS runtime
656 * barfs if the new socket is not TCP,
657 * even though it's just about to blow
658 * away all those parameters
660 if (acceptSocket.ProtocolType != ProtocolType.Tcp)
661 throw new SocketException ((int)SocketError.InvalidArgument);
664 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.AcceptReceive) {
665 Buffer = new byte [receiveSize],
668 SockFlags = SocketFlags.None,
669 AcceptSocket = acceptSocket,
672 QueueIOSelectorJob (ReadSem, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginAcceptReceiveCallback, sockares));
677 static IOAsyncCallback BeginAcceptReceiveCallback = new IOAsyncCallback (ares => {
678 SocketAsyncResult sockares = (SocketAsyncResult) ares;
679 Socket acc_socket = null;
682 if (sockares.AcceptSocket == null) {
683 acc_socket = sockares.socket.Accept ();
685 acc_socket = sockares.AcceptSocket;
686 sockares.socket.Accept (acc_socket);
688 } catch (Exception e) {
689 sockares.Complete (e);
693 /* It seems the MS runtime special-cases 0-length requested receive data. See bug 464201. */
695 if (sockares.Size > 0) {
698 total = acc_socket.Receive (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, out error);
700 sockares.Complete (new SocketException ((int) error));
703 } catch (Exception e) {
704 sockares.Complete (e);
709 sockares.Complete (acc_socket, total);
712 public Socket EndAccept (IAsyncResult asyncResult)
716 return EndAccept (out buffer, out bytes, asyncResult);
719 public Socket EndAccept (out byte[] buffer, out int bytesTransferred, IAsyncResult asyncResult)
721 ThrowIfDisposedAndClosed ();
723 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndAccept", "asyncResult");
725 if (!sockares.IsCompleted)
726 sockares.AsyncWaitHandle.WaitOne ();
728 sockares.CheckIfThrowDelayedException ();
730 buffer = sockares.Buffer;
731 bytesTransferred = sockares.Total;
733 return sockares.AcceptedSocket;
736 static SafeSocketHandle Accept_internal (SafeSocketHandle safeHandle, out int error, bool blocking)
739 safeHandle.RegisterForBlockingSyscall ();
740 var ret = Accept_internal (safeHandle.DangerousGetHandle (), out error, blocking);
741 return new SafeSocketHandle (ret, true);
743 safeHandle.UnRegisterForBlockingSyscall ();
747 /* Creates a new system socket, returning the handle */
748 [MethodImplAttribute(MethodImplOptions.InternalCall)]
749 extern static IntPtr Accept_internal (IntPtr sock, out int error, bool blocking);
755 public void Bind (EndPoint localEP)
757 #if FEATURE_NO_BSD_SOCKETS
758 throw new PlatformNotSupportedException ("System.Net.Sockets.Socket:Bind is not supported on this platform.");
760 ThrowIfDisposedAndClosed ();
763 throw new ArgumentNullException("localEP");
765 var ipEndPoint = localEP as IPEndPoint;
766 if (ipEndPoint != null) {
767 localEP = RemapIPEndPoint (ipEndPoint);
771 Bind_internal (m_Handle, localEP.Serialize(), out error);
774 throw new SocketException (error);
778 seed_endpoint = localEP;
779 #endif // FEATURE_NO_BSD_SOCKETS
782 private static void Bind_internal (SafeSocketHandle safeHandle, SocketAddress sa, out int error)
784 bool release = false;
786 safeHandle.DangerousAddRef (ref release);
787 Bind_internal (safeHandle.DangerousGetHandle (), sa, out error);
790 safeHandle.DangerousRelease ();
794 // Creates a new system socket, returning the handle
795 [MethodImplAttribute(MethodImplOptions.InternalCall)]
796 private extern static void Bind_internal(IntPtr sock, SocketAddress sa, out int error);
802 public void Listen (int backlog)
804 ThrowIfDisposedAndClosed ();
807 throw new SocketException ((int) SocketError.InvalidArgument);
810 Listen_internal(m_Handle, backlog, out error);
813 throw new SocketException (error);
818 static void Listen_internal (SafeSocketHandle safeHandle, int backlog, out int error)
820 bool release = false;
822 safeHandle.DangerousAddRef (ref release);
823 Listen_internal (safeHandle.DangerousGetHandle (), backlog, out error);
826 safeHandle.DangerousRelease ();
830 [MethodImplAttribute(MethodImplOptions.InternalCall)]
831 extern static void Listen_internal (IntPtr sock, int backlog, out int error);
837 public void Connect (IPAddress address, int port)
839 Connect (new IPEndPoint (address, port));
842 public void Connect (string host, int port)
844 Connect (Dns.GetHostAddresses (host), port);
847 public void Connect (EndPoint remoteEP)
849 ThrowIfDisposedAndClosed ();
851 if (remoteEP == null)
852 throw new ArgumentNullException ("remoteEP");
854 IPEndPoint ep = remoteEP as IPEndPoint;
855 /* Dgram uses Any to 'disconnect' */
856 if (ep != null && socketType != SocketType.Dgram) {
857 if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any))
858 throw new SocketException ((int) SocketError.AddressNotAvailable);
862 throw new InvalidOperationException ();
865 remoteEP = RemapIPEndPoint (ep);
868 SocketAddress serial = remoteEP.Serialize ();
871 Connect_internal (m_Handle, serial, out error, is_blocking);
873 if (error == 0 || error == 10035)
874 seed_endpoint = remoteEP; // Keep the ep around for non-blocking sockets
878 error = SOCKET_CLOSED_CODE;
879 throw new SocketException (error);
882 is_connected = !(socketType == SocketType.Dgram && ep != null && (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)));
886 public bool ConnectAsync (SocketAsyncEventArgs e)
888 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
890 ThrowIfDisposedAndClosed ();
893 throw new InvalidOperationException ("You may not perform this operation after calling the Listen method.");
894 if (e.RemoteEndPoint == null)
895 throw new ArgumentNullException ("remoteEP");
897 InitSocketAsyncEventArgs (e, null, e, SocketOperation.Connect);
900 IPAddress [] addresses;
901 SocketAsyncResult ares;
903 if (!GetCheckedIPs (e, out addresses)) {
904 //NOTE: DualMode may cause Socket's RemoteEndpoint to differ in AddressFamily from the
905 // SocketAsyncEventArgs, but the SocketAsyncEventArgs itself is not changed
906 ares = (SocketAsyncResult) BeginConnect (e.RemoteEndPoint, ConnectAsyncCallback, e);
908 DnsEndPoint dep = (DnsEndPoint)e.RemoteEndPoint;
909 ares = (SocketAsyncResult) BeginConnect (addresses, dep.Port, ConnectAsyncCallback, e);
912 if (ares.IsCompleted && ares.CompletedSynchronously) {
913 ares.CheckIfThrowDelayedException ();
916 } catch (Exception exc) {
917 e.socket_async_result.Complete (exc, true);
924 public static void CancelConnectAsync (SocketAsyncEventArgs e)
927 throw new ArgumentNullException("e");
929 if (e.in_progress != 0 && e.LastOperation == SocketAsyncOperation.Connect)
930 e.current_socket.Close();
933 static AsyncCallback ConnectAsyncCallback = new AsyncCallback (ares => {
934 SocketAsyncEventArgs e = (SocketAsyncEventArgs) ((SocketAsyncResult) ares).AsyncState;
936 if (Interlocked.Exchange (ref e.in_progress, 0) != 1)
937 throw new InvalidOperationException ("No operation in progress");
940 e.current_socket.EndConnect (ares);
941 } catch (SocketException se) {
942 e.SocketError = se.SocketErrorCode;
943 } catch (ObjectDisposedException) {
944 e.SocketError = SocketError.OperationAborted;
950 public IAsyncResult BeginConnect (string host, int port, AsyncCallback requestCallback, object state)
952 ThrowIfDisposedAndClosed ();
955 throw new ArgumentNullException ("host");
956 if (addressFamily != AddressFamily.InterNetwork && addressFamily != AddressFamily.InterNetworkV6)
957 throw new NotSupportedException ("This method is valid only for sockets in the InterNetwork and InterNetworkV6 families");
958 if (port <= 0 || port > 65535)
959 throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
961 throw new InvalidOperationException ();
963 return BeginConnect (Dns.GetHostAddresses (host), port, requestCallback, state);
966 public IAsyncResult BeginConnect (EndPoint remoteEP, AsyncCallback callback, object state)
968 ThrowIfDisposedAndClosed ();
970 if (remoteEP == null)
971 throw new ArgumentNullException ("remoteEP");
973 throw new InvalidOperationException ();
975 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Connect) {
979 BeginSConnect (sockares);
983 public IAsyncResult BeginConnect (IPAddress[] addresses, int port, AsyncCallback requestCallback, object state)
985 ThrowIfDisposedAndClosed ();
987 if (addresses == null)
988 throw new ArgumentNullException ("addresses");
989 if (addresses.Length == 0)
990 throw new ArgumentException ("Empty addresses list");
991 if (this.AddressFamily != AddressFamily.InterNetwork && this.AddressFamily != AddressFamily.InterNetworkV6)
992 throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families");
993 if (port <= 0 || port > 65535)
994 throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
996 throw new InvalidOperationException ();
998 SocketAsyncResult sockares = new SocketAsyncResult (this, requestCallback, state, SocketOperation.Connect) {
999 Addresses = addresses,
1003 is_connected = false;
1005 BeginMConnect (sockares);
1009 static void BeginMConnect (SocketAsyncResult sockares)
1011 Exception exc = null;
1013 for (int i = sockares.CurrentAddress; i < sockares.Addresses.Length; i++) {
1015 sockares.CurrentAddress++;
1016 sockares.EndPoint = new IPEndPoint (sockares.Addresses [i], sockares.Port);
1018 BeginSConnect (sockares);
1020 } catch (Exception e) {
1028 static void BeginSConnect (SocketAsyncResult sockares)
1030 EndPoint remoteEP = sockares.EndPoint;
1031 // Bug #75154: Connect() should not succeed for .Any addresses.
1032 if (remoteEP is IPEndPoint) {
1033 IPEndPoint ep = (IPEndPoint) remoteEP;
1034 if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)) {
1035 sockares.Complete (new SocketException ((int) SocketError.AddressNotAvailable), true);
1039 sockares.EndPoint = remoteEP = sockares.socket.RemapIPEndPoint (ep);
1044 if (sockares.socket.connect_in_progress) {
1045 // This could happen when multiple IPs are used
1046 // Calling connect() again will reset the connection attempt and cause
1047 // an error. Better to just close the socket and move on.
1048 sockares.socket.connect_in_progress = false;
1049 sockares.socket.m_Handle.Dispose ();
1050 sockares.socket.m_Handle = new SafeSocketHandle (sockares.socket.Socket_internal (sockares.socket.addressFamily, sockares.socket.socketType, sockares.socket.protocolType, out error), true);
1052 throw new SocketException (error);
1055 bool blk = sockares.socket.is_blocking;
1057 sockares.socket.Blocking = false;
1058 Connect_internal (sockares.socket.m_Handle, remoteEP.Serialize (), out error, false);
1060 sockares.socket.Blocking = true;
1064 sockares.socket.is_connected = true;
1065 sockares.socket.is_bound = true;
1066 sockares.Complete (true);
1070 if (error != (int) SocketError.InProgress && error != (int) SocketError.WouldBlock) {
1072 sockares.socket.is_connected = false;
1073 sockares.socket.is_bound = false;
1074 sockares.Complete (new SocketException (error), true);
1079 sockares.socket.is_connected = false;
1080 sockares.socket.is_bound = false;
1081 sockares.socket.connect_in_progress = true;
1083 IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, BeginConnectCallback, sockares));
1086 static IOAsyncCallback BeginConnectCallback = new IOAsyncCallback (ares => {
1087 SocketAsyncResult sockares = (SocketAsyncResult) ares;
1089 if (sockares.EndPoint == null) {
1090 sockares.Complete (new SocketException ((int)SocketError.AddressNotAvailable));
1095 int error = (int) sockares.socket.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
1098 sockares.socket.seed_endpoint = sockares.EndPoint;
1099 sockares.socket.is_connected = true;
1100 sockares.socket.is_bound = true;
1101 sockares.socket.connect_in_progress = false;
1103 sockares.Complete ();
1107 if (sockares.Addresses == null) {
1108 sockares.socket.connect_in_progress = false;
1109 sockares.Complete (new SocketException (error));
1113 if (sockares.CurrentAddress >= sockares.Addresses.Length) {
1114 sockares.Complete (new SocketException (error));
1118 BeginMConnect (sockares);
1119 } catch (Exception e) {
1120 sockares.socket.connect_in_progress = false;
1121 sockares.Complete (e);
1125 public void EndConnect (IAsyncResult asyncResult)
1127 ThrowIfDisposedAndClosed ();
1129 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndConnect", "asyncResult");
1131 if (!sockares.IsCompleted)
1132 sockares.AsyncWaitHandle.WaitOne();
1134 sockares.CheckIfThrowDelayedException();
1137 static void Connect_internal (SafeSocketHandle safeHandle, SocketAddress sa, out int error, bool blocking)
1140 safeHandle.RegisterForBlockingSyscall ();
1141 Connect_internal (safeHandle.DangerousGetHandle (), sa, out error, blocking);
1143 safeHandle.UnRegisterForBlockingSyscall ();
1147 /* Connects to the remote address */
1148 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1149 extern static void Connect_internal(IntPtr sock, SocketAddress sa, out int error, bool blocking);
1152 * - false when it is ok to use RemoteEndPoint
1153 * - true when addresses must be used (and addresses could be null/empty) */
1154 bool GetCheckedIPs (SocketAsyncEventArgs e, out IPAddress [] addresses)
1158 // Connect to the first address that match the host name, like:
1159 // http://blogs.msdn.com/ncl/archive/2009/07/20/new-ncl-features-in-net-4-0-beta-2.aspx
1160 // while skipping entries that do not match the address family
1161 DnsEndPoint dep = e.RemoteEndPoint as DnsEndPoint;
1163 addresses = Dns.GetHostAddresses (dep.Host);
1166 e.ConnectByNameError = null;
1175 /* According to the docs, the MS runtime will throw PlatformNotSupportedException
1176 * if the platform is newer than w2k. We should be able to cope... */
1177 public void Disconnect (bool reuseSocket)
1179 ThrowIfDisposedAndClosed ();
1182 Disconnect_internal (m_Handle, reuseSocket, out error);
1186 /* ERROR_NOT_SUPPORTED */
1187 throw new PlatformNotSupportedException ();
1189 throw new SocketException (error);
1193 is_connected = false;
1195 /* Do managed housekeeping here... */
1199 public bool DisconnectAsync (SocketAsyncEventArgs e)
1201 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1203 ThrowIfDisposedAndClosed ();
1205 InitSocketAsyncEventArgs (e, DisconnectAsyncCallback, e, SocketOperation.Disconnect);
1207 IOSelector.Add (e.socket_async_result.Handle, new IOSelectorJob (IOOperation.Write, BeginDisconnectCallback, e.socket_async_result));
1212 static AsyncCallback DisconnectAsyncCallback = new AsyncCallback (ares => {
1213 SocketAsyncEventArgs e = (SocketAsyncEventArgs) ((SocketAsyncResult) ares).AsyncState;
1215 if (Interlocked.Exchange (ref e.in_progress, 0) != 1)
1216 throw new InvalidOperationException ("No operation in progress");
1219 e.current_socket.EndDisconnect (ares);
1220 } catch (SocketException ex) {
1221 e.SocketError = ex.SocketErrorCode;
1222 } catch (ObjectDisposedException) {
1223 e.SocketError = SocketError.OperationAborted;
1229 public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state)
1231 ThrowIfDisposedAndClosed ();
1233 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Disconnect) {
1234 ReuseSocket = reuseSocket,
1237 IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, BeginDisconnectCallback, sockares));
1242 static IOAsyncCallback BeginDisconnectCallback = new IOAsyncCallback (ares => {
1243 SocketAsyncResult sockares = (SocketAsyncResult) ares;
1246 sockares.socket.Disconnect (sockares.ReuseSocket);
1247 } catch (Exception e) {
1248 sockares.Complete (e);
1252 sockares.Complete ();
1255 public void EndDisconnect (IAsyncResult asyncResult)
1257 ThrowIfDisposedAndClosed ();
1259 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndDisconnect", "asyncResult");
1261 if (!sockares.IsCompleted)
1262 sockares.AsyncWaitHandle.WaitOne ();
1264 sockares.CheckIfThrowDelayedException ();
1267 static void Disconnect_internal (SafeSocketHandle safeHandle, bool reuse, out int error)
1269 bool release = false;
1271 safeHandle.DangerousAddRef (ref release);
1272 Disconnect_internal (safeHandle.DangerousGetHandle (), reuse, out error);
1275 safeHandle.DangerousRelease ();
1279 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1280 extern static void Disconnect_internal (IntPtr sock, bool reuse, out int error);
1286 public int Receive (byte [] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode)
1288 ThrowIfDisposedAndClosed ();
1289 ThrowIfBufferNull (buffer);
1290 ThrowIfBufferOutOfRange (buffer, offset, size);
1295 fixed (byte* pbuffer = buffer) {
1296 ret = Receive_internal (m_Handle, &pbuffer[offset], size, socketFlags, out nativeError, is_blocking);
1300 errorCode = (SocketError) nativeError;
1301 if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress) {
1302 is_connected = false;
1305 is_connected = true;
1311 [CLSCompliant (false)]
1312 public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
1314 ThrowIfDisposedAndClosed ();
1316 if (buffers == null || buffers.Count == 0)
1317 throw new ArgumentNullException ("buffers");
1319 int numsegments = buffers.Count;
1323 GCHandle[] gch = new GCHandle[numsegments];
1326 fixed (WSABUF* bufarray = new WSABUF[numsegments]) {
1327 for (int i = 0; i < numsegments; i++) {
1328 ArraySegment<byte> segment = buffers[i];
1330 if (segment.Offset < 0 || segment.Count < 0 || segment.Count > segment.Array.Length - segment.Offset)
1331 throw new ArgumentOutOfRangeException ("segment");
1334 gch[i] = GCHandle.Alloc (segment.Array, GCHandleType.Pinned);
1337 bufarray[i].len = segment.Count;
1338 bufarray[i].buf = Marshal.UnsafeAddrOfPinnedArrayElement (segment.Array, segment.Offset);
1341 ret = Receive_internal (m_Handle, bufarray, numsegments, socketFlags, out nativeError, is_blocking);
1345 for (int i = 0; i < numsegments; i++) {
1346 if (gch[i].IsAllocated)
1351 errorCode = (SocketError) nativeError;
1356 public bool ReceiveAsync (SocketAsyncEventArgs e)
1358 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1360 ThrowIfDisposedAndClosed ();
1362 // LAME SPEC: the ArgumentException is never thrown, instead an NRE is
1363 // thrown when e.Buffer and e.BufferList are null (works fine when one is
1364 // set to a valid object)
1365 if (e.Buffer == null && e.BufferList == null)
1366 throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
1368 if (e.Buffer == null) {
1369 InitSocketAsyncEventArgs (e, ReceiveAsyncCallback, e, SocketOperation.ReceiveGeneric);
1371 e.socket_async_result.Buffers = e.BufferList;
1373 QueueIOSelectorJob (ReadSem, e.socket_async_result.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveGenericCallback, e.socket_async_result));
1375 InitSocketAsyncEventArgs (e, ReceiveAsyncCallback, e, SocketOperation.Receive);
1377 e.socket_async_result.Buffer = e.Buffer;
1378 e.socket_async_result.Offset = e.Offset;
1379 e.socket_async_result.Size = e.Count;
1381 QueueIOSelectorJob (ReadSem, e.socket_async_result.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveCallback, e.socket_async_result));
1387 static AsyncCallback ReceiveAsyncCallback = new AsyncCallback (ares => {
1388 SocketAsyncEventArgs e = (SocketAsyncEventArgs) ((SocketAsyncResult) ares).AsyncState;
1390 if (Interlocked.Exchange (ref e.in_progress, 0) != 1)
1391 throw new InvalidOperationException ("No operation in progress");
1394 e.BytesTransferred = e.current_socket.EndReceive (ares);
1395 } catch (SocketException se){
1396 e.SocketError = se.SocketErrorCode;
1397 } catch (ObjectDisposedException) {
1398 e.SocketError = SocketError.OperationAborted;
1404 public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
1406 ThrowIfDisposedAndClosed ();
1407 ThrowIfBufferNull (buffer);
1408 ThrowIfBufferOutOfRange (buffer, offset, size);
1410 /* As far as I can tell from the docs and from experimentation, a pointer to the
1411 * SocketError parameter is not supposed to be saved for the async parts. And as we don't
1412 * set any socket errors in the setup code, we just have to set it to Success. */
1413 errorCode = SocketError.Success;
1415 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Receive) {
1419 SockFlags = socketFlags,
1422 QueueIOSelectorJob (ReadSem, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveCallback, sockares));
1427 static IOAsyncCallback BeginReceiveCallback = new IOAsyncCallback (ares => {
1428 SocketAsyncResult sockares = (SocketAsyncResult) ares;
1433 fixed (byte* pbuffer = sockares.Buffer) {
1434 total = Receive_internal (sockares.socket.m_Handle, &pbuffer[sockares.Offset], sockares.Size, sockares.SockFlags, out sockares.error, sockares.socket.is_blocking);
1437 } catch (Exception e) {
1438 sockares.Complete (e);
1442 sockares.Complete (total);
1445 [CLSCompliant (false)]
1446 public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
1448 ThrowIfDisposedAndClosed ();
1450 if (buffers == null)
1451 throw new ArgumentNullException ("buffers");
1453 /* I assume the same SocketError semantics as above */
1454 errorCode = SocketError.Success;
1456 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.ReceiveGeneric) {
1458 SockFlags = socketFlags,
1461 QueueIOSelectorJob (ReadSem, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveGenericCallback, sockares));
1466 static IOAsyncCallback BeginReceiveGenericCallback = new IOAsyncCallback (ares => {
1467 SocketAsyncResult sockares = (SocketAsyncResult) ares;
1471 total = sockares.socket.Receive (sockares.Buffers, sockares.SockFlags);
1472 } catch (Exception e) {
1473 sockares.Complete (e);
1477 sockares.Complete (total);
1480 public int EndReceive (IAsyncResult asyncResult, out SocketError errorCode)
1482 ThrowIfDisposedAndClosed ();
1484 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndReceive", "asyncResult");
1486 if (!sockares.IsCompleted)
1487 sockares.AsyncWaitHandle.WaitOne ();
1489 errorCode = sockares.ErrorCode;
1491 if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress)
1492 is_connected = false;
1494 // If no socket error occurred, call CheckIfThrowDelayedException in case there are other
1495 // kinds of exceptions that should be thrown.
1496 if (errorCode == SocketError.Success)
1497 sockares.CheckIfThrowDelayedException();
1499 return sockares.Total;
1502 static unsafe int Receive_internal (SafeSocketHandle safeHandle, WSABUF* bufarray, int count, SocketFlags flags, out int error, bool blocking)
1505 safeHandle.RegisterForBlockingSyscall ();
1506 return Receive_internal (safeHandle.DangerousGetHandle (), bufarray, count, flags, out error, blocking);
1508 safeHandle.UnRegisterForBlockingSyscall ();
1512 [MethodImplAttribute (MethodImplOptions.InternalCall)]
1513 extern static unsafe int Receive_internal (IntPtr sock, WSABUF* bufarray, int count, SocketFlags flags, out int error, bool blocking);
1515 static unsafe int Receive_internal (SafeSocketHandle safeHandle, byte* buffer, int count, SocketFlags flags, out int error, bool blocking)
1518 safeHandle.RegisterForBlockingSyscall ();
1519 return Receive_internal (safeHandle.DangerousGetHandle (), buffer, count, flags, out error, blocking);
1521 safeHandle.UnRegisterForBlockingSyscall ();
1525 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1526 extern static unsafe int Receive_internal(IntPtr sock, byte* buffer, int count, SocketFlags flags, out int error, bool blocking);
1532 public int ReceiveFrom (byte [] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP)
1534 ThrowIfDisposedAndClosed ();
1535 ThrowIfBufferNull (buffer);
1536 ThrowIfBufferOutOfRange (buffer, offset, size);
1538 if (remoteEP == null)
1539 throw new ArgumentNullException ("remoteEP");
1541 SocketError errorCode;
1542 int ret = ReceiveFrom (buffer, offset, size, socketFlags, ref remoteEP, out errorCode);
1544 if (errorCode != SocketError.Success)
1545 throw new SocketException (errorCode);
1550 internal int ReceiveFrom (byte [] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, out SocketError errorCode)
1552 SocketAddress sockaddr = remoteEP.Serialize();
1557 fixed (byte* pbuffer = buffer) {
1558 cnt = ReceiveFrom_internal (m_Handle, &pbuffer[offset], size, socketFlags, ref sockaddr, out nativeError, is_blocking);
1562 errorCode = (SocketError) nativeError;
1563 if (errorCode != SocketError.Success) {
1564 if (errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress) {
1565 is_connected = false;
1566 } else if (errorCode == SocketError.WouldBlock && is_blocking) { // This might happen when ReceiveTimeout is set
1567 errorCode = SocketError.TimedOut;
1573 is_connected = true;
1576 /* If sockaddr is null then we're a connection oriented protocol and should ignore the
1577 * remoteEP parameter (see MSDN documentation for Socket.ReceiveFrom(...) ) */
1578 if (sockaddr != null) {
1579 /* Stupidly, EndPoint.Create() is an instance method */
1580 remoteEP = remoteEP.Create (sockaddr);
1583 seed_endpoint = remoteEP;
1588 public bool ReceiveFromAsync (SocketAsyncEventArgs e)
1590 ThrowIfDisposedAndClosed ();
1592 // We do not support recv into multiple buffers yet
1593 if (e.BufferList != null)
1594 throw new NotSupportedException ("Mono doesn't support using BufferList at this point.");
1595 if (e.RemoteEndPoint == null)
1596 throw new ArgumentNullException ("remoteEP", "Value cannot be null.");
1598 InitSocketAsyncEventArgs (e, ReceiveFromAsyncCallback, e, SocketOperation.ReceiveFrom);
1600 e.socket_async_result.Buffer = e.Buffer;
1601 e.socket_async_result.Offset = e.Offset;
1602 e.socket_async_result.Size = e.Count;
1603 e.socket_async_result.EndPoint = e.RemoteEndPoint;
1604 e.socket_async_result.SockFlags = e.SocketFlags;
1606 QueueIOSelectorJob (ReadSem, e.socket_async_result.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveFromCallback, e.socket_async_result));
1611 static AsyncCallback ReceiveFromAsyncCallback = new AsyncCallback (ares => {
1612 SocketAsyncEventArgs e = (SocketAsyncEventArgs) ((SocketAsyncResult) ares).AsyncState;
1614 if (Interlocked.Exchange (ref e.in_progress, 0) != 1)
1615 throw new InvalidOperationException ("No operation in progress");
1618 e.BytesTransferred = e.current_socket.EndReceiveFrom (ares, ref e.remote_ep);
1619 } catch (SocketException ex) {
1620 e.SocketError = ex.SocketErrorCode;
1621 } catch (ObjectDisposedException) {
1622 e.SocketError = SocketError.OperationAborted;
1628 public IAsyncResult BeginReceiveFrom (byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, object state)
1630 ThrowIfDisposedAndClosed ();
1631 ThrowIfBufferNull (buffer);
1632 ThrowIfBufferOutOfRange (buffer, offset, size);
1634 if (remoteEP == null)
1635 throw new ArgumentNullException ("remoteEP");
1637 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.ReceiveFrom) {
1641 SockFlags = socketFlags,
1642 EndPoint = remoteEP,
1645 QueueIOSelectorJob (ReadSem, sockares.Handle, new IOSelectorJob (IOOperation.Read, BeginReceiveFromCallback, sockares));
1650 static IOAsyncCallback BeginReceiveFromCallback = new IOAsyncCallback (ares => {
1651 SocketAsyncResult sockares = (SocketAsyncResult) ares;
1655 SocketError errorCode;
1656 total = sockares.socket.ReceiveFrom (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, ref sockares.EndPoint, out errorCode);
1658 if (errorCode != SocketError.Success) {
1659 sockares.Complete (new SocketException (errorCode));
1662 } catch (Exception e) {
1663 sockares.Complete (e);
1667 sockares.Complete (total);
1670 public int EndReceiveFrom(IAsyncResult asyncResult, ref EndPoint endPoint)
1672 ThrowIfDisposedAndClosed ();
1674 if (endPoint == null)
1675 throw new ArgumentNullException ("endPoint");
1677 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndReceiveFrom", "asyncResult");
1679 if (!sockares.IsCompleted)
1680 sockares.AsyncWaitHandle.WaitOne();
1682 sockares.CheckIfThrowDelayedException();
1684 endPoint = sockares.EndPoint;
1686 return sockares.Total;
1691 static unsafe int ReceiveFrom_internal (SafeSocketHandle safeHandle, byte* buffer, int count, SocketFlags flags, ref SocketAddress sockaddr, out int error, bool blocking)
1694 safeHandle.RegisterForBlockingSyscall ();
1695 return ReceiveFrom_internal (safeHandle.DangerousGetHandle (), buffer, count, flags, ref sockaddr, out error, blocking);
1697 safeHandle.UnRegisterForBlockingSyscall ();
1701 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1702 extern static unsafe int ReceiveFrom_internal(IntPtr sock, byte* buffer, int count, SocketFlags flags, ref SocketAddress sockaddr, out int error, bool blocking);
1706 #region ReceiveMessageFrom
1708 [MonoTODO ("Not implemented")]
1709 public int ReceiveMessageFrom (byte[] buffer, int offset, int size, ref SocketFlags socketFlags, ref EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
1711 ThrowIfDisposedAndClosed ();
1712 ThrowIfBufferNull (buffer);
1713 ThrowIfBufferOutOfRange (buffer, offset, size);
1715 if (remoteEP == null)
1716 throw new ArgumentNullException ("remoteEP");
1718 // FIXME: figure out how we get hold of the IPPacketInformation
1719 throw new NotImplementedException ();
1722 [MonoTODO ("Not implemented")]
1723 public bool ReceiveMessageFromAsync (SocketAsyncEventArgs e)
1725 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1727 ThrowIfDisposedAndClosed ();
1729 throw new NotImplementedException ();
1733 public IAsyncResult BeginReceiveMessageFrom (byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, object state)
1735 ThrowIfDisposedAndClosed ();
1736 ThrowIfBufferNull (buffer);
1737 ThrowIfBufferOutOfRange (buffer, offset, size);
1739 if (remoteEP == null)
1740 throw new ArgumentNullException ("remoteEP");
1742 throw new NotImplementedException ();
1746 public int EndReceiveMessageFrom (IAsyncResult asyncResult, ref SocketFlags socketFlags, ref EndPoint endPoint, out IPPacketInformation ipPacketInformation)
1748 ThrowIfDisposedAndClosed ();
1750 if (endPoint == null)
1751 throw new ArgumentNullException ("endPoint");
1753 /*SocketAsyncResult sockares =*/ ValidateEndIAsyncResult (asyncResult, "EndReceiveMessageFrom", "asyncResult");
1755 throw new NotImplementedException ();
1762 public int Send (byte [] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode)
1764 ThrowIfDisposedAndClosed ();
1765 ThrowIfBufferNull (buffer);
1766 ThrowIfBufferOutOfRange (buffer, offset, size);
1769 errorCode = SocketError.Success;
1777 fixed (byte *pbuffer = buffer) {
1778 sent += Send_internal (m_Handle, &pbuffer[offset + sent], size - sent, socketFlags, out nativeError, is_blocking);
1782 errorCode = (SocketError)nativeError;
1783 if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress) {
1784 is_connected = false;
1788 is_connected = true;
1790 } while (sent < size);
1795 [CLSCompliant (false)]
1796 public int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
1798 ThrowIfDisposedAndClosed ();
1800 if (buffers == null)
1801 throw new ArgumentNullException ("buffers");
1802 if (buffers.Count == 0)
1803 throw new ArgumentException ("Buffer is empty", "buffers");
1805 int numsegments = buffers.Count;
1809 GCHandle[] gch = new GCHandle[numsegments];
1812 fixed (WSABUF* bufarray = new WSABUF[numsegments]) {
1813 for(int i = 0; i < numsegments; i++) {
1814 ArraySegment<byte> segment = buffers[i];
1816 if (segment.Offset < 0 || segment.Count < 0 || segment.Count > segment.Array.Length - segment.Offset)
1817 throw new ArgumentOutOfRangeException ("segment");
1820 gch[i] = GCHandle.Alloc (segment.Array, GCHandleType.Pinned);
1823 bufarray[i].len = segment.Count;
1824 bufarray[i].buf = Marshal.UnsafeAddrOfPinnedArrayElement (segment.Array, segment.Offset);
1827 ret = Send_internal (m_Handle, bufarray, numsegments, socketFlags, out nativeError, is_blocking);
1831 for (int i = 0; i < numsegments; i++) {
1832 if (gch[i].IsAllocated)
1837 errorCode = (SocketError)nativeError;
1842 public bool SendAsync (SocketAsyncEventArgs e)
1844 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1846 ThrowIfDisposedAndClosed ();
1848 if (e.Buffer == null && e.BufferList == null)
1849 throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
1851 if (e.Buffer == null) {
1852 InitSocketAsyncEventArgs (e, SendAsyncCallback, e, SocketOperation.SendGeneric);
1854 e.socket_async_result.Buffers = e.BufferList;
1856 QueueIOSelectorJob (WriteSem, e.socket_async_result.Handle, new IOSelectorJob (IOOperation.Write, BeginSendGenericCallback, e.socket_async_result));
1858 InitSocketAsyncEventArgs (e, SendAsyncCallback, e, SocketOperation.Send);
1860 e.socket_async_result.Buffer = e.Buffer;
1861 e.socket_async_result.Offset = e.Offset;
1862 e.socket_async_result.Size = e.Count;
1864 QueueIOSelectorJob (WriteSem, e.socket_async_result.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendCallback ((SocketAsyncResult) s, 0), e.socket_async_result));
1870 static AsyncCallback SendAsyncCallback = new AsyncCallback (ares => {
1871 SocketAsyncEventArgs e = (SocketAsyncEventArgs) ((SocketAsyncResult) ares).AsyncState;
1873 if (Interlocked.Exchange (ref e.in_progress, 0) != 1)
1874 throw new InvalidOperationException ("No operation in progress");
1877 e.BytesTransferred = e.current_socket.EndSend (ares);
1878 } catch (SocketException se){
1879 e.SocketError = se.SocketErrorCode;
1880 } catch (ObjectDisposedException) {
1881 e.SocketError = SocketError.OperationAborted;
1887 public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
1889 ThrowIfDisposedAndClosed ();
1890 ThrowIfBufferNull (buffer);
1891 ThrowIfBufferOutOfRange (buffer, offset, size);
1893 if (!is_connected) {
1894 errorCode = SocketError.NotConnected;
1898 errorCode = SocketError.Success;
1900 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.Send) {
1904 SockFlags = socketFlags,
1907 QueueIOSelectorJob (WriteSem, sockares.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendCallback ((SocketAsyncResult) s, 0), sockares));
1912 static void BeginSendCallback (SocketAsyncResult sockares, int sent_so_far)
1918 fixed (byte *pbuffer = sockares.Buffer) {
1919 total = Socket.Send_internal (sockares.socket.m_Handle, &pbuffer[sockares.Offset], sockares.Size, sockares.SockFlags, out sockares.error, false);
1922 } catch (Exception e) {
1923 sockares.Complete (e);
1927 if (sockares.error == 0) {
1928 sent_so_far += total;
1929 sockares.Offset += total;
1930 sockares.Size -= total;
1932 if (sockares.socket.CleanedUp) {
1933 sockares.Complete (total);
1937 if (sockares.Size > 0) {
1938 IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendCallback ((SocketAsyncResult) s, sent_so_far), sockares));
1939 return; // Have to finish writing everything. See bug #74475.
1942 sockares.Total = sent_so_far;
1945 sockares.Complete (total);
1948 [CLSCompliant (false)]
1949 public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
1951 ThrowIfDisposedAndClosed ();
1953 if (buffers == null)
1954 throw new ArgumentNullException ("buffers");
1956 if (!is_connected) {
1957 errorCode = SocketError.NotConnected;
1961 errorCode = SocketError.Success;
1963 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.SendGeneric) {
1965 SockFlags = socketFlags,
1968 QueueIOSelectorJob (WriteSem, sockares.Handle, new IOSelectorJob (IOOperation.Write, BeginSendGenericCallback, sockares));
1973 static IOAsyncCallback BeginSendGenericCallback = new IOAsyncCallback (ares => {
1974 SocketAsyncResult sockares = (SocketAsyncResult) ares;
1978 total = sockares.socket.Send (sockares.Buffers, sockares.SockFlags);
1979 } catch (Exception e) {
1980 sockares.Complete (e);
1984 sockares.Complete (total);
1987 public int EndSend (IAsyncResult asyncResult, out SocketError errorCode)
1989 ThrowIfDisposedAndClosed ();
1991 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndSend", "asyncResult");
1993 if (!sockares.IsCompleted)
1994 sockares.AsyncWaitHandle.WaitOne ();
1996 errorCode = sockares.ErrorCode;
1998 if (errorCode != SocketError.Success && errorCode != SocketError.WouldBlock && errorCode != SocketError.InProgress)
1999 is_connected = false;
2001 /* If no socket error occurred, call CheckIfThrowDelayedException in
2002 * case there are other kinds of exceptions that should be thrown.*/
2003 if (errorCode == SocketError.Success)
2004 sockares.CheckIfThrowDelayedException ();
2006 return sockares.Total;
2009 static unsafe int Send_internal (SafeSocketHandle safeHandle, WSABUF* bufarray, int count, SocketFlags flags, out int error, bool blocking)
2012 safeHandle.RegisterForBlockingSyscall ();
2013 return Send_internal (safeHandle.DangerousGetHandle (), bufarray, count, flags, out error, blocking);
2015 safeHandle.UnRegisterForBlockingSyscall ();
2019 [MethodImplAttribute (MethodImplOptions.InternalCall)]
2020 extern static unsafe int Send_internal (IntPtr sock, WSABUF* bufarray, int count, SocketFlags flags, out int error, bool blocking);
2022 static unsafe int Send_internal (SafeSocketHandle safeHandle, byte* buffer, int count, SocketFlags flags, out int error, bool blocking)
2025 safeHandle.RegisterForBlockingSyscall ();
2026 return Send_internal (safeHandle.DangerousGetHandle (), buffer, count, flags, out error, blocking);
2028 safeHandle.UnRegisterForBlockingSyscall ();
2032 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2033 extern static unsafe int Send_internal(IntPtr sock, byte* buffer, int count, SocketFlags flags, out int error, bool blocking);
2039 public int SendTo (byte [] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP)
2041 ThrowIfDisposedAndClosed ();
2042 ThrowIfBufferNull (buffer);
2043 ThrowIfBufferOutOfRange (buffer, offset, size);
2045 if (remoteEP == null)
2046 throw new ArgumentNullException("remoteEP");
2051 fixed (byte *pbuffer = buffer) {
2052 ret = SendTo_internal (m_Handle, &pbuffer[offset], size, socketFlags, remoteEP.Serialize (), out error, is_blocking);
2056 SocketError err = (SocketError) error;
2058 if (err != SocketError.WouldBlock && err != SocketError.InProgress)
2059 is_connected = false;
2060 throw new SocketException (error);
2063 is_connected = true;
2065 seed_endpoint = remoteEP;
2070 public bool SendToAsync (SocketAsyncEventArgs e)
2072 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
2074 ThrowIfDisposedAndClosed ();
2076 if (e.BufferList != null)
2077 throw new NotSupportedException ("Mono doesn't support using BufferList at this point.");
2078 if (e.RemoteEndPoint == null)
2079 throw new ArgumentNullException ("remoteEP", "Value cannot be null.");
2081 InitSocketAsyncEventArgs (e, SendToAsyncCallback, e, SocketOperation.SendTo);
2083 e.socket_async_result.Buffer = e.Buffer;
2084 e.socket_async_result.Offset = e.Offset;
2085 e.socket_async_result.Size = e.Count;
2086 e.socket_async_result.SockFlags = e.SocketFlags;
2087 e.socket_async_result.EndPoint = e.RemoteEndPoint;
2089 QueueIOSelectorJob (WriteSem, e.socket_async_result.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendToCallback ((SocketAsyncResult) s, 0), e.socket_async_result));
2094 static AsyncCallback SendToAsyncCallback = new AsyncCallback (ares => {
2095 SocketAsyncEventArgs e = (SocketAsyncEventArgs) ((SocketAsyncResult) ares).AsyncState;
2097 if (Interlocked.Exchange (ref e.in_progress, 0) != 1)
2098 throw new InvalidOperationException ("No operation in progress");
2101 e.BytesTransferred = e.current_socket.EndSendTo (ares);
2102 } catch (SocketException ex) {
2103 e.SocketError = ex.SocketErrorCode;
2104 } catch (ObjectDisposedException) {
2105 e.SocketError = SocketError.OperationAborted;
2111 public IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP, AsyncCallback callback, object state)
2113 ThrowIfDisposedAndClosed ();
2114 ThrowIfBufferNull (buffer);
2115 ThrowIfBufferOutOfRange (buffer, offset, size);
2117 SocketAsyncResult sockares = new SocketAsyncResult (this, callback, state, SocketOperation.SendTo) {
2121 SockFlags = socketFlags,
2122 EndPoint = remoteEP,
2125 QueueIOSelectorJob (WriteSem, sockares.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendToCallback ((SocketAsyncResult) s, 0), sockares));
2130 static void BeginSendToCallback (SocketAsyncResult sockares, int sent_so_far)
2134 total = sockares.socket.SendTo (sockares.Buffer, sockares.Offset, sockares.Size, sockares.SockFlags, sockares.EndPoint);
2136 if (sockares.error == 0) {
2137 sent_so_far += total;
2138 sockares.Offset += total;
2139 sockares.Size -= total;
2142 if (sockares.Size > 0) {
2143 IOSelector.Add (sockares.Handle, new IOSelectorJob (IOOperation.Write, s => BeginSendToCallback ((SocketAsyncResult) s, sent_so_far), sockares));
2144 return; // Have to finish writing everything. See bug #74475.
2147 sockares.Total = sent_so_far;
2148 } catch (Exception e) {
2149 sockares.Complete (e);
2153 sockares.Complete ();
2156 public int EndSendTo (IAsyncResult asyncResult)
2158 ThrowIfDisposedAndClosed ();
2160 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndSendTo", "result");
2162 if (!sockares.IsCompleted)
2163 sockares.AsyncWaitHandle.WaitOne();
2165 sockares.CheckIfThrowDelayedException();
2167 return sockares.Total;
2170 static unsafe int SendTo_internal (SafeSocketHandle safeHandle, byte* buffer, int count, SocketFlags flags, SocketAddress sa, out int error, bool blocking)
2173 safeHandle.RegisterForBlockingSyscall ();
2174 return SendTo_internal (safeHandle.DangerousGetHandle (), buffer, count, flags, sa, out error, blocking);
2176 safeHandle.UnRegisterForBlockingSyscall ();
2180 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2181 extern static unsafe int SendTo_internal (IntPtr sock, byte* buffer, int count, SocketFlags flags, SocketAddress sa, out int error, bool blocking);
2187 public void SendFile (string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
2189 ThrowIfDisposedAndClosed ();
2192 throw new NotSupportedException ();
2194 throw new InvalidOperationException ();
2197 if (!SendFile_internal (m_Handle, fileName, preBuffer, postBuffer, flags, out error, is_blocking) || error != 0) {
2198 SocketException exc = new SocketException (error);
2199 if (exc.ErrorCode == 2 || exc.ErrorCode == 3)
2200 throw new FileNotFoundException ();
2205 public IAsyncResult BeginSendFile (string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags, AsyncCallback callback, object state)
2207 ThrowIfDisposedAndClosed ();
2210 throw new NotSupportedException ();
2211 if (!File.Exists (fileName))
2212 throw new FileNotFoundException ();
2214 SendFileHandler handler = new SendFileHandler (SendFile);
2216 return new SendFileAsyncResult (handler, handler.BeginInvoke (fileName, preBuffer, postBuffer, flags, ar => callback (new SendFileAsyncResult (handler, ar)), state));
2219 public void EndSendFile (IAsyncResult asyncResult)
2221 ThrowIfDisposedAndClosed ();
2223 if (asyncResult == null)
2224 throw new ArgumentNullException ("asyncResult");
2226 SendFileAsyncResult ares = asyncResult as SendFileAsyncResult;
2228 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
2230 ares.Delegate.EndInvoke (ares.Original);
2233 static bool SendFile_internal (SafeSocketHandle safeHandle, string filename, byte [] pre_buffer, byte [] post_buffer, TransmitFileOptions flags, out int error, bool blocking)
2236 safeHandle.RegisterForBlockingSyscall ();
2237 return SendFile_internal (safeHandle.DangerousGetHandle (), filename, pre_buffer, post_buffer, flags, out error, blocking);
2239 safeHandle.UnRegisterForBlockingSyscall ();
2243 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2244 extern static bool SendFile_internal (IntPtr sock, string filename, byte [] pre_buffer, byte [] post_buffer, TransmitFileOptions flags, out int error, bool blocking);
2246 delegate void SendFileHandler (string fileName, byte [] preBuffer, byte [] postBuffer, TransmitFileOptions flags);
2248 sealed class SendFileAsyncResult : IAsyncResult {
2252 public SendFileAsyncResult (SendFileHandler d, IAsyncResult ares)
2258 public object AsyncState {
2259 get { return ares.AsyncState; }
2262 public WaitHandle AsyncWaitHandle {
2263 get { return ares.AsyncWaitHandle; }
2266 public bool CompletedSynchronously {
2267 get { return ares.CompletedSynchronously; }
2270 public bool IsCompleted {
2271 get { return ares.IsCompleted; }
2274 public SendFileHandler Delegate {
2278 public IAsyncResult Original {
2279 get { return ares; }
2287 [MonoTODO ("Not implemented")]
2288 public bool SendPacketsAsync (SocketAsyncEventArgs e)
2290 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
2292 ThrowIfDisposedAndClosed ();
2294 throw new NotImplementedException ();
2299 #region DuplicateAndClose
2301 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2302 static extern bool Duplicate_internal(IntPtr handle, int targetProcessId, out IntPtr duplicateHandle, out MonoIOError error);
2304 [MonoLimitation ("We do not support passing sockets across processes, we merely allow this API to pass the socket across AppDomains")]
2305 public SocketInformation DuplicateAndClose (int targetProcessId)
2307 var si = new SocketInformation ();
2309 (is_listening ? SocketInformationOptions.Listening : 0) |
2310 (is_connected ? SocketInformationOptions.Connected : 0) |
2311 (is_blocking ? 0 : SocketInformationOptions.NonBlocking) |
2312 (useOverlappedIO ? SocketInformationOptions.UseOnlyOverlappedIO : 0);
2314 IntPtr duplicateHandle;
2315 if (!Duplicate_internal (Handle, targetProcessId, out duplicateHandle, out MonoIOError error))
2316 throw MonoIO.GetException (error);
2318 si.ProtocolInformation = Mono.DataConverter.Pack ("iiiil", (int)addressFamily, (int)socketType, (int)protocolType, is_bound ? 1 : 0, (long)duplicateHandle);
2326 #region GetSocketOption
2328 public void GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue)
2330 ThrowIfDisposedAndClosed ();
2332 if (optionValue == null)
2333 throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer");
2336 GetSocketOption_arr_internal (m_Handle, optionLevel, optionName, ref optionValue, out error);
2339 throw new SocketException (error);
2342 public byte [] GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int optionLength)
2344 ThrowIfDisposedAndClosed ();
2347 byte[] byte_val = new byte [optionLength];
2348 GetSocketOption_arr_internal (m_Handle, optionLevel, optionName, ref byte_val, out error);
2351 throw new SocketException (error);
2356 public object GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName)
2358 ThrowIfDisposedAndClosed ();
2362 GetSocketOption_obj_internal (m_Handle, optionLevel, optionName, out obj_val, out error);
2365 throw new SocketException (error);
2367 if (optionName == SocketOptionName.Linger)
2368 return (LingerOption) obj_val;
2369 else if (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)
2370 return (MulticastOption) obj_val;
2371 else if (obj_val is int)
2372 return (int) obj_val;
2377 static void GetSocketOption_arr_internal (SafeSocketHandle safeHandle, SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val, out int error)
2379 bool release = false;
2381 safeHandle.DangerousAddRef (ref release);
2382 GetSocketOption_arr_internal (safeHandle.DangerousGetHandle (), level, name, ref byte_val, out error);
2385 safeHandle.DangerousRelease ();
2389 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2390 extern static void GetSocketOption_arr_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val, out int error);
2392 static void GetSocketOption_obj_internal (SafeSocketHandle safeHandle, SocketOptionLevel level, SocketOptionName name, out object obj_val, out int error)
2394 bool release = false;
2396 safeHandle.DangerousAddRef (ref release);
2397 GetSocketOption_obj_internal (safeHandle.DangerousGetHandle (), level, name, out obj_val, out error);
2400 safeHandle.DangerousRelease ();
2404 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2405 extern static void GetSocketOption_obj_internal(IntPtr socket, SocketOptionLevel level, SocketOptionName name, out object obj_val, out int error);
2409 #region SetSocketOption
2411 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue)
2413 ThrowIfDisposedAndClosed ();
2415 // I'd throw an ArgumentNullException, but this is what MS does.
2416 if (optionValue == null)
2417 throw new SocketException ((int) SocketError.Fault, "Error trying to dereference an invalid pointer");
2420 SetSocketOption_internal (m_Handle, optionLevel, optionName, null, optionValue, 0, out error);
2423 if (error == (int) SocketError.InvalidArgument)
2424 throw new ArgumentException ();
2425 throw new SocketException (error);
2429 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue)
2431 ThrowIfDisposedAndClosed ();
2433 // NOTE: if a null is passed, the byte[] overload is used instead...
2434 if (optionValue == null)
2435 throw new ArgumentNullException("optionValue");
2439 if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.Linger) {
2440 LingerOption linger = optionValue as LingerOption;
2442 throw new ArgumentException ("A 'LingerOption' value must be specified.", "optionValue");
2443 SetSocketOption_internal (m_Handle, optionLevel, optionName, linger, null, 0, out error);
2444 } else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
2445 MulticastOption multicast = optionValue as MulticastOption;
2446 if (multicast == null)
2447 throw new ArgumentException ("A 'MulticastOption' value must be specified.", "optionValue");
2448 SetSocketOption_internal (m_Handle, optionLevel, optionName, multicast, null, 0, out error);
2449 } else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
2450 IPv6MulticastOption multicast = optionValue as IPv6MulticastOption;
2451 if (multicast == null)
2452 throw new ArgumentException ("A 'IPv6MulticastOption' value must be specified.", "optionValue");
2453 SetSocketOption_internal (m_Handle, optionLevel, optionName, multicast, null, 0, out error);
2455 throw new ArgumentException ("Invalid value specified.", "optionValue");
2459 if (error == (int) SocketError.InvalidArgument)
2460 throw new ArgumentException ();
2461 throw new SocketException (error);
2465 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
2467 int int_val = optionValue ? 1 : 0;
2469 SetSocketOption (optionLevel, optionName, int_val);
2472 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
2474 ThrowIfDisposedAndClosed ();
2477 SetSocketOption_internal (m_Handle, optionLevel, optionName, null, null, optionValue, out error);
2480 if (error == (int) SocketError.InvalidArgument)
2481 throw new ArgumentException ();
2482 throw new SocketException (error);
2486 static void SetSocketOption_internal (SafeSocketHandle safeHandle, SocketOptionLevel level, SocketOptionName name, object obj_val, byte [] byte_val, int int_val, out int error)
2488 bool release = false;
2490 safeHandle.DangerousAddRef (ref release);
2491 SetSocketOption_internal (safeHandle.DangerousGetHandle (), level, name, obj_val, byte_val, int_val, out error);
2494 safeHandle.DangerousRelease ();
2498 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2499 extern static void SetSocketOption_internal (IntPtr socket, SocketOptionLevel level, SocketOptionName name, object obj_val, byte [] byte_val, int int_val, out int error);
2505 public int IOControl (int ioControlCode, byte [] optionInValue, byte [] optionOutValue)
2508 throw new ObjectDisposedException (GetType ().ToString ());
2511 int result = IOControl_internal (m_Handle, ioControlCode, optionInValue, optionOutValue, out error);
2514 throw new SocketException (error);
2516 throw new InvalidOperationException ("Must use Blocking property instead.");
2521 static int IOControl_internal (SafeSocketHandle safeHandle, int ioctl_code, byte [] input, byte [] output, out int error)
2523 bool release = false;
2525 safeHandle.DangerousAddRef (ref release);
2526 return IOControl_internal (safeHandle.DangerousGetHandle (), ioctl_code, input, output, out error);
2529 safeHandle.DangerousRelease ();
2533 /* See Socket.IOControl, WSAIoctl documentation in MSDN. The common options between UNIX
2534 * and Winsock are FIONREAD, FIONBIO and SIOCATMARK. Anything else will depend on the system
2535 * except SIO_KEEPALIVE_VALS which is properly handled on both windows and linux. */
2536 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2537 extern static int IOControl_internal (IntPtr sock, int ioctl_code, byte [] input, byte [] output, out int error);
2543 public void Close ()
2549 public void Close (int timeout)
2551 linger_timeout = timeout;
2555 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2556 internal extern static void Close_internal (IntPtr socket, out int error);
2562 public void Shutdown (SocketShutdown how)
2564 ThrowIfDisposedAndClosed ();
2567 throw new SocketException (10057); // Not connected
2570 Shutdown_internal (m_Handle, how, out error);
2573 throw new SocketException (error);
2576 static void Shutdown_internal (SafeSocketHandle safeHandle, SocketShutdown how, out int error)
2578 bool release = false;
2580 safeHandle.DangerousAddRef (ref release);
2581 Shutdown_internal (safeHandle.DangerousGetHandle (), how, out error);
2584 safeHandle.DangerousRelease ();
2588 [MethodImplAttribute (MethodImplOptions.InternalCall)]
2589 internal extern static void Shutdown_internal (IntPtr socket, SocketShutdown how, out int error);
2595 protected virtual void Dispose (bool disposing)
2601 bool was_connected = is_connected;
2602 is_connected = false;
2604 if (m_Handle != null) {
2611 m_Handle.Dispose ();
2615 void Linger (IntPtr handle)
2617 if (!is_connected || linger_timeout <= 0)
2620 /* We don't want to receive any more data */
2622 Shutdown_internal (handle, SocketShutdown.Receive, out error);
2627 int seconds = linger_timeout / 1000;
2628 int ms = linger_timeout % 1000;
2630 /* If the other end closes, this will return 'true' with 'Available' == 0 */
2631 Poll_internal (handle, SelectMode.SelectRead, ms * 1000, out error);
2637 LingerOption linger = new LingerOption (true, seconds);
2638 SetSocketOption_internal (handle, SocketOptionLevel.Socket, SocketOptionName.Linger, linger, null, 0, out error);
2639 /* Not needed, we're closing upon return */
2647 void ThrowIfDisposedAndClosed (Socket socket)
2649 if (socket.CleanedUp && socket.is_closed)
2650 throw new ObjectDisposedException (socket.GetType ().ToString ());
2653 void ThrowIfDisposedAndClosed ()
2655 if (CleanedUp && is_closed)
2656 throw new ObjectDisposedException (GetType ().ToString ());
2659 void ThrowIfBufferNull (byte[] buffer)
2662 throw new ArgumentNullException ("buffer");
2665 void ThrowIfBufferOutOfRange (byte[] buffer, int offset, int size)
2668 throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
2669 if (offset > buffer.Length)
2670 throw new ArgumentOutOfRangeException ("offset", "offset must be <= buffer.Length");
2672 throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
2673 if (size > buffer.Length - offset)
2674 throw new ArgumentOutOfRangeException ("size", "size must be <= buffer.Length - offset");
2679 if (protocolType == ProtocolType.Udp)
2680 throw new SocketException ((int)SocketError.ProtocolOption);
2683 SocketAsyncResult ValidateEndIAsyncResult (IAsyncResult ares, string methodName, string argName)
2686 throw new ArgumentNullException (argName);
2688 SocketAsyncResult sockares = ares as SocketAsyncResult;
2689 if (sockares == null)
2690 throw new ArgumentException ("Invalid IAsyncResult", argName);
2691 if (Interlocked.CompareExchange (ref sockares.EndCalled, 1, 0) == 1)
2692 throw new InvalidOperationException (methodName + " can only be called once per asynchronous operation");
2697 void QueueIOSelectorJob (SemaphoreSlim sem, IntPtr handle, IOSelectorJob job)
2699 var task = sem.WaitAsync();
2700 // fast path without Task<Action> allocation.
2701 if (task.IsCompleted) {
2703 job.MarkDisposed ();
2706 IOSelector.Add (handle, job);
2710 task.ContinueWith( t => {
2712 job.MarkDisposed ();
2715 IOSelector.Add(handle, job);
2720 void InitSocketAsyncEventArgs (SocketAsyncEventArgs e, AsyncCallback callback, object state, SocketOperation operation)
2722 e.socket_async_result.Init (this, callback, state, operation);
2723 if (e.AcceptSocket != null) {
2724 e.socket_async_result.AcceptSocket = e.AcceptSocket;
2726 e.current_socket = this;
2727 e.SetLastOperation (SocketOperationToSocketAsyncOperation (operation));
2728 e.SocketError = SocketError.Success;
2729 e.BytesTransferred = 0;
2732 SocketAsyncOperation SocketOperationToSocketAsyncOperation (SocketOperation op)
2735 case SocketOperation.Connect:
2736 return SocketAsyncOperation.Connect;
2737 case SocketOperation.Accept:
2738 return SocketAsyncOperation.Accept;
2739 case SocketOperation.Disconnect:
2740 return SocketAsyncOperation.Disconnect;
2741 case SocketOperation.Receive:
2742 case SocketOperation.ReceiveGeneric:
2743 return SocketAsyncOperation.Receive;
2744 case SocketOperation.ReceiveFrom:
2745 return SocketAsyncOperation.ReceiveFrom;
2746 case SocketOperation.Send:
2747 case SocketOperation.SendGeneric:
2748 return SocketAsyncOperation.Send;
2749 case SocketOperation.SendTo:
2750 return SocketAsyncOperation.SendTo;
2752 throw new NotImplementedException (String.Format ("Operation {0} is not implemented", op));
2756 IPEndPoint RemapIPEndPoint (IPEndPoint input) {
2757 // If socket is DualMode ensure we automatically handle mapping IPv4 addresses to IPv6.
2758 if (IsDualMode && input.AddressFamily == AddressFamily.InterNetwork)
2759 return new IPEndPoint (input.Address.MapToIPv6 (), input.Port);
2764 [StructLayout (LayoutKind.Sequential)]
2770 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2771 internal static extern void cancel_blocking_socket_operation (Thread thread);
2773 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2774 internal static extern bool SupportsPortReuse (ProtocolType proto);
2776 internal static int FamilyHint {
2779 // MONO_HINT_UNSPECIFIED = 0,
2780 // MONO_HINT_IPV4 = 1,
2781 // MONO_HINT_IPV6 = 2,
2784 if (OSSupportsIPv4) {
2788 if (OSSupportsIPv6) {
2789 hint = hint == 0 ? 2 : 0;
2796 static bool IsProtocolSupported (NetworkInterfaceComponent networkInterface)
2801 var nics = NetworkInterface.GetAllNetworkInterfaces ();
2802 foreach (var adapter in nics) {
2803 if (adapter.Supports (networkInterface))