Ejemplo n.º 1
0
        public bool RecvAsync()
        {
            // 写入数据内容
            int lastPos = RecvBuffer.Position;

            RecvBuffer.WriteBytes(RecvEventArgs.Buffer, RecvEventArgs.Offset, RecvEventArgs.BytesTransferred);
            RecvBuffer.Clear(RecvBuffer.Position - lastPos); // Position位置回退
            BytesTransferred = BytesTransferred + RecvEventArgs.BytesTransferred;

            bool result = true;

            while (BytesTransferred >= sizeof(short))
            {
                // 包头检查
                short head = RecvBuffer.ReadInt16();
                if (head != Packet.HEAD)
                {
                    App.Logger.Error("包头错误, 关闭远程连接!");
                    return(false);
                }
                BytesTransferred = BytesTransferred - sizeof(short);

                // 包长
                int length = RecvBuffer.ReadInt32();
                if ((length > 1024 * 1024) | (RecvBuffer.Position > BufferSize))
                {
                    // 最大缓存保护
                    App.Logger.Error("字节流溢出,即将关闭远程连接!");
                    return(false);
                }
                BytesTransferred = BytesTransferred - sizeof(int);

                lastPos = RecvBuffer.Position;
                Packet.Serialization(RecvBuffer, false);
                RecvBuffer.Clear(RecvBuffer.Position - lastPos);

                Packet oPacket = PacketFactory.CreatePacket(Packet.PacketID);
                if (oPacket != null)
                {
                    lastPos = RecvBuffer.Position;
                    oPacket.Serialization(RecvBuffer, false);
                    int oDataLength = RecvBuffer.Position - lastPos;
                    BytesTransferred = BytesTransferred - oDataLength;
                    // 接收包
                    oPacket.state = this;
                    AsyncNet.Recive(oPacket);
                }
                else
                {
                    App.Logger.Error("协议解析出错, 即将关闭远程连接");
                    return(false);
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        public bool RecvAsync()
        {
            lock (sync)
            {
                // 写入数据内容
                int lastPos = RecvBuffer.Position;
                RecvBuffer.WriteBytes(RecvEventArgs.Buffer, RecvEventArgs.Offset, RecvEventArgs.BytesTransferred);
                RecvBuffer.Clear(RecvBuffer.Position - lastPos); // Position位置回退

                BytesTransferred = BytesTransferred + RecvEventArgs.BytesTransferred;

                bool result = true;

                int byteLenght = 0;
                // 当字节长度大于一个包头时进行处理
                while (BytesTransferred >= sizeof(short))
                {
                    // 包头检查
                    short head = RecvBuffer.ReadInt16();
                    if (head != HEAD)
                    {
                        App.Error("包头错误, 关闭远程连接!");
                        return(false);
                    }
                    BytesTransferred = BytesTransferred - sizeof(short);
                    byteLenght       = byteLenght + sizeof(short);

                    // 包长
                    int length = RecvBuffer.ReadInt32();
                    if ((length > 1024 * 1024) | (RecvBuffer.Position > BufferSize))
                    {
                        // 最大缓存保护
                        App.Error("字节流溢出,即将关闭远程连接!");
                        return(false);
                    }
                    BytesTransferred = BytesTransferred - sizeof(int);
                    byteLenght       = byteLenght + sizeof(int);

                    lastPos = RecvBuffer.Position;

                    int packetID = RecvBuffer.ReadInt32();
                    RecvBuffer.Clear(RecvBuffer.Position - lastPos);

                    IPacket oPacket = PacketMgr.CreatePacket(packetID);
                    try
                    {
                        lastPos = RecvBuffer.Position;
                        oPacket.Serialization(RecvBuffer, false);
                        int oDataLength = RecvBuffer.Position - lastPos;
                        BytesTransferred = BytesTransferred - oDataLength;
                        byteLenght       = byteLenght + oDataLength;
                        // 接收包
                        oPacket.SetOwner(Owner);
                        oPacket.SetSession(this);
                        PacketMgr.Push(oPacket);
                    }
                    catch (Exception e)
                    {
                        App.Error("协议解析出错, 即将关闭远程连接\n{0}", e);
                        return(false);
                    }
                }

                RecvBuffer.Clear(byteLenght);

                return(result);
            }
        }