private static async Task SafeRpcSendAsync(IRemote remote) { TestPacket2 packet2 = new TestPacket2() { Value = new Random().Next() }; var res = await remote.SendAsyncSafeAwait <TestPacket2>(packet2); Assert.AreEqual(packet2.Value, res.Value); }
private static async Task SafeRpcSendAsyncTimeOut(IRemote remote) { TestPacket1 packet2 = new TestPacket1() { Value = new Random().Next() }; TaskCompletionSource <Exception> source = new TaskCompletionSource <Exception>(); var data = remote.SendAsyncSafeAwait <TestPacket2>(packet2, ex => { source.SetResult(ex); }); var(result, complete) = await source.Task.WaitAsync(3000); Assert.AreEqual(true, complete); Assert.AreEqual(typeof(TimeoutException), result.GetType()); }
private static async Task TestRpc(int clientIndex, IRemote remote) { var res2 = await remote.SendAsyncSafeAwait <TestPacket2>(new TestPacket2() { Value = clientIndex }, (ex) => { if (ex is TimeoutException timeout) { Console.WriteLine($"Rpc调用超时----------------------------------------- {clientIndex}"); } else { Console.WriteLine($"Rpc调用异常--------------------{ex}------------- {clientIndex}"); } }); Console.WriteLine($"Rpc调用返回----------------------------------------- {res2.Value}"); }
/// <summary> /// 连接服务器 /// </summary> private static async void ConnectAsync() { var ex = await remote.ConnectAsync(new IPEndPoint(IPAddress.IPv6Loopback, 54321)); remote.ReceiveCallbackMgr = new ReceiveCallbackMgr(); if (ex == null) { //没有异常,连接成功 Console.WriteLine("连接成功"); //创建一个登陆消息 var login = new Login2Gate() { Account = $"TestClient", Password = "******" }; //有返回值,这个是一个RPC过程,Exception在网络中传递 var resp = await remote.SendAsyncSafeAwait <Login2GateResult>(login); if (resp.Code == EnumRpcCallbackResultStatus.Success) { Console.WriteLine("登录成功"); } else { Console.WriteLine("登录失败"); } //没有返回值,不是RPC过程 } else { //连接失败 Console.WriteLine(ex.ToString()); } }