直接调用
直接调用,则是不使用任何代理,直接Call RPC,使用比较简单。
【TcpTouchRpcClient】
TcpTouchRpcClient client = new RRQMConfig().SetRemoteIPHost("127.0.0.1:7789").SetVerifyToken("TouchRpc").BuildWithTcpTouchRpcClient();//直接调用时,第一个参数为服务名+方法名(必须全小写)//第二个参数为调用配置参数,可设置调用超时时间,取消调用等功能。//后续参数为调用参数。bool result = client.Invoke<bool>("{namespace}.myrpcserver.login", InvokeOption.WaitInvoke, textBox1.Text,textBox2.Text);
【UdpTouchRpc】
UdpTouchRpc client = new UdpTouchRpc();client.Setup(new RRQMConfig().SetBindIPHost(7794).SetRemoteIPHost("127.0.0.1:7791"));//设置目标地址。client.Start();bool result = client.Invoke<bool>("{namespace}.myrpcserver.login", InvokeOption.WaitInvoke, textBox1.Text, textBox2.Text);
【HttpTouchRpcClient】
HttpTouchRpcClient client = new RRQMConfig().SetRemoteIPHost("127.0.0.1:7790").SetVerifyToken("TouchRpc").BuildWithHttpTouchRpcClient();//直接调用时,第一个参数为服务名+方法名(必须全小写)//第二个参数为调用配置参数,可设置调用超时时间,取消调用等功能。//后续参数为调用参数。bool result = client.Invoke<bool>("{namespace}.myrpcserver.login", InvokeOption.WaitInvoke, textBox1.Text, textBox2.Text);
代理调用RPC
代理调用的便捷在于,客户端不用再知道哪些服务可调,也不用再纠结调用的参数类型正不正确,因为这些,代理工具都会替你做好。
如何生成获取代理文件?
调用
当代理被客户端获取以后,客户端项目中会多出一个RRQMProxy的文件(或者如果是服务器生成的本地代理,则需要复制到客户端项目中),在该文件中,则包含了所有的代理方法和代理类,可直接由代理类发起调用。
屏幕截图.png
【生成的代理】
using System;using RRQMCore;using RRQMSocket;using RRQMSocket.RPC;using RRQMSocket.RPC.TouchRpc;using System.Collections.Generic;using System.Diagnostics;using System.Text;using System.Threading.Tasks;namespace RRQMProxy{public interface IMyRpcServer:IRemoteServer{///<summary>///登录///</summary>/// <exception cref="System.TimeoutException">调用超时</exception>/// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>/// <exception cref="RRQMCore.RRQMException">其他异常</exception>System.Boolean Login(System.String account,System.String password,IInvokeOption invokeOption = default);///<summary>///登录///</summary>/// <exception cref="System.TimeoutException">调用超时</exception>/// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>/// <exception cref="RRQMCore.RRQMException">其他异常</exception>Task<System.Boolean> LoginAsync(System.String account,System.String password,IInvokeOption invokeOption = default);///<summary>///性能测试///</summary>/// <exception cref="System.TimeoutException">调用超时</exception>/// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>/// <exception cref="RRQMCore.RRQMException">其他异常</exception>System.Int32 Performance(System.Int32 a,IInvokeOption invokeOption = default);///<summary>///性能测试///</summary>/// <exception cref="System.TimeoutException">调用超时</exception>/// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>/// <exception cref="RRQMCore.RRQMException">其他异常</exception>Task<System.Int32> PerformanceAsync(System.Int32 a,IInvokeOption invokeOption = default);}public class MyRpcServer :IMyRpcServer{public MyRpcServer(IRpcClient client){this.Client=client;}public IRpcClient Client{get;private set; }///<summary>///登录///</summary>/// <exception cref="System.TimeoutException">调用超时</exception>/// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>/// <exception cref="RRQMCore.RRQMException">其他异常</exception>public System.Boolean Login(System.String account,System.String password,IInvokeOption invokeOption = default){if(Client==null){throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");}object[] parameters = new object[]{account,password};System.Boolean returnData=Client.Invoke<System.Boolean>("touchrpcserverapp.myrpcserver.login",invokeOption, parameters);return returnData;}///<summary>///登录///</summary>public Task<System.Boolean> LoginAsync(System.String account,System.String password,IInvokeOption invokeOption = default){if(Client==null){throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");}object[] parameters = new object[]{account,password};return Client.InvokeAsync<System.Boolean>("touchrpcserverapp.myrpcserver.login",invokeOption, parameters);}///<summary>///性能测试///</summary>/// <exception cref="System.TimeoutException">调用超时</exception>/// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>/// <exception cref="RRQMCore.RRQMException">其他异常</exception>public System.Int32 Performance(System.Int32 a,IInvokeOption invokeOption = default){if(Client==null){throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");}object[] parameters = new object[]{a};System.Int32 returnData=Client.Invoke<System.Int32>("touchrpcserverapp.myrpcserver.performance",invokeOption, parameters);return returnData;}///<summary>///性能测试///</summary>public Task<System.Int32> PerformanceAsync(System.Int32 a,IInvokeOption invokeOption = default){if(Client==null){throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");}object[] parameters = new object[]{a};return Client.InvokeAsync<System.Int32>("touchrpcserverapp.myrpcserver.performance",invokeOption, parameters);}}public static class MyRpcServerExtensions{///<summary>///登录///</summary>/// <exception cref="System.TimeoutException">调用超时</exception>/// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>/// <exception cref="RRQMCore.RRQMException">其他异常</exception>public static System.Boolean Login<TClient>(this TClient client,System.String account,System.String password,IInvokeOption invokeOption = default) where TClient:RRQMSocket.RPC.IRpcClient{if (client.TryCanInvoke?.Invoke(client)==false){throw new RpcException("Rpc无法执行。");}object[] parameters = new object[]{account,password};System.Boolean returnData=client.Invoke<System.Boolean>("touchrpcserverapp.myrpcserver.login",invokeOption, parameters);return returnData;}///<summary>///登录///</summary>public static Task<System.Boolean> LoginAsync<TClient>(this TClient client,System.String account,System.String password,IInvokeOption invokeOption = default) where TClient:RRQMSocket.RPC.IRpcClient{if (client.TryCanInvoke?.Invoke(client)==false){throw new RpcException("Rpc无法执行。");}object[] parameters = new object[]{account,password};return client.InvokeAsync<System.Boolean>("touchrpcserverapp.myrpcserver.login",invokeOption, parameters);}///<summary>///性能测试///</summary>/// <exception cref="System.TimeoutException">调用超时</exception>/// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>/// <exception cref="RRQMCore.RRQMException">其他异常</exception>public static System.Int32 Performance<TClient>(this TClient client,System.Int32 a,IInvokeOption invokeOption = default) where TClient:RRQMSocket.RPC.IRpcClient{if (client.TryCanInvoke?.Invoke(client)==false){throw new RpcException("Rpc无法执行。");}object[] parameters = new object[]{a};System.Int32 returnData=client.Invoke<System.Int32>("touchrpcserverapp.myrpcserver.performance",invokeOption, parameters);return returnData;}///<summary>///性能测试///</summary>public static Task<System.Int32> PerformanceAsync<TClient>(this TClient client,System.Int32 a,IInvokeOption invokeOption = default) where TClient:RRQMSocket.RPC.IRpcClient{if (client.TryCanInvoke?.Invoke(client)==false){throw new RpcException("Rpc无法执行。");}object[] parameters = new object[]{a};return client.InvokeAsync<System.Int32>("touchrpcserverapp.myrpcserver.performance",invokeOption, parameters);}}}
使用代理扩展直接调用。
【TcpTouchRpcClient】
TcpTouchRpcClient client = new TcpTouchRpcClient();client.Setup("127.0.0.1:7789");client.Connect("TouchRpc");//扩展调用时,首先要保证本地已有代理文件,然后调用和和本地调用一样。只是会多一个调用配置参数。bool result = client.Login(textBox1.Text, textBox2.Text, InvokeOption.WaitInvoke);
【UdpTouchRpc】
UdpTouchRpc client = new UdpTouchRpc();client.Setup(new RRQMConfig().SetBindIPHost(7794).SetRemoteIPHost("127.0.0.1:7791"));//设置目标地址。client.Start();MyRpcServer myRpcServer = new MyRpcServer(client);//MyRpcServer类是由代码工具生成的类。//代理调用时,基本和本地调用一样。只是会多一个调用配置参数。bool result = myRpcServer.Login(textBox1.Text, textBox2.Text, InvokeOption.WaitInvoke);
【HttpTouchRpcClient】
HttpTouchRpcClient client = new HttpTouchRpcClient();client.Setup("127.0.0.1:7790");client.Connect("TouchRpc");//扩展调用时,首先要保证本地已有代理文件,然后调用和和本地调用一样。只是会多一个调用配置参数。bool result = client.Login(textBox1.Text, textBox2.Text, InvokeOption.WaitInvoke);
调用建议
在实际使用时,大家很注意的一个问题,就是断线重连的问题,但实际上,RPC本身不应该出现状态,所以,断线问题最好也是无状态的。
例如:下列示例,每次请求时,检测在线状态,如果不在线,则重新初始化。
本示例只做建议。如果确实需要断线重连,则订阅相应事件或调用UseReconnection即可。
public static class MainClient{private static TcpRpcClient client;public static TcpRpcClient Client => client;static MyRpcServer mainServer;/// <summary>/// 检验client是否在线/// 不在线,则重新初始化。/// </summary>/// <returns></returns>public static bool CheckOnline(){try{if (client == null){client = new TcpRpcClient ();mainServer = new MyRpcServer (client);client.Setup("127.0.0.1:7789");try{client.Connect("123RPC");client.DiscoveryService("RPC");}catch{return false;}}if (!client.Online){client.Dispose();client = null;mainServer = null;return CheckOnline();}return true;}catch{return false;}}public static Task<string> TestOne(int i){return Task.Run(()=>{try{if (CheckOnline()){return mainServer.TestOne(i);}else{return string.Empty;}}catch (Exception ex){return string.Empty;}});}}
