/// <summary> /// Begins an asynchronous request to shut the connection down. /// </summary> /// <param name="callback">The <see cref="AsyncCallback"/> delegate.</param> /// <param name="state">An object containing state information for this request.</param> /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous shutdown.</returns> /// <exception cref="InvalidOperationException"><see cref="BeginShutdown"/> has already been called.</exception> // Thanks to Michael J. Moore and Stefan Bernbo for notifying us about a bug in this method. public IAsyncResult BeginShutdown(AsyncCallback callback, object state) { if (m_ShutdownResult != null) throw new InvalidOperationException(); AsyncResult ar = new AsyncResult(callback, state, null); m_ShutdownResult = ar; if (!this.Connected) { ar.Notify(null); } else if (SecureProtocol == SecureProtocol.None) { base.Shutdown(SocketShutdown.Both); ar.Notify(null); } else { m_Controller.BeginShutdown(new AsyncCallback(this.OnShutdown), null); } return ar; }
/// <summary> /// Ends an asynchronous request to shut the connection down. /// </summary> /// <param name="asyncResult">An <see cref="IAsyncResult"/> that references the asynchronous shutdown.</param> /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception> /// <exception cref="InvalidOperationException"><see cref="BeginShutdown"/> has not been called first.</exception> /// <exception cref="ArgumentException"><paramref name="asyncResult"/> has not been returned by a call to <see cref="BeginShutdown"/>.</exception> public void EndShutdown(IAsyncResult asyncResult) { if (asyncResult == null) throw new ArgumentNullException(); if (m_ShutdownResult == null) throw new InvalidOperationException(); if (asyncResult != m_ShutdownResult) throw new ArgumentException(); // Process the EndSecureShutdown // block if the operation hasn't ended yet AsyncResult ar = m_ShutdownResult; while (!ar.IsCompleted) { ar.AsyncWaitHandle.WaitOne(200, false); } m_ShutdownResult = null; //if (ar.AsyncException != null) // eat exceptions; they're not really important // throw ar.AsyncException; }
/// <summary> /// Begins an asynchronous request for a connection to a network device. /// </summary> /// <param name="remoteEP">An <see cref="EndPoint"/> that represents the remote device.</param> /// <param name="callback">The <see cref="AsyncCallback"/> delegate.</param> /// <param name="state">An object that contains state information for this request.</param> /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous connection.</returns> /// <exception cref="ArgumentNullException"><paramref name="remoteEP"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception> /// <exception cref="SocketException">An operating system error occurs while creating the SecureSocket.</exception> /// <exception cref="ObjectDisposedException">The SecureSocket has been closed.</exception> public override IAsyncResult BeginConnect(EndPoint remoteEP, AsyncCallback callback, object state) { if (SecureProtocol == SecureProtocol.None) return base.BeginConnect(remoteEP, callback, state); // secure BeginConnect if (remoteEP == null) throw new ArgumentNullException(); if (m_ConnectResult != null) throw new SocketException(); // BeginConnect already called AsyncResult ret = new AsyncResult(callback, state, null); m_ConnectResult = ret; base.BeginConnect(remoteEP, new AsyncCallback(OnConnect), null); return ret; }
/// <summary> /// Ends a pending asynchronous connection request. /// </summary> /// <param name="asyncResult">The result of the asynchronous operation.</param> /// <exception cref="ArgumentNullException"><paramref name="asyncResult"/> is a null reference (<b>Nothing</b> in Visual Basic).</exception> /// <exception cref="ArgumentException"><paramref name="asyncResult"/> was not returned by a call to the <see cref="BeginConnect"/> method.</exception> /// <exception cref="InvalidOperationException"><see cref="EndConnect"/> was previously called for the asynchronous connection.</exception> /// <exception cref="SocketException">An operating system error occurs while accessing the SecureSocket.</exception> /// <exception cref="ObjectDisposedException">The SecureSocket has been closed.</exception> /// <exception cref="SecurityException">An error occurred while negotiating the security protocol.</exception> public override void EndConnect(IAsyncResult asyncResult) { if (SecureProtocol == SecureProtocol.None) { base.EndConnect(asyncResult); return; } // Make sure everything is in order if (asyncResult == null) throw new ArgumentNullException(); if (m_ConnectResult == null) throw new InvalidOperationException(); if (asyncResult != m_ConnectResult) throw new ArgumentException(); // Process the (secure) EndConnect // block if the operation hasn't ended yet AsyncResult ar = m_ConnectResult; while (!ar.IsCompleted) { ar.AsyncWaitHandle.WaitOne(200, false); } m_ConnectResult = null; if (ar.AsyncException != null) throw ar.AsyncException; }