Ejemplo n.º 1
0
        public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e)
        {
            bool retval;

            // Throw if multiple buffers specified.
            if (e.m_BufferList != null)
            {
                throw new ArgumentException(SR.GetString(SR.net_multibuffernotsupported), "BufferList");
            }

            // Throw if RemoteEndPoint is null.
            if (e.RemoteEndPoint == null)
            {
                throw new ArgumentNullException("remoteEP");
            }

            EndPoint    endPointSnapshot = e.RemoteEndPoint;
            DnsEndPoint dnsEP            = endPointSnapshot as DnsEndPoint;

            if (dnsEP != null)
            {
                Socket attemptSocket = null;
                MultipleConnectAsync multipleConnectAsync = null;
                if (dnsEP.AddressFamily == AddressFamily.Unspecified)
                {
                    multipleConnectAsync = new MultipleSocketMultipleConnectAsync(socketType, protocolType);
                }
                else
                {
                    attemptSocket        = new Socket(dnsEP.AddressFamily, socketType, protocolType);
                    multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false);
                }

                e.StartOperationCommon(attemptSocket);
                e.StartOperationWrapperConnect(multipleConnectAsync);

                retval = multipleConnectAsync.StartConnectAsync(e, dnsEP);
            }
            else
            {
                Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType);
                retval = attemptSocket.ConnectAsync(e);
            }

            return(retval);
        }
Ejemplo n.º 2
0
        public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e)
        {
            bool retval;

            if (s_loggingEnabled)
            {
                Logging.Enter(Logging.Sockets, null, "ConnectAsync", "");
            }

            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            if (e._bufferList != null)
            {
                throw new ArgumentException(SR.net_multibuffernotsupported, "BufferList");
            }
            if (e.RemoteEndPoint == null)
            {
                throw new ArgumentNullException("remoteEP");
            }

            EndPoint endPointSnapshot = e.RemoteEndPoint;
            DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint;

            if (dnsEP != null)
            {
                Socket attemptSocket = null;
                MultipleConnectAsync multipleConnectAsync = null;
                if (dnsEP.AddressFamily == AddressFamily.Unspecified)
                {
                    // Disable CS0162 and CS0429: Unreachable code detected
                    //
                    // SuportsMultipleConnectAttempts is a constant; when false, the following lines will trigger CS0162 or CS0429.
#pragma warning disable 162, 429
                    multipleConnectAsync = SocketPal.SupportsMultipleConnectAttempts ?
                        (MultipleConnectAsync)(new DualSocketMultipleConnectAsync(socketType, protocolType)) :
                        (MultipleConnectAsync)(new MultipleSocketMultipleConnectAsync(socketType, protocolType));
#pragma warning restore
                }
                else
                {
                    attemptSocket = new Socket(dnsEP.AddressFamily, socketType, protocolType);
                    multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false);
                }

                e.StartOperationCommon(attemptSocket);
                e.StartOperationWrapperConnect(multipleConnectAsync);

                retval = multipleConnectAsync.StartConnectAsync(e, dnsEP);
            }
            else
            {
                Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType);
                retval = attemptSocket.ConnectAsync(e);
            }

            if (s_loggingEnabled)
            {
                Logging.Exit(Logging.Sockets, null, "ConnectAsync", retval);
            }
            return retval;
        }
