public static SocketError ReceiveAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { return handle.AsyncContext.ReceiveAsync(buffer, offset, count, socketFlags, asyncResult.CompletionCallback); }
public static SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { asyncResult.SocketAddress = socketAddress; return handle.AsyncContext.ReceiveFromAsync(buffer, offset, count, socketFlags, socketAddress.Buffer, socketAddress.InternalSize, asyncResult.CompletionCallback); }
public static SocketError SendAsync(SafeCloseSocket handle, IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { return handle.AsyncContext.SendAsync(buffers, GetPlatformSocketFlags(socketFlags), asyncResult.CompletionCallback); }
public static SocketError SendAsync(SafeCloseSocket handle, BufferOffsetSize[] buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { return handle.AsyncContext.SendAsync(new BufferList(buffers), GetPlatformSocketFlags(socketFlags), asyncResult.CompletionCallback); }
public static unsafe SocketError SendAsync(SafeCloseSocket handle, IList <ArraySegment <byte> > buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSASend. asyncResult.SetUnmanagedStructures(buffers); try { // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = Interop.Winsock.WSASend( handle, asyncResult._wsaBuffers, asyncResult._wsaBuffers.Length, out bytesTransferred, socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); return(asyncResult.ProcessOverlappedResult(errorCode == SocketError.Success, bytesTransferred)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }
internal IAsyncResult UnsafeBeginMultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags, AsyncCallback callback, object state) { // Unsafe - don't flow. OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback); DoBeginMultipleSend(buffers, socketFlags, asyncResult); return asyncResult; }
public static unsafe SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecvFrom. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, true); int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecvFrom( handle, ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return(errorCode); }
internal IAsyncResult BeginReceive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginReceive", ""); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // Validate input parameters. if (buffers == null) { throw new ArgumentNullException("buffers"); } if (buffers.Count == 0) { throw new ArgumentException(SR.Format(SR.net_sockets_zerolist, "buffers"), "buffers"); } // We need to flow the context here. But we don't need to lock the context - we don't use it until the callback. OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback); asyncResult.StartPostingAsyncOp(false); // Run the receive with this asyncResult. errorCode = DoBeginReceive(buffers, socketFlags, asyncResult); if (errorCode != SocketError.Success && errorCode != SocketError.IOPending) { asyncResult = null; } else { // We're not throwing, so finish the async op posting code so we can return to the user. // If the operation already finished, the callback will be called from here. asyncResult.FinishPostingAsyncOp(ref Caches.ReceiveClosureCache); } if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginReceive", asyncResult); } return asyncResult; }
public static unsafe SocketError ReceiveAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecv. // This call will use completion ports. asyncResult.SetUnmanagedStructures(buffer, offset, count, null, false /* don't pin null RemoteEP*/); // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecv( handle, ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return(errorCode); }
public static unsafe SocketError ReceiveAsync(SafeCloseSocket handle, IList <ArraySegment <byte> > buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSASend. // This call will use completion ports. asyncResult.SetUnmanagedStructures(buffers); // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecv( handle, asyncResult._wsaBuffers, asyncResult._wsaBuffers.Length, out bytesTransferred, ref socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return(errorCode); }
public static unsafe SocketError SendToAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSASendTo. // This call will use completion ports. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, false /* don't pin RemoteEP*/); int bytesTransferred; SocketError errorCode = Interop.Winsock.WSASendTo( handle, ref asyncResult._singleBuffer, 1, // There is only ever 1 buffer being sent. out bytesTransferred, socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.SocketAddress.Size, asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return(errorCode); }
public static unsafe SocketError ReceiveAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecv. asyncResult.SetUnmanagedStructures(buffer, offset, count, null, false /* don't pin null RemoteEP*/); try { // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecv( handle, ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); return(asyncResult.ProcessOverlappedResult(errorCode == SocketError.Success, bytesTransferred)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }
public static unsafe SocketError SendToAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSASendTo. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, false /* don't pin RemoteEP*/); try { int bytesTransferred; SocketError errorCode = Interop.Winsock.WSASendTo( handle, ref asyncResult._singleBuffer, 1, // There is only ever 1 buffer being sent. out bytesTransferred, socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.SocketAddress.Size, asyncResult.OverlappedHandle, IntPtr.Zero); return(asyncResult.ProcessOverlappedResult(errorCode == SocketError.Success, bytesTransferred)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }
private SocketError DoBeginSend(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { GlobalLog.Print("Socket#" + Logging.HashString(this) + "::BeginSend() SRC:" + Logging.ObjectToString(LocalEndPoint) + " DST:" + Logging.ObjectToString(RemoteEndPoint) + " buffers:" + buffers); // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { GlobalLog.Print("BeginSend: asyncResult:" + Logging.HashString(asyncResult)); errorCode = SocketPal.SendAsync(_handle, buffers, socketFlags, asyncResult); GlobalLog.Print("Socket#" + Logging.HashString(this) + "::BeginSend() Interop.Winsock.WSASend returns:" + errorCode.ToString() + " returning AsyncResult:" + Logging.HashString(asyncResult)); } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // Throw an appropriate SocketException if the native call fails synchronously. if (errorCode != SocketError.Success) { UpdateStatusAfterSocketError(errorCode); if (s_loggingEnabled) { Logging.Exception(Logging.Sockets, this, "BeginSend", new SocketException((int)errorCode)); } } return errorCode; }
public static unsafe SocketError ReceiveAsync(SafeCloseSocket handle, IList <ArraySegment <byte> > buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSASend. asyncResult.SetUnmanagedStructures(buffers); try { int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecv( handle.DangerousGetHandle(), // to minimize chances of handle recycling from misuse, this should use DangerousAddRef/Release, but it adds too much overhead asyncResult._wsaBuffers, asyncResult._wsaBuffers.Length, out bytesTransferred, ref socketFlags, asyncResult.DangerousOverlappedPointer, // SafeHandle was just created in SetUnmanagedStructures IntPtr.Zero); GC.KeepAlive(handle); // small extra safe guard against handle getting collected/finalized while P/Invoke in progress return(asyncResult.ProcessOverlappedResult(errorCode == SocketError.Success, bytesTransferred)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }
internal IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, out SocketError errorCode, AsyncCallback callback, object state) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginReceive", ""); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // Validate input parameters. if (buffer == null) { throw new ArgumentNullException("buffer"); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException("offset"); } if (size < 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException("size"); } // We need to flow the context here. But we don't need to lock the context - we don't use it until the callback. OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback); asyncResult.StartPostingAsyncOp(false); // Run the receive with this asyncResult. errorCode = DoBeginReceive(buffer, offset, size, socketFlags, asyncResult); if (errorCode != SocketError.Success && errorCode != SocketError.IOPending) { asyncResult = null; } else { // We're not throwing, so finish the async op posting code so we can return to the user. // If the operation already finished, the callback will be called from here. asyncResult.FinishPostingAsyncOp(ref Caches.ReceiveClosureCache); } if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginReceive", asyncResult); } return asyncResult; }
private void DoBeginMultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginMultipleSend", ""); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // Validate input parameters. GlobalLog.Assert(buffers != null, "Socket#{0}::DoBeginMultipleSend()|buffers == null", Logging.HashString(this)); GlobalLog.Print("Socket#" + Logging.HashString(this) + "::DoBeginMultipleSend() buffers.Length:" + buffers.Length.ToString()); // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { errorCode = SocketPal.SendAsync(_handle, buffers, socketFlags, asyncResult); GlobalLog.Print("Socket#" + Logging.HashString(this) + "::BeginMultipleSend() Interop.Winsock.WSASend returns:" + errorCode.ToString() + " size:" + buffers.Length.ToString() + " returning AsyncResult:" + Logging.HashString(asyncResult)); } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // Throw an appropriate SocketException if the native call fails synchronously. if (errorCode != SocketError.Success) { // Update the internal state of this socket according to the error before throwing. SocketException socketException = new SocketException((int)errorCode); UpdateStatusAfterSocketError(socketException); if (s_loggingEnabled) { Logging.Exception(Logging.Sockets, this, "BeginMultipleSend", socketException); } throw socketException; } if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginMultipleSend", asyncResult); } }
// Routine Description: // // BeginReceiveFrom - Async implimentation of RecvFrom call, // // Called when we want to start an async receive. // We kick off the receive, and if it completes synchronously we'll // call the callback. Otherwise we'll return an IASyncResult, which // the caller can use to wait on or retrieve the final status, as needed. // // Uses Winsock 2 overlapped I/O. // // Arguments: // // ReadBuffer - status line that we wish to parse // Index - Offset into ReadBuffer to begin reading from // Request - Size of Buffer to recv // Flags - Additonal Flags that may be passed to the underlying winsock call // remoteEP - EndPoint that are to receive from // Callback - Delegate function that holds callback, called on completeion of I/O // State - State used to track callback, set by caller, not required // // Return Value: // // IAsyncResult - Async result used to retreive result internal IAsyncResult BeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, object state) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginReceiveFrom", ""); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // Validate input parameters. if (buffer == null) { throw new ArgumentNullException("buffer"); } if (remoteEP == null) { throw new ArgumentNullException("remoteEP"); } if (!CanTryAddressFamily(remoteEP.AddressFamily)) { throw new ArgumentException(SR.Format(SR.net_InvalidEndPointAddressFamily, remoteEP.AddressFamily, _addressFamily), "remoteEP"); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException("offset"); } if (size < 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException("size"); } if (_rightEndPoint == null) { throw new InvalidOperationException(SR.net_sockets_mustbind); } // We don't do a CAS demand here because the contents of remoteEP aren't used by // WSARecvFrom; all that matters is that we generate a unique-to-this-call SocketAddress // with the right address family Internals.SocketAddress socketAddress = SnapshotAndSerialize(ref remoteEP); // Set up the result and set it to collect the context. OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback); asyncResult.StartPostingAsyncOp(false); // Start the ReceiveFrom. DoBeginReceiveFrom(buffer, offset, size, socketFlags, remoteEP, socketAddress, asyncResult); // Capture the context, maybe call the callback, and return. asyncResult.FinishPostingAsyncOp(ref Caches.ReceiveClosureCache); if (asyncResult.CompletedSynchronously && !asyncResult.SocketAddressOriginal.Equals(asyncResult.SocketAddress)) { try { remoteEP = remoteEP.Create(asyncResult.SocketAddress); } catch { } } if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginReceiveFrom", asyncResult); } return asyncResult; }
public static unsafe SocketError SendToAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSASendTo. // This call will use completion ports. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, false /* don't pin RemoteEP*/); int bytesTransferred; SocketError errorCode = Interop.Winsock.WSASendTo( handle, ref asyncResult._singleBuffer, 1, // There is only ever 1 buffer being sent. out bytesTransferred, socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.SocketAddress.Size, asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return errorCode; }
internal IAsyncResult BeginMultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags, AsyncCallback callback, object state) { // Set up the async result and start the flow. OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback); asyncResult.StartPostingAsyncOp(false); // Start the send. DoBeginMultipleSend(buffers, socketFlags, asyncResult); // Finish it up (capture, complete). asyncResult.FinishPostingAsyncOp(ref Caches.SendClosureCache); return asyncResult; }
public static unsafe SocketError ReceiveAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecv. // This call will use completion ports. asyncResult.SetUnmanagedStructures(buffer, offset, count, null, false /* don't pin null RemoteEP*/); // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecv( handle, ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return errorCode; }
private void DoBeginMultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { if(s_LoggingEnabled)Logging.Enter(Logging.Sockets, this, "BeginMultipleSend", ""); if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // // parameter validation // GlobalLog.Assert(buffers != null, "Socket#{0}::DoBeginMultipleSend()|buffers == null", ValidationHelper.HashString(this)); GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginMultipleSend() buffers.Length:" + buffers.Length.ToString()); // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { // Set up asyncResult for overlapped WSASend. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffers, ref Caches.SendOverlappedCache); // This can throw ObjectDisposedException. int bytesTransferred; errorCode = UnsafeNclNativeMethods.OSSOCK.WSASend( m_Handle, asyncResult.m_WSABuffers, asyncResult.m_WSABuffers.Length, out bytesTransferred, socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode!=SocketError.Success) { errorCode = (SocketError)Marshal.GetLastWin32Error(); } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginMultipleSend() UnsafeNclNativeMethods.OSSOCK.WSASend returns:" + errorCode.ToString() + " size:" + buffers.Length.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult)); } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // // if the asynchronous native call fails synchronously // we'll throw a SocketException // if (errorCode!=SocketError.Success) { // // update our internal state after this socket error and throw // asyncResult.ExtractCache(ref Caches.SendOverlappedCache); SocketException socketException = new SocketException(errorCode); UpdateStatusAfterSocketError(socketException); if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "BeginMultipleSend", socketException); throw socketException; } if(s_LoggingEnabled)Logging.Exit(Logging.Sockets, this, "BeginMultipleSend", asyncResult); }
public static unsafe SocketError ReceiveAsync(SafeCloseSocket handle, IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSASend. // This call will use completion ports. asyncResult.SetUnmanagedStructures(buffers); // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecv( handle, asyncResult._wsaBuffers, asyncResult._wsaBuffers.Length, out bytesTransferred, ref socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return errorCode; }
public static SocketError SendAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { return handle.AsyncContext.SendAsync(buffer, offset, count, GetPlatformSocketFlags(socketFlags), asyncResult.CompletionCallback); }
public static unsafe SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecvFrom. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, true); int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecvFrom( handle, ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode != SocketError.Success) { errorCode = GetLastSocketError(); } return errorCode; }
public static SocketError SendToAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { asyncResult.SocketAddress = socketAddress; return handle.AsyncContext.SendToAsync(buffer, offset, count, GetPlatformSocketFlags(socketFlags), socketAddress.Buffer, socketAddress.Size, asyncResult.CompletionCallback); }
private SocketError DoBeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { if (GlobalLog.IsEnabled) { GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::BeginSend() SRC:" + LoggingHash.ObjectToString(LocalEndPoint) + " DST:" + LoggingHash.ObjectToString(RemoteEndPoint) + " size:" + size.ToString()); } // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { // Get the Send going. if (GlobalLog.IsEnabled) { GlobalLog.Print("BeginSend: asyncResult:" + LoggingHash.HashString(asyncResult) + " size:" + size.ToString()); } errorCode = SocketPal.SendAsync(_handle, buffer, offset, size, socketFlags, asyncResult); if (GlobalLog.IsEnabled) { GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::BeginSend() Interop.Winsock.WSASend returns:" + errorCode.ToString() + " size:" + size.ToString() + " returning AsyncResult:" + LoggingHash.HashString(asyncResult)); } } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // Throw an appropriate SocketException if the native call fails synchronously. if (errorCode != SocketError.Success) { UpdateStatusAfterSocketError(errorCode); if (s_loggingEnabled) { NetEventSource.Exception(NetEventSource.ComponentType.Socket, this, nameof(BeginSend), new SocketException((int)errorCode)); } } return errorCode; }
public static SocketError ReceiveAsync(SafeCloseSocket handle, IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { return handle.AsyncContext.ReceiveAsync(buffers, socketFlags, asyncResult.CompletionCallback); }
private void DoBeginSendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint endPointSnapshot, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { if (GlobalLog.IsEnabled) { GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::DoBeginSendTo() size:" + size.ToString()); } EndPoint oldEndPoint = _rightEndPoint; // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { if (_rightEndPoint == null) { _rightEndPoint = endPointSnapshot; } errorCode = SocketPal.SendToAsync(_handle, buffer, offset, size, socketFlags, socketAddress, asyncResult); if (GlobalLog.IsEnabled) { GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::DoBeginSendTo() Interop.Winsock.WSASend returns:" + errorCode.ToString() + " size:" + size + " returning AsyncResult:" + LoggingHash.HashString(asyncResult)); } } catch (ObjectDisposedException) { _rightEndPoint = oldEndPoint; throw; } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // Throw an appropriate SocketException if the native call fails synchronously. if (errorCode != SocketError.Success) { // Update the internal state of this socket according to the error before throwing. _rightEndPoint = oldEndPoint; SocketException socketException = new SocketException((int)errorCode); UpdateStatusAfterSocketError(socketException); if (s_loggingEnabled) { NetEventSource.Exception(NetEventSource.ComponentType.Socket, this, nameof(BeginSendTo), socketException); } throw socketException; } if (GlobalLog.IsEnabled) { GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::DoBeginSendTo() size:" + size.ToString() + " returning AsyncResult:" + LoggingHash.HashString(asyncResult)); } }
internal IAsyncResult UnsafeBeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "UnsafeBeginSend", ""); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // No need to flow the context. OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback); SocketError errorCode = DoBeginSend(buffer, offset, size, socketFlags, asyncResult); if (errorCode != SocketError.Success && errorCode != SocketError.IOPending) { throw new SocketException((int)errorCode); } if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "UnsafeBeginSend", asyncResult); } return asyncResult; }
private SocketError DoBeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { if (GlobalLog.IsEnabled) { GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::BeginReceive() size:" + size.ToString()); } #if DEBUG IntPtr lastHandle = _handle.DangerousGetHandle(); #endif // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { errorCode = SocketPal.ReceiveAsync(_handle, buffer, offset, size, socketFlags, asyncResult); if (GlobalLog.IsEnabled) { GlobalLog.Print("Socket#" + LoggingHash.HashString(this) + "::BeginReceive() Interop.Winsock.WSARecv returns:" + errorCode.ToString() + " returning AsyncResult:" + LoggingHash.HashString(asyncResult)); } } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // Throw an appropriate SocketException if the native call fails synchronously. if (errorCode != SocketError.Success) { // TODO: https://github.com/dotnet/corefx/issues/5426 //if (GlobalLog.IsEnabled) //{ // GlobalLog.AssertFormat("Socket#{0}::DoBeginReceive()|GetLastWin32Error() returned zero.", LoggingHash.HashString(this)); //} // Update the internal state of this socket according to the error before throwing. UpdateStatusAfterSocketError(errorCode); if (s_loggingEnabled) { NetEventSource.Exception(NetEventSource.ComponentType.Socket, this, nameof(BeginReceive), new SocketException((int)errorCode)); } asyncResult.InvokeCallback(new SocketException((int)errorCode)); } #if DEBUG else { _lastReceiveHandle = lastHandle; _lastReceiveThread = Environment.CurrentManagedThreadId; _lastReceiveTick = Environment.TickCount; } #endif return errorCode; }
// Routine Description: // // BeginSendTo - Async implimentation of SendTo, // // This routine may go pending at which time, // but any case the callback Delegate will be called upon completion // // Arguments: // // WriteBuffer - Buffer to transmit // Index - Offset into WriteBuffer to begin sending from // Size - Size of Buffer to transmit // Flags - Specific Socket flags to pass to winsock // remoteEP - EndPoint to transmit To // Callback - Delegate function that holds callback, called on completeion of I/O // State - State used to track callback, set by caller, not required // // Return Value: // // IAsyncResult - Async result used to retreive result internal IAsyncResult BeginSendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint remoteEP, AsyncCallback callback, object state) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "BeginSendTo", ""); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // Validate input parameters. if (buffer == null) { throw new ArgumentNullException("buffer"); } if (remoteEP == null) { throw new ArgumentNullException("remoteEP"); } if (offset < 0 || offset > buffer.Length) { throw new ArgumentOutOfRangeException("offset"); } if (size < 0 || size > buffer.Length - offset) { throw new ArgumentOutOfRangeException("size"); } // This will check the permissions for connect. EndPoint endPointSnapshot = remoteEP; Internals.SocketAddress socketAddress = CheckCacheRemote(ref endPointSnapshot, false); // Set up the async result and indicate to flow the context. OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback); asyncResult.StartPostingAsyncOp(false); // Post the send. DoBeginSendTo(buffer, offset, size, socketFlags, endPointSnapshot, socketAddress, asyncResult); // Finish, possibly posting the callback. The callback won't be posted before this point is reached. asyncResult.FinishPostingAsyncOp(ref Caches.SendClosureCache); if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "BeginSendTo", asyncResult); } return asyncResult; }
private SocketError DoBeginSend(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginSend() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(RemoteEndPoint) + " buffers:" + buffers); // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { // Set up asyncResult for overlapped WSASend. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffers, ref Caches.SendOverlappedCache); GlobalLog.Print("BeginSend: asyncResult:" + ValidationHelper.HashString(asyncResult)); // This can throw ObjectDisposedException. int bytesTransferred; errorCode = UnsafeNclNativeMethods.OSSOCK.WSASend( m_Handle, asyncResult.m_WSABuffers, asyncResult.m_WSABuffers.Length, out bytesTransferred, socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode!=SocketError.Success) { errorCode = (SocketError)Marshal.GetLastWin32Error(); } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginSend() UnsafeNclNativeMethods.OSSOCK.WSASend returns:" + errorCode.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult)); } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // // if the asynchronous native call fails synchronously // we'll throw a SocketException // if (errorCode != SocketError.Success) { asyncResult.ExtractCache(ref Caches.SendOverlappedCache); UpdateStatusAfterSocketError(errorCode); if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "BeginSend", new SocketException(errorCode)); } return errorCode; }
internal IAsyncResult UnsafeBeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, object state) { if (s_loggingEnabled) { Logging.Enter(Logging.Sockets, this, "UnsafeBeginReceive", ""); } if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // No need to flow the context. OverlappedAsyncResult asyncResult = new OverlappedAsyncResult(this, state, callback); DoBeginReceive(buffer, offset, size, socketFlags, asyncResult); if (s_loggingEnabled) { Logging.Exit(Logging.Sockets, this, "UnsafeBeginReceive", asyncResult); } return asyncResult; }
private SocketError DoBeginReceive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { #if DEBUG IntPtr lastHandle = m_Handle.DangerousGetHandle(); #endif // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { // Set up asyncResult for overlapped WSASend. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffers, ref Caches.ReceiveOverlappedCache); // This can throw ObjectDisposedException. int bytesTransferred; errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecv( m_Handle, asyncResult.m_WSABuffers, asyncResult.m_WSABuffers.Length, out bytesTransferred, ref socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (errorCode!=SocketError.Success) { errorCode = (SocketError)Marshal.GetLastWin32Error(); GlobalLog.Assert(errorCode != SocketError.Success, "Socket#{0}::DoBeginReceive()|GetLastWin32Error() returned zero.", ValidationHelper.HashString(this)); } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginReceive() UnsafeNclNativeMethods.OSSOCK.WSARecv returns:" + errorCode.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult)); } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // // if the asynchronous native call fails synchronously // we'll throw a SocketException // if (errorCode != SocketError.Success) { // // update our internal state after this socket error and throw asyncResult.ExtractCache(ref Caches.ReceiveOverlappedCache); UpdateStatusAfterSocketError(errorCode); if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "BeginReceive", new SocketException(errorCode)); } #if DEBUG else { m_LastReceiveHandle = lastHandle; m_LastReceiveThread = Thread.CurrentThread.ManagedThreadId; m_LastReceiveTick = Environment.TickCount; } #endif return errorCode; }
private SocketError DoBeginReceive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { #if DEBUG IntPtr lastHandle = _handle.DangerousGetHandle(); #endif // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { errorCode = SocketPal.ReceiveAsync(_handle, buffers, socketFlags, asyncResult); GlobalLog.Print("Socket#" + Logging.HashString(this) + "::DoBeginReceive() Interop.Winsock.WSARecv returns:" + errorCode.ToString() + " returning AsyncResult:" + Logging.HashString(asyncResult)); } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // Throw an appropriate SocketException if the native call fails synchronously. if (errorCode != SocketError.Success) { // Update the internal state of this socket according to the error before throwing. UpdateStatusAfterSocketError(errorCode); if (s_loggingEnabled) { Logging.Exception(Logging.Sockets, this, "BeginReceive", new SocketException((int)errorCode)); } } #if DEBUG else { _lastReceiveHandle = lastHandle; _lastReceiveThread = Environment.CurrentManagedThreadId; _lastReceiveTick = Environment.TickCount; } #endif return errorCode; }
private void DoBeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint endPointSnapshot, SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { EndPoint oldEndPoint = m_RightEndPoint; GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginReceiveFrom() size:" + size.ToString()); // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { // Set up asyncResult for overlapped WSARecvFrom. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffer, offset, size, socketAddress, true /* pin remoteEP*/, ref Caches.ReceiveOverlappedCache); // save a copy of the original EndPoint in the asyncResult asyncResult.SocketAddressOriginal = endPointSnapshot.Serialize(); if (m_RightEndPoint == null) { m_RightEndPoint = endPointSnapshot; } int bytesTransferred; errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecvFrom( m_Handle, ref asyncResult.m_SingleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero ); if (errorCode!=SocketError.Success) { errorCode = (SocketError)Marshal.GetLastWin32Error(); } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginReceiveFrom() UnsafeNclNativeMethods.OSSOCK.WSARecvFrom returns:" + errorCode.ToString() + " size:" + size.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult)); } catch (ObjectDisposedException) { m_RightEndPoint = oldEndPoint; throw; } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // // if the asynchronous native call fails synchronously // we'll throw a SocketException // if (errorCode!=SocketError.Success) { // // update our internal state after this socket error and throw // m_RightEndPoint = oldEndPoint; asyncResult.ExtractCache(ref Caches.ReceiveOverlappedCache); SocketException socketException = new SocketException(errorCode); UpdateStatusAfterSocketError(socketException); if(s_LoggingEnabled)Logging.Exception(Logging.Sockets, this, "BeginReceiveFrom", socketException); throw socketException; } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::DoBeginReceiveFrom() size:" + size.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult)); }
private void DoBeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint endPointSnapshot, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { EndPoint oldEndPoint = _rightEndPoint; GlobalLog.Print("Socket#" + Logging.HashString(this) + "::DoBeginReceiveFrom() size:" + size.ToString()); // Guarantee to call CheckAsyncCallOverlappedResult if we call SetUnamangedStructures with a cache in order to // avoid a Socket leak in case of error. SocketError errorCode = SocketError.SocketError; try { // Save a copy of the original EndPoint in the asyncResult. asyncResult.SocketAddressOriginal = IPEndPointExtensions.Serialize(endPointSnapshot); if (_rightEndPoint == null) { _rightEndPoint = endPointSnapshot; } errorCode = SocketPal.ReceiveFromAsync(_handle, buffer, offset, size, socketFlags, socketAddress, asyncResult); GlobalLog.Print("Socket#" + Logging.HashString(this) + "::DoBeginReceiveFrom() Interop.Winsock.WSARecvFrom returns:" + errorCode.ToString() + " size:" + size.ToString() + " returning AsyncResult:" + Logging.HashString(asyncResult)); } catch (ObjectDisposedException) { _rightEndPoint = oldEndPoint; throw; } finally { errorCode = asyncResult.CheckAsyncCallOverlappedResult(errorCode); } // Throw an appropriate SocketException if the native call fails synchronously. if (errorCode != SocketError.Success) { // Update the internal state of this socket according to the error before throwing. _rightEndPoint = oldEndPoint; SocketException socketException = new SocketException((int)errorCode); UpdateStatusAfterSocketError(socketException); if (s_loggingEnabled) { Logging.Exception(Logging.Sockets, this, "BeginReceiveFrom", socketException); } throw socketException; } GlobalLog.Print("Socket#" + Logging.HashString(this) + "::DoBeginReceiveFrom() size:" + size.ToString() + " returning AsyncResult:" + Logging.HashString(asyncResult)); }
public static unsafe SocketError ReceiveFromAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, Internals.SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { // Set up asyncResult for overlapped WSARecvFrom. asyncResult.SetUnmanagedStructures(buffer, offset, count, socketAddress, true); try { int bytesTransferred; SocketError errorCode = Interop.Winsock.WSARecvFrom( handle, ref asyncResult._singleBuffer, 1, out bytesTransferred, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero); return(asyncResult.ProcessOverlappedResult(errorCode == SocketError.Success, bytesTransferred)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }