public void CompletionCallback(IntPtr acceptedFileDescriptor, byte[] socketAddress, int socketAddressLen, SocketError errorCode)
        {
            _buffer   = null;
            _numBytes = 0;

            if (errorCode == SocketError.Success)
            {
                Internals.SocketAddress remoteSocketAddress = IPEndPointExtensions.Serialize(_listenSocket._rightEndPoint);
                System.Buffer.BlockCopy(socketAddress, 0, remoteSocketAddress.Buffer, 0, socketAddressLen);

                _acceptedSocket = _listenSocket.CreateAcceptSocket(
                    SocketPal.CreateSocket(acceptedFileDescriptor),
                    _listenSocket._rightEndPoint.Create(remoteSocketAddress));
            }

            base.CompletionCallback(0, errorCode);
        }
        public void CompletionCallback(int acceptedFileDescriptor, byte[] socketAddress, int socketAddressLen, SocketError errorCode)
        {
            // TODO: receive bytes on accepted socket if requested

            _buffer = null;
            _localBytesTransferred = 0;

            if (errorCode == SocketError.Success)
            {
                Internals.SocketAddress remoteSocketAddress = IPEndPointExtensions.Serialize(_listenSocket._rightEndPoint);
                System.Buffer.BlockCopy(socketAddress, 0, remoteSocketAddress.Buffer, 0, socketAddressLen);

                _acceptedSocket = _listenSocket.CreateAcceptSocket(
                    SafeCloseSocket.CreateSocket(acceptedFileDescriptor),
                    _listenSocket._rightEndPoint.Create(remoteSocketAddress));
            }

            base.CompletionCallback(0, errorCode);
        }
        // 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;

            Internals.SocketAddress remoteSocketAddress = null;
            if (errorCode == SocketError.Success)
            {
                _localBytesTransferred = numBytes;
                if (NetEventSource.IsEnabled)
                {
                    LogBuffer(numBytes);
                }

                // get the endpoint
                remoteSocketAddress = IPEndPointExtensions.Serialize(_listenSocket._rightEndPoint);

                IntPtr localAddr;
                int    localAddrLength;
                IntPtr remoteAddr;

                // set the socket context
                try
                {
                    _listenSocket.GetAcceptExSockaddrs(
                        Marshal.UnsafeAddrOfPinnedArrayElement(_buffer, 0),
                        _buffer.Length - (_addressBufferLength * 2),
                        _addressBufferLength,
                        _addressBufferLength,
                        out localAddr,
                        out localAddrLength,
                        out remoteAddr,
                        out remoteSocketAddress.InternalSize);

                    Marshal.Copy(remoteAddr, remoteSocketAddress.Buffer, 0, remoteSocketAddress.Size);

                    IntPtr handle = _listenSocket.SafeHandle.DangerousGetHandle();

                    errorCode = Interop.Winsock.setsockopt(
                        _acceptSocket.SafeHandle,
                        SocketOptionLevel.Socket,
                        SocketOptionName.UpdateAcceptContext,
                        ref handle,
                        Marshal.SizeOf(handle));

                    if (errorCode == SocketError.SocketError)
                    {
                        errorCode = (SocketError)Marshal.GetLastWin32Error();
                    }

                    if (NetEventSource.IsEnabled)
                    {
                        NetEventSource.Info(this, $"setsockopt handle:{handle}, AcceptSocket:{_acceptSocket}, returns:{errorCode}");
                    }
                }
                catch (ObjectDisposedException)
                {
                    errorCode = SocketError.OperationAborted;
                }

                ErrorCode = (int)errorCode;
            }

            if (errorCode != SocketError.Success)
            {
                return(null);
            }

            return(_listenSocket.UpdateAcceptSocket(_acceptSocket, _listenSocket._rightEndPoint.Create(remoteSocketAddress)));
        }
Exemplo n.º 4
0
        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);
            }
        }
Exemplo n.º 5
0
        // 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;

            Internals.SocketAddress remoteSocketAddress = null;
            if (errorCode == SocketError.Success)
            {
                _localBytesTransferred = numBytes;
                if (SocketsEventSource.Log.IsEnabled())
                {
                    LogBuffer((long)numBytes);
                }

                // get the endpoint
                remoteSocketAddress = IPEndPointExtensions.Serialize(_listenSocket._rightEndPoint);

                IntPtr localAddr;
                int    localAddrLength;
                IntPtr remoteAddr;

                // set the socket context
                try
                {
                    _listenSocket.GetAcceptExSockaddrs(
                        Marshal.UnsafeAddrOfPinnedArrayElement(_buffer, 0),
                        _buffer.Length - (_addressBufferLength * 2),
                        _addressBufferLength,
                        _addressBufferLength,
                        out localAddr,
                        out localAddrLength,
                        out remoteAddr,
                        out remoteSocketAddress.InternalSize);

                    Marshal.Copy(remoteAddr, remoteSocketAddress.Buffer, 0, remoteSocketAddress.Size);

                    IntPtr handle = _listenSocket.SafeHandle.DangerousGetHandle();

                    errorCode = Interop.Winsock.setsockopt(
                        _acceptSocket.SafeHandle,
                        SocketOptionLevel.Socket,
                        SocketOptionName.UpdateAcceptContext,
                        ref handle,
                        Marshal.SizeOf(handle));

                    if (errorCode == SocketError.SocketError)
                    {
                        errorCode = (SocketError)Marshal.GetLastWin32Error();
                    }

                    if (GlobalLog.IsEnabled)
                    {
                        GlobalLog.Print("AcceptOverlappedAsyncResult#" + LoggingHash.HashString(this) + "::PostCallback() setsockopt handle:" + handle.ToString() + " AcceptSocket:" + LoggingHash.HashString(_acceptSocket) + " itsHandle:" + _acceptSocket.SafeHandle.DangerousGetHandle().ToString() + " returns:" + errorCode.ToString());
                    }
                }
                catch (ObjectDisposedException)
                {
                    errorCode = SocketError.OperationAborted;
                }

                ErrorCode = (int)errorCode;
            }

            if (errorCode != SocketError.Success)
            {
                return(null);
            }

            return(_listenSocket.UpdateAcceptSocket(_acceptSocket, _listenSocket._rightEndPoint.Create(remoteSocketAddress)));
        }
Exemplo n.º 6
0
        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();
        }