Example #1
0
        public void SetBitTest()
        {
            ByteExtension.SetBit(0x00, -1)
            .AreEqualWith <byte>(0x00);

            ByteExtension.SetBit(0x00, 0)
            .AreEqualWith <byte>(0x01);

            ByteExtension.SetBit(0x00, 1)
            .AreEqualWith <byte>(0x02);

            ByteExtension.SetBit(0x00, 2)
            .AreEqualWith <byte>(0x04);

            ByteExtension.SetBit(0x00, 3)
            .AreEqualWith <byte>(0x08);

            ByteExtension.SetBit(0x00, 4)
            .AreEqualWith <byte>(0x10);

            ByteExtension.SetBit(0x00, 5)
            .AreEqualWith <byte>(0x20);

            ByteExtension.SetBit(0x00, 6)
            .AreEqualWith <byte>(0x40);

            ByteExtension.SetBit(0x00, 7)
            .AreEqualWith <byte>(0x80);

            ByteExtension.SetBit(0x00, 8)
            .AreEqualWith <byte>(0x00);
        }
Example #2
0
        public void GetBitTest()
        {
            ByteExtension.GetBit(0xFF, 8)
            .IsFalse();

            ByteExtension.GetBit(0xFF, 7)
            .IsTrue();

            ByteExtension.GetBit(0xFF, 6)
            .IsTrue();

            ByteExtension.GetBit(0xFF, 5)
            .IsTrue();

            ByteExtension.GetBit(0xFF, 4)
            .IsTrue();

            ByteExtension.GetBit(0xFF, 3)
            .IsTrue();

            ByteExtension.GetBit(0xFF, 2)
            .IsTrue();

            ByteExtension.GetBit(0xFF, 1)
            .IsTrue();

            ByteExtension.GetBit(0xFF, 0)
            .IsTrue();

            ByteExtension.GetBit(0xFF, -1)
            .IsFalse();
        }
Example #3
0
        public void ClearBitTest()
        {
            ByteExtension.ClearBit(0xFF, -1)
            .AreEqualWith <byte>(0xFF);

            ByteExtension.ClearBit(0xFF, 0)
            .AreEqualWith <byte>(0xFE);

            ByteExtension.ClearBit(0xFF, 1)
            .AreEqualWith <byte>(0xFD);

            ByteExtension.ClearBit(0xFF, 2)
            .AreEqualWith <byte>(0xFB);

            ByteExtension.ClearBit(0xFF, 3)
            .AreEqualWith <byte>(0xF7);

            ByteExtension.ClearBit(0xFF, 4)
            .AreEqualWith <byte>(0xEF);

            ByteExtension.ClearBit(0xFF, 5)
            .AreEqualWith <byte>(0xDF);

            ByteExtension.ClearBit(0xFF, 6)
            .AreEqualWith <byte>(0xBF);

            ByteExtension.ClearBit(0xFF, 7)
            .AreEqualWith <byte>(0x7F);


            ByteExtension.ClearBit(0xFF, 8)
            .AreEqualWith <byte>(0xFF);
        }
Example #4
0
 static public void Byte_To_BitArray()
 {
     Assert.IsTrue(ByteExtension.ConvertByte_To_Int(1) == 1);
     Assert.IsTrue(ByteExtension.ConvertByte_To_Int(1, 8, 1) == 0);
     Assert.IsTrue(ByteExtension.ConvertByte_To_Int(2, 8, 0) == 2);
     Assert.IsTrue(ByteExtension.ConvertByte_To_Int(127, 8, 2) == (127 - 3));
 }
Example #5
0
        public void ToBytesTest1()
        {
            ByteExtension.ToBytes("00 01 02 03", " ").Length.AreEqualWith(4);

            ByteExtension.ToBytes("0x00 0x01 0x02 0x03", " ").Length.AreEqualWith(4);

            ByteExtension.ToBytes("0X00 0X01 0X02 0X03", " ").Length.AreEqualWith(4);
        }
Example #6
0
        public void ComparesNotDimesionalEqualArrays()
        {
            byte[,] array1 = { { 1, 2 }, { 2, 3 }, { 4, 5 } };
            byte[,] array2 = { { 1, 2 } };
            bool equal = ByteExtension.AreEqual(array1, array2);

            Assert.IsFalse(equal);
        }
