RpcRoot.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using NTMiner.Rpc;
  2. using NTMiner.Rpc.Impl;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net.Http;
  7. namespace NTMiner {
  8. public static partial class RpcRoot {
  9. public static readonly IJsonRpcHelper JsonRpc = new JsonRpcHelper();
  10. static RpcRoot() {
  11. }
  12. public static string OfficialServerHost { get; private set; }
  13. public static int OfficialServerPort;
  14. public static string OfficialServerAddress = SetOfficialServerAddress("server.ntminer.com:3339");
  15. public static string SetOfficialServerAddress(string address) {
  16. if (!address.Contains(":")) {
  17. address = address + ":" + 3339;
  18. }
  19. OfficialServerAddress = address;
  20. string[] parts = address.Split(':');
  21. if (parts.Length != 2 || !int.TryParse(parts[1], out int port)) {
  22. throw new InvalidProgramException();
  23. }
  24. OfficialServerHost = parts[0];
  25. OfficialServerPort = port;
  26. return address;
  27. }
  28. /// <summary>
  29. /// 给定一个类型,返回基于命名约定的控制器名。如果给定的类型名不以Consoller为后缀则引发
  30. /// InvalidProgramException异常,如果给定的类型是接口类型但不以I开头同样会异常。
  31. /// </summary>
  32. /// <typeparam name="T"></typeparam>
  33. /// <returns></returns>
  34. public static string GetControllerName<T>() {
  35. Type t = typeof(T);
  36. string name = t.Name;
  37. if (t.IsGenericType) {
  38. name = name.Substring(0, name.IndexOf('`'));
  39. }
  40. if (!name.EndsWith("Controller")) {
  41. throw new InvalidProgramException("控制器类型名需要以Controller为后缀");
  42. }
  43. int startIndex = 0;
  44. int length = name.Length - "Controller".Length;
  45. if (t.IsInterface) {
  46. if (name[0] != 'I') {
  47. throw new InvalidProgramException("接口类型名需要以I为开头");
  48. }
  49. startIndex = 1;
  50. length -= 1;
  51. }
  52. return name.Substring(startIndex, length);
  53. }
  54. public static string GetUrl(string host, int port, string controller, string action, Dictionary<string, string> query) {
  55. string queryString = string.Empty;
  56. if (query != null && query.Count != 0) {
  57. queryString = "?" + string.Join("&", query.Select(a => a.Key + "=" + a.Value));
  58. }
  59. return $"http://{host}:{port.ToString()}/api/{controller}/{action}{queryString}";
  60. }
  61. public static HttpClient CreateHttpClient() {
  62. return new HttpClient {
  63. Timeout = TimeSpan.FromSeconds(10)
  64. };
  65. }
  66. public static void SetTimeout(this HttpClient client, int? timeountMilliseconds) {
  67. if (!timeountMilliseconds.HasValue || timeountMilliseconds.Value < 0) {
  68. return;
  69. }
  70. if (timeountMilliseconds != 0) {
  71. if (timeountMilliseconds < 100) {
  72. timeountMilliseconds *= 1000;
  73. }
  74. client.Timeout = TimeSpan.FromMilliseconds(timeountMilliseconds.Value);
  75. }
  76. }
  77. }
  78. }