Пример #1
0
 public void Pack(BinaryWriter writer)
 {
     log.Debug("Pack");
     writer.Write(SeqID);
     writer.Write((uint)Header);
     writer.Write(Checksum);
     writer.Write(RecId);
     writer.Write(Interval);
     writer.Write(DataLen);
     writer.Write(Iteration);
     if ((Header & PacketHeaderFlags.ServerSwitch) != 0)
     {
         ServerSwitchData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.Referral) != 0)
     {
         ReferralData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.NegativeAck) != 0)
     {
         NegativeAckData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.EmptyAck) != 0)
     {
         EmptyAckData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.PositiveAck) != 0)
     {
         PositiveAckData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.LoginRequest) != 0)
     {
         LoginRequestData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.ServerSwitchRequest) != 0)
     {
         ServerSwitchRequestData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.ConnectRequest) != 0)
     {
         ConnectRequestData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.ConnectResponse) != 0)
     {
         ConnectResponseData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.NetError) != 0)
     {
         NetErrorData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.CloseConnection) != 0)
     {
         CloseConnectionData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.CICMDCommand) != 0)
     {
         CicmdData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.TimeSync) != 0)
     {
         TimeSyncData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.EchoRequest) != 0)
     {
         EchoRequestData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.EchoResponse) != 0)
     {
         EchoResponseData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.Flow) != 0)
     {
         FlowData.Pack(writer);
     }
     if ((Header & PacketHeaderFlags.BlobFragments) != 0)
     {
         foreach (var fragment in Fragments)
         {
             fragment.Pack(writer);
         }
     }
 }
Пример #2
0
        public void Unpack(BinaryReader reader)
        {
            var bytesRemaining = reader.BytesRemaining();

            log.DebugFormat("Unpack bytes: {0}", bytesRemaining);
            log.Assert(bytesRemaining >= PacketHeaderSize, "Packet size too small");
            log.Assert(bytesRemaining <= MaxPacketSize, "Packet size too big");

            SeqID     = reader.ReadUInt32();
            Header    = (PacketHeaderFlags)reader.ReadUInt32();
            Checksum  = reader.ReadUInt32();
            RecId     = reader.ReadUInt16();
            Interval  = reader.ReadUInt16();
            DataLen   = reader.ReadUInt16();
            Iteration = reader.ReadUInt16();

            log.DebugFormat("SeqID: {0}, Header: {1}, Checksum {2}, RecId: {3}, Interval {4}, DataLen: {5}, Iteration: {6}", SeqID, Header, Checksum, RecId, Interval, DataLen, Iteration);

            bytesRemaining = reader.BytesRemaining();
#if NETWORKVALIDATION
            log.Assert(bytesRemaining == DataLen, "Size of reader " + DataLen + " does not equal packet length: " + bytesRemaining);
#endif

            if ((Header & PacketHeaderFlags.ServerSwitch) != 0)
            {
                ServerSwitchData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.Referral) != 0)
            {
                ReferralData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.NegativeAck) != 0)
            {
                NegativeAckData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.EmptyAck) != 0)
            {
                EmptyAckData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.PositiveAck) != 0)
            {
                PositiveAckData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.LoginRequest) != 0)
            {
                LoginRequestData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.ServerSwitchRequest) != 0)
            {
                ServerSwitchRequestData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.ConnectRequest) != 0)
            {
                ConnectRequestData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.ConnectResponse) != 0)
            {
                ConnectResponseData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.NetError) != 0)
            {
                NetErrorData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.CloseConnection) != 0)
            {
                CloseConnectionData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.CICMDCommand) != 0)
            {
                CicmdData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.TimeSync) != 0)
            {
                TimeSyncData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.EchoRequest) != 0)
            {
                EchoRequestData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.EchoResponse) != 0)
            {
                EchoResponseData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.Flow) != 0)
            {
                FlowData.Unpack(reader);
            }
            if ((Header & PacketHeaderFlags.BlobFragments) != 0)
            {
                log.Debug("Reading Blob Fragments");
                Fragments.Clear();
                bytesRemaining = reader.BytesRemaining();
                uint fragNum = 0;
                while (bytesRemaining >= 16)
                {
                    log.DebugFormat("Bytes Remaining: {0}", bytesRemaining);
                    BlobFrag newItem = new BlobFrag(reader);
#if NETWORKDEBUG
                    newItem.NetworkPacketNum   = NetworkPacketNum;
                    newItem.NetworkFragmentNum = ++fragNum;
#endif
                    Fragments.Add(newItem);
                    bytesRemaining = reader.BytesRemaining();
                }
#if NETWORKVALIDATION
                log.Assert(bytesRemaining == 0, "Bytes still remaining in packet: " + bytesRemaining);
#endif
            }
        }