Example #7
0
        public void ComparesNotEqualContentArrays()
        {
            byte[,] array1 = { { 1, 8 }, { 2, 7 }, { 4, 6 } };
            byte[,] array2 = { { 1, 2 }, { 2, 3 }, { 4, 5 } };
            bool equal = ByteExtension.AreEqual(array1, array2);

            Assert.IsFalse(equal);
        }
Example #8
0
        public void ComparesArraysCorrectly()
        {
            byte[,] array1 = { { 1, 2 }, { 2, 3 }, { 4, 5 } };
            byte[,] array2 = array1.Clone() as byte[, ];
            bool equal = ByteExtension.AreEqual(array1, array2);

            Assert.IsTrue(equal);
        }
Example #9
0
 public void ToByteTest()
 {
     ByteExtension.ToByte("FF").AreEqualWith <byte>(0xFF);
     ByteExtension.ToByte("0F").AreEqualWith <byte>(0x0F);
     ByteExtension.ToByte("F0").AreEqualWith <byte>(0xF0);
     ByteExtension.ToByte("00").AreEqualWith <byte>(0x00);
     ByteExtension.ToByte("12").AreEqualWith <byte>(0x12);
     ByteExtension.ToByte("F").AreEqualWith <byte>(0x0F);
 }
Example #10
0
        public void ToHexTest2()
        {
            IEnumerable <byte> bytes    = new byte[] { 1, 2, 3, 4 };
            string             Prefix   = "0X";
            string             split    = " ";
            string             expected = "0X01 0X02 0X03 0X04";
            string             actual;

            actual = ByteExtension.ToHex(bytes, Prefix, split);
            Assert.AreEqual(expected, actual);
        }
Example #11
0
        public void ToBytesTest()
        {
            byte[] data = ByteExtension.ToBytes("00010203");
            data.Length.AreEqualWith(4);

            data = ByteExtension.ToBytes("0x000x010x020x03");
            data.Length.AreEqualWith(4);

            data = ByteExtension.ToBytes("0X000X010X020X03");
            data.Length.AreEqualWith(4);
        }
Example #12
0
        public void ReverseBitTest()
        {
            byte b        = 0;
            int  index    = 0;
            byte expected = 0;
            byte actual;

            actual = ByteExtension.ReverseBit(b, index);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Example #13
0
        internal void SendCToSResponse(byte[] content)
        {
            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);

            writer.Write((short)(1 + content.Length));
            writer.Write((byte)CToSMessage.Response);
            writer.Write(content);
            NetworkService.Instance.Send(stream.ToArray());
            Field.ClearCommands();
            DuelWindow.OnResponsed();
            Debug.Log("-->ctos:Response:" + ByteExtension.ByteToString(content));
        }
        public IEnumerator Convert_Struct_To_Byte_To_Struct_Test()
        {
            SPacketTest_Struct pPacket1 = new SPacketTest_Struct(1, "테스트Test123!@#");
            SPacketTest_Struct pPacket2 = new SPacketTest_Struct();

            Assert.AreNotEqual(pPacket1, pPacket2);

            byte[] arrData = ByteExtension.ConvertByteArray(pPacket1);
            ByteExtension.ConvertPacket(arrData, out pPacket2);

            Assert.AreEqual(pPacket1, pPacket2);

            yield return(null);
        }
        public bool JoinServer(string hostAddress, int port, string playerName, string roomCode = null)
        {
            if (Connected || hostAddress == null || playerName == null)
            {
                return(false);
            }
            try
            {
                clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                clientSocket.Connect(hostAddress, port);
                stream    = new NetworkStream(clientSocket);
                Connected = true;
            }
            catch (Exception e)
            {
                Connected = false;
                Debug.Log(e.Message + e.GetType() + e.StackTrace);
                return(false);
            }
            MainGame.Instance.Duel.Start();
            receiveThread = new Thread(Receive);
            receiveThread.Start();
            if (playerName.Length > 20)
            {
                playerName = playerName.Substring(0, 20);
            }
            //playerinfo
            byte[]       result = new byte[43];
            BinaryWriter writer = new BinaryWriter(new MemoryStream(result));

            writer.Write((ushort)41);
            writer.Write((byte)CToSMessage.PlayerInfo);
            writer.Write(ByteExtension.StringToUnicode(playerName, 40));
            Send(result);
            //joingame
            byte[] data = new byte[51];
            writer = new BinaryWriter(new MemoryStream(data));
            writer.Write((ushort)49);
            writer.Write((byte)CToSMessage.JoinGame);
            writer.Write((int)Config.YGOVersion);
            writer.Write((uint)0);
            if (roomCode == null)
            {
                roomCode = string.Empty;
            }
            writer.Write(ByteExtension.StringToUnicode(roomCode, 40));
            Send(data);
            return(true);
        }
