Exemplo n.º 1
0
        /// <summary>
        /// 连接服务器
        /// </summary>
        private static async void ConnectAsync()
        {
            IRemote remote = new TcpRemote();
            var     ex     = await remote.ConnectAsync(new IPEndPoint(IPAddress.IPv6Loopback, 54321));

            if (ex == null)
            {
                ///没有异常,连接成功
                Console.WriteLine("连接成功");

                ///创建一个登陆消息
                var login = new Login2Gate
                {
                    Account  = $"TestClient",
                    Password = "******"
                };

                ///有返回值,这个是一个RPC过程,Exception在网络中传递
                var resp = await remote.SendAsyncSafeAwait <Login2GateResult>(login);

                if (resp.IsSuccess)
                {
                    Console.WriteLine("登陆成功");
                }

                ///没有返回值,不是RPC过程
            }
            else
            {
                ///连接失败
                Console.WriteLine(ex.ToString());
            }
        }
Exemplo n.º 2
0
        public void TestLazyTcpSend()
        {
            const int Port = 54324;
            CancellationTokenSource cancellation = new CancellationTokenSource();

            PrepareEnvironment(cancellation);
            StartTcpListen(Port, cancellation);

            TcpRemote remote = new TcpRemote();

            remote.RpcCallbackPool.RpcTimeOutMilliseconds = 2000;
            remote.ConnectAsync(new IPEndPoint(IPAddress.Loopback, Port)).Wait();
            TestLazySendAsync(remote).Wait();
            cancellation.Cancel();
        }
Exemplo n.º 3
0
        private static async void Connect(int index)
        {
            IRemote remote = new TcpRemote();
            var     res    = await remote.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 54321));

            if (res == null)
            {
                Console.WriteLine($"Remote{index}:Success");
            }
            else
            {
                Console.WriteLine($"Remote:{res}");
            }

            //remote.SendAsync(new Packet1());
        }
Exemplo n.º 4
0
        private static async void NewRemote(int clientIndex)
        {
            IRemote remote = new TcpRemote()
            {
            };

            remote.OnReceiveCallback += receiver.TestReceive;
            var res = await remote.ConnectAsync(new IPEndPoint(IPAddress.Loopback, 54321));

            if (res == null)
            {
                Console.WriteLine($"Remote{clientIndex}:Success");
            }
            else
            {
                throw res;
            }

            await TestRpc(clientIndex, remote);

            Stopwatch look1 = new Stopwatch();
            var       msg   = new TestPacket1 {
                Value = 0
            };

            look1.Start();

            await Task.Run(() =>
            {
                for (int i = 0; i < MessageCount; i++)
                {
                    //Console.WriteLine($"Remote{clientIndex}:发送{nameof(Packet1)}=={i}");
                    msg.Value = i;
                    remote.SendAsync(msg);
                }
            });

            look1.Stop();
            Console.WriteLine($"Remote{clientIndex}: SendAsync{MessageCount}包 ------ 发送总时间: {look1.ElapsedMilliseconds}----- 平均每秒发送:{MessageCount * 1000 / (look1.ElapsedMilliseconds + 1)}");


            //Remote.BroadCastAsync(new Packet1 { Value = -99999 },remote);

            //var (Result, Excption) = await remote.SendAsync<Packet2>(new Packet1 { Value = 100 });
            //Console.WriteLine($"RPC接收消息{nameof(Packet2)}--{Result.Value}");
        }