Ejemplo n.º 1
0
        public Packet FromBytes(byte[] data, int len, bool encrypted)
        {
            if (len < CommonDefine.TCP_HEAD_8_SIZE)
            {
                return(null);
            }

            uint packetLength = (uint)BitConverter.ToUInt16(data, 2);

            if (packetLength < CommonDefine.TCP_HEAD_8_SIZE || len < packetLength)
            {
                return(null);
            }

            if (encrypted && EnableEncrypt)
            {
                byte[] decData = new byte[packetLength];
                Buffer.BlockCopy(data, 0, decData, 0, (int)packetLength);
                data = Decrypt(decData);
            }

            if (data == null)
            {
                return(null);
            }

            var head = GameConvert.ByteToStruct <CMD_Head_8>(data, CommonDefine.TCP_HEAD_8_SIZE);

            if (head.DataSize != packetLength)
            {
                return(null);
            }

            var packet = NetPacketPool.PopPacket(Packet.enPacketType.Network);

            packet.MainCmd   = head.MainCmdID;
            packet.SubCmd    = head.SubCmdID;
            packet.CheckCode = head.CheckCode;
            packet.Version   = head.MessageVer;
            packet.Size      = head.DataSize;
            packet.HeadSize  = (uint)CommonDefine.TCP_HEAD_8_SIZE;

            int dataLen = (int)packet.Size - CommonDefine.TCP_HEAD_8_SIZE;

            if (dataLen > 0)
            {
                packet.Data = new byte[dataLen];
                Buffer.BlockCopy(data, CommonDefine.TCP_HEAD_8_SIZE, packet.Data, 0, dataLen);
            }

            return(packet);
        }
Ejemplo n.º 2
0
        public bool Initialize()
        {
            NetworkStatusChangeEvent = null;

            NetPacketPool.CreatePool();
            ByteBufferPool.CreatePool();

            var len = Enum.GetValues(typeof(ConnectionID)).Length;

            _connections = new Connection[len];
            for (int i = 0; i < _connections.Length; i++)
            {
                _connections[i] = new Connection(i,
                                                 new PacketBuilder(GameApp.Options.EnableEncryption));
                _connections[i].Regiester(this);
            }

            return(true);
        }