Esempio n. 1
0
        private void HandleIncomingData(IAsyncResult parameter)
        {
            //处于暂停状态
            if (ServerIsSuspend)
            {
                return;
            }
            if (parameter == null)
            {
                return;
            }
            if (parameter.AsyncState == null)
            {
                return;
            }

            ClientConnection asyncState = (ClientConnection)parameter.AsyncState;

            if (asyncState.Socket == null)
            {
                Disconnect(asyncState.ConnectionId);
                return;
            }

            try
            {
                int length = asyncState.Socket.EndReceive(parameter);
                if (length == 0)
                {
                    this.RaiseDisconnectedEvent(asyncState);
                }
                else
                {
                    if (asyncState.Buffer.Length > 0)
                    {
                        byte[] destinationArray = new byte[length];
                        Array.Copy(asyncState.Buffer, 0, destinationArray, 0, length);
                        if (this.OnDataIn != null)
                        {
                            this.OnDataIn(this, new ZYBDataInEventArgs(asyncState, destinationArray));
                        }
                        this.StartWaitingForData(asyncState);
                    }
                    else
                    {
                        this.RaiseDisconnectedEvent(asyncState);
                    }
                }
            }
            catch (ObjectDisposedException)
            {
                this.RaiseDisconnectedEvent(asyncState);
            }
            catch (SocketException exception)
            {
                if (exception.ErrorCode == 0x2746)
                {
                    this.RaiseDisconnectedEvent(asyncState);
                }
                this.RaiseErrorEvent(asyncState, exception);
            }
        }
Esempio n. 2
0
 public ZYBConnectedEventArgs(ClientConnection connection)
 {
     this.ConnectionId = connection.ConnectionId;
     this.socket       = connection.Socket;
 }