Exemplo n.º 1
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.º 2
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();
        }