internal void FinishOperationSyncSuccess(int bytesTransferred, SocketFlags flags) { SetResults(SocketError.Success, bytesTransferred, flags); if (NetEventSource.IsEnabled || Socket.s_perfCountersEnabled) { LogBytesTransferred(bytesTransferred, _completedOperation); } SocketError socketError = SocketError.Success; switch (_completedOperation) { case SocketAsyncOperation.Accept: // Get the endpoint. Internals.SocketAddress remoteSocketAddress = IPEndPointExtensions.Serialize(_currentSocket._rightEndPoint); socketError = FinishOperationAccept(remoteSocketAddress); if (socketError == SocketError.Success) { _acceptSocket = _currentSocket.UpdateAcceptSocket(_acceptSocket, _currentSocket._rightEndPoint.Create(remoteSocketAddress)); if (NetEventSource.IsEnabled) { NetEventSource.Accepted(_acceptSocket, _acceptSocket.RemoteEndPoint, _acceptSocket.LocalEndPoint); } } else { SetResults(socketError, bytesTransferred, flags); _acceptSocket = null; _currentSocket.UpdateStatusAfterSocketError(socketError); } break; case SocketAsyncOperation.Connect: socketError = FinishOperationConnect(); if (socketError == SocketError.Success) { if (NetEventSource.IsEnabled) { NetEventSource.Connected(_currentSocket, _currentSocket.LocalEndPoint, _currentSocket.RemoteEndPoint); } // Mark socket connected. _currentSocket.SetToConnected(); _connectSocket = _currentSocket; } else { SetResults(socketError, bytesTransferred, flags); _currentSocket.UpdateStatusAfterSocketError(socketError); } break; case SocketAsyncOperation.Disconnect: _currentSocket.SetToDisconnected(); _currentSocket._remoteEndPoint = null; break; case SocketAsyncOperation.ReceiveFrom: // Deal with incoming address. _socketAddress.InternalSize = GetSocketAddressSize(); Internals.SocketAddress socketAddressOriginal = IPEndPointExtensions.Serialize(_remoteEndPoint); if (!socketAddressOriginal.Equals(_socketAddress)) { try { _remoteEndPoint = _remoteEndPoint.Create(_socketAddress); } catch { } } break; case SocketAsyncOperation.ReceiveMessageFrom: // Deal with incoming address. _socketAddress.InternalSize = GetSocketAddressSize(); socketAddressOriginal = IPEndPointExtensions.Serialize(_remoteEndPoint); if (!socketAddressOriginal.Equals(_socketAddress)) { try { _remoteEndPoint = _remoteEndPoint.Create(_socketAddress); } catch { } } FinishOperationReceiveMessageFrom(); break; case SocketAsyncOperation.SendPackets: FinishOperationSendPackets(); break; } Complete(); }
internal void FinishOperationSuccess(SocketError socketError, int bytesTransferred, SocketFlags flags) { SetResults(socketError, bytesTransferred, flags); switch (_completedOperation) { case SocketAsyncOperation.Accept: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, false); } } // Get the endpoint. Internals.SocketAddress remoteSocketAddress = IPEndPointExtensions.Serialize(_currentSocket._rightEndPoint); socketError = FinishOperationAccept(remoteSocketAddress); if (socketError == SocketError.Success) { _acceptSocket = _currentSocket.UpdateAcceptSocket(_acceptSocket, _currentSocket._rightEndPoint.Create(remoteSocketAddress)); if (s_loggingEnabled) { SocketsEventSource.Accepted(_acceptSocket, _acceptSocket.RemoteEndPoint, _acceptSocket.LocalEndPoint); } } else { SetResults(socketError, bytesTransferred, SocketFlags.None); _acceptSocket = null; } break; case SocketAsyncOperation.Connect: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, true); } } socketError = FinishOperationConnect(); // Mark socket connected. if (socketError == SocketError.Success) { if (s_loggingEnabled) { SocketsEventSource.Connected(_currentSocket, _currentSocket.LocalEndPoint, _currentSocket.RemoteEndPoint); } _currentSocket.SetToConnected(); _connectSocket = _currentSocket; } break; case SocketAsyncOperation.Disconnect: _currentSocket.SetToDisconnected(); _currentSocket._remoteEndPoint = null; break; case SocketAsyncOperation.Receive: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, false); } } break; case SocketAsyncOperation.ReceiveFrom: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, false); } } // Deal with incoming address. _socketAddress.InternalSize = GetSocketAddressSize(); Internals.SocketAddress socketAddressOriginal = IPEndPointExtensions.Serialize(_remoteEndPoint); if (!socketAddressOriginal.Equals(_socketAddress)) { try { _remoteEndPoint = _remoteEndPoint.Create(_socketAddress); } catch { } } break; case SocketAsyncOperation.ReceiveMessageFrom: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, false); } } // Deal with incoming address. _socketAddress.InternalSize = GetSocketAddressSize(); socketAddressOriginal = IPEndPointExtensions.Serialize(_remoteEndPoint); if (!socketAddressOriginal.Equals(_socketAddress)) { try { _remoteEndPoint = _remoteEndPoint.Create(_socketAddress); } catch { } } FinishOperationReceiveMessageFrom(); break; case SocketAsyncOperation.Send: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, true); } } break; case SocketAsyncOperation.SendPackets: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogSendPacketsBuffers(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, true); } } FinishOperationSendPackets(); break; case SocketAsyncOperation.SendTo: if (bytesTransferred > 0) { // Log and Perf counters. if (s_loggingEnabled) { LogBuffer(bytesTransferred); } if (Socket.s_perfCountersEnabled) { UpdatePerfCounters(bytesTransferred, true); } } break; } if (socketError != SocketError.Success) { // Asynchronous failure or something went wrong after async success. SetResults(socketError, bytesTransferred, flags); _currentSocket.UpdateStatusAfterSocketError(socketError); } // Complete the operation and raise completion event. Complete(); if (_contextCopy == null) { OnCompleted(this); } else { ExecutionContext.Run(_contextCopy, _executionCallback, null); } }
// // This method will be called by us when the IO completes synchronously and // by the ThreadPool when the IO completes asynchronously. (only called on WinNT) // internal override object PostCompletion(int numBytes) { SocketError errorCode = (SocketError)ErrorCode; SocketAddress remoteSocketAddress = null; if (errorCode == SocketError.Success) { m_LocalBytesTransferred = numBytes; if (Logging.On) { LogBuffer((long)numBytes); } //get the endpoint remoteSocketAddress = m_ListenSocket.m_RightEndPoint.Serialize(); IntPtr localAddr; int localAddrLength; IntPtr remoteAddr; //set the socket context try { m_ListenSocket.GetAcceptExSockaddrs( Marshal.UnsafeAddrOfPinnedArrayElement(m_Buffer, 0), m_Buffer.Length - (m_AddressBufferLength * 2), m_AddressBufferLength, m_AddressBufferLength, out localAddr, out localAddrLength, out remoteAddr, out remoteSocketAddress.m_Size ); Marshal.Copy(remoteAddr, remoteSocketAddress.m_Buffer, 0, remoteSocketAddress.m_Size); IntPtr handle = m_ListenSocket.SafeHandle.DangerousGetHandle(); errorCode = UnsafeNclNativeMethods.OSSOCK.setsockopt( m_AcceptSocket.SafeHandle, SocketOptionLevel.Socket, SocketOptionName.UpdateAcceptContext, ref handle, Marshal.SizeOf(handle)); if (errorCode == SocketError.SocketError) { errorCode = (SocketError)Marshal.GetLastWin32Error(); } GlobalLog.Print("AcceptOverlappedAsyncResult#" + ValidationHelper.HashString(this) + "::PostCallback() setsockopt handle:" + handle.ToString() + " AcceptSocket:" + ValidationHelper.HashString(m_AcceptSocket) + " itsHandle:" + m_AcceptSocket.SafeHandle.DangerousGetHandle().ToString() + " returns:" + errorCode.ToString()); } catch (ObjectDisposedException) { errorCode = SocketError.OperationAborted; } ErrorCode = (int)errorCode; } if (errorCode == SocketError.Success) { return(m_ListenSocket.UpdateAcceptSocket(m_AcceptSocket, m_ListenSocket.m_RightEndPoint.Create(remoteSocketAddress), false)); } else { return(null); } }