示例#1
0
        public void ReadPackets()
        {
            using (StreamReader fileStream = new StreamReader(fileToRead))
            {
                var str = fileStream.ReadToEnd();
                G2Log.Write("File : " + str);
                byte[] bytes = StringToByteArray(str);

                NodePeer       p      = new NodePeer(IPAddress.Parse("127.0.0.1"), 16546, 0, false);
                G2PacketReader reader = new G2PacketReader(p);
                reader.Read(bytes, bytes.Length);
                G2Log.Write("G2PacketTest: ReadPacket file " + fileToRead);
                G2Packet pack = p.Buffer.PollPacketToReceive();
                G2Log.Write(pack.ToString());
                //if(pack.type == G2PacketType.LNI) testLNI (pack);
            }
        }
示例#2
0
        public void testReader()
        {
            G2Network.Instance.SelfAddress = System.Net.IPAddress.Parse("127.0.0.1");
            G2Packet lni = Settings.SmartLNIPacket();
            G2Packet na  = new G2PacketNA(new NodeAddress(System.Net.IPAddress.Parse("127.0.0.1"), 6345));

            for (int i = 0; i < 300; i++)
            {
                lni.AddChild(na);
            }
            lni.FinalizePacket();
            G2Log.Write(lni.ToString());
            ByteBuffer     b      = lni.ToBuffer();
            G2PacketReader reader = new G2PacketReader(new NodePeer(System.Net.IPAddress.Parse("127.0.0.1"), 6346, 0, false));
            int            nb     = 3;

            byte[][] bytes = new byte[nb][];
            int      start = 0;
            int      div   = b.Length / nb;

            for (int i = 0; i < nb; i++)
            {
                if (i == nb - 1)
                {
                    div += b.Length % nb;
                }
                bytes [i] = new byte[div];
                Array.Copy(b.Bytes, start, bytes [i], 0, div);
                start += div;
            }


            for (int i = 0; i < nb; i++)
            {
                bool enough = reader.Read(bytes [i], bytes[i].Length);
                if (enough)
                {
                    G2Log.Write("PacketReader Test Success");
                    break;
                }
            }
            G2Packet   lni2 = Settings.SmartLNIPacket();
            ByteBuffer b2   = lni2.ToBuffer();
            bool       succ = reader.Read(b2.Bytes, b2.DataOffset);

            if (succ)
            {
                G2Log.Write("PacketReader Test Unit Packet Success");
            }
            else
            {
                G2Log.Write("PacketReader Test Unit Packet FAILED");
            }


            b.Append(b2);
            succ = reader.Read(b.Bytes, b.DataOffset);
            if (succ)
            {
                G2Log.Write("PacketReader Test Sequential Packet Success");
            }
            else
            {
                G2Log.Write("PacketReader TEst Sequential Packet FAILED");
            }
        }