Ejemplo n.º 1
0
        private static void TestSerializerPacketA()
        {
            Serializer sender       = new WriterSerialize();
            PacketA    sendedPacket = new PacketA()
            {
                Position = new Vector3(-29.158f, 50.735f, 150.2875f), f = 100.191f, comment = "Je suis CON !!"
            };
            bool sendingAuthorized = sendedPacket.Serialize(sender);

            sendedPacket = new PacketA()
            {
                Position = new Vector3(50, 100, 40), f = 45.02f, comment = "Je suis un test."
            };
            sendingAuthorized |= sendedPacket.Serialize(sender);
            sender.BitPacking.PushTempInBuffer();
            byte[] data = sender.BitPacking.GetByteBuffer();
            if (sendingAuthorized)
            {
                Serializer receiver = new ReaderSerialize();
                receiver.BitPacking = BitPacker.FromArray(data);
                PacketA receivedPacket  = new PacketA();
                PacketA receivedPacket2 = new PacketA();
                bool    isValid         = receivedPacket.Serialize(receiver);
                isValid = receivedPacket2.Serialize(receiver);
            }
        }
Ejemplo n.º 2
0
        private static void TestCRC()
        {
            // Sender side
            // ============================================================================================
            BitPacker bufferSender = new BitPacker();

            bufferSender.WriteValue(0x39, 6);
            bufferSender.WriteValue(0x2B, 6);
            Console.WriteLine("Data envoyées sans CRC :" + bufferSender.ToString());

            Parameters parameters = CrcStdParams.StandartParameters[CrcAlgorithms.Crc32];
            Crc        crcSender  = new Crc(parameters);

            crcSender.ComputeHash(bufferSender.GetByteBuffer());
            uint value = (uint)CrcHelper.FromBigEndian(crcSender.Hash, crcSender.HashSize);

            bufferSender.AlignToNextWriteByte();
            bufferSender.WriteValue(value, 32);

            /*
             * Console.WriteLine("Data envoyées avec CRC :" + bufferSender.ToString());
             *
             * // Trucage des données pour test du CRC
             * bufferSender = new BitPacking();
             * bufferSender.WriteValue(0x39, BitsRequired(0, 63));
             * bufferSender.WriteValue(0x2B, BitsRequired(0, 63));
             *
             * bufferSender.AlignToNextByte();
             * bufferSender.WriteValue(value, (uint)crcSender.HashSize);
             * bufferSender.PushTempInBuffer();
             * // Console.WriteLine("Valeur du CRC :" + Convert.ToString(value, toBase: 16));
             *
             * Console.WriteLine("=================================================================");
             */
            // Receiver side
            // ============================================================================================
            BitPacker bufferReceiver = BitPacker.FromArray(bufferSender.GetByteBuffer());
            //Console.WriteLine("Réception data avec CRC :" + bufferReceiver.ToString());

            Crc crcReceiver = new Crc(parameters);

            uint crcValue = bufferReceiver.ReadValue(crcReceiver.HashSize, true, bufferReceiver.BitLength - 32);

            Console.WriteLine("Réception data sans CRC :" + bufferReceiver.ToString());

            parameters.Check = crcValue;

            if (crcReceiver.IsRight(bufferReceiver.GetByteBuffer()))
            {
                Console.WriteLine("Paquet accepté !");
            }
            else
            {
                Console.WriteLine("Refused packet !");
            }
        }
Ejemplo n.º 3
0
        private static void TestBitPacker()
        {
            BitPacker bitPacker = new BitPacker();

            bitPacker.WriteValue(80, 32);
            bitPacker.WriteValue(0x800800, 32);

            Console.WriteLine(bitPacker.ToString());
            bitPacker.OverrideValue(4160749567, 32, 16);
            Console.WriteLine(bitPacker.ToString());
            bitPacker.PushTempInBuffer();
            Console.WriteLine(bitPacker.ToString());
        }
Ejemplo n.º 4
0
        public static BitPacker FromArray(byte[] pBuffer, bool pPushTempInBuffer = true)
        {
            int length           = pBuffer.Length;
            int nbOfBytePerIndex = BUFFER_BIT_SIZE / 8;

            byte[] b = new byte[length + nbOfBytePerIndex - (length % nbOfBytePerIndex)];
            pBuffer.CopyTo(b, 0);
            Span <uint> spanUint = MemoryMarshal.Cast <byte, uint>(new Span <byte>(b));
            BitPacker   result   = new BitPacker(spanUint, 8 * length);

            result._temp = spanUint[spanUint.Length - 1];
            return(result);
        }
Ejemplo n.º 5
0
        private static void TestSerializerPacketB()
        {
            Serializer    sender     = new WriterSerialize();
            List <IActor> senderList = new List <IActor>();
            Tank          t          = new Tank()
            {
                Name = "Toto", Life = 100, Position = new Vector2(150), ShouldBeSend = true
            };

            t.Shoot();
            senderList.Add(t);
            senderList.Add(new Loot()
            {
                NbOfAmmo = 1, AmmoType = eAmmoType.Grenada, Position = new Vector2(150), IsActive = true, ShouldBeSend = true
            });
            senderList.Add(new Tank()
            {
                Name = "Tata", Life = 80, Position = new Vector2(100), ShouldBeSend = true
            });
            senderList.Add(new Loot()
            {
                AmmoType = eAmmoType.Bullet, Position = new Vector2(360), IsActive = true, ShouldBeSend = true
            });
            senderList.Add(new Tank()
            {
                Life = 50, Position = new Vector2(50), ShouldBeSend = true
            });

            PacketB sendedPacket = new PacketB()
            {
                Actors = senderList
            };
            bool sendingAuthorized = sendedPacket.Serialize(sender);

            sender.BitPacking.PushTempInBuffer();

            byte[] dataSending = sender.BitPacking.GetByteBuffer();

            if (sendingAuthorized)
            {
                Serializer receiver = new ReaderSerialize();
                receiver.BitPacking = BitPacker.FromArray(dataSending);
                List <IActor> receiverList   = new List <IActor>();
                PacketB       receivedPacket = new PacketB()
                {
                    Actors = receiverList
                };
                bool isValid = receivedPacket.Serialize(receiver);
            }
        }
Ejemplo n.º 6
0
        private static void Devlog()
        {
            BitPacker bitPacker = new BitPacker();

            bitPacker.WriteValue(80, 7);
            bitPacker.WriteValue(3850, 12);
            bitPacker.WriteValue(750, 10);

            bitPacker.PushTempInBuffer();
            Console.WriteLine(bitPacker.ToString());

            byte[] data = bitPacker.GetByteBuffer();

            Console.WriteLine("Reader Creation : ");
            BitPacker reader = BitPacker.FromArray(data);

            Console.WriteLine(reader.ReadValue(7));
            Console.WriteLine(reader.ReadValue(12));
            Console.WriteLine(reader.ReadValue(10));
        }