/// <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()); } }
public void TestMethod1() { Login2Gate login2Gate = new Login2Gate() { Account = "test", Password = "******" }; { var b = MessagePack.MessagePackSerializer.Serialize(login2Gate); var res = MessagePack.MessagePackSerializer.Deserialize <Login2Gate>(b); Assert.AreEqual(login2Gate.Account, res.Account); Assert.AreEqual(login2Gate.Password, res.Password); } { using (MemoryStream ms = new MemoryStream(1024)) { Serializer.Serialize(ms, login2Gate); ms.Seek(0, SeekOrigin.Begin); var res = Serializer.Deserialize <Login2Gate>(ms); Assert.AreEqual(login2Gate.Account, res.Account); Assert.AreEqual(login2Gate.Password, res.Password); } Protobuf_netLUT.Regist(typeof(Login2Gate).Assembly); using (var buffer = BufferPool.Rent(1024)) { var length = MessageLUT.Serialize(login2Gate, buffer.Memory.Span); var res = MessageLUT.Deserialize(1003, buffer.Memory.Slice(0, length.length)) as Login2Gate; Assert.AreEqual(login2Gate.Account, res.Account); Assert.AreEqual(login2Gate.Password, res.Password); } } }
/// <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()); } }