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

EndDisconnect() public method

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

#if FEATURE_PAL
            throw new PlatformNotSupportedException(SR.GetString(SR.WinNTRequired));
#endif // FEATURE_PAL

            if (asyncResult == null)
            {
                throw new ArgumentNullException(nameof(asyncResult));
            }


            //get async result and check for errors
            LazyAsyncResult castedAsyncResult = asyncResult as LazyAsyncResult;
            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, nameof(EndDisconnect)));
            }

            //wait for completion if it hasn't occured
            castedAsyncResult.InternalWaitForCompletion();
            castedAsyncResult.EndCalled = true;

            if (NetEventSource.IsEnabled) NetEventSource.Info(this);

            //
            // if the asynchronous native call failed asynchronously
            // we'll throw a SocketException
            //
            if ((SocketError)castedAsyncResult.ErrorCode != SocketError.Success)
            {
                //
                // update our internal state after this socket error and throw
                //
                SocketException socketException = new SocketException(castedAsyncResult.ErrorCode);
                UpdateStatusAfterSocketError(socketException);
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
                throw socketException;
            }

            if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
        }

Usage Example

示例#1
0
文件: Socket.cs 项目: soywiz/NodeNet
 public void DisconnectAsync(Action Action = null)
 {
     AsyncQueue.Add(() =>
     {
         try
         {
             NativeSocket.BeginDisconnect(true, (IAsyncResult) =>
             {
                 try
                 {
                     NativeSocket.EndDisconnect(IAsyncResult);
                     Core.EnqueueTask(() =>
                     {
                         if (Action != null)
                         {
                             Action();
                         }
                         AsyncQueue.Next();
                     });
                 }
                 catch (SocketException SocketException)
                 {
                     SocketExceptionThrown(SocketException);
                 }
             }, null);
         }
         catch (SocketException SocketException)
         {
             SocketExceptionThrown(SocketException);
         }
     });
 }
All Usage Examples Of System.Net.Sockets.Socket::EndDisconnect