public static IPacket SendName(string s)
 {
     var pack = new MutablePacket();
     pack.WriteUShort((ushort)OperationCode.SetName); //OPCode
     pack.WriteStandardString(s);
     return pack;
 }
 public static IPacket SendMessage(string s)
 {
     var pack = new MutablePacket();
     pack.WriteShort(2); //OPCode
     pack.WriteStandardString(s);
     return pack;
 }
        public async Task TestPacketWriteByteArray()
        {
            var packet = new MutablePacket();
            packet.Write(new byte[] { 5, 11, 27, 30 });
            var bytes = packet.Lock();
            Assert.IsTrue(bytes.Length >= 4 && bytes[0] == 5 && bytes[1] == 11 && bytes[2] == 27 && bytes[3] == 30, "Failed to write byte array to packet");
            if (packet.Locked)
            {
                var task = new Task(() => packet.Write(new byte[] { 99, 12 }));
                task.Start();
                Assert.IsTrue(await TaskTimesOut(TimeSpan.FromSeconds(2), task), "Packet reported being locked after locking but was still mutable");
            }

            else
            {
                bytes[0] = 72;
                var newBytes = packet.Lock();
                if (newBytes[0] != 5)
                    Assert.Fail("Packet reported not being locked but the buffer was returned by reference not copy. Return a full copy of the buffer if you wish to unlock else ensure you set the buffer to a locked state when Lock() is called.");
            }
        }
        public async Task TestPacketWriteUint()
        {
            const int size = sizeof(uint);
            const int lastIndex = size - 1;

            var packet = new MutablePacket();
            packet.WriteInt(201);
            var bytes = packet.Lock();
            Assert.IsTrue(bytes.Length >= size && (bytes[0] == 201 || bytes[lastIndex] == 201), "Failed to write unsigned integer to packet");
            if (packet.Locked)
            {
                var task = new Task(() => packet.WriteInt(3935));
                task.Start();
                Assert.IsTrue(await TaskTimesOut(TimeSpan.FromSeconds(2), task), "Packet reported being locked after locking but was still mutable");
            }

            else
            {
                bytes[0] = 94;
                var newBytes = packet.Lock();
                if (newBytes[0] != 201 && newBytes[lastIndex] != 201)
                    Assert.Fail("Packet reported not being locked but the buffer was returned by reference not copy. Return a full copy of the buffer if you wish to unlock else ensure you set the buffer to a locked state when Lock() is called.");
            }
        }
        public async Task TestPacketWriteStandardString()
        {
            const string str = "kaka";

            var packet = new MutablePacket();

            packet.WriteStandardString(str);
            var bytes = packet.Lock();
            if (packet.Locked)
            {
                var task = new Task(() => packet.WriteString("sdgsgd"));
                task.Start();
                Assert.IsTrue(await TaskTimesOut(TimeSpan.FromSeconds(2), task), "Packet reported being locked after locking but was still mutable");
            }

            else
            {
                bytes[0] = 212;
                var newBytes = packet.Lock();
                if (bytes[0] != newBytes[0])
                    Assert.Fail("Packet reported not being locked but the buffer was returned by reference not copy. Return a full copy of the buffer if you wish to unlock else ensure you set the buffer to a locked state when Lock() is called.");
            }
        }
        public async Task TestPacketWriteDouble()
        {
            const int size = sizeof(double);

            var packet = new MutablePacket();
            packet.WriteDouble(55.0);
            var bytes = packet.Lock();
            Assert.IsTrue(bytes.Length >= size, "Failed to write double to packet");
            if (packet.Locked)
            {
                var task = new Task(() => packet.WriteDouble(2425.1246435));
                task.Start();
                Assert.IsTrue(await TaskTimesOut(TimeSpan.FromSeconds(2), task), "Packet reported being locked after locking but was still mutable");
            }
            else
            {
                bytes[0] = 97;
                var newBytes = packet.Lock();
                if (bytes[0] != newBytes[0])
                    Assert.Fail("Packet reported not being locked but the buffer was returned by reference not copy. Return a full copy of the buffer if you wish to unlock else ensure you set the buffer to a locked state when Lock() is called.");
            }
        }