System.Net.Sockets.Socket.UpdateAcceptSocket C# (CSharp) Method

UpdateAcceptSocket() private method

private UpdateAcceptSocket ( Socket socket, EndPoint remoteEP ) : Socket
socket Socket
remoteEP EndPoint
return Socket
        internal Socket UpdateAcceptSocket(Socket socket, EndPoint remoteEP)
        {
            // Internal state of the socket is inherited from listener.
            socket._addressFamily = _addressFamily;
            socket._socketType = _socketType;
            socket._protocolType = _protocolType;
            socket._rightEndPoint = _rightEndPoint;
            socket._remoteEndPoint = remoteEP;

            // The socket is connected.
            socket.SetToConnected();

            // if the socket is returned by an End(), the socket might have
            // inherited the WSAEventSelect() call from the accepting socket.
            // we need to cancel this otherwise the socket will be in non-blocking
            // mode and we cannot force blocking mode using the ioctlsocket() in
            // Socket.set_Blocking(), since it fails returning 10022 as documented in MSDN.
            // (note that the m_AsyncEvent event will not be created in this case.

            socket._willBlock = _willBlock;

            // We need to make sure the Socket is in the right blocking state
            // even if we don't have to call UnsetAsyncEventSelect
            socket.InternalSetBlocking(_willBlock);

            return socket;
        }

Usage Example

示例#1
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();
        }
All Usage Examples Of System.Net.Sockets.Socket::UpdateAcceptSocket