コード例 #1
0
ファイル: BaseClient.cs プロジェクト: fiki574/egoAPB
        private void ReceiveLengthCallback(IAsyncResult ar)
        {
            if (Socket == null)
            {
                return;
            }

            try
            {
                int length = Socket.EndReceive(ar);
                if (length <= 0)
                {
                    Disconnect();
                    return;
                }

                ReceivedLength += length;
                if (ReceivedLength == 4)
                {
                    NextPacketLength = BitConverter.ToInt32(RecvBuffer, 0) - 4;
                    ReceivedLength   = 0;
                    BeginReceiveData();
                }
                else
                {
                    BeginReceiveLength();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Disconnect();
            }
        }
コード例 #2
0
        /// <summary>
        /// </summary>
        /// <param name="ar">The ar.</param>
        protected virtual void EndDataReceive(IAsyncResult ar)
        {
            if (this.socket == null || !this.socket.Connected)
            {
                return;
            }
            int cnt = 0;

            try
            {
                ProxySocket socket = (ProxySocket)ar.AsyncState;
                cnt = socket.EndReceive(ar);
                if (cnt != 0)
                {
                    ByteBuffer byteBuffer = new ByteBuffer(receiveBuf, 0, cnt);
                    try
                    {
                        policy.PushIn(byteBuffer);
                    }
                    catch (Exception e)
                    {
                        policy.PushIn(policy.CreateErrorPacket(ErrorPacketType.RUNTIME_ERROR, this.Name, e));
                    }

                    //创建一个新的字节对象
                    receiveBuf.Initialize();
                }
                BeginDataReceive(socket);
            }
            catch (Exception e)
            {
                policy.OnNetworkError(e);
            }
        }
コード例 #3
0
        private void ReceiveLengthCallback(IAsyncResult ar)
        {
            if (_socket == null)
            {
                return;
            }

            try
            {
                int length = _socket.EndReceive(ar);
                if (length <= 0)
                {
                    Log.Warn($"Received invalid packet length {length}, disconnecting");
                    Disconnect();
                    return;
                }

                _receivedLength += length;

                if (_receivedLength == 4)
                {
                    _nextPacketLength = BitConverter.ToInt32(_recvBuffer, 0) - 4;
                    Log.Debug($"Received length {_nextPacketLength}");
                    _receivedLength = 0;
                    BeginReceiveData();
                    // TODO: Ensure not bigger than recv buffer
                }
                else
                {
                    BeginReceiveLength();
                }
            }
            catch (Exception e)
            {
                Log.Warn("Exception occurred while receiving, disconnecting", e);
                Disconnect();
            }
        }