Пример #1
0
 public Session(uint conv, CallbackHandle callback)
 {
     _kcp = new Kcp(conv, callback);
     _kcp.NoDelay(1, 10, 2, 1);//fast
     _kcp.WndSize(64, 64);
     _kcp.SetMtu(512);
 }
Пример #2
0
        static void Main(string[] args)
        {
            int recv = 0;

            Console.WriteLine("Hello World!");

            Parallel.For(0, 10, i =>
            {
                uint conv          = (uint)(i + 1);
                var handlerServer  = new CallbackHandle();
                Session s          = new Session(conv, handlerServer);
                s.MessageReceived += (session, d) =>
                {
                    var str = Encoding.UTF8.GetString(d.Span);
                    Console.WriteLine("服务端收到数据:{0}", str);
                };

                var handlerClient    = new CallbackHandle();
                Session c            = new Session(conv, handlerClient);
                handlerServer.DoPost = (d) =>
                {
                    c.MockInput(d.Span);
                };
                s.SetConnected();

                handlerClient.DoPost = (d) =>
                {
                    s.MockInput(d.Span);
                };
                c.MessageReceived += (session, d) =>
                {
                    var str = Encoding.UTF8.GetString(d.Span);

                    Interlocked.Increment(ref recv);
                    //Console.WriteLine("客户端收到数据:{0}, 回传Ack", str);
                    //var ack = "ack";
                    //var data = Encoding.UTF8.GetBytes(ack);
                    //session.Send(data);
                };
                c.SetConnected();

                for (int j = 0; j < 100; j++)
                {
                    s.Send(Encoding.UTF8.GetBytes(j.ToString()));
                }
            });

            while (true)
            {
                var str = Console.ReadLine();
                if (str == "q")
                {
                    break;
                }
                //s.Send(Encoding.UTF8.GetBytes(str));
                Console.WriteLine($"收到: {recv}");
            }


            Console.WriteLine("byebye");
        }