System.Net.Sockets.Socket.EndConnect C# (CSharp) Method

EndConnect() public method

public EndConnect ( IAsyncResult asyncResult ) : void
asyncResult IAsyncResult
return void
        public void EndConnect(IAsyncResult asyncResult)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, asyncResult);
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            // Validate input parameters.
            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }

            LazyAsyncResult castedAsyncResult = null;
            EndPoint remoteEndPoint = null;
            ConnectOverlappedAsyncResult coar;
            MultipleAddressConnectAsyncResult macar;
            ConnectAsyncResult car;

            coar = asyncResult as ConnectOverlappedAsyncResult;
            if (coar == null)
            {
                macar = asyncResult as MultipleAddressConnectAsyncResult;
                if (macar == null)
                {
                    car = asyncResult as ConnectAsyncResult;
                    if (car != null)
                    {
                        remoteEndPoint = car.RemoteEndPoint;
                        castedAsyncResult = car;
                    }
                }
                else
                {
                    remoteEndPoint = macar.RemoteEndPoint;
                    castedAsyncResult = macar;
                }
            }
            else
            {
                remoteEndPoint = coar.RemoteEndPoint;
                castedAsyncResult = coar;
            }

            if (castedAsyncResult == null || castedAsyncResult.AsyncObject != this)
            {
                throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult));
            }
            if (castedAsyncResult.EndCalled)
            {
                throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndConnect"));
            }

            castedAsyncResult.InternalWaitForCompletion();
            castedAsyncResult.EndCalled = true;

            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"asyncResult:{asyncResult}");

            if (castedAsyncResult.Result is Exception)
            {
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, castedAsyncResult.Result);
                throw (Exception)castedAsyncResult.Result;
            }
            if ((SocketError)castedAsyncResult.ErrorCode != SocketError.Success)
            {
                // Update the internal state of this socket according to the error before throwing.
                SocketException socketException = SocketExceptionFactory.CreateSocketException(castedAsyncResult.ErrorCode, remoteEndPoint);
                UpdateStatusAfterSocketError(socketException);
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
                throw socketException;
            }
            if (NetEventSource.IsEnabled)
            {
                NetEventSource.Connected(this, LocalEndPoint, RemoteEndPoint);
                NetEventSource.Exit(this, "");
            }
        }

Usage Example

示例#1
0
 protected override void OnOpen(EventArgs e)
 {
     lock (this)
     {
         var proxy = this.m_tun2socks.Server;
         this.m_server = new Socket(proxy.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
         this.m_server.BeginConnect(proxy, (ar) =>
         {
             bool closeing = false;
             try
             {
                 Socket socket = this.m_server;
                 if (ar == null || socket == null)
                 {
                     closeing = true;
                 }
                 else
                 {
                     socket.EndConnect(ar);
                     HandshakeToServerAsync();
                 }
             }
             catch (Exception)
             {
                 closeing = true;
             }
             if (closeing)
             {
                 this.Close();
             }
         }, null);
     }
 }
All Usage Examples Of System.Net.Sockets.Socket::EndConnect