Ejemplo n.º 3
0
        public bool ConnectAsync(SocketAsyncEventArgs e)
        {
            bool retval;

            if (s_loggingEnabled)
            {
                Logging.Enter(Logging.Sockets, this, "ConnectAsync", "");
            }

            if (CleanedUp)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }

            if (e == null)
            {
                throw new ArgumentNullException("e");
            }
            if (e._bufferList != null)
            {
                throw new ArgumentException(SR.net_multibuffernotsupported, "BufferList");
            }
            if (e.RemoteEndPoint == null)
            {
                throw new ArgumentNullException("remoteEP");
            }
            if (_isListening)
            {
                throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
            }

            // Check permissions for connect and prepare SocketAddress.
            EndPoint endPointSnapshot = e.RemoteEndPoint;
            DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint;

            if (dnsEP != null)
            {
                if (s_loggingEnabled)
                {
                    Logging.PrintInfo(Logging.Sockets, "Socket#" + Logging.HashString(this) + "::ConnectAsync " + SR.net_log_socket_connect_dnsendpoint);
                }

                if (dnsEP.AddressFamily != AddressFamily.Unspecified && !CanTryAddressFamily(dnsEP.AddressFamily))
                {
                    throw new NotSupportedException(SR.net_invalidversion);
                }

                MultipleConnectAsync multipleConnectAsync = new SingleSocketMultipleConnectAsync(this, true);

                e.StartOperationCommon(this);
                e.StartOperationWrapperConnect(multipleConnectAsync);

                retval = multipleConnectAsync.StartConnectAsync(e, dnsEP);
            }
            else
            {
                // Throw if remote address family doesn't match socket.
                if (!CanTryAddressFamily(e.RemoteEndPoint.AddressFamily))
                {
                    throw new NotSupportedException(SR.net_invalidversion);
                }

                e._socketAddress = CheckCacheRemote(ref endPointSnapshot, false);

                // Do wildcard bind if socket not bound.
                if (_rightEndPoint == null)
                {
                    if (endPointSnapshot.AddressFamily == AddressFamily.InterNetwork)
                    {
                        InternalBind(new IPEndPoint(IPAddress.Any, 0));
                    }
                    else
                    {
                        InternalBind(new IPEndPoint(IPAddress.IPv6Any, 0));
                    }
                }

                // Save the old RightEndPoint and prep new RightEndPoint.           
                EndPoint oldEndPoint = _rightEndPoint;
                if (_rightEndPoint == null)
                {
                    _rightEndPoint = endPointSnapshot;
                }

                // Prepare for the native call.
                e.StartOperationCommon(this);
                e.StartOperationConnect();

                // Make the native call.
                int bytesTransferred;
                SocketError socketError = SocketError.Success;
                try
                {
                    socketError = e.DoOperationConnect(this, _handle, out bytesTransferred);
                }
                catch (Exception ex)
                {
                    _rightEndPoint = oldEndPoint;

                    // Clear in-use flag on event args object. 
                    e.Complete();
                    throw ex;
                }

                // Handle failure where completion port is not posted.
                if (socketError != SocketError.Success && socketError != SocketError.IOPending)
                {
                    e.FinishOperationSyncFailure(socketError, bytesTransferred, SocketFlags.None);
                    retval = false;
                }
                else
                {
                    retval = true;
                }
            }

            if (s_loggingEnabled)
            {
                Logging.Exit(Logging.Sockets, this, "ConnectAsync", retval);
            }
            return retval;
        }
Ejemplo n.º 4
0
        public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e) {

            bool retval;

            if (s_LoggingEnabled) Logging.Enter(Logging.Sockets, null, "ConnectAsync", "");

            // Throw if multiple buffers specified.
            if (e.m_BufferList != null) {
                throw new ArgumentException(SR.GetString(SR.net_multibuffernotsupported), "BufferList");
            }

            // Throw if RemoteEndPoint is null.
            if (e.RemoteEndPoint == null) {
                throw new ArgumentNullException("remoteEP");
            }

            EndPoint endPointSnapshot = e.RemoteEndPoint;
            DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint;

            if (dnsEP != null) {
                Socket attemptSocket = null;
                MultipleConnectAsync multipleConnectAsync = null;
                if (dnsEP.AddressFamily == AddressFamily.Unspecified) {
                    multipleConnectAsync = new MultipleSocketMultipleConnectAsync(socketType, protocolType);
                } else {
                    attemptSocket = new Socket(dnsEP.AddressFamily, socketType, protocolType);
                    multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false);
                }

                e.StartOperationCommon(attemptSocket);
                e.StartOperationWrapperConnect(multipleConnectAsync);

                retval = multipleConnectAsync.StartConnectAsync(e, dnsEP);
            } else {
                Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType);
                retval = attemptSocket.ConnectAsync(e);
            }

            if (s_LoggingEnabled) Logging.Exit(Logging.Sockets, null, "ConnectAsync", retval);
            return retval;
        }
