直接调用

直接调用,则是不使用任何代理,直接Call RPC,使用比较简单。

【TcpTouchRpcClient】

  1. TcpTouchRpcClient client = new RRQMConfig()
  2. .SetRemoteIPHost("127.0.0.1:7789")
  3. .SetVerifyToken("TouchRpc")
  4. .BuildWithTcpTouchRpcClient();
  5. //直接调用时,第一个参数为服务名+方法名(必须全小写)
  6. //第二个参数为调用配置参数,可设置调用超时时间,取消调用等功能。
  7. //后续参数为调用参数。
  8. bool result = client.Invoke<bool>("{namespace}.myrpcserver.login", InvokeOption.WaitInvoke, textBox1.Text,textBox2.Text);

【UdpTouchRpc】

  1. UdpTouchRpc client = new UdpTouchRpc();
  2. client.Setup(new RRQMConfig()
  3. .SetBindIPHost(7794)
  4. .SetRemoteIPHost("127.0.0.1:7791"));//设置目标地址。
  5. client.Start();
  6. bool result = client.Invoke<bool>("{namespace}.myrpcserver.login", InvokeOption.WaitInvoke, textBox1.Text, textBox2.Text);

【HttpTouchRpcClient】

  1. HttpTouchRpcClient client = new RRQMConfig()
  2. .SetRemoteIPHost("127.0.0.1:7790")
  3. .SetVerifyToken("TouchRpc")
  4. .BuildWithHttpTouchRpcClient();
  5. //直接调用时,第一个参数为服务名+方法名(必须全小写)
  6. //第二个参数为调用配置参数,可设置调用超时时间,取消调用等功能。
  7. //后续参数为调用参数。
  8. bool result = client.Invoke<bool>("{namespace}.myrpcserver.login", InvokeOption.WaitInvoke, textBox1.Text, textBox2.Text);

代理调用RPC

代理调用的便捷在于,客户端不用再知道哪些服务可调,也不用再纠结调用的参数类型正不正确,因为这些,代理工具都会替你做好。

如何生成获取代理文件?

获取代理文件详情

调用

当代理被客户端获取以后,客户端项目中会多出一个RRQMProxy的文件(或者如果是服务器生成的本地代理,则需要复制到客户端项目中),在该文件中,则包含了所有的代理方法代理类,可直接由代理类发起调用。
调用服务 - 图1屏幕截图.png

