private SetUnmanagedStructures ( IList |
||
buffers | IList |
|
리턴 | void |
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; } }
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); 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 ref asyncResult._singleBuffer, 1, 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; } }
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; } }
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; } }
public static unsafe SocketError SendAsync(SafeCloseSocket handle, byte[] buffer, int offset, int count, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { // Set up unmanaged structures for overlapped WSASend. asyncResult.SetUnmanagedStructures(buffer, offset, count, null); try { // This can throw ObjectDisposedException. int bytesTransferred; SocketError errorCode = Interop.Winsock.WSASend( handle, ref asyncResult._singleBuffer, 1, // There is only ever 1 buffer being sent. out bytesTransferred, socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); return(asyncResult.ProcessOverlappedResult(errorCode == SocketError.Success, bytesTransferred)); } catch { asyncResult.ReleaseUnmanagedStructures(); throw; } }
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. // 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 SendAsync(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.WSASend( 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, 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; } }
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 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); }
private SocketError DoBeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginSend() SRC:" + ValidationHelper.ToString(LocalEndPoint) + " DST:" + ValidationHelper.ToString(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 { // Set up asyncResult for overlapped WSASend. // This call will use completion ports on WinNT and Overlapped IO on Win9x. asyncResult.SetUnmanagedStructures(buffer, offset, size, null, false /*don't pin null remoteEP*/, ref Caches.SendOverlappedCache); // // Get the Send going. // GlobalLog.Print("BeginSend: asyncResult:" + ValidationHelper.HashString(asyncResult) + " size:" + size.ToString()); int bytesTransferred; // This can throw ObjectDisposedException. errorCode = UnsafeNclNativeMethods.OSSOCK.WSASend( m_Handle, ref asyncResult.m_SingleBuffer, 1, // only ever 1 buffer being sent 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() + " size:" + size.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(Logging.On)Logging.Exception(Logging.Sockets, this, "BeginSend", new SocketException(errorCode)); } return errorCode; }
private void DoBeginMultipleSend(BufferOffsetSize[] buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { if (Socket.s_LoggingEnabled) Logging.Enter(Logging.Sockets, (object) this, "BeginMultipleSend", ""); if (this.CleanedUp) throw new ObjectDisposedException(this.GetType().FullName); SocketError socketError = SocketError.SocketError; try { asyncResult.SetUnmanagedStructures(buffers, ref this.Caches.SendOverlappedCache); int bytesTransferred; socketError = UnsafeNclNativeMethods.OSSOCK.WSASend(this.m_Handle, asyncResult.m_WSABuffers, asyncResult.m_WSABuffers.Length, out bytesTransferred, socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (socketError != SocketError.Success) socketError = (SocketError) Marshal.GetLastWin32Error(); } finally { socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError); } if (socketError != SocketError.Success) { asyncResult.ExtractCache(ref this.Caches.SendOverlappedCache); SocketException socketException = new SocketException(socketError); this.UpdateStatusAfterSocketError(socketException); if (Socket.s_LoggingEnabled) Logging.Exception(Logging.Sockets, (object) this, "BeginMultipleSend", (Exception) socketException); throw socketException; } else { if (!Socket.s_LoggingEnabled) return; Logging.Exit(Logging.Sockets, (object) this, "BeginMultipleSend", (object) asyncResult); } }
private SocketError DoBeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { SocketError socketError = SocketError.SocketError; try { asyncResult.SetUnmanagedStructures(buffer, offset, size, (SocketAddress) null, false, ref this.Caches.ReceiveOverlappedCache); int bytesTransferred; socketError = UnsafeNclNativeMethods.OSSOCK.WSARecv(this.m_Handle, ref asyncResult.m_SingleBuffer, 1, out bytesTransferred, out socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (socketError != SocketError.Success) socketError = (SocketError) Marshal.GetLastWin32Error(); } finally { socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError); } if (socketError != SocketError.Success) { asyncResult.ExtractCache(ref this.Caches.ReceiveOverlappedCache); this.UpdateStatusAfterSocketError(socketError); if (Socket.s_LoggingEnabled) Logging.Exception(Logging.Sockets, (object) this, "BeginReceive", (Exception) new SocketException(socketError)); asyncResult.InvokeCallback((object) new SocketException(socketError)); } return socketError; }
private SocketError DoBeginSend(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { SocketError socketError = SocketError.SocketError; try { asyncResult.SetUnmanagedStructures(buffers, ref this.Caches.SendOverlappedCache); int bytesTransferred; socketError = UnsafeNclNativeMethods.OSSOCK.WSASend(this.m_Handle, asyncResult.m_WSABuffers, asyncResult.m_WSABuffers.Length, out bytesTransferred, socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (socketError != SocketError.Success) socketError = (SocketError) Marshal.GetLastWin32Error(); } finally { socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError); } if (socketError != SocketError.Success) { asyncResult.ExtractCache(ref this.Caches.SendOverlappedCache); this.UpdateStatusAfterSocketError(socketError); if (Socket.s_LoggingEnabled) Logging.Exception(Logging.Sockets, (object) this, "BeginSend", (Exception) new SocketException(socketError)); } return socketError; }
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); }
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)); }
/*++ 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 --*/ /// <include file='doc\Socket.uex' path='docs/doc[@for="Socket.BeginReceiveFrom"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public IAsyncResult BeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, ref EndPoint remoteEP, AsyncCallback callback, Object state) { if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // // parameter validation // 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"); } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginReceiveFrom() size:" + size.ToString()); // // Allocate the async result and the event we'll pass to the // thread pool. // OverlappedAsyncResult asyncResult = new OverlappedAsyncResult( this, state, callback ); // // Set up asyncResult for overlapped WSARecvFrom. // This call will use // completion ports on WinNT and Overlapped IO on Win9x. // EndPoint endPointSnapshot = remoteEP; if (remoteEP.GetType()==typeof(IPEndPoint)) { endPointSnapshot = new IPEndPoint(((IPEndPoint)remoteEP).Address, ((IPEndPoint)remoteEP).Port); } asyncResult.SetUnmanagedStructures( buffer, offset, size, socketFlags, endPointSnapshot, true // pin remoteEP ); // save a copy of the original EndPoint in the asyncResult asyncResult.m_SocketAddressOriginal = endPointSnapshot.Serialize(); // This will check the permissions for connect. CheckCacheRemote(asyncResult.m_SocketAddress, endPointSnapshot, false); int errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecvFrom( m_Handle, ref asyncResult.m_WSABuffer, 1, OverlappedAsyncResult.m_BytesTransferred, ref asyncResult.m_Flags, asyncResult.m_GCHandleSocketAddress.AddrOfPinnedObject(), asyncResult.m_GCHandleSocketAddressSize.AddrOfPinnedObject(), asyncResult.IntOverlapped, IntPtr.Zero ); if (errorCode!=SocketErrors.Success) { errorCode = Marshal.GetLastWin32Error(); } asyncResult.CheckAsyncCallOverlappedResult(errorCode); // // if the asynchronous native call fails synchronously // we'll throw a SocketException // if (asyncResult.ErrorCode!=SocketErrors.Success) { // // update our internal state after this socket error and throw // UpdateStatusAfterSocketError(); throw new SocketException(asyncResult.ErrorCode); } if (m_RightEndPoint==null) { // // save a copy of the EndPoint so we can use it for Create() // m_RightEndPoint = endPointSnapshot; } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginReceiveFrom() size:" + size.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(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; }
private void DoBeginSendTo(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint endPointSnapshot, SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { EndPoint endPoint = this.m_RightEndPoint; SocketError socketError = SocketError.SocketError; try { asyncResult.SetUnmanagedStructures(buffer, offset, size, socketAddress, false, ref this.Caches.SendOverlappedCache); if (this.m_RightEndPoint == null) this.m_RightEndPoint = endPointSnapshot; int bytesTransferred; socketError = UnsafeNclNativeMethods.OSSOCK.WSASendTo(this.m_Handle, ref asyncResult.m_SingleBuffer, 1, out bytesTransferred, socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.SocketAddress.Size, asyncResult.OverlappedHandle, IntPtr.Zero); if (socketError != SocketError.Success) socketError = (SocketError) Marshal.GetLastWin32Error(); } catch (ObjectDisposedException ex) { this.m_RightEndPoint = endPoint; throw; } finally { socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError); } if (socketError == SocketError.Success) return; this.m_RightEndPoint = endPoint; asyncResult.ExtractCache(ref this.Caches.SendOverlappedCache); SocketException socketException = new SocketException(socketError); this.UpdateStatusAfterSocketError(socketException); if (Socket.s_LoggingEnabled) Logging.Exception(Logging.Sockets, (object) this, "BeginSendTo", (Exception) socketException); throw socketException; }
internal IAsyncResult BeginMultipleSend( BufferOffsetSize[] buffers, SocketFlags socketFlags, AsyncCallback callback, Object state) { // // parameter validation // GlobalLog.Assert(buffers!=null, "Socket:BeginMultipleSend(): buffers==null", ""); GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginMultipleSend() buffers.Length:" + buffers.Length.ToString()); // // Allocate the async result and the event we'll pass to the // thread pool. // OverlappedAsyncResult asyncResult = new OverlappedAsyncResult( this, state, callback ); // // Set up asyncResult for overlapped WSASend. // This call will use // completion ports on WinNT and Overlapped IO on Win9x. // asyncResult.SetUnmanagedStructures( buffers, socketFlags); // // Get the Send going. // int errorCode = UnsafeNclNativeMethods.OSSOCK.WSASend( m_Handle, asyncResult.m_WSABuffers, asyncResult.m_WSABuffers.Length, OverlappedAsyncResult.m_BytesTransferred, asyncResult.m_Flags, asyncResult.IntOverlapped, IntPtr.Zero ); if (errorCode!=SocketErrors.Success) { errorCode = Marshal.GetLastWin32Error(); } asyncResult.CheckAsyncCallOverlappedResult(errorCode); GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginMultipleSend() UnsafeNclNativeMethods.OSSOCK.WSASend returns:" + errorCode.ToString() + " size:" + buffers.Length.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult)); // // if the asynchronous native call fails synchronously // we'll throw a SocketException // if (asyncResult.ErrorCode!=SocketErrors.Success) { // // update our internal state after this socket error and throw // UpdateStatusAfterSocketError(); throw new SocketException(asyncResult.ErrorCode); } return asyncResult; }
private void DoBeginReceiveFrom(byte[] buffer, int offset, int size, SocketFlags socketFlags, EndPoint endPointSnapshot, SocketAddress socketAddress, OverlappedAsyncResult asyncResult) { EndPoint rightEndPoint = this.m_RightEndPoint; SocketError socketError = SocketError.SocketError; try { int num; asyncResult.SetUnmanagedStructures(buffer, offset, size, socketAddress, true, ref this.Caches.ReceiveOverlappedCache); asyncResult.SocketAddressOriginal = endPointSnapshot.Serialize(); if (this.m_RightEndPoint == null) { this.m_RightEndPoint = endPointSnapshot; } socketError = UnsafeNclNativeMethods.OSSOCK.WSARecvFrom(this.m_Handle, ref asyncResult.m_SingleBuffer, 1, out num, ref socketFlags, asyncResult.GetSocketAddressPtr(), asyncResult.GetSocketAddressSizePtr(), asyncResult.OverlappedHandle, IntPtr.Zero); if (socketError != SocketError.Success) { socketError = (SocketError) Marshal.GetLastWin32Error(); } } catch (ObjectDisposedException) { this.m_RightEndPoint = rightEndPoint; throw; } finally { socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError); } if (socketError != SocketError.Success) { this.m_RightEndPoint = rightEndPoint; asyncResult.ExtractCache(ref this.Caches.ReceiveOverlappedCache); SocketException socketException = new SocketException(socketError); this.UpdateStatusAfterSocketError(socketException); if (s_LoggingEnabled) { Logging.Exception(Logging.Sockets, this, "BeginReceiveFrom", socketException); } throw socketException; } }
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 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; }
private SocketError DoBeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { SocketError socketError = SocketError.SocketError; try { int num; asyncResult.SetUnmanagedStructures(buffer, offset, size, null, false, ref this.Caches.SendOverlappedCache); socketError = UnsafeNclNativeMethods.OSSOCK.WSASend(this.m_Handle, ref asyncResult.m_SingleBuffer, 1, out num, socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (socketError != SocketError.Success) { socketError = (SocketError) Marshal.GetLastWin32Error(); } } finally { socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError); } if (socketError != SocketError.Success) { asyncResult.ExtractCache(ref this.Caches.SendOverlappedCache); this.UpdateStatusAfterSocketError(socketError); if (s_LoggingEnabled) { Logging.Exception(Logging.Sockets, this, "BeginSend", new SocketException(socketError)); } } return socketError; }
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; }
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; }
/*++ Routine Description: BeginReceive - Async implimentation of Recv 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 Size - Size of Buffer to recv 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 --*/ /// <include file='doc\Socket.uex' path='docs/doc[@for="Socket.BeginReceive"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, Object state) { if (CleanedUp) { throw new ObjectDisposedException(this.GetType().FullName); } // // parameter validation // 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"); } GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginReceive() size:" + size.ToString()); // // Allocate the async result and the event we'll pass to the // thread pool. // OverlappedAsyncResult asyncResult = new OverlappedAsyncResult( this, state, callback ); // // Set up asyncResult for overlapped WSARecv. // This call will use // completion ports on WinNT and Overlapped IO on Win9x. // asyncResult.SetUnmanagedStructures( buffer, offset, size, socketFlags, null, false // don't pin null RemoteEP ); // // Get the Receive going. // int errorCode = UnsafeNclNativeMethods.OSSOCK.WSARecv( m_Handle, ref asyncResult.m_WSABuffer, 1, OverlappedAsyncResult.m_BytesTransferred, ref asyncResult.m_Flags, asyncResult.IntOverlapped, IntPtr.Zero ); if (errorCode!=SocketErrors.Success) { errorCode = Marshal.GetLastWin32Error(); } #if COMNET_PERFLOGGING long timer = 0; Microsoft.Win32.SafeNativeMethods.QueryPerformanceCounter(out timer); Console.WriteLine(timer + ", Socket#" + this.m_Handle + "::WSARecv(" + asyncResult.GetHashCode() + " 0x" + ((long)asyncResult.IntOverlapped).ToString("X8") + ") returns:" + errorCode); #endif // #if COMNET_PERFLOGGING asyncResult.CheckAsyncCallOverlappedResult(errorCode); GlobalLog.Print("Socket#" + ValidationHelper.HashString(this) + "::BeginReceive() UnsafeNclNativeMethods.OSSOCK.WSARecv returns:" + errorCode.ToString() + " size:" + size.ToString() + " returning AsyncResult:" + ValidationHelper.HashString(asyncResult)); // // if the asynchronous native call fails synchronously // we'll throw a SocketException // if (asyncResult.ErrorCode!=SocketErrors.Success) { // // update our internal state after this socket error and throw // UpdateStatusAfterSocketError(); throw new SocketException(asyncResult.ErrorCode); } 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; }
private SocketError DoBeginReceive(IList<ArraySegment<byte>> buffers, SocketFlags socketFlags, OverlappedAsyncResult asyncResult) { SocketError socketError = SocketError.SocketError; try { int num; asyncResult.SetUnmanagedStructures(buffers, ref this.Caches.ReceiveOverlappedCache); socketError = UnsafeNclNativeMethods.OSSOCK.WSARecv(this.m_Handle, asyncResult.m_WSABuffers, asyncResult.m_WSABuffers.Length, out num, ref socketFlags, asyncResult.OverlappedHandle, IntPtr.Zero); if (socketError != SocketError.Success) { socketError = (SocketError) Marshal.GetLastWin32Error(); } } finally { socketError = asyncResult.CheckAsyncCallOverlappedResult(socketError); } if (socketError != SocketError.Success) { asyncResult.ExtractCache(ref this.Caches.ReceiveOverlappedCache); this.UpdateStatusAfterSocketError(socketError); if (s_LoggingEnabled) { Logging.Exception(Logging.Sockets, this, "BeginReceive", new SocketException(socketError)); } } return socketError; }