vCloudTerminalBuilderV2.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using JsonRpcLite.Network;
  2. using JsonRpcLite.Rpc;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using Vinno.IUS.Common.Log;
  12. using Vinno.vCloud.Common.FIS.Helper;
  13. using WingInterfaceLibrary.DTO.ServerInfo;
  14. using WingInterfaceLibrary.Interface;
  15. using WingInterfaceLibrary.Request.FastestServer;
  16. namespace Vinno.vCloud.Common.FIS
  17. {
  18. public class vCloudTerminalBuilderV2
  19. {
  20. private static readonly string _getFasterServerUrl = "api2.flyinsono.com:80";
  21. /// <summary>
  22. /// connect default server before get terminal connection faster address info from address info list
  23. /// if get null ,can not connected to internet
  24. /// </summary>
  25. /// <returns></returns>
  26. public static ServerInfo GetFasterServer(string filePath = null)
  27. {
  28. try
  29. {
  30. string settingFilePath = AppDomain.CurrentDomain.BaseDirectory;
  31. if (string.IsNullOrEmpty(settingFilePath))
  32. {
  33. if (!string.IsNullOrEmpty(filePath))
  34. {
  35. settingFilePath = filePath;
  36. }
  37. else
  38. {
  39. return null;
  40. }
  41. }
  42. ServerInfo fasterServer = null;
  43. var speedResults = new ConcurrentDictionary<ServerInfo, int>();
  44. var serverFilePath = Path.Combine(settingFilePath, "FISServerSettingsV2.conf");
  45. var serverSettings = GetFasterServerList();
  46. if (serverSettings == null)
  47. {
  48. if (File.Exists(serverFilePath))
  49. {
  50. var stringValue = File.ReadAllText(serverFilePath);
  51. serverSettings = JsonConvert.DeserializeObject<DefaultServerSettings>(stringValue);
  52. }
  53. if (serverSettings == null)
  54. {
  55. serverSettings = new DefaultServerSettings
  56. {
  57. Servers = new List<ServerInfo>
  58. {
  59. new ServerInfo { Host = "api2.flyinsono.com", Port = 80 },
  60. },
  61. MasterServer = new ServerInfo { Host = "api2.flyinsono.com", Port = 80 }
  62. };
  63. }
  64. }
  65. Parallel.ForEach(serverSettings.Servers, serverInfo =>
  66. {
  67. var speed = GetConnectServerSpeed(serverInfo.ToString());
  68. speedResults.TryAdd(serverInfo, speed);
  69. });
  70. if (speedResults.Count == 0)
  71. {
  72. fasterServer = serverSettings.MasterServer;
  73. }
  74. else
  75. {
  76. foreach (var result in speedResults)
  77. {
  78. if (fasterServer == null)
  79. {
  80. fasterServer = result.Key;
  81. }
  82. else
  83. {
  84. if (speedResults[fasterServer] > result.Value)
  85. {
  86. fasterServer = result.Key;
  87. }
  88. }
  89. }
  90. }
  91. Logger.WriteLineInfo($"Get faster server is {fasterServer.Host}:{fasterServer.Port}");
  92. return fasterServer;
  93. }
  94. catch (Exception ex)
  95. {
  96. Logger.WriteLineError($"Get faster server error: {ex}");
  97. }
  98. return new ServerInfo { Host = "api2.flyinsono.com", Port = 80 };
  99. }
  100. private static DefaultServerSettings GetFasterServerList(bool isUseHttps = false)
  101. {
  102. try
  103. {
  104. string prefix;
  105. if (isUseHttps)
  106. {
  107. prefix = "https://";
  108. }
  109. else
  110. {
  111. prefix = "http://";
  112. }
  113. using (var client = new JsonRpcClient())
  114. {
  115. var clientEngine = new JsonRpcHttpClientEngine($"{prefix}{_getFasterServerUrl}");
  116. client.UseEngine(clientEngine);
  117. var vinnoServerService = client.CreateProxy<IVinnoServerService>();
  118. var queryServerInfoRequest = new QueryServerInfoRequest();
  119. List<ServerInfoDTO> result = JsonRpcHelper.GetServerInfoList(vinnoServerService, queryServerInfoRequest);
  120. if (result == null || result.Count == 0)
  121. {
  122. return null;
  123. }
  124. string host;
  125. int port = 80;
  126. var serverSetting = new DefaultServerSettings() { Servers = new List<ServerInfo>() };
  127. foreach (var server in result)
  128. {
  129. var serverArray = server.Host.Split(':');
  130. if (serverArray.Length == 1)
  131. {
  132. host = serverArray[0];
  133. }
  134. else if (serverArray.Length == 2)
  135. {
  136. host = serverArray[0];
  137. int.TryParse(serverArray[1], out port);
  138. }
  139. else
  140. {
  141. throw new Exception($"Server Info Format is invalid:{server.Host}");
  142. }
  143. var serverInfo = new ServerInfo() { Host = host, Port = port };
  144. if (serverSetting.MasterServer == null)
  145. {
  146. serverSetting.MasterServer = serverInfo;
  147. }
  148. serverSetting.Servers.Add(serverInfo);
  149. }
  150. return serverSetting;
  151. }
  152. }
  153. catch (Exception ex)
  154. {
  155. Logger.WriteLineError($"vCloudTerminalBuilder2 GetFasterServerList Error:{ex}");
  156. return null;
  157. }
  158. }
  159. /// <summary>
  160. /// Connect to server
  161. /// </summary>
  162. /// <param name="connectionInfo">The connection Info</param>
  163. /// <returns></returns>
  164. public IvCloudTerminalV2 Connect(ConnectionInfo connectionInfo)
  165. {
  166. if (string.IsNullOrEmpty(connectionInfo?.Account?.Name) || string.IsNullOrEmpty(connectionInfo?.Account?.Password))
  167. {
  168. return null;
  169. }
  170. var terminalV2 = new vCloudTerminalV2(connectionInfo);
  171. try
  172. {
  173. terminalV2.Connect();
  174. }
  175. catch (Exception e)
  176. {
  177. Logger.WriteLineError($"Termnial Login ex:{e}");
  178. }
  179. return terminalV2;
  180. }
  181. /// <summary>
  182. /// Register Account
  183. /// </summary>
  184. /// <param name="connectionInfo"></param>
  185. /// <returns></returns>
  186. public bool Register(ConnectionInfo connectionInfo)
  187. {
  188. return false;
  189. }
  190. /// <summary>
  191. /// test connet server speed
  192. /// </summary>
  193. /// <param name="serverUrl"></param>
  194. /// <returns></returns>
  195. private static int GetConnectServerSpeed(string serverUrl, bool isUseHttps = false)
  196. {
  197. try
  198. {
  199. var prefix = "http://";
  200. if (isUseHttps)
  201. {
  202. prefix = "https://";
  203. }
  204. using (var client = new JsonRpcClient())
  205. {
  206. var clientEngine = new JsonRpcHttpClientEngine($"{prefix}{serverUrl}");
  207. client.UseEngine(clientEngine);
  208. var vinnoServerService = client.CreateProxy<IVinnoServerService>(3000);
  209. var stopWatch = new Stopwatch();
  210. var successTimes = 0;
  211. var totalTime = 0;
  212. for (var i = 0; i < 3; i++)
  213. {
  214. if (i > 0)
  215. {
  216. ////TODO(FISJuly): removed?
  217. Thread.Sleep(1000);
  218. }
  219. stopWatch.Restart();
  220. try
  221. {
  222. var result = JsonRpcHelper.Echo(vinnoServerService);
  223. stopWatch.Stop();
  224. if (result != null && result.Code == 0)
  225. {
  226. Logger.WriteLineInfo($"Connect serverUrl:{serverUrl}, spend time:{stopWatch.ElapsedMilliseconds}ms");
  227. totalTime += (int)stopWatch.ElapsedMilliseconds;
  228. successTimes++;
  229. }
  230. }
  231. catch (Exception ex)
  232. {
  233. stopWatch.Stop();
  234. Logger.WriteLineError($"Connect serverUrl:{serverUrl} error, Exception:{ex}");
  235. continue;
  236. }
  237. }
  238. if (successTimes == 0)
  239. {
  240. Logger.WriteLineInfo($"Connect serverUrl:{serverUrl} fail, the success times is 0");
  241. return -1;
  242. }
  243. else
  244. {
  245. var averageValue = totalTime / successTimes;
  246. Logger.WriteLineInfo($"Connect serverUrl:{serverUrl},successTimes:{successTimes},spend time averageValue:{averageValue}ms");
  247. return averageValue;
  248. }
  249. }
  250. }
  251. catch (Exception e)
  252. {
  253. Logger.WriteLineError($"Connect Server:{serverUrl}, SpeedTest exception:{e}");
  254. return -1;
  255. }
  256. }
  257. }
  258. }