【生成的代理】

  1. using System;
  2. using RRQMCore;
  3. using RRQMSocket;
  4. using RRQMSocket.RPC;
  5. using RRQMSocket.RPC.TouchRpc;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace RRQMProxy
  11. {
  12. public interface IMyRpcServer:IRemoteServer
  13. {
  14. ///<summary>
  15. ///登录
  16. ///</summary>
  17. /// <exception cref="System.TimeoutException">调用超时</exception>
  18. /// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>
  19. /// <exception cref="RRQMCore.RRQMException">其他异常</exception>
  20. System.Boolean Login(System.String account,System.String password,IInvokeOption invokeOption = default);
  21. ///<summary>
  22. ///登录
  23. ///</summary>
  24. /// <exception cref="System.TimeoutException">调用超时</exception>
  25. /// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>
  26. /// <exception cref="RRQMCore.RRQMException">其他异常</exception>
  27. Task<System.Boolean> LoginAsync(System.String account,System.String password,IInvokeOption invokeOption = default);
  28. ///<summary>
  29. ///性能测试
  30. ///</summary>
  31. /// <exception cref="System.TimeoutException">调用超时</exception>
  32. /// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>
  33. /// <exception cref="RRQMCore.RRQMException">其他异常</exception>
  34. System.Int32 Performance(System.Int32 a,IInvokeOption invokeOption = default);
  35. ///<summary>
  36. ///性能测试
  37. ///</summary>
  38. /// <exception cref="System.TimeoutException">调用超时</exception>
  39. /// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>
  40. /// <exception cref="RRQMCore.RRQMException">其他异常</exception>
  41. Task<System.Int32> PerformanceAsync(System.Int32 a,IInvokeOption invokeOption = default);
  42. }
  43. public class MyRpcServer :IMyRpcServer
  44. {
  45. public MyRpcServer(IRpcClient client)
  46. {
  47. this.Client=client;
  48. }
  49. public IRpcClient Client{get;private set; }
  50. ///<summary>
  51. ///登录
  52. ///</summary>
  53. /// <exception cref="System.TimeoutException">调用超时</exception>
  54. /// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>
  55. /// <exception cref="RRQMCore.RRQMException">其他异常</exception>
  56. public System.Boolean Login(System.String account,System.String password,IInvokeOption invokeOption = default)
  57. {
  58. if(Client==null)
  59. {
  60. throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");
  61. }
  62. object[] parameters = new object[]{account,password};
  63. System.Boolean returnData=Client.Invoke<System.Boolean>("touchrpcserverapp.myrpcserver.login",invokeOption, parameters);
  64. return returnData;
  65. }
  66. ///<summary>
  67. ///登录
  68. ///</summary>
  69. public Task<System.Boolean> LoginAsync(System.String account,System.String password,IInvokeOption invokeOption = default)
  70. {
  71. if(Client==null)
  72. {
  73. throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");
  74. }
  75. object[] parameters = new object[]{account,password};
  76. return Client.InvokeAsync<System.Boolean>("touchrpcserverapp.myrpcserver.login",invokeOption, parameters);
  77. }
  78. ///<summary>
  79. ///性能测试
  80. ///</summary>
  81. /// <exception cref="System.TimeoutException">调用超时</exception>
  82. /// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>
  83. /// <exception cref="RRQMCore.RRQMException">其他异常</exception>
  84. public System.Int32 Performance(System.Int32 a,IInvokeOption invokeOption = default)
  85. {
  86. if(Client==null)
  87. {
  88. throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");
  89. }
  90. object[] parameters = new object[]{a};
  91. System.Int32 returnData=Client.Invoke<System.Int32>("touchrpcserverapp.myrpcserver.performance",invokeOption, parameters);
  92. return returnData;
  93. }
  94. ///<summary>
  95. ///性能测试
  96. ///</summary>
  97. public Task<System.Int32> PerformanceAsync(System.Int32 a,IInvokeOption invokeOption = default)
  98. {
  99. if(Client==null)
  100. {
  101. throw new RpcException("IRpcClient为空,请先初始化或者进行赋值");
  102. }
  103. object[] parameters = new object[]{a};
  104. return Client.InvokeAsync<System.Int32>("touchrpcserverapp.myrpcserver.performance",invokeOption, parameters);
  105. }
  106. }
  107. public static class MyRpcServerExtensions
  108. {
  109. ///<summary>
  110. ///登录
  111. ///</summary>
  112. /// <exception cref="System.TimeoutException">调用超时</exception>
  113. /// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>
  114. /// <exception cref="RRQMCore.RRQMException">其他异常</exception>
  115. public static System.Boolean Login<TClient>(this TClient client,System.String account,System.String password,IInvokeOption invokeOption = default) where TClient:
  116. RRQMSocket.RPC.IRpcClient{
  117. if (client.TryCanInvoke?.Invoke(client)==false)
  118. {
  119. throw new RpcException("Rpc无法执行。");
  120. }
  121. object[] parameters = new object[]{account,password};
  122. System.Boolean returnData=client.Invoke<System.Boolean>("touchrpcserverapp.myrpcserver.login",invokeOption, parameters);
  123. return returnData;
  124. }
  125. ///<summary>
  126. ///登录
  127. ///</summary>
  128. public static Task<System.Boolean> LoginAsync<TClient>(this TClient client,System.String account,System.String password,IInvokeOption invokeOption = default) where TClient:
  129. RRQMSocket.RPC.IRpcClient{
  130. if (client.TryCanInvoke?.Invoke(client)==false)
  131. {
  132. throw new RpcException("Rpc无法执行。");
  133. }
  134. object[] parameters = new object[]{account,password};
  135. return client.InvokeAsync<System.Boolean>("touchrpcserverapp.myrpcserver.login",invokeOption, parameters);
  136. }
  137. ///<summary>
  138. ///性能测试
  139. ///</summary>
  140. /// <exception cref="System.TimeoutException">调用超时</exception>
  141. /// <exception cref="RRQMSocket.RPC.RRQMRpcInvokeException">Rpc调用异常</exception>
  142. /// <exception cref="RRQMCore.RRQMException">其他异常</exception>
  143. public static System.Int32 Performance<TClient>(this TClient client,System.Int32 a,IInvokeOption invokeOption = default) where TClient:
  144. RRQMSocket.RPC.IRpcClient{
  145. if (client.TryCanInvoke?.Invoke(client)==false)
  146. {
  147. throw new RpcException("Rpc无法执行。");
  148. }
  149. object[] parameters = new object[]{a};
  150. System.Int32 returnData=client.Invoke<System.Int32>("touchrpcserverapp.myrpcserver.performance",invokeOption, parameters);
  151. return returnData;
  152. }
  153. ///<summary>
  154. ///性能测试
  155. ///</summary>
  156. public static Task<System.Int32> PerformanceAsync<TClient>(this TClient client,System.Int32 a,IInvokeOption invokeOption = default) where TClient:
  157. RRQMSocket.RPC.IRpcClient{
  158. if (client.TryCanInvoke?.Invoke(client)==false)
  159. {
  160. throw new RpcException("Rpc无法执行。");
  161. }
  162. object[] parameters = new object[]{a};
  163. return client.InvokeAsync<System.Int32>("touchrpcserverapp.myrpcserver.performance",invokeOption, parameters);
  164. }
  165. }
  166. }

