public bool ReceiveMessageFromAsync(SocketAsyncEventArgs e) { bool retval; if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "ReceiveMessageFromAsync", ""); } if (CleanedUp) { throw new ObjectDisposedException(GetType().FullName); } if (e == null) { throw new ArgumentNullException("e"); } if (e.RemoteEndPoint == null) { throw new ArgumentNullException("RemoteEndPoint"); } if (!CanTryAddressFamily(e.RemoteEndPoint.AddressFamily)) { throw new ArgumentException(SR.Format(SR.net_InvalidEndPointAddressFamily, e.RemoteEndPoint.AddressFamily, _addressFamily), "RemoteEndPoint"); } // We don't do a CAS demand here because the contents of remoteEP aren't used by // WSARecvMsg; all that matters is that we generate a unique-to-this-call SocketAddress // with the right address family. EndPoint endPointSnapshot = e.RemoteEndPoint; e._socketAddress = SnapshotAndSerialize(ref endPointSnapshot); // DualMode may have updated the endPointSnapshot, and it has to have the same AddressFamily as // e.m_SocketAddres for Create to work later. e.RemoteEndPoint = endPointSnapshot; SetReceivingPacketInformation(); // Prepare for the native call. e.StartOperationCommon(this); e.StartOperationReceiveMessageFrom(); // Make the native call. int bytesTransferred; SocketError socketError; try { socketError = e.DoOperationReceiveMessageFrom(this, _handle, out bytesTransferred); } catch (Exception ex) { // Clear in-use flag on event args object. e.Complete(); throw ex; } // Handle completion when 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, "ReceiveMessageFromAsync", retval); } return retval; }
/// <summary> /// Begins to asynchronously receive the specified number of bytes of data into the specified location in the data buffer, using the specified <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.SocketFlags"/>, and stores the endpoint and packet information. /// </summary> /// /// <returns> /// Returns true if the I/O operation is pending. The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will be raised upon completion of the operation. Returns false if the I/O operation completed synchronously. In this case, The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will not be raised and the <paramref name="e"/> object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation. /// </returns> /// <param name="e">The <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object to use for this asynchronous socket operation.</param><exception cref="T:System.ArgumentNullException">The <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.RemoteEndPoint"/> cannot be null.</exception><exception cref="T:System.NotSupportedException">Windows XP or later is required for this method.</exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> has been closed. </exception><exception cref="T:System.Net.Sockets.SocketException">An error occurred when attempting to access the socket. </exception> public bool ReceiveMessageFromAsync(SocketAsyncEventArgs e) { if (Socket.s_LoggingEnabled) Logging.Enter(Logging.Sockets, (object) this, "ReceiveMessageFromAsync", ""); if (this.CleanedUp) throw new ObjectDisposedException(this.GetType().FullName); if (e.RemoteEndPoint == null) throw new ArgumentNullException("RemoteEndPoint"); if (!this.CanTryAddressFamily(e.RemoteEndPoint.AddressFamily)) { throw new ArgumentException(SR.GetString("net_InvalidEndPointAddressFamily", (object) e.RemoteEndPoint.AddressFamily, (object) this.addressFamily), "RemoteEndPoint"); } else { EndPoint remoteEndPoint = e.RemoteEndPoint; e.m_SocketAddress = this.SnapshotAndSerialize(ref remoteEndPoint); e.RemoteEndPoint = remoteEndPoint; this.SetReceivingPacketInformation(); e.StartOperationCommon(this); e.StartOperationReceiveMessageFrom(); this.BindToCompletionPort(); int bytesTransferred; SocketError socketError; try { socketError = this.WSARecvMsg(this.m_Handle, e.m_PtrWSAMessageBuffer, out bytesTransferred, (SafeHandle) e.m_PtrNativeOverlapped, IntPtr.Zero); } catch (Exception ex) { e.Complete(); throw ex; } if (socketError != SocketError.Success) socketError = (SocketError) Marshal.GetLastWin32Error(); bool flag; if (socketError != SocketError.Success && socketError != SocketError.IOPending) { e.FinishOperationSyncFailure(socketError, bytesTransferred, SocketFlags.None); flag = false; } else flag = true; if (Socket.s_LoggingEnabled) Logging.Exit(Logging.Sockets, (object) this, "ReceiveMessageFromAsync", (object) (bool) (flag ? 1 : 0)); return flag; } }
// // ReceiveMessageFromAsync // public bool ReceiveMessageFromAsync(SocketAsyncEventArgs e) { bool retval; if(s_LoggingEnabled) Logging.Enter(Logging.Sockets, this, "ReceiveMessageFromAsync", ""); // Throw if socket disposed if(CleanedUp) { throw new ObjectDisposedException(GetType().FullName); } // Throw if remote endpoint property is null. if(e.RemoteEndPoint == null) { throw new ArgumentNullException("RemoteEndPoint"); } if(!CanTryAddressFamily(e.RemoteEndPoint.AddressFamily)) { throw new ArgumentException(SR.GetString(SR.net_InvalidEndPointAddressFamily, e.RemoteEndPoint.AddressFamily, addressFamily), "RemoteEndPoint"); } // We don't do a CAS demand here because the contents of remoteEP aren't used by // WSARecvMsg; all that matters is that we generate a unique-to-this-call SocketAddress // with the right address family EndPoint endPointSnapshot = e.RemoteEndPoint; e.m_SocketAddress = SnapshotAndSerialize(ref endPointSnapshot); // DualMode may have updated the endPointSnapshot, and it has to have the same AddressFamily as // e.m_SocketAddres for Create to work later. e.RemoteEndPoint = endPointSnapshot; SetReceivingPacketInformation(); // Prepare for the native call. e.StartOperationCommon(this); e.StartOperationReceiveMessageFrom(); BindToCompletionPort(); // Make the native call. int bytesTransferred; SocketError socketError; try { socketError = WSARecvMsg( m_Handle, e.m_PtrWSAMessageBuffer, out bytesTransferred, e.m_PtrNativeOverlapped, IntPtr.Zero); } catch(Exception ex) { // clear in-use on event arg object e.Complete(); throw ex; } // Native method emits single catch-all error code when error occurs. // Must get Win32 error for specific error code. if(socketError != SocketError.Success) { socketError = (SocketError)Marshal.GetLastWin32Error(); } // Handle completion when 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, "ReceiveMessageFromAsync", retval); return retval; }
public bool ReceiveMessageFromAsync(SocketAsyncEventArgs e) { bool flag; int num; SocketError error; if (s_LoggingEnabled) { Logging.Enter(Logging.Sockets, this, "ReceiveMessageFromAsync", ""); } if (this.CleanedUp) { throw new ObjectDisposedException(base.GetType().FullName); } if (e.RemoteEndPoint == null) { throw new ArgumentNullException("RemoteEndPoint"); } if (e.RemoteEndPoint.AddressFamily != this.addressFamily) { throw new ArgumentException(SR.GetString("net_InvalidEndPointAddressFamily", new object[] { e.RemoteEndPoint.AddressFamily, this.addressFamily }), "RemoteEndPoint"); } EndPoint remoteEndPoint = e.RemoteEndPoint; e.m_SocketAddress = this.SnapshotAndSerialize(ref remoteEndPoint); this.SetReceivingPacketInformation(); e.StartOperationCommon(this); e.StartOperationReceiveMessageFrom(); this.BindToCompletionPort(); try { error = this.WSARecvMsg(this.m_Handle, e.m_PtrWSAMessageBuffer, out num, e.m_PtrNativeOverlapped, IntPtr.Zero); } catch (Exception exception) { e.Complete(); throw exception; } if (error != SocketError.Success) { error = (SocketError) Marshal.GetLastWin32Error(); } if ((error != SocketError.Success) && (error != SocketError.IOPending)) { e.FinishOperationSyncFailure(error, num, SocketFlags.None); flag = false; } else { flag = true; } if (s_LoggingEnabled) { Logging.Exit(Logging.Sockets, this, "ReceiveMessageFromAsync", flag); } return flag; }