Exemplo n.º 1
0
        void NetworkServer_NotifyReceivedPackage(object sender, IocpPacketEventArgs e)
        {
            NetworkComputeBuffer ComputeBuffer = null;

            lock (ComputeTable)
            {
                if (ComputeTable.ContainsKey(e.token.UniqueId))
                {
                    ComputeBuffer = ComputeTable[e.token.UniqueId];
                }
            }

            //附加数据到缓冲区
            ComputeBuffer.TotalBuffer.Write(e.Buffer, 0, e.Buffer.Length);

            //开始拆分封包数据
            while (ComputeBuffer.TotalBuffer.Length >= Shared.GetPackageInformationLength())                  //封包是否符合逻辑
            {
                //开始读取封包
                BinaryReader ComputeReader = ComputeBuffer.GetReader();

                uint   CurrentBlockLength      = ComputeReader.ReadUInt32();                //当前数据分块的长度
                ushort CurrentPackageSignature = ComputeReader.ReadUInt16();                //封包标记

                if (
                    CurrentBlockLength <= 0 ||                                   //包大小是否负数
                    CurrentBlockLength > 0x100000 ||                             //包大小大于1MB
                    CurrentBlockLength < Shared.GetPackageInformationLength() || //包头的大小
                    CurrentPackageSignature != Shared.PackageSignature           //包标记
                    )
                {
                    throw new BadPackageException();
                }

                if (CurrentBlockLength > ComputeBuffer.TotalBuffer.Length)                 //是否完整的数据,就等待客户端下次发送数据到达
                {
                    break;
                }

                int CurrentBlockDataLength = (int)CurrentBlockLength - Shared.GetPackageInformationLength();

                //解密数据
                byte[] encrypted = ComputeReader.ReadBytes(CurrentBlockDataLength);
                byte[] decrypted = AESEx.AESDecrypt(encrypted, CommonConfig.key, CommonConfig.iv);


                if (USERNotifyReceivedPackage != null)
                {
                    USERNotifyReceivedPackage(this, new USER_NetworkReceivedArgs(e.token, decrypted));
                }

                int nextBufferLength = (int)(ComputeBuffer.TotalBuffer.Length - CurrentBlockLength);
                ComputeBuffer.TotalBuffer.SetLength(0);
                ComputeBuffer.TotalBuffer.Write(ComputeReader.ReadBytes(nextBufferLength), 0, nextBufferLength);
            }
        }
Exemplo n.º 2
0
        void NetworkServer_NotifyReceivedPackage(object sender, IocpPacketEventArgs e)
        {
            NetworkComputeBuffer ComputeBuffer = null;
            lock (ComputeTable)
            {
                if (ComputeTable.ContainsKey(e.token.UniqueId))
                {
                    ComputeBuffer = ComputeTable[e.token.UniqueId];
                }
            }

            //附加数据到缓冲区
            ComputeBuffer.TotalBuffer.Write(e.Buffer, 0, e.Buffer.Length);

            //开始拆分封包数据
            while (ComputeBuffer.TotalBuffer.Length >= Shared.GetPackageInformationLength())      //封包是否符合逻辑
            {
                //开始读取封包
                BinaryReader ComputeReader = ComputeBuffer.GetReader();

                uint CurrentBlockLength = ComputeReader.ReadUInt32();       //当前数据分块的长度
                ushort CurrentPackageSignature = ComputeReader.ReadUInt16();       //封包标记

                if (
                    CurrentBlockLength <= 0 ||  //包大小是否负数
                    CurrentBlockLength > 0x100000 || //包大小大于1MB
                    CurrentBlockLength < Shared.GetPackageInformationLength() || //包头的大小
                    CurrentPackageSignature != Shared.PackageSignature     //包标记
                    )
                {
                    throw new BadPackageException();
                }

                if (CurrentBlockLength > ComputeBuffer.TotalBuffer.Length) //是否完整的数据,就等待客户端下次发送数据到达
                {
                    break;
                }

                int CurrentBlockDataLength = (int)CurrentBlockLength - Shared.GetPackageInformationLength();

                //解密数据
                byte[] encrypted = ComputeReader.ReadBytes(CurrentBlockDataLength);
                byte[] decrypted = AESEx.AESDecrypt(encrypted, CommonConfig.key, CommonConfig.iv);

                if (USERNotifyReceivedPackage != null)
                {
                    USERNotifyReceivedPackage(this, new USER_NetworkReceivedArgs(e.token, decrypted));
                }

                int nextBufferLength = (int)(ComputeBuffer.TotalBuffer.Length - CurrentBlockLength);
                ComputeBuffer.TotalBuffer.SetLength(0);
                ComputeBuffer.TotalBuffer.Write(ComputeReader.ReadBytes(nextBufferLength), 0, nextBufferLength);
            }
        }
Exemplo n.º 3
0
 void NetworkServer_NotifySendComplete(object sender, IocpPacketEventArgs e)
 {
 }
Exemplo n.º 4
0
 void NetworkServer_NotifySendComplete(object sender, IocpPacketEventArgs e)
 {
 }