Ejemplo n.º 5
0
        public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e)
        {
            bool retval;

            if (s_loggingEnabled)
            {
                NetEventSource.Enter(NetEventSource.ComponentType.Socket, null, nameof(ConnectAsync), "");
            }

            if (e == null)
            {
                throw new ArgumentNullException(nameof(e));
            }
            if (e._bufferList != null)
            {
                throw new ArgumentException(SR.net_multibuffernotsupported, "BufferList");
            }
            if (e.RemoteEndPoint == null)
            {
                throw new ArgumentNullException("remoteEP");
            }

            EndPoint endPointSnapshot = e.RemoteEndPoint;
            DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint;

            if (dnsEP != null)
            {
                Socket attemptSocket = null;
                MultipleConnectAsync multipleConnectAsync = null;
                if (dnsEP.AddressFamily == AddressFamily.Unspecified)
                {
                    // Disable CS0162 and CS0429: Unreachable code detected
                    //
                    // SuportsMultipleConnectAttempts is a constant; when false, the following lines will trigger CS0162 or CS0429.
                    //
                    // This is the only *Connect* API that actually supports multiple endpoint attempts, as it's responsible 
                    // for creating each Socket instance and can create one per attempt (with the instance methods, once a 
                    // connect fails, on unix systems that socket can't be used for subsequent connect attempts).
#pragma warning disable 162, 429
                    multipleConnectAsync = SocketPal.SupportsMultipleConnectAttempts ?
                        (MultipleConnectAsync)(new DualSocketMultipleConnectAsync(socketType, protocolType)) :
                        (MultipleConnectAsync)(new MultipleSocketMultipleConnectAsync(socketType, protocolType));
#pragma warning restore
                }
                else
                {
                    attemptSocket = new Socket(dnsEP.AddressFamily, socketType, protocolType);
                    multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false);
                }

                e.StartOperationCommon(attemptSocket);
                e.StartOperationWrapperConnect(multipleConnectAsync);

                retval = multipleConnectAsync.StartConnectAsync(e, dnsEP);
            }
            else
            {
                Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType);
                retval = attemptSocket.ConnectAsync(e);
            }

            if (s_loggingEnabled)
            {
                NetEventSource.Exit(NetEventSource.ComponentType.Socket, null, nameof(ConnectAsync), retval);
            }
            return retval;
        }
 public static bool ConnectAsync(System.Net.Sockets.SocketType socketType, System.Net.Sockets.ProtocolType protocolType, SocketAsyncEventArgs e)
 {
     bool flag;
     if (s_LoggingEnabled)
     {
         Logging.Enter(Logging.Sockets, (string) null, "ConnectAsync", "");
     }
     if (e.m_BufferList != null)
     {
         throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList");
     }
     if (e.RemoteEndPoint == null)
     {
         throw new ArgumentNullException("remoteEP");
     }
     EndPoint remoteEndPoint = e.RemoteEndPoint;
     DnsEndPoint endPoint = remoteEndPoint as DnsEndPoint;
     if (endPoint != null)
     {
         Socket socket = null;
         MultipleConnectAsync args = null;
         if (endPoint.AddressFamily == System.Net.Sockets.AddressFamily.Unspecified)
         {
             args = new MultipleSocketMultipleConnectAsync(socketType, protocolType);
         }
         else
         {
             socket = new Socket(endPoint.AddressFamily, socketType, protocolType);
             args = new SingleSocketMultipleConnectAsync(socket, false);
         }
         e.StartOperationCommon(socket);
         e.StartOperationWrapperConnect(args);
         flag = args.StartConnectAsync(e, endPoint);
     }
     else
     {
         flag = new Socket(remoteEndPoint.AddressFamily, socketType, protocolType).ConnectAsync(e);
     }
     if (s_LoggingEnabled)
     {
         Logging.Exit(Logging.Sockets, (string) null, "ConnectAsync", flag);
     }
     return flag;
 }
 public bool ConnectAsync(SocketAsyncEventArgs e)
 {
     bool flag;
     if (s_LoggingEnabled)
     {
         Logging.Enter(Logging.Sockets, this, "ConnectAsync", "");
     }
     if (this.CleanedUp)
     {
         throw new ObjectDisposedException(base.GetType().FullName);
     }
     if (e.m_BufferList != null)
     {
         throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList");
     }
     if (e.RemoteEndPoint == null)
     {
         throw new ArgumentNullException("remoteEP");
     }
     if (this.isListening)
     {
         throw new InvalidOperationException(SR.GetString("net_sockets_mustnotlisten"));
     }
     EndPoint remoteEndPoint = e.RemoteEndPoint;
     DnsEndPoint endPoint = remoteEndPoint as DnsEndPoint;
     if (endPoint != null)
     {
         if (s_LoggingEnabled)
         {
             Logging.PrintInfo(Logging.Sockets, "Socket#" + ValidationHelper.HashString(this) + "::ConnectAsync Connecting to a DnsEndPoint");
         }
         if ((endPoint.AddressFamily != System.Net.Sockets.AddressFamily.Unspecified) && (endPoint.AddressFamily != this.addressFamily))
         {
             throw new NotSupportedException(SR.GetString("net_invalidversion"));
         }
         MultipleConnectAsync args = new SingleSocketMultipleConnectAsync(this, true);
         e.StartOperationCommon(this);
         e.StartOperationWrapperConnect(args);
         flag = args.StartConnectAsync(e, endPoint);
     }
     else
     {
         int num;
         if (this.addressFamily != e.RemoteEndPoint.AddressFamily)
         {
             throw new NotSupportedException(SR.GetString("net_invalidversion"));
         }
         e.m_SocketAddress = this.CheckCacheRemote(ref remoteEndPoint, false);
         if (this.m_RightEndPoint == null)
         {
             if (remoteEndPoint.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
             {
                 this.InternalBind(new IPEndPoint(IPAddress.Any, 0));
             }
             else
             {
                 this.InternalBind(new IPEndPoint(IPAddress.IPv6Any, 0));
             }
         }
         EndPoint rightEndPoint = this.m_RightEndPoint;
         if (this.m_RightEndPoint == null)
         {
             this.m_RightEndPoint = remoteEndPoint;
         }
         e.StartOperationCommon(this);
         e.StartOperationConnect();
         this.BindToCompletionPort();
         SocketError success = SocketError.Success;
         try
         {
             if (!this.ConnectEx(this.m_Handle, e.m_PtrSocketAddressBuffer, e.m_SocketAddress.m_Size, e.m_PtrSingleBuffer, e.Count, out num, e.m_PtrNativeOverlapped))
             {
                 success = (SocketError) Marshal.GetLastWin32Error();
             }
         }
         catch (Exception exception)
         {
             this.m_RightEndPoint = rightEndPoint;
             e.Complete();
             throw exception;
         }
         if ((success != SocketError.Success) && (success != SocketError.IOPending))
         {
             e.FinishOperationSyncFailure(success, num, SocketFlags.None);
             flag = false;
         }
         else
         {
             flag = true;
         }
     }
     if (s_LoggingEnabled)
     {
         Logging.Exit(Logging.Sockets, this, "ConnectAsync", flag);
     }
     return flag;
 }