Beispiel #1
0
        private static void ProtocolsDemo()
        {
            INetProtocolProvider protocolProvider = NetTool.CreateProtocolProvider();

            //数据内容打包成字节
            byte[] content = new byte[] { 1, 3, 4, 0xfe, 0x01, 0xfd, 0x02 };
            byte[] buffer  = protocolProvider.Encode(new Packet(1, 0x10, 5, content));

            //使用接收管理缓冲池解析数据包
            INetPacketProvider pkgProvider = NetTool.CreatePacketsProvider(1024);
            bool rt = pkgProvider.SetBlocks(buffer, 0, buffer.Length);

            rt = pkgProvider.SetBlocks(buffer, 0, buffer.Length);
            var dePkg = pkgProvider.GetBlocks();

            //解析数据包成结构信息
            // var dePkg = protocolProvider.Decode(buffer, 0, buffer.Length);
        }
Beispiel #2
0
        private static void PacketDemo()
        {
            var protocolProvider = NetTool.CreateProtocolProvider();
            var packetProvider   = NetTool.CreatePacketsProvider(4096 * 32);

            var pk1 = protocolProvider.Encode(new Packet(1, 0x10, 5, new byte[] { 1, 2 }));
            var pk2 = protocolProvider.Encode(new Packet(1, 0x10, 5, new byte[] { 3, 4 }));


            List <byte> buffer = new List <byte>();

            buffer.AddRange(pk1);
            //混淆测试值
            buffer.Add(11);
            buffer.AddRange(pk2);
            //混淆测试值
            buffer.Add(6);

            bool isOk = packetProvider.SetBlocks(buffer.ToArray(), 0, buffer.Count);
            var  pks  = packetProvider.GetBlocks();
        }
Beispiel #3
0
        private static void PacketSocketDemo()
        {
            int port = 13145;
            var netServerProvider = NetTool.CreateTcpSever();
            INetProtocolProvider netProtocolProvider = NetTool.CreateProtocolProvider();
            INetPacketProvider   netPacketProvider   = NetTool.CreatePacketsProvider(4096 * 32);//最大容量,必须大于发送缓冲区,建议是设置大于8倍以上

            int pktCnt = 0;

            netServerProvider.ReceivedOffsetCallback = new OnReceivedSegmentHandler((SegmentToken session) =>
            {
                bool isEn = netPacketProvider.SetBlocks(session.Data.buffer, session.Data.offset, session.Data.size);
                if (isEn == false)
                {
                    Log.L("entry queue failed");
                }
                List <Packet> packets = netPacketProvider.GetBlocks();

                pktCnt += packets.Count;

                if (packets.Count > 0)
                {
                    foreach (var pkt in packets)
                    {
                        Log.L(Encoding.UTF8.GetString(pkt.message));
                    }
                }
                else
                {
                    Log.L("got null item from queue");
                }

                Log.L("pktCnt:" + pktCnt);
            });

            bool isStart = netServerProvider.Start(port);

            if (isStart)
            {
                var  netClientProvider = NetTool.CreateTcpClient();
                bool isConneted        = netClientProvider.ConnectTo(port, "127.0.0.1");
                if (isConneted)
                {
                    //for (int i = 0; i < content.Length; ++i)
                    //{
                    //    content[i] = (byte)(i > 255 ? 255 : i);
                    //}
                    int i = 0;
                    for (; i < 100000; ++i)
                    {
                        byte[] content = Encoding.UTF8.GetBytes("hello 哈哈 http://anystore.bouyeijiang.com" + DateTime.Now + i.ToString());

                        byte[] buffer = netProtocolProvider.Encode(new Packet(1, 0x10, 5, content));

                        netClientProvider.Send(new SegmentOffset(buffer));
                    }
                }

                Console.ReadKey();
            }
        }