Example #16
0
    private byte[] CombineBuffer_Header_And_Packet(INetworkPacket pPacket, byte[] arrByte)
    {
        bool          bIsGenerate;
        Packet_Header pPacketHeader;

        OnGeneratePacketHeader(pPacket, out bIsGenerate, out pPacketHeader);

        _pBufferSend.Clear();
        if (bIsGenerate)
        {
            pPacketHeader.INetworkPacketHeader_Set_Header(arrByte.Length, pPacket.INetworkPacket_Get_PacketID());
            _pBufferSend.Enqueue(ByteExtension.ConvertByteArray(pPacketHeader));
        }
        _pBufferSend.Enqueue(ByteExtension.ConvertByteArray(pPacket));

        return(_pBufferSend.Dequeue_OrNull(_pBufferSend.Count));
    }
Example #17
0
    private byte[] CombineBuffer_Header_And_Packet <Packet>(Packet pPacket, Packet_Header pPacketHeader) where Packet : INetworkPacket
    {
        bool bIsGenerate = pPacketHeader != null;

        if (bIsGenerate == false)
        {
            OnGeneratePacketHeader(pPacket, out bIsGenerate, out pPacketHeader);
        }

        _pBufferSend.Clear();
        if (bIsGenerate)
        {
            pPacketHeader.INetworkPacketHeader_Set_Header(ByteExtension.SizeOf <Packet>(), pPacket.INetworkPacket_Get_PacketID());
            _pBufferSend.Enqueue(ByteExtension.ConvertByteArray(pPacketHeader));
        }
        _pBufferSend.Enqueue(ByteExtension.ConvertByteArray(pPacket));

        return(_pBufferSend.Dequeue_OrNull(_pBufferSend.Count));
    }
        public void RingBuffer_Enqueue_Dequeue_Test()
        {
            SPacketTest_Struct pPacketTest  = new SPacketTest_Struct(1, "인큐_디큐_테스트");
            SPacketTest_Struct pPacketTest2 = new SPacketTest_Struct(2, "더미데이터");

            byte[] arrPacketData = ByteExtension.ConvertByteArray(pPacketTest);
            int    iDataSize     = arrPacketData.Length;

            var pBuffer = new CCircularBuffer <byte>(10240);

            pBuffer.Enqueue(arrPacketData);

            Assert.AreNotEqual(pPacketTest.iValue, pPacketTest2.iValue);
            Assert.AreNotEqual(pPacketTest.strValue, pPacketTest2.strValue);

            byte[] arrPacketData2 = pBuffer.Dequeue_OrNull(iDataSize);
            arrPacketData2.Convert_ToStruct(out pPacketTest2);

            Assert.AreEqual(pPacketTest.iValue, pPacketTest2.iValue);
            Assert.AreEqual(pPacketTest.strValue, pPacketTest2.strValue);
        }
Example #19
0
 private byte[] CombineBuffer_Header_And_Packet <Packet>(Packet pPacket) where Packet : INetworkPacket
 {
     return(CombineBuffer_Header_And_Packet(pPacket, ByteExtension.ConvertByteArray(pPacket)));
 }
Example #20
0
 public void ToHexTest1()
 {
     ByteExtension.ToHex(new byte[] { 1, 2, 3, 4 })
     .AreEqualWith("01020304");
 }
Example #21
0
 public void ToHexTest()
 {
     ByteExtension.ToHex(0x00).AreEqualWith("00");
     ByteExtension.ToHex(0xFF).AreEqualWith("FF");
     ByteExtension.ToHex(0xAA).AreEqualWith("AA");
 }