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)
10 // Copyright (C) 2001, 2002 Phillip Pearson and Ximian, Inc.
11 // http://www.myelin.co.nz
12 // (c) 2004-2011 Novell, Inc. (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using System.Collections;
38 using System.Collections.Generic;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
41 using System.Threading;
42 using System.Reflection;
44 using System.Net.Configuration;
47 using System.Net.NetworkInformation;
49 namespace System.Net.Sockets
51 public partial class Socket : IDisposable
53 const int SOCKET_CLOSED_CODE = 10004;
54 const string TIMEOUT_EXCEPTION_MSG = "A connection attempt failed because the connected party did not properly respond" +
55 "after a period of time, or established connection failed because connected host has failed to respond";
58 * These two fields are looked up by name by the runtime, don't change
59 * their name without also updating the runtime code.
61 static int ipv4_supported = -1;
62 static int ipv6_supported = -1;
64 /* true if we called Close_internal */
68 bool use_overlapped_io;
72 /* the field "safe_handle" is looked up by name by the runtime */
73 SafeSocketHandle safe_handle;
75 AddressFamily address_family;
76 SocketType socket_type;
77 ProtocolType protocol_type;
80 * This EndPoint is used when creating new endpoints. Because
81 * there are many types of EndPoints possible,
82 * seed_endpoint.Create(addr) is used for creating new ones.
83 * As such, this value is set on Bind, SentTo, ReceiveFrom,
86 internal EndPoint seed_endpoint = null;
88 internal Queue readQ = new Queue (2);
89 internal Queue writeQ = new Queue (2);
91 internal bool is_blocking = true;
92 internal bool is_bound;
94 /* When true, the socket was connected at the time of the last IO operation */
95 internal bool is_connected;
97 internal bool is_disposed;
98 internal bool connect_in_progress;
104 if (ipv4_supported == -1) {
106 Socket tmp = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
115 if (ipv6_supported == -1) {
116 // We need to put a try/catch around ConfigurationManager methods as will always throw an exception
117 // when run in a mono embedded application. This occurs as embedded applications do not have a setup
118 // for application config. The exception is not thrown when called from a normal .NET application.
120 // We, then, need to guard calls to the ConfigurationManager. If the config is not found or throws an
121 // exception, will fall through to the existing Socket / API directly below in the code.
123 // Also note that catching ConfigurationErrorsException specifically would require library dependency
124 // System.Configuration, and wanted to avoid that.
126 #if CONFIGURATION_DEP
128 SettingsSection config;
129 config = (SettingsSection) System.Configuration.ConfigurationManager.GetSection ("system.net/settings");
131 ipv6_supported = config.Ipv6.Enabled ? -1 : 0;
137 NetConfig config = System.Configuration.ConfigurationSettings.GetConfig("system.net/settings") as NetConfig;
139 ipv6_supported = config.ipv6Enabled ? -1 : 0;
145 if (ipv6_supported != 0) {
147 Socket tmp = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
158 [MonoTODO ("Currently hardcoded to IPv4. Ideally, support v4/v6 dual-stack.")]
159 public Socket (SocketType socketType, ProtocolType protocolType)
160 : this (AddressFamily.InterNetwork, socketType, protocolType)
164 public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
166 #if NET_2_1 && !MOBILE
167 switch (addressFamily) {
168 case AddressFamily.InterNetwork: // ok
169 case AddressFamily.InterNetworkV6: // ok
170 case AddressFamily.Unknown: // SocketException will be thrown later (with right error #)
172 // case AddressFamily.Unspecified:
174 throw new ArgumentException ("addressFamily");
177 switch (socketType) {
178 case SocketType.Stream: // ok
179 case SocketType.Unknown: // SocketException will be thrown later (with right error #)
182 throw new ArgumentException ("socketType");
185 switch (protocolType) {
186 case ProtocolType.Tcp: // ok
187 case ProtocolType.Unspecified: // ok
188 case ProtocolType.Unknown: // SocketException will be thrown later (with right error #)
191 throw new ArgumentException ("protocolType");
194 this.address_family = addressFamily;
195 this.socket_type = socketType;
196 this.protocol_type = protocolType;
199 var handle = Socket_internal (addressFamily, socketType, protocolType, out error);
201 this.safe_handle = new SafeSocketHandle (handle, true);
204 throw new SocketException (error);
206 #if !NET_2_1 || MOBILE
212 public Socket (SocketInformation socketInformation)
214 this.is_listening = (socketInformation.Options & SocketInformationOptions.Listening) != 0;
215 this.is_connected = (socketInformation.Options & SocketInformationOptions.Connected) != 0;
216 this.is_blocking = (socketInformation.Options & SocketInformationOptions.NonBlocking) == 0;
217 this.use_overlapped_io = (socketInformation.Options & SocketInformationOptions.UseOnlyOverlappedIO) != 0;
219 var result = Mono.DataConverter.Unpack ("iiiil", socketInformation.ProtocolInformation, 0);
221 this.address_family = (AddressFamily) (int) result [0];
222 this.socket_type = (SocketType) (int) result [1];
223 this.protocol_type = (ProtocolType) (int) result [2];
224 this.is_bound = (ProtocolType) (int) result [3] != 0;
225 this.safe_handle = new SafeSocketHandle ((IntPtr) (long) result [4], true);
231 /* private constructor used by Accept, which already has a socket handle to use */
232 internal Socket(AddressFamily family, SocketType type, ProtocolType proto, SafeSocketHandle safe_handle)
234 this.address_family = family;
235 this.socket_type = type;
236 this.protocol_type = proto;
238 this.safe_handle = safe_handle;
239 this.is_connected = true;
247 void SocketDefaults ()
250 /* Need to test IPv6 further */
251 if (address_family == AddressFamily.InterNetwork
252 // || address_family == AddressFamily.InterNetworkV6
254 /* This is the default, but it probably has nasty side
255 * effects on Linux, as the socket option is kludged by
256 * turning on or off PMTU discovery... */
257 this.DontFragment = false;
260 /* Microsoft sets these to 8192, but we are going to keep them
261 * both to the OS defaults as these have a big performance impact.
262 * on WebClient performance. */
263 // this.ReceiveBufferSize = 8192;
264 // this.SendBufferSize = 8192;
265 } catch (SocketException) {
269 /* Creates a new system socket, returning the handle */
270 [MethodImplAttribute(MethodImplOptions.InternalCall)]
271 extern IntPtr Socket_internal (AddressFamily family, SocketType type, ProtocolType proto, out int error);
277 public static bool SupportsIPv4 {
278 get { return ipv4_supported == 1; }
281 [ObsoleteAttribute ("Use OSSupportsIPv6 instead")]
282 public static bool SupportsIPv6 {
283 get { return ipv6_supported == 1; }
287 public static bool OSSupportsIPv4 {
288 get { return ipv4_supported == 1; }
293 public static bool OSSupportsIPv6 {
294 get { return ipv6_supported == 1; }
297 public static bool OSSupportsIPv6 {
299 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces ();
301 foreach (NetworkInterface adapter in nics) {
302 if (adapter.Supports (NetworkInterfaceComponent.IPv6))
311 public int Available {
313 ThrowIfDisposedAndClosed ();
316 ret = Available_internal (safe_handle, out error);
319 throw new SocketException (error);
325 static int Available_internal (SafeSocketHandle safeHandle, out int error)
327 bool release = false;
329 safeHandle.DangerousAddRef (ref release);
330 return Available_internal (safeHandle.DangerousGetHandle (), out error);
333 safeHandle.DangerousRelease ();
337 /* Returns the amount of data waiting to be read on socket */
338 [MethodImplAttribute(MethodImplOptions.InternalCall)]
339 extern static int Available_internal (IntPtr socket, out int error);
341 public bool DontFragment {
343 ThrowIfDisposedAndClosed ();
345 switch (address_family) {
346 case AddressFamily.InterNetwork:
347 return ((int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.DontFragment)) != 0;
348 case AddressFamily.InterNetworkV6:
349 return ((int) GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DontFragment)) != 0;
351 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
355 ThrowIfDisposedAndClosed ();
357 switch (address_family) {
358 case AddressFamily.InterNetwork:
359 SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DontFragment, value ? 1 : 0);
361 case AddressFamily.InterNetworkV6:
362 SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DontFragment, value ? 1 : 0);
365 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
370 public bool EnableBroadcast {
372 ThrowIfDisposedAndClosed ();
374 if (protocol_type != ProtocolType.Udp)
375 throw new SocketException ((int) SocketError.ProtocolOption);
377 return ((int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast)) != 0;
380 ThrowIfDisposedAndClosed ();
382 if (protocol_type != ProtocolType.Udp)
383 throw new SocketException ((int) SocketError.ProtocolOption);
385 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Broadcast, value ? 1 : 0);
389 public bool ExclusiveAddressUse {
391 ThrowIfDisposedAndClosed ();
393 return ((int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse)) != 0;
396 ThrowIfDisposedAndClosed ();
399 throw new InvalidOperationException ("Bind has already been called for this socket");
401 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, value ? 1 : 0);
405 public bool IsBound {
411 public LingerOption LingerState {
413 ThrowIfDisposedAndClosed ();
415 return (LingerOption) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger);
418 ThrowIfDisposedAndClosed ();
419 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, value);
423 public bool MulticastLoopback {
425 ThrowIfDisposedAndClosed ();
427 /* Even though this option can be set for TCP sockets on Linux, throw
428 * this exception anyway to be compatible (the MSDN docs say
429 * "Setting this property on a Transmission Control Protocol (TCP)
430 * socket will have no effect." but the MS runtime throws the
432 if (protocol_type == ProtocolType.Tcp)
433 throw new SocketException ((int)SocketError.ProtocolOption);
435 switch (address_family) {
436 case AddressFamily.InterNetwork:
437 return ((int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback)) != 0;
438 case AddressFamily.InterNetworkV6:
439 return ((int) GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback)) != 0;
441 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
445 ThrowIfDisposedAndClosed ();
447 /* Even though this option can be set for TCP sockets on Linux, throw
448 * this exception anyway to be compatible (the MSDN docs say
449 * "Setting this property on a Transmission Control Protocol (TCP)
450 * socket will have no effect." but the MS runtime throws the
452 if (protocol_type == ProtocolType.Tcp)
453 throw new SocketException ((int)SocketError.ProtocolOption);
455 switch (address_family) {
456 case AddressFamily.InterNetwork:
457 SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastLoopback, value ? 1 : 0);
459 case AddressFamily.InterNetworkV6:
460 SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.MulticastLoopback, value ? 1 : 0);
463 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
468 [MonoTODO ("This doesn't do anything on Mono yet")]
469 public bool UseOnlyOverlappedIO {
470 get { return use_overlapped_io; }
471 set { use_overlapped_io = value; }
474 public IntPtr Handle {
475 get { return safe_handle.DangerousGetHandle (); }
478 // Wish: support non-IP endpoints.
479 public EndPoint LocalEndPoint {
481 ThrowIfDisposedAndClosed ();
483 /* If the seed EndPoint is null, Connect, Bind, etc has not yet
484 * been called. MS returns null in this case. */
485 if (seed_endpoint == null)
489 SocketAddress sa = LocalEndPoint_internal (safe_handle, (int) address_family, out error);
492 throw new SocketException (error);
494 return seed_endpoint.Create (sa);
498 static SocketAddress LocalEndPoint_internal (SafeSocketHandle safeHandle, int family, out int error)
500 bool release = false;
502 safeHandle.DangerousAddRef (ref release);
503 return LocalEndPoint_internal (safeHandle.DangerousGetHandle (), family, out error);
506 safeHandle.DangerousRelease ();
510 /* Returns the local endpoint details in addr and port */
511 [MethodImplAttribute(MethodImplOptions.InternalCall)]
512 extern static SocketAddress LocalEndPoint_internal (IntPtr socket, int family, out int error);
514 public SocketType SocketType {
515 get { return socket_type; }
518 public int SendTimeout {
520 ThrowIfDisposedAndClosed ();
522 return (int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
525 ThrowIfDisposedAndClosed ();
528 throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than -1");
530 /* According to the MSDN docs we should adjust values between 1 and
531 * 499 to 500, but the MS runtime doesn't do this. */
535 SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, value);
539 public int ReceiveTimeout {
541 ThrowIfDisposedAndClosed ();
543 return (int) GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
546 ThrowIfDisposedAndClosed ();
549 throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than -1");
554 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, value);
558 public AddressFamily AddressFamily {
559 get { return address_family; }
562 public bool Blocking {
563 get { return is_blocking; }
565 ThrowIfDisposedAndClosed ();
568 Blocking_internal (safe_handle, value, out error);
571 throw new SocketException (error);
577 static void Blocking_internal (SafeSocketHandle safeHandle, bool block, out int error)
579 bool release = false;
581 safeHandle.DangerousAddRef (ref release);
582 Blocking_internal (safeHandle.DangerousGetHandle (), block, out error);
585 safeHandle.DangerousRelease ();
589 [MethodImplAttribute(MethodImplOptions.InternalCall)]
590 internal extern static void Blocking_internal(IntPtr socket, bool block, out int error);
592 public bool Connected {
593 get { return is_connected; }
594 internal set { is_connected = value; }
597 public ProtocolType ProtocolType {
598 get { return protocol_type; }
601 public bool NoDelay {
603 ThrowIfDisposedAndClosed ();
606 return ((int) GetSocketOption (SocketOptionLevel.Tcp, SocketOptionName.NoDelay)) != 0;
610 ThrowIfDisposedAndClosed ();
613 SetSocketOption (SocketOptionLevel.Tcp, SocketOptionName.NoDelay, value ? 1 : 0);
617 public int ReceiveBufferSize {
619 ThrowIfDisposedAndClosed ();
621 return (int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);
624 ThrowIfDisposedAndClosed ();
627 throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
629 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, value);
633 public int SendBufferSize {
635 ThrowIfDisposedAndClosed ();
637 return (int) GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
640 ThrowIfDisposedAndClosed ();
643 throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
645 SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.SendBuffer, value);
651 ThrowIfDisposedAndClosed ();
653 switch (address_family) {
654 case AddressFamily.InterNetwork:
655 return (short) (int) GetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive);
656 case AddressFamily.InterNetworkV6:
657 return (short) (int) GetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit);
659 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
663 ThrowIfDisposedAndClosed ();
666 throw new ArgumentOutOfRangeException ("value", "The value specified for a set operation is less than zero");
668 switch (address_family) {
669 case AddressFamily.InterNetwork:
670 SetSocketOption (SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, value);
672 case AddressFamily.InterNetworkV6:
673 SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.HopLimit, value);
676 throw new NotSupportedException ("This property is only valid for InterNetwork and InterNetworkV6 sockets");
681 public EndPoint RemoteEndPoint {
683 ThrowIfDisposedAndClosed ();
685 /* If the seed EndPoint is null, Connect, Bind, etc has
686 * not yet been called. MS returns null in this case. */
687 if (!is_connected || seed_endpoint == null)
691 SocketAddress sa = RemoteEndPoint_internal (safe_handle, (int) address_family, out error);
694 throw new SocketException (error);
696 return seed_endpoint.Create (sa);
700 static SocketAddress RemoteEndPoint_internal (SafeSocketHandle safeHandle, int family, out int error)
702 bool release = false;
704 safeHandle.DangerousAddRef (ref release);
705 return RemoteEndPoint_internal (safeHandle.DangerousGetHandle (), family, out error);
708 safeHandle.DangerousRelease ();
712 /* Returns the remote endpoint details in addr and port */
713 [MethodImplAttribute(MethodImplOptions.InternalCall)]
714 extern static SocketAddress RemoteEndPoint_internal (IntPtr socket, int family, out int error);
720 public static void Select (IList checkRead, IList checkWrite, IList checkError, int microSeconds)
722 var list = new List<Socket> ();
723 AddSockets (list, checkRead, "checkRead");
724 AddSockets (list, checkWrite, "checkWrite");
725 AddSockets (list, checkError, "checkError");
728 throw new ArgumentNullException ("checkRead, checkWrite, checkError", "All the lists are null or empty.");
730 /* The 'sockets' array contains:
731 * - READ socket 0-n, null,
732 * - WRITE socket 0-n, null,
733 * - ERROR socket 0-n, null */
734 Socket [] sockets = list.ToArray ();
737 Select_internal (ref sockets, microSeconds, out error);
740 throw new SocketException (error);
742 if (sockets == null) {
743 if (checkRead != null)
745 if (checkWrite != null)
747 if (checkError != null)
753 int count = sockets.Length;
754 IList currentList = checkRead;
756 for (int i = 0; i < count; i++) {
757 Socket sock = sockets [i];
758 if (sock == null) { // separator
759 if (currentList != null) {
760 // Remove non-signaled sockets after the current one
761 int to_remove = currentList.Count - currentIdx;
762 for (int k = 0; k < to_remove; k++)
763 currentList.RemoveAt (currentIdx);
765 currentList = (mode == 0) ? checkWrite : checkError;
771 if (mode == 1 && currentList == checkWrite && !sock.is_connected) {
772 if ((int) sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error) == 0)
773 sock.is_connected = true;
776 /* Remove non-signaled sockets before the current one */
777 while (((Socket) currentList [currentIdx]) != sock)
778 currentList.RemoveAt (currentIdx);
784 static void AddSockets (List<Socket> sockets, IList list, string name)
787 foreach (Socket sock in list) {
788 if (sock == null) // MS throws a NullRef
789 throw new ArgumentNullException ("name", "Contains a null element");
797 [MethodImplAttribute(MethodImplOptions.InternalCall)]
798 extern static void Select_internal (ref Socket [] sockets, int microSeconds, out int error);
804 public Socket Accept()
806 ThrowIfDisposedAndClosed ();
809 SafeSocketHandle safe_handle = Accept_internal (this.safe_handle, out error, is_blocking);
813 error = SOCKET_CLOSED_CODE;
814 throw new SocketException(error);
817 Socket accepted = new Socket (this.AddressFamily, this.SocketType, this.ProtocolType, safe_handle) {
818 seed_endpoint = this.seed_endpoint,
819 Blocking = this.Blocking,
825 internal void Accept (Socket acceptSocket)
827 ThrowIfDisposedAndClosed ();
830 SafeSocketHandle safe_handle = Accept_internal (this.safe_handle, out error, is_blocking);
834 error = SOCKET_CLOSED_CODE;
835 throw new SocketException (error);
838 acceptSocket.address_family = this.AddressFamily;
839 acceptSocket.socket_type = this.SocketType;
840 acceptSocket.protocol_type = this.ProtocolType;
841 acceptSocket.safe_handle = safe_handle;
842 acceptSocket.is_connected = true;
843 acceptSocket.seed_endpoint = this.seed_endpoint;
844 acceptSocket.Blocking = this.Blocking;
846 // FIXME: figure out what if anything else needs to be reset
849 public bool AcceptAsync (SocketAsyncEventArgs e)
851 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
853 ThrowIfDisposedAndClosed ();
856 throw new InvalidOperationException ("You must call the Bind method before performing this operation.");
858 throw new InvalidOperationException ("You must call the Listen method before performing this operation.");
859 if (e.BufferList != null)
860 throw new ArgumentException ("Multiple buffers cannot be used with this method.");
862 throw new ArgumentOutOfRangeException ("e.Count");
864 Socket acceptSocket = e.AcceptSocket;
865 if (acceptSocket != null) {
866 if (acceptSocket.is_bound || acceptSocket.is_connected)
867 throw new InvalidOperationException ("AcceptSocket: The socket must not be bound or connected.");
871 e.Worker.Init (this, e, SocketOperation.Accept);
873 SocketAsyncResult sockares = e.Worker.result;
877 readQ.Enqueue (e.Worker);
882 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
887 public IAsyncResult BeginAccept(AsyncCallback callback, object state)
889 ThrowIfDisposedAndClosed ();
891 if (!is_bound || !is_listening)
892 throw new InvalidOperationException ();
894 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.Accept);
898 readQ.Enqueue (sockares.Worker);
903 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
908 public IAsyncResult BeginAccept (int receiveSize, AsyncCallback callback, object state)
910 ThrowIfDisposedAndClosed ();
913 throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero");
915 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.AcceptReceive) {
916 Buffer = new byte [receiveSize],
919 SockFlags = SocketFlags.None,
924 readQ.Enqueue (sockares.Worker);
929 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
934 public IAsyncResult BeginAccept (Socket acceptSocket, int receiveSize, AsyncCallback callback, object state)
936 ThrowIfDisposedAndClosed ();
939 throw new ArgumentOutOfRangeException ("receiveSize", "receiveSize is less than zero");
941 if (acceptSocket != null) {
942 ThrowIfDisposedAndClosed (acceptSocket);
944 if (acceptSocket.IsBound)
945 throw new InvalidOperationException ();
947 /* For some reason the MS runtime
948 * barfs if the new socket is not TCP,
949 * even though it's just about to blow
950 * away all those parameters
952 if (acceptSocket.ProtocolType != ProtocolType.Tcp)
953 throw new SocketException ((int)SocketError.InvalidArgument);
956 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.AcceptReceive) {
957 Buffer = new byte [receiveSize],
960 SockFlags = SocketFlags.None,
961 AcceptSocket = acceptSocket,
966 readQ.Enqueue (sockares.Worker);
971 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
976 public Socket EndAccept (IAsyncResult result)
980 return EndAccept (out buffer, out bytes, result);
983 public Socket EndAccept (out byte[] buffer, IAsyncResult asyncResult)
986 return EndAccept (out buffer, out bytes, asyncResult);
989 public Socket EndAccept (out byte[] buffer, out int bytesTransferred, IAsyncResult asyncResult)
991 ThrowIfDisposedAndClosed ();
993 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndAccept", "asyncResult");
995 if (!sockares.IsCompleted)
996 sockares.AsyncWaitHandle.WaitOne ();
998 sockares.CheckIfThrowDelayedException ();
1000 buffer = sockares.Buffer;
1001 bytesTransferred = sockares.Total;
1003 return sockares.Socket;
1006 static SafeSocketHandle Accept_internal (SafeSocketHandle safeHandle, out int error, bool blocking)
1009 safeHandle.RegisterForBlockingSyscall ();
1010 var ret = Accept_internal (safeHandle.DangerousGetHandle (), out error, blocking);
1011 return new SafeSocketHandle (ret, true);
1013 safeHandle.UnRegisterForBlockingSyscall ();
1017 /* Creates a new system socket, returning the handle */
1018 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1019 extern static IntPtr Accept_internal (IntPtr sock, out int error, bool blocking);
1025 public void Connect (IPAddress address, int port)
1027 Connect (new IPEndPoint (address, port));
1030 public void Connect (string host, int port)
1032 Connect (Dns.GetHostAddresses (host), port);
1035 public void Connect (IPAddress[] addresses, int port)
1037 ThrowIfDisposedAndClosed ();
1039 if (addresses == null)
1040 throw new ArgumentNullException ("addresses");
1041 if (this.AddressFamily != AddressFamily.InterNetwork && this.AddressFamily != AddressFamily.InterNetworkV6)
1042 throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families");
1044 throw new InvalidOperationException ();
1046 // FIXME: do non-blocking sockets Poll here?
1048 foreach (IPAddress address in addresses) {
1049 IPEndPoint iep = new IPEndPoint (address, port);
1051 Connect_internal (safe_handle, iep.Serialize (), out error);
1053 is_connected = true;
1055 seed_endpoint = iep;
1058 if (error != (int)SocketError.InProgress && error != (int)SocketError.WouldBlock)
1062 Poll (-1, SelectMode.SelectWrite);
1063 error = (int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
1065 is_connected = true;
1067 seed_endpoint = iep;
1074 throw new SocketException (error);
1078 public void Connect (EndPoint remoteEP)
1080 ThrowIfDisposedAndClosed ();
1082 if (remoteEP == null)
1083 throw new ArgumentNullException ("remoteEP");
1085 IPEndPoint ep = remoteEP as IPEndPoint;
1086 /* Dgram uses Any to 'disconnect' */
1087 if (ep != null && socket_type != SocketType.Dgram) {
1088 if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any))
1089 throw new SocketException ((int) SocketError.AddressNotAvailable);
1093 throw new InvalidOperationException ();
1095 SocketAddress serial = remoteEP.Serialize ();
1098 Connect_internal (safe_handle, serial, out error);
1100 if (error == 0 || error == 10035)
1101 seed_endpoint = remoteEP; // Keep the ep around for non-blocking sockets
1105 error = SOCKET_CLOSED_CODE;
1106 throw new SocketException (error);
1109 is_connected = !(socket_type == SocketType.Dgram && ep != null && (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)));
1113 public bool ConnectAsync (SocketAsyncEventArgs e)
1115 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1117 ThrowIfDisposedAndClosed ();
1120 throw new InvalidOperationException ("You may not perform this operation after calling the Listen method.");
1121 if (e.RemoteEndPoint == null)
1122 throw new ArgumentNullException ("remoteEP");
1125 e.Worker.Init (this, e, SocketOperation.Connect);
1127 SocketAsyncResult result = e.Worker.result;
1130 IPAddress [] addresses;
1133 if (!GetCheckedIPs (e, out addresses)) {
1134 result.EndPoint = e.RemoteEndPoint;
1135 ares = BeginConnect (e.RemoteEndPoint, SocketAsyncEventArgs.Dispatcher, e);
1137 DnsEndPoint dep = (e.RemoteEndPoint as DnsEndPoint);
1138 result.Addresses = addresses;
1139 result.Port = dep.Port;
1140 ares = BeginConnect (addresses, dep.Port, SocketAsyncEventArgs.Dispatcher, e);
1143 if (ares.IsCompleted && ares.CompletedSynchronously) {
1144 ((SocketAsyncResult) ares).CheckIfThrowDelayedException ();
1147 } catch (Exception exc) {
1148 result.Complete (exc, true);
1155 public IAsyncResult BeginConnect (IPAddress address, int port, AsyncCallback callback, object state)
1157 ThrowIfDisposedAndClosed ();
1159 if (address == null)
1160 throw new ArgumentNullException ("address");
1161 if (address.ToString ().Length == 0)
1162 throw new ArgumentException ("The length of the IP address is zero");
1163 if (port <= 0 || port > 65535)
1164 throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
1166 throw new InvalidOperationException ();
1168 return BeginConnect (new IPEndPoint (address, port), callback, state);
1171 public IAsyncResult BeginConnect (string host, int port, AsyncCallback callback, object state)
1173 ThrowIfDisposedAndClosed ();
1176 throw new ArgumentNullException ("host");
1177 if (address_family != AddressFamily.InterNetwork && address_family != AddressFamily.InterNetworkV6)
1178 throw new NotSupportedException ("This method is valid only for sockets in the InterNetwork and InterNetworkV6 families");
1179 if (port <= 0 || port > 65535)
1180 throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
1182 throw new InvalidOperationException ();
1184 return BeginConnect (Dns.GetHostAddresses (host), port, callback, state);
1187 public IAsyncResult BeginConnect (EndPoint end_point, AsyncCallback callback, object state)
1189 ThrowIfDisposedAndClosed ();
1191 if (end_point == null)
1192 throw new ArgumentNullException ("end_point");
1194 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.Connect) {
1195 EndPoint = end_point,
1198 // Bug #75154: Connect() should not succeed for .Any addresses.
1199 if (end_point is IPEndPoint) {
1200 IPEndPoint ep = (IPEndPoint) end_point;
1201 if (ep.Address.Equals (IPAddress.Any) || ep.Address.Equals (IPAddress.IPv6Any)) {
1202 sockares.Complete (new SocketException ((int) SocketError.AddressNotAvailable), true);
1209 if (connect_in_progress) {
1210 // This could happen when multiple IPs are used
1211 // Calling connect() again will reset the connection attempt and cause
1212 // an error. Better to just close the socket and move on.
1213 connect_in_progress = false;
1214 safe_handle.Dispose ();
1215 var handle = Socket_internal (address_family, socket_type, protocol_type, out error);
1216 safe_handle = new SafeSocketHandle (handle, true);
1218 throw new SocketException (error);
1221 bool blk = is_blocking;
1224 Connect_internal (safe_handle, end_point.Serialize (), out error);
1230 is_connected = true;
1232 sockares.Complete (true);
1236 if (error != (int) SocketError.InProgress && error != (int) SocketError.WouldBlock) {
1238 is_connected = false;
1240 sockares.Complete (new SocketException (error), true);
1245 is_connected = false;
1247 connect_in_progress = true;
1249 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
1254 public IAsyncResult BeginConnect (IPAddress[] addresses, int port, AsyncCallback callback, object state)
1256 ThrowIfDisposedAndClosed ();
1258 if (addresses == null)
1259 throw new ArgumentNullException ("addresses");
1260 if (addresses.Length == 0)
1261 throw new ArgumentException ("Empty addresses list");
1262 if (this.AddressFamily != AddressFamily.InterNetwork && this.AddressFamily != AddressFamily.InterNetworkV6)
1263 throw new NotSupportedException ("This method is only valid for addresses in the InterNetwork or InterNetworkV6 families");
1264 if (port <= 0 || port > 65535)
1265 throw new ArgumentOutOfRangeException ("port", "Must be > 0 and < 65536");
1267 throw new InvalidOperationException ();
1269 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.Connect) {
1270 Addresses = addresses,
1274 is_connected = false;
1276 return BeginMConnect (sockares);
1279 internal IAsyncResult BeginMConnect (SocketAsyncResult sockares)
1281 IAsyncResult ares = null;
1282 Exception exc = null;
1284 for (int i = sockares.CurrentAddress; i < sockares.Addresses.Length; i++) {
1286 sockares.CurrentAddress++;
1288 ares = BeginConnect (new IPEndPoint (sockares.Addresses [i], sockares.Port), null, sockares);
1289 if (ares.IsCompleted && ares.CompletedSynchronously) {
1290 ((SocketAsyncResult) ares).CheckIfThrowDelayedException ();
1291 sockares.DoMConnectCallback ();
1295 } catch (Exception e) {
1307 public void EndConnect (IAsyncResult result)
1309 ThrowIfDisposedAndClosed ();
1311 SocketAsyncResult sockares = ValidateEndIAsyncResult (result, "EndConnect", "result");
1313 if (!sockares.IsCompleted)
1314 sockares.AsyncWaitHandle.WaitOne();
1316 sockares.CheckIfThrowDelayedException();
1319 static void Connect_internal (SafeSocketHandle safeHandle, SocketAddress sa, out int error)
1322 safeHandle.RegisterForBlockingSyscall ();
1323 Connect_internal (safeHandle.DangerousGetHandle (), sa, out error);
1325 safeHandle.UnRegisterForBlockingSyscall ();
1329 /* Connects to the remote address */
1330 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1331 extern static void Connect_internal(IntPtr sock, SocketAddress sa, out int error);
1334 * - false when it is ok to use RemoteEndPoint
1335 * - true when addresses must be used (and addresses could be null/empty) */
1336 bool GetCheckedIPs (SocketAsyncEventArgs e, out IPAddress [] addresses)
1340 // Connect to the first address that match the host name, like:
1341 // http://blogs.msdn.com/ncl/archive/2009/07/20/new-ncl-features-in-net-4-0-beta-2.aspx
1342 // while skipping entries that do not match the address family
1343 DnsEndPoint dep = e.RemoteEndPoint as DnsEndPoint;
1345 addresses = Dns.GetHostAddresses (dep.Host);
1348 e.ConnectByNameError = null;
1357 /* According to the docs, the MS runtime will throw PlatformNotSupportedException
1358 * if the platform is newer than w2k. We should be able to cope... */
1359 public void Disconnect (bool reuseSocket)
1361 ThrowIfDisposedAndClosed ();
1364 Disconnect_internal (safe_handle, reuseSocket, out error);
1368 /* ERROR_NOT_SUPPORTED */
1369 throw new PlatformNotSupportedException ();
1371 throw new SocketException (error);
1375 is_connected = false;
1377 /* Do managed housekeeping here... */
1381 public bool DisconnectAsync (SocketAsyncEventArgs e)
1383 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1385 ThrowIfDisposedAndClosed ();
1388 e.Worker.Init (this, e, SocketOperation.Disconnect);
1390 SocketAsyncResult sockares = e.Worker.result;
1392 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
1398 public IAsyncResult BeginDisconnect (bool reuseSocket, AsyncCallback callback, object state)
1400 ThrowIfDisposedAndClosed ();
1402 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.Disconnect) {
1403 ReuseSocket = reuseSocket,
1406 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
1411 public void EndDisconnect (IAsyncResult asyncResult)
1413 ThrowIfDisposedAndClosed ();
1415 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndDisconnect", "asyncResult");
1417 if (!sockares.IsCompleted)
1418 sockares.AsyncWaitHandle.WaitOne ();
1420 sockares.CheckIfThrowDelayedException ();
1423 static void Disconnect_internal (SafeSocketHandle safeHandle, bool reuse, out int error)
1425 bool release = false;
1427 safeHandle.DangerousAddRef (ref release);
1428 Disconnect_internal (safeHandle.DangerousGetHandle (), reuse, out error);
1431 safeHandle.DangerousRelease ();
1435 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1436 extern static void Disconnect_internal (IntPtr sock, bool reuse, out int error);
1442 public int Receive (byte [] buffer)
1444 return Receive (buffer, SocketFlags.None);
1447 public int Receive (byte [] buffer, SocketFlags flags)
1449 ThrowIfDisposedAndClosed ();
1450 ThrowIfBufferNull (buffer);
1451 ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
1454 int ret = Receive_nochecks (buffer, 0, buffer.Length, flags, out error);
1456 if (error != SocketError.Success) {
1457 if (error == SocketError.WouldBlock && is_blocking) // This might happen when ReceiveTimeout is set
1458 throw new SocketException ((int) error, TIMEOUT_EXCEPTION_MSG);
1459 throw new SocketException ((int) error);
1465 public int Receive (byte [] buffer, int size, SocketFlags flags)
1467 ThrowIfDisposedAndClosed ();
1468 ThrowIfBufferNull (buffer);
1469 ThrowIfBufferOutOfRange (buffer, 0, size);
1472 int ret = Receive_nochecks (buffer, 0, size, flags, out error);
1474 if (error != SocketError.Success) {
1475 if (error == SocketError.WouldBlock && is_blocking) // This might happen when ReceiveTimeout is set
1476 throw new SocketException ((int) error, TIMEOUT_EXCEPTION_MSG);
1477 throw new SocketException ((int) error);
1483 public int Receive (byte [] buffer, int offset, int size, SocketFlags flags)
1485 ThrowIfDisposedAndClosed ();
1486 ThrowIfBufferNull (buffer);
1487 ThrowIfBufferOutOfRange (buffer, offset, size);
1490 int ret = Receive_nochecks (buffer, offset, size, flags, out error);
1492 if (error != SocketError.Success) {
1493 if (error == SocketError.WouldBlock && is_blocking) // This might happen when ReceiveTimeout is set
1494 throw new SocketException ((int) error, TIMEOUT_EXCEPTION_MSG);
1495 throw new SocketException ((int) error);
1501 public int Receive (byte [] buffer, int offset, int size, SocketFlags flags, out SocketError error)
1503 ThrowIfDisposedAndClosed ();
1504 ThrowIfBufferNull (buffer);
1505 ThrowIfBufferOutOfRange (buffer, offset, size);
1507 return Receive_nochecks (buffer, offset, size, flags, out error);
1510 public int Receive (IList<ArraySegment<byte>> buffers)
1513 int ret = Receive (buffers, SocketFlags.None, out error);
1515 if (error != SocketError.Success)
1516 throw new SocketException ((int) error);
1521 [CLSCompliant (false)]
1522 public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
1525 int ret = Receive (buffers, socketFlags, out error);
1527 if (error != SocketError.Success)
1528 throw new SocketException ((int) error);
1533 [CLSCompliant (false)]
1534 public int Receive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
1536 ThrowIfDisposedAndClosed ();
1538 if (buffers == null || buffers.Count == 0)
1539 throw new ArgumentNullException ("buffers");
1541 int numsegments = buffers.Count;
1545 /* Only example I can find of sending a byte array reference directly into an internal
1546 * call is in System.Runtime.Remoting/System.Runtime.Remoting.Channels.Ipc.Win32/NamedPipeSocket.cs,
1547 * so taking a lead from that... */
1548 WSABUF[] bufarray = new WSABUF[numsegments];
1549 GCHandle[] gch = new GCHandle[numsegments];
1551 for (int i = 0; i < numsegments; i++) {
1552 ArraySegment<byte> segment = buffers[i];
1554 if (segment.Offset < 0 || segment.Count < 0 || segment.Count > segment.Array.Length - segment.Offset)
1555 throw new ArgumentOutOfRangeException ("segment");
1557 gch[i] = GCHandle.Alloc (segment.Array, GCHandleType.Pinned);
1558 bufarray[i].len = segment.Count;
1559 bufarray[i].buf = Marshal.UnsafeAddrOfPinnedArrayElement (segment.Array, segment.Offset);
1563 ret = Receive_internal (safe_handle, bufarray, socketFlags, out nativeError);
1565 for (int i = 0; i < numsegments; i++) {
1566 if (gch[i].IsAllocated)
1571 errorCode = (SocketError) nativeError;
1576 public bool ReceiveAsync (SocketAsyncEventArgs e)
1578 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1580 ThrowIfDisposedAndClosed ();
1582 // LAME SPEC: the ArgumentException is never thrown, instead an NRE is
1583 // thrown when e.Buffer and e.BufferList are null (works fine when one is
1584 // set to a valid object)
1585 if (e.Buffer == null && e.BufferList == null)
1586 throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
1589 e.Worker.Init (this, e, e.Buffer != null ? SocketOperation.Receive : SocketOperation.ReceiveGeneric);
1591 SocketAsyncResult sockares = e.Worker.result;
1592 sockares.SockFlags = e.SocketFlags;
1594 if (e.Buffer != null) {
1595 sockares.Buffer = e.Buffer;
1596 sockares.Offset = e.Offset;
1597 sockares.Size = e.Count;
1599 sockares.Buffers = e.BufferList;
1604 readQ.Enqueue (e.Worker);
1605 count = readQ.Count;
1609 // Receive takes care of ReceiveGeneric
1610 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
1616 public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state)
1618 ThrowIfDisposedAndClosed ();
1619 ThrowIfBufferNull (buffer);
1620 ThrowIfBufferOutOfRange (buffer, offset, size);
1622 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.Receive) {
1626 SockFlags = socket_flags,
1631 readQ.Enqueue (sockares.Worker);
1632 count = readQ.Count;
1636 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
1641 public IAsyncResult BeginReceive (byte[] buffer, int offset, int size, SocketFlags flags, out SocketError error, AsyncCallback callback, object state)
1643 /* As far as I can tell from the docs and from experimentation, a pointer to the
1644 * SocketError parameter is not supposed to be saved for the async parts. And as we don't
1645 * set any socket errors in the setup code, we just have to set it to Success. */
1646 error = SocketError.Success;
1647 return BeginReceive (buffer, offset, size, flags, callback, state);
1650 [CLSCompliant (false)]
1651 public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
1653 ThrowIfDisposedAndClosed ();
1655 if (buffers == null)
1656 throw new ArgumentNullException ("buffers");
1658 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.ReceiveGeneric) {
1660 SockFlags = socketFlags,
1665 readQ.Enqueue (sockares.Worker);
1666 count = readQ.Count;
1670 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
1675 [CLSCompliant (false)]
1676 public IAsyncResult BeginReceive (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
1678 /* I assume the same SocketError semantics as above */
1679 errorCode = SocketError.Success;
1680 return BeginReceive (buffers, socketFlags, callback, state);
1683 public int EndReceive (IAsyncResult result)
1686 int bytesReceived = EndReceive (result, out error);
1688 if (error != SocketError.Success) {
1689 if (error != SocketError.WouldBlock && error != SocketError.InProgress)
1690 is_connected = false;
1691 throw new SocketException ((int)error);
1694 return bytesReceived;
1697 public int EndReceive (IAsyncResult asyncResult, out SocketError errorCode)
1699 ThrowIfDisposedAndClosed ();
1701 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndReceive", "asyncResult");
1703 if (!sockares.IsCompleted)
1704 sockares.AsyncWaitHandle.WaitOne ();
1706 // If no socket error occurred, call CheckIfThrowDelayedException in case there are other
1707 // kinds of exceptions that should be thrown.
1708 if ((errorCode = sockares.ErrorCode) == SocketError.Success)
1709 sockares.CheckIfThrowDelayedException();
1711 return sockares.Total;
1714 internal int Receive_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
1717 int ret = Receive_internal (safe_handle, buf, offset, size, flags, out nativeError);
1719 error = (SocketError) nativeError;
1720 if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
1721 is_connected = false;
1724 is_connected = true;
1730 static int Receive_internal (SafeSocketHandle safeHandle, WSABUF[] bufarray, SocketFlags flags, out int error)
1733 safeHandle.RegisterForBlockingSyscall ();
1734 return Receive_internal (safeHandle.DangerousGetHandle (), bufarray, flags, out error);
1736 safeHandle.UnRegisterForBlockingSyscall ();
1740 [MethodImplAttribute (MethodImplOptions.InternalCall)]
1741 extern static int Receive_internal (IntPtr sock, WSABUF[] bufarray, SocketFlags flags, out int error);
1743 static int Receive_internal (SafeSocketHandle safeHandle, byte[] buffer, int offset, int count, SocketFlags flags, out int error)
1746 safeHandle.RegisterForBlockingSyscall ();
1747 return Receive_internal (safeHandle.DangerousGetHandle (), buffer, offset, count, flags, out error);
1749 safeHandle.UnRegisterForBlockingSyscall ();
1753 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1754 extern static int Receive_internal(IntPtr sock, byte[] buffer, int offset, int count, SocketFlags flags, out int error);
1760 public int ReceiveFrom (byte [] buffer, ref EndPoint remoteEP)
1762 ThrowIfDisposedAndClosed ();
1763 ThrowIfBufferNull (buffer);
1764 ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
1766 if (remoteEP == null)
1767 throw new ArgumentNullException ("remoteEP");
1769 return ReceiveFrom_nochecks (buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP);
1772 public int ReceiveFrom (byte [] buffer, SocketFlags flags, ref EndPoint remoteEP)
1774 ThrowIfDisposedAndClosed ();
1775 ThrowIfBufferNull (buffer);
1776 ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
1778 if (remoteEP == null)
1779 throw new ArgumentNullException ("remoteEP");
1781 return ReceiveFrom_nochecks (buffer, 0, buffer.Length, flags, ref remoteEP);
1784 public int ReceiveFrom (byte [] buffer, int size, SocketFlags flags, ref EndPoint remoteEP)
1786 ThrowIfDisposedAndClosed ();
1787 ThrowIfBufferNull (buffer);
1788 ThrowIfBufferOutOfRange (buffer, 0, size);
1790 if (remoteEP == null)
1791 throw new ArgumentNullException ("remoteEP");
1793 return ReceiveFrom_nochecks (buffer, 0, size, flags, ref remoteEP);
1796 public int ReceiveFrom (byte [] buffer, int offset, int size, SocketFlags flags, ref EndPoint remoteEP)
1798 ThrowIfDisposedAndClosed ();
1799 ThrowIfBufferNull (buffer);
1800 ThrowIfBufferOutOfRange (buffer, offset, size);
1802 if (remoteEP == null)
1803 throw new ArgumentNullException ("remoteEP");
1805 return ReceiveFrom_nochecks (buffer, offset, size, flags, ref remoteEP);
1808 public bool ReceiveFromAsync (SocketAsyncEventArgs e)
1810 ThrowIfDisposedAndClosed ();
1812 // We do not support recv into multiple buffers yet
1813 if (e.BufferList != null)
1814 throw new NotSupportedException ("Mono doesn't support using BufferList at this point.");
1815 if (e.RemoteEndPoint == null)
1816 throw new ArgumentNullException ("remoteEP", "Value cannot be null.");
1819 e.Worker.Init (this, e, SocketOperation.ReceiveFrom);
1821 SocketAsyncResult sockares = e.Worker.result;
1822 sockares.Buffer = e.Buffer;
1823 sockares.Offset = e.Offset;
1824 sockares.Size = e.Count;
1825 sockares.EndPoint = e.RemoteEndPoint;
1826 sockares.SockFlags = e.SocketFlags;
1830 readQ.Enqueue (e.Worker);
1831 count = readQ.Count;
1835 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
1840 public IAsyncResult BeginReceiveFrom (byte[] buffer, int offset, int size, SocketFlags socket_flags, ref EndPoint remote_end, AsyncCallback callback, object state)
1842 ThrowIfDisposedAndClosed ();
1843 ThrowIfBufferNull (buffer);
1844 ThrowIfBufferOutOfRange (buffer, offset, size);
1846 if (remote_end == null)
1847 throw new ArgumentNullException ("remote_end");
1849 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.ReceiveFrom) {
1853 SockFlags = socket_flags,
1854 EndPoint = remote_end,
1859 readQ.Enqueue (sockares.Worker);
1860 count = readQ.Count;
1864 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
1869 public int EndReceiveFrom(IAsyncResult result, ref EndPoint end_point)
1871 ThrowIfDisposedAndClosed ();
1873 if (end_point == null)
1874 throw new ArgumentNullException ("remote_end");
1876 SocketAsyncResult sockares = ValidateEndIAsyncResult (result, "EndReceiveFrom", "result");
1878 if (!sockares.IsCompleted)
1879 sockares.AsyncWaitHandle.WaitOne();
1881 sockares.CheckIfThrowDelayedException();
1883 end_point = sockares.EndPoint;
1885 return sockares.Total;
1888 internal int ReceiveFrom_nochecks (byte [] buf, int offset, int size, SocketFlags flags, ref EndPoint remote_end)
1891 return ReceiveFrom_nochecks_exc (buf, offset, size, flags, ref remote_end, true, out error);
1894 internal int ReceiveFrom_nochecks_exc (byte [] buf, int offset, int size, SocketFlags flags, ref EndPoint remote_end, bool throwOnError, out int error)
1896 SocketAddress sockaddr = remote_end.Serialize();
1898 int cnt = ReceiveFrom_internal (safe_handle, buf, offset, size, flags, ref sockaddr, out error);
1900 SocketError err = (SocketError) error;
1902 if (err != SocketError.WouldBlock && err != SocketError.InProgress) {
1903 is_connected = false;
1904 } else if (err == SocketError.WouldBlock && is_blocking) { // This might happen when ReceiveTimeout is set
1906 throw new SocketException ((int) SocketError.TimedOut, TIMEOUT_EXCEPTION_MSG);
1907 error = (int) SocketError.TimedOut;
1912 throw new SocketException (error);
1917 is_connected = true;
1920 /* If sockaddr is null then we're a connection oriented protocol and should ignore the
1921 * remote_end parameter (see MSDN documentation for Socket.ReceiveFrom(...) ) */
1922 if (sockaddr != null) {
1923 /* Stupidly, EndPoint.Create() is an instance method */
1924 remote_end = remote_end.Create (sockaddr);
1927 seed_endpoint = remote_end;
1932 static int ReceiveFrom_internal (SafeSocketHandle safeHandle, byte[] buffer, int offset, int count, SocketFlags flags, ref SocketAddress sockaddr, out int error)
1935 safeHandle.RegisterForBlockingSyscall ();
1936 return ReceiveFrom_internal (safeHandle.DangerousGetHandle (), buffer, offset, count, flags, ref sockaddr, out error);
1938 safeHandle.UnRegisterForBlockingSyscall ();
1942 [MethodImplAttribute(MethodImplOptions.InternalCall)]
1943 extern static int ReceiveFrom_internal(IntPtr sock, byte[] buffer, int offset, int count, SocketFlags flags, ref SocketAddress sockaddr, out int error);
1947 #region ReceiveMessageFrom
1949 [MonoTODO ("Not implemented")]
1950 public int ReceiveMessageFrom (byte[] buffer, int offset, int size, ref SocketFlags socketFlags, ref EndPoint remoteEP, out IPPacketInformation ipPacketInformation)
1952 ThrowIfDisposedAndClosed ();
1953 ThrowIfBufferNull (buffer);
1954 ThrowIfBufferOutOfRange (buffer, offset, size);
1956 if (remoteEP == null)
1957 throw new ArgumentNullException ("remoteEP");
1959 // FIXME: figure out how we get hold of the IPPacketInformation
1960 throw new NotImplementedException ();
1963 [MonoTODO ("Not implemented")]
1964 public bool ReceiveMessageFromAsync (SocketAsyncEventArgs e)
1966 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
1968 ThrowIfDisposedAndClosed ();
1970 throw new NotImplementedException ();
1974 public IAsyncResult BeginReceiveMessageFrom (byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, object state)
1976 ThrowIfDisposedAndClosed ();
1977 ThrowIfBufferNull (buffer);
1978 ThrowIfBufferOutOfRange (buffer, offset, size);
1980 if (remoteEP == null)
1981 throw new ArgumentNullException ("remoteEP");
1983 throw new NotImplementedException ();
1987 public int EndReceiveMessageFrom (IAsyncResult asyncResult, ref SocketFlags socketFlags, ref EndPoint endPoint, out IPPacketInformation ipPacketInformation)
1989 ThrowIfDisposedAndClosed ();
1991 if (endPoint == null)
1992 throw new ArgumentNullException ("endPoint");
1994 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndReceiveMessageFrom", "asyncResult");
1996 throw new NotImplementedException ();
2003 public int Send (byte [] buffer)
2005 ThrowIfDisposedAndClosed ();
2006 ThrowIfBufferNull (buffer);
2007 ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
2010 int ret = Send_nochecks (buffer, 0, buffer.Length, SocketFlags.None, out error);
2012 if (error != SocketError.Success)
2013 throw new SocketException ((int) error);
2018 public int Send (byte [] buffer, SocketFlags flags)
2020 ThrowIfDisposedAndClosed ();
2021 ThrowIfBufferNull (buffer);
2022 ThrowIfBufferOutOfRange (buffer, 0, buffer.Length);
2025 int ret = Send_nochecks (buffer, 0, buffer.Length, flags, out error);
2027 if (error != SocketError.Success)
2028 throw new SocketException ((int) error);
2033 public int Send (byte [] buffer, int size, SocketFlags flags)
2035 ThrowIfDisposedAndClosed ();
2036 ThrowIfBufferNull (buffer);
2037 ThrowIfBufferOutOfRange (buffer, 0, size);
2040 int ret = Send_nochecks (buffer, 0, size, flags, out error);
2042 if (error != SocketError.Success)
2043 throw new SocketException ((int) error);
2048 public int Send (byte [] buffer, int offset, int size, SocketFlags flags)
2050 ThrowIfDisposedAndClosed ();
2051 ThrowIfBufferNull (buffer);
2052 ThrowIfBufferOutOfRange (buffer, offset, size);
2055 int ret = Send_nochecks (buffer, offset, size, flags, out error);
2057 if (error != SocketError.Success)
2058 throw new SocketException ((int) error);
2063 public int Send (byte [] buffer, int offset, int size, SocketFlags flags, out SocketError error)
2065 ThrowIfDisposedAndClosed ();
2066 ThrowIfBufferNull (buffer);
2067 ThrowIfBufferOutOfRange (buffer, offset, size);
2069 return Send_nochecks (buffer, offset, size, flags, out error);
2073 int Send (IList<ArraySegment<byte>> buffers)
2076 int ret = Send (buffers, SocketFlags.None, out error);
2078 if (error != SocketError.Success)
2079 throw new SocketException ((int) error);
2085 int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags)
2088 int ret = Send (buffers, socketFlags, out error);
2090 if (error != SocketError.Success)
2091 throw new SocketException ((int) error);
2096 [CLSCompliant (false)]
2097 public int Send (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode)
2099 ThrowIfDisposedAndClosed ();
2101 if (buffers == null)
2102 throw new ArgumentNullException ("buffers");
2103 if (buffers.Count == 0)
2104 throw new ArgumentException ("Buffer is empty", "buffers");
2106 int numsegments = buffers.Count;
2110 WSABUF[] bufarray = new WSABUF[numsegments];
2111 GCHandle[] gch = new GCHandle[numsegments];
2113 for(int i = 0; i < numsegments; i++) {
2114 ArraySegment<byte> segment = buffers[i];
2116 if (segment.Offset < 0 || segment.Count < 0 || segment.Count > segment.Array.Length - segment.Offset)
2117 throw new ArgumentOutOfRangeException ("segment");
2119 gch[i] = GCHandle.Alloc (segment.Array, GCHandleType.Pinned);
2120 bufarray[i].len = segment.Count;
2121 bufarray[i].buf = Marshal.UnsafeAddrOfPinnedArrayElement (segment.Array, segment.Offset);
2125 ret = Send_internal (safe_handle, bufarray, socketFlags, out nativeError);
2127 for(int i = 0; i < numsegments; i++) {
2128 if (gch[i].IsAllocated) {
2134 errorCode = (SocketError)nativeError;
2139 internal int Send_nochecks (byte [] buf, int offset, int size, SocketFlags flags, out SocketError error)
2142 error = SocketError.Success;
2147 int ret = Send_internal (safe_handle, buf, offset, size, flags, out nativeError);
2149 error = (SocketError)nativeError;
2151 if (error != SocketError.Success && error != SocketError.WouldBlock && error != SocketError.InProgress) {
2152 is_connected = false;
2155 is_connected = true;
2161 public bool SendAsync (SocketAsyncEventArgs e)
2163 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
2165 ThrowIfDisposedAndClosed ();
2167 if (e.Buffer == null && e.BufferList == null)
2168 throw new NullReferenceException ("Either e.Buffer or e.BufferList must be valid buffers.");
2171 e.Worker.Init (this, e, e.Buffer != null ? SocketOperation.Send : SocketOperation.SendGeneric);
2173 SocketAsyncResult sockares = e.Worker.result;
2174 sockares.SockFlags = e.SocketFlags;
2176 if (e.Buffer != null) {
2177 sockares.Buffer = e.Buffer;
2178 sockares.Offset = e.Offset;
2179 sockares.Size = e.Count;
2181 sockares.Buffers = e.BufferList;
2186 writeQ.Enqueue (e.Worker);
2187 count = writeQ.Count;
2191 // Send takes care of SendGeneric
2192 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
2198 public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
2200 if (!is_connected) {
2201 errorCode = SocketError.NotConnected;
2202 throw new SocketException ((int) errorCode);
2205 errorCode = SocketError.Success;
2206 return BeginSend (buffer, offset, size, socketFlags, callback, state);
2209 public IAsyncResult BeginSend (byte[] buffer, int offset, int size, SocketFlags socket_flags, AsyncCallback callback, object state)
2211 ThrowIfDisposedAndClosed ();
2212 ThrowIfBufferNull (buffer);
2213 ThrowIfBufferOutOfRange (buffer, offset, size);
2216 throw new SocketException ((int)SocketError.NotConnected);
2218 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.Send) {
2222 SockFlags = socket_flags,
2227 writeQ.Enqueue (sockares.Worker);
2228 count = writeQ.Count;
2232 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
2237 public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, AsyncCallback callback, object state)
2239 ThrowIfDisposedAndClosed ();
2241 if (buffers == null)
2242 throw new ArgumentNullException ("buffers");
2244 throw new SocketException ((int)SocketError.NotConnected);
2246 SocketAsyncResult sockares = new SocketAsyncResult (this, state, callback, SocketOperation.SendGeneric) {
2248 SockFlags = socketFlags,
2253 writeQ.Enqueue (sockares.Worker);
2254 count = writeQ.Count;
2258 socket_pool_queue (SocketAsyncWorker.Dispatcher, sockares);
2263 [CLSCompliant (false)]
2264 public IAsyncResult BeginSend (IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state)
2266 if (!is_connected) {
2267 errorCode = SocketError.NotConnected;
2268 throw new SocketException ((int)errorCode);
2271 errorCode = SocketError.Success;
2272 return BeginSend (buffers, socketFlags, callback, state);
2275 public int EndSend (IAsyncResult result)
2278 int bytesSent = EndSend (result, out error);
2280 if (error != SocketError.Success) {
2281 if (error != SocketError.WouldBlock && error != SocketError.InProgress)
2282 is_connected = false;
2283 throw new SocketException ((int)error);
2289 public int EndSend (IAsyncResult asyncResult, out SocketError errorCode)
2291 ThrowIfDisposedAndClosed ();
2293 SocketAsyncResult sockares = ValidateEndIAsyncResult (asyncResult, "EndSend", "asyncResult");
2295 if (!sockares.IsCompleted)
2296 sockares.AsyncWaitHandle.WaitOne ();
2298 /* If no socket error occurred, call CheckIfThrowDelayedException in
2299 * case there are other kinds of exceptions that should be thrown.*/
2300 if ((errorCode = sockares.ErrorCode) == SocketError.Success)
2301 sockares.CheckIfThrowDelayedException ();
2303 return sockares.Total;
2306 static int Send_internal (SafeSocketHandle safeHandle, WSABUF[] bufarray, SocketFlags flags, out int error)
2308 bool release = false;
2310 safeHandle.DangerousAddRef (ref release);
2311 return Send_internal (safeHandle.DangerousGetHandle (), bufarray, flags, out error);
2314 safeHandle.DangerousRelease ();
2318 [MethodImplAttribute (MethodImplOptions.InternalCall)]
2319 extern static int Send_internal (IntPtr sock, WSABUF[] bufarray, SocketFlags flags, out int error);
2321 static int Send_internal (SafeSocketHandle safeHandle, byte[] buf, int offset, int count, SocketFlags flags, out int error)
2324 safeHandle.RegisterForBlockingSyscall ();
2325 return Send_internal (safeHandle.DangerousGetHandle (), buf, offset, count, flags, out error);
2327 safeHandle.UnRegisterForBlockingSyscall ();
2331 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2332 extern static int Send_internal(IntPtr sock, byte[] buf, int offset, int count, SocketFlags flags, out int error);
2336 void CheckRange (byte[] buffer, int offset, int size)
2339 throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
2340 if (offset > buffer.Length)
2341 throw new ArgumentOutOfRangeException ("offset", "offset must be <= buffer.Length");
2343 throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
2344 if (size > buffer.Length - offset)
2345 throw new ArgumentOutOfRangeException ("size", "size must be <= buffer.Length - offset");
2348 delegate void SendFileHandler (string fileName, byte [] preBuffer, byte [] postBuffer, TransmitFileOptions flags);
2350 sealed class SendFileAsyncResult : IAsyncResult {
2354 public SendFileAsyncResult (SendFileHandler d, IAsyncResult ares)
2360 public object AsyncState {
2361 get { return ares.AsyncState; }
2364 public WaitHandle AsyncWaitHandle {
2365 get { return ares.AsyncWaitHandle; }
2368 public bool CompletedSynchronously {
2369 get { return ares.CompletedSynchronously; }
2372 public bool IsCompleted {
2373 get { return ares.IsCompleted; }
2376 public SendFileHandler Delegate {
2380 public IAsyncResult Original {
2381 get { return ares; }
2385 public IAsyncResult BeginSendFile (string fileName,
2386 AsyncCallback callback,
2389 if (is_disposed && is_closed)
2390 throw new ObjectDisposedException (GetType ().ToString ());
2393 throw new NotSupportedException ();
2395 if (!File.Exists (fileName))
2396 throw new FileNotFoundException ();
2398 return BeginSendFile (fileName, null, null, 0, callback, state);
2401 public IAsyncResult BeginSendFile (string fileName,
2404 TransmitFileOptions flags,
2405 AsyncCallback callback,
2408 if (is_disposed && is_closed)
2409 throw new ObjectDisposedException (GetType ().ToString ());
2412 throw new NotSupportedException ();
2414 if (!File.Exists (fileName))
2415 throw new FileNotFoundException ();
2417 SendFileHandler d = new SendFileHandler (SendFile);
2418 return new SendFileAsyncResult (d, d.BeginInvoke (fileName, preBuffer, postBuffer, flags, ar => {
2419 SendFileAsyncResult sfar = new SendFileAsyncResult (d, ar);
2424 public IAsyncResult BeginSendTo(byte[] buffer, int offset,
2426 SocketFlags socket_flags,
2427 EndPoint remote_end,
2428 AsyncCallback callback,
2430 if (is_disposed && is_closed)
2431 throw new ObjectDisposedException (GetType ().ToString ());
2434 throw new ArgumentNullException ("buffer");
2436 CheckRange (buffer, offset, size);
2438 SocketAsyncResult req = new SocketAsyncResult (this, state, callback, SocketOperation.SendTo);
2439 req.Buffer = buffer;
2440 req.Offset = offset;
2442 req.SockFlags = socket_flags;
2443 req.EndPoint = remote_end;
2446 writeQ.Enqueue (req.Worker);
2447 count = writeQ.Count;
2450 socket_pool_queue (SocketAsyncWorker.Dispatcher, req);
2454 // Creates a new system socket, returning the handle
2455 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2456 private extern static void Bind_internal(IntPtr sock,
2460 private static void Bind_internal (SafeSocketHandle safeHandle,
2464 bool release = false;
2466 safeHandle.DangerousAddRef (ref release);
2467 Bind_internal (safeHandle.DangerousGetHandle (), sa, out error);
2470 safeHandle.DangerousRelease ();
2474 public void Bind(EndPoint local_end) {
2475 if (is_disposed && is_closed)
2476 throw new ObjectDisposedException (GetType ().ToString ());
2478 if (local_end == null)
2479 throw new ArgumentNullException("local_end");
2483 Bind_internal (safe_handle, local_end.Serialize(), out error);
2485 throw new SocketException (error);
2489 seed_endpoint = local_end;
2496 [MonoLimitation ("We do not support passing sockets across processes, we merely allow this API to pass the socket across AppDomains")]
2497 public SocketInformation DuplicateAndClose (int targetProcessId)
2499 var si = new SocketInformation ();
2501 (is_listening ? SocketInformationOptions.Listening : 0) |
2502 (is_connected ? SocketInformationOptions.Connected : 0) |
2503 (is_blocking ? 0 : SocketInformationOptions.NonBlocking) |
2504 (use_overlapped_io ? SocketInformationOptions.UseOnlyOverlappedIO : 0);
2506 si.ProtocolInformation = Mono.DataConverter.Pack ("iiiil", (int)address_family, (int)socket_type, (int)protocol_type, is_bound ? 1 : 0, (long)Handle);
2514 public void EndSendFile (IAsyncResult asyncResult)
2516 if (is_disposed && is_closed)
2517 throw new ObjectDisposedException (GetType ().ToString ());
2519 if (asyncResult == null)
2520 throw new ArgumentNullException ("asyncResult");
2522 SendFileAsyncResult ares = asyncResult as SendFileAsyncResult;
2524 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
2526 ares.Delegate.EndInvoke (ares.Original);
2529 public int EndSendTo (IAsyncResult result)
2531 if (is_disposed && is_closed)
2532 throw new ObjectDisposedException (GetType ().ToString ());
2535 throw new ArgumentNullException ("result");
2537 SocketAsyncResult req = result as SocketAsyncResult;
2539 throw new ArgumentException ("Invalid IAsyncResult", "result");
2541 if (Interlocked.CompareExchange (ref req.EndCalled, 1, 0) == 1)
2542 throw InvalidAsyncOp ("EndSendTo");
2543 if (!result.IsCompleted)
2544 result.AsyncWaitHandle.WaitOne();
2546 req.CheckIfThrowDelayedException();
2550 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2551 private extern static void GetSocketOption_arr_internal(IntPtr socket,
2552 SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val,
2555 private static void GetSocketOption_arr_internal (SafeSocketHandle safeHandle,
2556 SocketOptionLevel level, SocketOptionName name, ref byte[] byte_val,
2559 bool release = false;
2561 safeHandle.DangerousAddRef (ref release);
2562 GetSocketOption_arr_internal (safeHandle.DangerousGetHandle (), level, name, ref byte_val, out error);
2565 safeHandle.DangerousRelease ();
2569 public void GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue)
2571 if (is_disposed && is_closed)
2572 throw new ObjectDisposedException (GetType ().ToString ());
2574 if (optionValue == null)
2575 throw new SocketException ((int) SocketError.Fault,
2576 "Error trying to dereference an invalid pointer");
2580 GetSocketOption_arr_internal (safe_handle, optionLevel, optionName, ref optionValue,
2583 throw new SocketException (error);
2586 public byte [] GetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, int length)
2588 if (is_disposed && is_closed)
2589 throw new ObjectDisposedException (GetType ().ToString ());
2591 byte[] byte_val=new byte[length];
2594 GetSocketOption_arr_internal (safe_handle, optionLevel, optionName, ref byte_val,
2597 throw new SocketException (error);
2602 // See Socket.IOControl, WSAIoctl documentation in MSDN. The
2603 // common options between UNIX and Winsock are FIONREAD,
2604 // FIONBIO and SIOCATMARK. Anything else will depend on the
2605 // system except SIO_KEEPALIVE_VALS which is properly handled
2606 // on both windows and linux.
2607 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2608 extern static int WSAIoctl (IntPtr sock, int ioctl_code, byte [] input,
2609 byte [] output, out int error);
2611 private static int WSAIoctl (SafeSocketHandle safeHandle, int ioctl_code, byte [] input,
2612 byte [] output, out int error)
2614 bool release = false;
2616 safeHandle.DangerousAddRef (ref release);
2617 return WSAIoctl (safeHandle.DangerousGetHandle (), ioctl_code, input, output, out error);
2620 safeHandle.DangerousRelease ();
2624 public int IOControl (int ioctl_code, byte [] in_value, byte [] out_value)
2627 throw new ObjectDisposedException (GetType ().ToString ());
2630 int result = WSAIoctl (safe_handle, ioctl_code, in_value, out_value,
2634 throw new SocketException (error);
2637 throw new InvalidOperationException ("Must use Blocking property instead.");
2642 public int IOControl (IOControlCode ioControlCode, byte[] optionInValue, byte[] optionOutValue)
2644 return IOControl ((int) ioControlCode, optionInValue, optionOutValue);
2647 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2648 private extern static void Listen_internal(IntPtr sock, int backlog, out int error);
2650 private static void Listen_internal (SafeSocketHandle safeHandle, int backlog, out int error)
2652 bool release = false;
2654 safeHandle.DangerousAddRef (ref release);
2655 Listen_internal (safeHandle.DangerousGetHandle (), backlog, out error);
2658 safeHandle.DangerousRelease ();
2662 public void Listen (int backlog)
2664 if (is_disposed && is_closed)
2665 throw new ObjectDisposedException (GetType ().ToString ());
2668 throw new SocketException ((int)SocketError.InvalidArgument);
2671 Listen_internal(safe_handle, backlog, out error);
2674 throw new SocketException (error);
2676 is_listening = true;
2679 public bool Poll (int time_us, SelectMode mode)
2681 if (is_disposed && is_closed)
2682 throw new ObjectDisposedException (GetType ().ToString ());
2684 if (mode != SelectMode.SelectRead &&
2685 mode != SelectMode.SelectWrite &&
2686 mode != SelectMode.SelectError)
2687 throw new NotSupportedException ("'mode' parameter is not valid.");
2690 bool result = Poll_internal (safe_handle, mode, time_us, out error);
2692 throw new SocketException (error);
2694 if (mode == SelectMode.SelectWrite && result && !is_connected) {
2695 /* Update the is_connected state; for
2696 * non-blocking Connect()s this is
2697 * when we can find out that the
2698 * connect succeeded.
2700 if ((int)GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error) == 0) {
2701 is_connected = true;
2710 [MonoTODO ("Not implemented")]
2711 public bool SendPacketsAsync (SocketAsyncEventArgs e)
2713 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
2715 if (is_disposed && is_closed)
2716 throw new ObjectDisposedException (GetType ().ToString ());
2718 throw new NotImplementedException ();
2723 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2724 private extern static bool SendFile (IntPtr sock, string filename, byte [] pre_buffer, byte [] post_buffer, TransmitFileOptions flags);
2726 private static bool SendFile (SafeSocketHandle safeHandle, string filename, byte [] pre_buffer, byte [] post_buffer, TransmitFileOptions flags)
2729 safeHandle.RegisterForBlockingSyscall ();
2730 return SendFile (safeHandle.DangerousGetHandle (), filename, pre_buffer, post_buffer, flags);
2732 safeHandle.UnRegisterForBlockingSyscall ();
2736 public void SendFile (string fileName)
2738 if (is_disposed && is_closed)
2739 throw new ObjectDisposedException (GetType ().ToString ());
2742 throw new NotSupportedException ();
2745 throw new InvalidOperationException ();
2747 SendFile (fileName, null, null, 0);
2750 public void SendFile (string fileName, byte[] preBuffer, byte[] postBuffer, TransmitFileOptions flags)
2752 if (is_disposed && is_closed)
2753 throw new ObjectDisposedException (GetType ().ToString ());
2756 throw new NotSupportedException ();
2759 throw new InvalidOperationException ();
2761 if (!SendFile (safe_handle, fileName, preBuffer, postBuffer, flags)) {
2762 SocketException exc = new SocketException ();
2763 if (exc.ErrorCode == 2 || exc.ErrorCode == 3)
2764 throw new FileNotFoundException ();
2769 public bool SendToAsync (SocketAsyncEventArgs e)
2771 // NO check is made whether e != null in MS.NET (NRE is thrown in such case)
2773 if (is_disposed && is_closed)
2774 throw new ObjectDisposedException (GetType ().ToString ());
2775 if (e.BufferList != null)
2776 throw new NotSupportedException ("Mono doesn't support using BufferList at this point.");
2777 if (e.RemoteEndPoint == null)
2778 throw new ArgumentNullException ("remoteEP", "Value cannot be null.");
2781 e.Worker.Init (this, e, SocketOperation.SendTo);
2782 SocketAsyncResult res = e.Worker.result;
2783 res.Buffer = e.Buffer;
2784 res.Offset = e.Offset;
2786 res.SockFlags = e.SocketFlags;
2787 res.EndPoint = e.RemoteEndPoint;
2790 writeQ.Enqueue (e.Worker);
2791 count = writeQ.Count;
2794 socket_pool_queue (SocketAsyncWorker.Dispatcher, res);
2798 public int SendTo (byte [] buffer, EndPoint remote_end)
2800 if (is_disposed && is_closed)
2801 throw new ObjectDisposedException (GetType ().ToString ());
2804 throw new ArgumentNullException ("buffer");
2806 if (remote_end == null)
2807 throw new ArgumentNullException ("remote_end");
2809 return SendTo_nochecks (buffer, 0, buffer.Length, SocketFlags.None, remote_end);
2812 public int SendTo (byte [] buffer, SocketFlags flags, EndPoint remote_end)
2814 if (is_disposed && is_closed)
2815 throw new ObjectDisposedException (GetType ().ToString ());
2818 throw new ArgumentNullException ("buffer");
2820 if (remote_end == null)
2821 throw new ArgumentNullException ("remote_end");
2823 return SendTo_nochecks (buffer, 0, buffer.Length, flags, remote_end);
2826 public int SendTo (byte [] buffer, int size, SocketFlags flags, EndPoint remote_end)
2828 if (is_disposed && is_closed)
2829 throw new ObjectDisposedException (GetType ().ToString ());
2832 throw new ArgumentNullException ("buffer");
2834 if (remote_end == null)
2835 throw new ArgumentNullException ("remote_end");
2837 CheckRange (buffer, 0, size);
2839 return SendTo_nochecks (buffer, 0, size, flags, remote_end);
2842 [MethodImplAttribute(MethodImplOptions.InternalCall)]
2843 private extern static int SendTo_internal(IntPtr sock,
2851 private static int SendTo_internal (SafeSocketHandle safeHandle,
2860 safeHandle.RegisterForBlockingSyscall ();
2861 return SendTo_internal (safeHandle.DangerousGetHandle (), buffer, offset, count, flags, sa, out error);
2863 safeHandle.UnRegisterForBlockingSyscall ();
2867 public int SendTo (byte [] buffer, int offset, int size, SocketFlags flags,
2868 EndPoint remote_end)
2870 if (is_disposed && is_closed)
2871 throw new ObjectDisposedException (GetType ().ToString ());
2874 throw new ArgumentNullException ("buffer");
2876 if (remote_end == null)
2877 throw new ArgumentNullException("remote_end");
2879 CheckRange (buffer, offset, size);
2881 return SendTo_nochecks (buffer, offset, size, flags, remote_end);
2884 internal int SendTo_nochecks (byte [] buffer, int offset, int size, SocketFlags flags,
2885 EndPoint remote_end)
2887 SocketAddress sockaddr = remote_end.Serialize ();
2891 ret = SendTo_internal (safe_handle, buffer, offset, size, flags, sockaddr, out error);
2893 SocketError err = (SocketError) error;
2895 if (err != SocketError.WouldBlock && err != SocketError.InProgress)
2896 is_connected = false;
2898 throw new SocketException (error);
2901 is_connected = true;
2903 seed_endpoint = remote_end;
2908 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, byte [] optionValue)
2910 if (is_disposed && is_closed)
2911 throw new ObjectDisposedException (GetType ().ToString ());
2913 // I'd throw an ArgumentNullException, but this is what MS does.
2914 if (optionValue == null)
2915 throw new SocketException ((int) SocketError.Fault,
2916 "Error trying to dereference an invalid pointer");
2920 SetSocketOption_internal (safe_handle, optionLevel, optionName, null,
2921 optionValue, 0, out error);
2924 if (error == (int) SocketError.InvalidArgument)
2925 throw new ArgumentException ();
2926 throw new SocketException (error);
2930 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, object optionValue)
2932 if (is_disposed && is_closed)
2933 throw new ObjectDisposedException (GetType ().ToString ());
2935 // NOTE: if a null is passed, the byte[] overload is used instead...
2936 if (optionValue == null)
2937 throw new ArgumentNullException("optionValue");
2941 if (optionLevel == SocketOptionLevel.Socket && optionName == SocketOptionName.Linger) {
2942 LingerOption linger = optionValue as LingerOption;
2944 throw new ArgumentException ("A 'LingerOption' value must be specified.", "optionValue");
2945 SetSocketOption_internal (safe_handle, optionLevel, optionName, linger, null, 0, out error);
2946 } else if (optionLevel == SocketOptionLevel.IP && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
2947 MulticastOption multicast = optionValue as MulticastOption;
2948 if (multicast == null)
2949 throw new ArgumentException ("A 'MulticastOption' value must be specified.", "optionValue");
2950 SetSocketOption_internal (safe_handle, optionLevel, optionName, multicast, null, 0, out error);
2951 } else if (optionLevel == SocketOptionLevel.IPv6 && (optionName == SocketOptionName.AddMembership || optionName == SocketOptionName.DropMembership)) {
2952 IPv6MulticastOption multicast = optionValue as IPv6MulticastOption;
2953 if (multicast == null)
2954 throw new ArgumentException ("A 'IPv6MulticastOption' value must be specified.", "optionValue");
2955 SetSocketOption_internal (safe_handle, optionLevel, optionName, multicast, null, 0, out error);
2957 throw new ArgumentException ("Invalid value specified.", "optionValue");
2961 if (error == (int) SocketError.InvalidArgument)
2962 throw new ArgumentException ();
2963 throw new SocketException (error);
2967 public void SetSocketOption (SocketOptionLevel optionLevel, SocketOptionName optionName, bool optionValue)
2969 if (is_disposed && is_closed)
2970 throw new ObjectDisposedException (GetType ().ToString ());
2973 int int_val = (optionValue) ? 1 : 0;
2974 SetSocketOption_internal (safe_handle, optionLevel, optionName, null, null, int_val, out error);
2976 if (error == (int) SocketError.InvalidArgument)
2977 throw new ArgumentException ();
2978 throw new SocketException (error);
2982 void ThrowIfDisposedAndClosed (Socket socket)
2984 if (socket.is_disposed && socket.is_closed)
2985 throw new ObjectDisposedException (socket.GetType ().ToString ());
2988 void ThrowIfDisposedAndClosed ()
2990 if (is_disposed && is_closed)
2991 throw new ObjectDisposedException (GetType ().ToString ());
2994 void ThrowIfBufferNull (byte[] buffer)
2997 throw new ArgumentNullException ("buffer");
3000 void ThrowIfBufferOutOfRange (byte[] buffer, int offset, int size)
3003 throw new ArgumentOutOfRangeException ("offset", "offset must be >= 0");
3004 if (offset > buffer.Length)
3005 throw new ArgumentOutOfRangeException ("offset", "offset must be <= buffer.Length");
3007 throw new ArgumentOutOfRangeException ("size", "size must be >= 0");
3008 if (size > buffer.Length - offset)
3009 throw new ArgumentOutOfRangeException ("size", "size must be <= buffer.Length - offset");
3014 #if !NET_2_1 || MOBILE
3015 if (protocol_type == ProtocolType.Udp)
3016 throw new SocketException ((int)SocketError.ProtocolOption);
3020 SocketAsyncResult ValidateEndIAsyncResult (IAsyncResult ares, string methodName, string argName)
3023 throw new ArgumentNullException (argName);
3025 SocketAsyncResult sockares = ares as SocketAsyncResult;
3026 if (sockares == null)
3027 throw new ArgumentException ("Invalid IAsyncResult", argName);
3028 if (Interlocked.CompareExchange (ref sockares.EndCalled, 1, 0) == 1)
3029 throw InvalidAsyncOp (methodName);