使用代理扩展直接调用。

【TcpTouchRpcClient】

  1. TcpTouchRpcClient client = new TcpTouchRpcClient();
  2. client.Setup("127.0.0.1:7789");
  3. client.Connect("TouchRpc");
  4. //扩展调用时,首先要保证本地已有代理文件,然后调用和和本地调用一样。只是会多一个调用配置参数。
  5. bool result = client.Login(textBox1.Text, textBox2.Text, InvokeOption.WaitInvoke);

【UdpTouchRpc】

  1. UdpTouchRpc client = new UdpTouchRpc();
  2. client.Setup(new RRQMConfig()
  3. .SetBindIPHost(7794)
  4. .SetRemoteIPHost("127.0.0.1:7791"));//设置目标地址。
  5. client.Start();
  6. MyRpcServer myRpcServer = new MyRpcServer(client);//MyRpcServer类是由代码工具生成的类。
  7. //代理调用时,基本和本地调用一样。只是会多一个调用配置参数。
  8. bool result = myRpcServer.Login(textBox1.Text, textBox2.Text, InvokeOption.WaitInvoke);

【HttpTouchRpcClient】

  1. HttpTouchRpcClient client = new HttpTouchRpcClient();
  2. client.Setup("127.0.0.1:7790");
  3. client.Connect("TouchRpc");
  4. //扩展调用时,首先要保证本地已有代理文件,然后调用和和本地调用一样。只是会多一个调用配置参数。
  5. bool result = client.Login(textBox1.Text, textBox2.Text, InvokeOption.WaitInvoke);

调用建议

在实际使用时,大家很注意的一个问题,就是断线重连的问题,但实际上,RPC本身不应该出现状态,所以,断线问题最好也是无状态的。
例如:下列示例,每次请求时,检测在线状态,如果不在线,则重新初始化。
本示例只做建议。如果确实需要断线重连,则订阅相应事件或调用UseReconnection即可。

  1. public static class MainClient
  2. {
  3. private static TcpRpcClient client;
  4. public static TcpRpcClient Client => client;
  5. static MyRpcServer mainServer;
  6. /// <summary>
  7. /// 检验client是否在线
  8. /// 不在线,则重新初始化。
  9. /// </summary>
  10. /// <returns></returns>
  11. public static bool CheckOnline()
  12. {
  13. try
  14. {
  15. if (client == null)
  16. {
  17. client = new TcpRpcClient ();
  18. mainServer = new MyRpcServer (client);
  19. client.Setup("127.0.0.1:7789");
  20. try
  21. {
  22. client.Connect("123RPC");
  23. client.DiscoveryService("RPC");
  24. }
  25. catch
  26. {
  27. return false;
  28. }
  29. }
  30. if (!client.Online)
  31. {
  32. client.Dispose();
  33. client = null;
  34. mainServer = null;
  35. return CheckOnline();
  36. }
  37. return true;
  38. }
  39. catch
  40. {
  41. return false;
  42. }
  43. }
  44. public static Task<string> TestOne(int i)
  45. {
  46. return Task.Run(()=>
  47. {
  48. try
  49. {
  50. if (CheckOnline())
  51. {
  52. return mainServer.TestOne(i);
  53. }
  54. else
  55. {
  56. return string.Empty;
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. return string.Empty;
  62. }
  63. });
  64. }
  65. }