Esempio n. 1
0
        public void TryReadFrom_ReturnsFalseWhenSentTimeIsNotPresent()
        {
            var packetWriter = new ENetPacketWriter();

            packetWriter.WriteNetEndian((ushort)ENET_PROTOCOL_HEADER_FLAG_SENT_TIME);

            var packetReader = new ENetPacketReader(packetWriter.GetWrittenBuffer().Span);

            Assert.IsFalse(ENetProtocolHeader.TryReadFrom(ref packetReader, out var _));
        }
Esempio n. 2
0
        public void TryReadFrom_DoesNotReadSentTimeWhenFlagIsNotSet()
        {
            var packetWriter = new ENetPacketWriter();

            packetWriter.WriteValue <ushort>(0);
            packetWriter.WriteValue <ushort>(0);

            var packetReader = new ENetPacketReader(packetWriter.GetWrittenBuffer().Span);

            Assert.IsTrue(ENetProtocolHeader.TryReadFrom(ref packetReader, out var header));
            Assert.AreEqual(2, packetReader.Left.Length);
        }
Esempio n. 3
0
        public void TryReadFrom_ReadsSentTimeWhenFlagIsSet()
        {
            const ushort sentTime = 0b1100_1100_0011_1010;

            var packetWriter = new ENetPacketWriter();

            packetWriter.WriteNetEndian((ushort)ENET_PROTOCOL_HEADER_FLAG_SENT_TIME);
            packetWriter.WriteNetEndian((ushort)sentTime);

            var packetReader = new ENetPacketReader(packetWriter.GetWrittenBuffer().Span);

            Assert.IsTrue(ENetProtocolHeader.TryReadFrom(ref packetReader, out var header));
            Assert.AreEqual(sentTime, header.SentTime);
        }
Esempio n. 4
0
        public void WriteTo_DoesNotWriteSentWhenFlagIsNotSet()
        {
            var writeHeader = new ENetProtocolHeader()
            {
                Flags    = 0,
                SentTime = 12031,
            };

            var packetWriter = new ENetPacketWriter();

            writeHeader.WriteTo(packetWriter);

            Assert.AreEqual(2, packetWriter.GetWrittenBuffer().Length);
        }
Esempio n. 5
0
        public void WriteBytesTest()
        {
            var bytes = Encoding.UTF8.GetBytes("Lazy cat jumps over sleepy dog.");

            binaryWriter.WriteBytes(bytes);
            var writtenBytes = binaryWriter.GetWrittenBuffer().ToArray();

            Assert.IsTrue(writtenBytes.SequenceEqual(bytes));
        }
Esempio n. 6
0
        public void WriteTo_WritesSentTimeWhenFlagIsSet()
        {
            var writeHeader = new ENetProtocolHeader()
            {
                Flags    = ENET_PROTOCOL_HEADER_FLAG_SENT_TIME,
                SentTime = 123,
            };

            var packetWriter = new ENetPacketWriter();

            writeHeader.WriteTo(packetWriter);

            var packetReader = new ENetPacketReader(packetWriter.GetWrittenBuffer().Span);

            Assert.True(ENetProtocolHeader.TryReadFrom(ref packetReader, out var readHeader));

            Assert.AreEqual(writeHeader.Flags, readHeader.Flags);
            Assert.AreEqual(writeHeader.SentTime, readHeader.SentTime);
        }
Esempio n. 7
0
        public void BasicReadWriteTest()
        {
            var writeHeader = new ENetProtocolHeader()
            {
                Flags     = ENET_PROTOCOL_HEADER_FLAG_SENT_TIME | ENET_PROTOCOL_HEADER_FLAG_COMPRESSED,
                PeerId    = 0b101010_010101, // 12bit
                SentTime  = ushort.MaxValue, // 2byte
                SessionId = 0b11,            // 2bit
            };

            var packetWriter = new ENetPacketWriter();

            writeHeader.WriteTo(packetWriter);

            var packetReader = new ENetPacketReader(packetWriter.GetWrittenBuffer().Span);

            Assert.IsTrue(ENetProtocolHeader.TryReadFrom(ref packetReader, out var readHeader));
            Assert.AreEqual(writeHeader.Flags, readHeader.Flags);
            Assert.AreEqual(writeHeader.PeerId, readHeader.PeerId);
            Assert.AreEqual(writeHeader.SentTime, readHeader.SentTime);
            Assert.AreEqual(writeHeader.SessionId, readHeader.SessionId);
        }