vCloudTerminalBuilderV2.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.Net.NetworkInformation;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using Vinno.IUS.Common.Log;
  13. using Vinno.vCloud.Common.FIS.Helper;
  14. using WingInterfaceLibrary.DTO.ServerInfo;
  15. using WingInterfaceLibrary.Interface;
  16. using WingInterfaceLibrary.Request.FastestServer;
  17. namespace Vinno.vCloud.Common.FIS
  18. {
  19. public class vCloudTerminalBuilderV2
  20. {
  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. DefaultServerSettings serverSettings = null;
  46. //serverSettings = GetFasterServerList(true);
  47. if (serverSettings == null)
  48. {
  49. if (File.Exists(serverFilePath))
  50. {
  51. var stringValue = File.ReadAllText(serverFilePath);
  52. serverSettings = JsonConvert.DeserializeObject<DefaultServerSettings>(stringValue);
  53. }
  54. if (serverSettings == null)
  55. {
  56. serverSettings = new DefaultServerSettings
  57. {
  58. Servers = new List<ServerInfo>
  59. {
  60. new ServerInfo { Host = "bj.flyinsono.com", Port = 443 },
  61. new ServerInfo { Host = "hk.flyinsono.com", Port = 443 },
  62. new ServerInfo { Host = "fra.flyinsono.com", Port = 443 },
  63. },
  64. MasterServer = new ServerInfo { Host = "hk.flyinsono.com", Port = 443 }
  65. };
  66. }
  67. }
  68. Parallel.ForEach(serverSettings.Servers, serverInfo =>
  69. {
  70. var speed = GetConnectServerSpeed(serverInfo.Host);
  71. if (speed != -1)
  72. {
  73. speedResults.TryAdd(serverInfo, speed);
  74. }
  75. });
  76. if (speedResults.Count == 0)
  77. {
  78. fasterServer = serverSettings.MasterServer;
  79. }
  80. else
  81. {
  82. foreach (var result in speedResults)
  83. {
  84. if (fasterServer == null)
  85. {
  86. fasterServer = result.Key;
  87. }
  88. else
  89. {
  90. if (speedResults[fasterServer] > result.Value)
  91. {
  92. fasterServer = result.Key;
  93. }
  94. }
  95. }
  96. }
  97. Logger.WriteLineInfo($"Get faster server is {fasterServer.Host}:{fasterServer.Port}");
  98. return fasterServer;
  99. }
  100. catch (Exception ex)
  101. {
  102. Logger.WriteLineError($"Get faster server error: {ex}");
  103. }
  104. return new ServerInfo { Host = "hk.flyinsono.com", Port = 443 };
  105. }
  106. private static DefaultServerSettings GetFasterServerList(bool isUseHttps = true)
  107. {
  108. try
  109. {
  110. string prefix;
  111. if (isUseHttps)
  112. {
  113. prefix = "https://";
  114. }
  115. else
  116. {
  117. prefix = "http://";
  118. }
  119. using (var client = new JsonRpcClient())
  120. {
  121. var clientEngine = new JsonRpcHttpClientEngine($"{prefix}hk.flyinsono.com:443");
  122. client.UseEngine(clientEngine);
  123. var vinnoServerService = client.CreateProxy<IVinnoServerService>(6000);
  124. var queryServerInfoRequest = new QueryServerInfoRequest();
  125. List<ServerInfoDTO> result = JsonRpcHelper.GetServerInfoList(vinnoServerService, queryServerInfoRequest);
  126. if (result == null || result.Count == 0)
  127. {
  128. return null;
  129. }
  130. string host;
  131. int port = 443;
  132. var serverSetting = new DefaultServerSettings() { Servers = new List<ServerInfo>() };
  133. foreach (var server in result)
  134. {
  135. var serverArray = server.Host.Split(':');
  136. if (serverArray.Length == 1)
  137. {
  138. host = serverArray[0];
  139. }
  140. else if (serverArray.Length == 2)
  141. {
  142. host = serverArray[0];
  143. int.TryParse(serverArray[1], out port);
  144. }
  145. else
  146. {
  147. throw new Exception($"Server Info Format is invalid:{server.Host}");
  148. }
  149. var serverInfo = new ServerInfo() { Host = host, Port = port };
  150. if (serverSetting.MasterServer == null)
  151. {
  152. serverSetting.MasterServer = serverInfo;
  153. }
  154. serverSetting.Servers.Add(serverInfo);
  155. }
  156. return serverSetting;
  157. }
  158. }
  159. catch (Exception ex)
  160. {
  161. Logger.WriteLineError($"vCloudTerminalBuilder2 GetFasterServerList Error:{ex}");
  162. return null;
  163. }
  164. }
  165. /// <summary>
  166. /// Connect to server
  167. /// </summary>
  168. /// <param name="connectionInfo">The connection Info</param>
  169. /// <returns></returns>
  170. public IvCloudTerminalV2 Connect(ConnectionInfo connectionInfo, bool isUseHttps, bool isUseOldTerminalSdk, string uniqueId, string terminalId)
  171. {
  172. if (string.IsNullOrEmpty(connectionInfo?.Account?.Name) || string.IsNullOrEmpty(connectionInfo?.Account?.Password))
  173. {
  174. return null;
  175. }
  176. var terminalV2 = new vCloudTerminalV2(connectionInfo, isUseHttps, isUseOldTerminalSdk);
  177. try
  178. {
  179. terminalV2.Connect(uniqueId, terminalId);
  180. }
  181. catch (Exception e)
  182. {
  183. Logger.WriteLineError($"Termnial Login ex:{e}");
  184. }
  185. return terminalV2;
  186. }
  187. /// <summary>
  188. /// Register Account
  189. /// </summary>
  190. /// <param name="connectionInfo"></param>
  191. /// <returns></returns>
  192. public bool Register(ConnectionInfo connectionInfo)
  193. {
  194. return false;
  195. }
  196. /// <summary>
  197. /// test connet server speed
  198. /// </summary>
  199. /// <param name="serverUrl"></param>
  200. /// <returns></returns>
  201. private static int GetConnectServerSpeed(string serverUrl)
  202. {
  203. try
  204. {
  205. using (var ping = new Ping())
  206. {
  207. var successTimes = 0;
  208. var totalTime = 0;
  209. for (var i = 0; i < 4; i++)//第一次为预热
  210. {
  211. try
  212. {
  213. var reply = ping.Send(serverUrl, 3000);
  214. if (reply.Status == IPStatus.Success && i > 0)
  215. {
  216. Logger.WriteLineInfo($"Connect serverUrl:{serverUrl}, spend time:{reply.RoundtripTime}ms");
  217. totalTime += (int)reply.RoundtripTime;
  218. successTimes++;
  219. }
  220. }
  221. catch (Exception ex)
  222. {
  223. Logger.WriteLineError($"Connect serverUrl:{serverUrl} error, Exception:{ex}");
  224. continue;
  225. }
  226. }
  227. if (successTimes == 0)
  228. {
  229. Logger.WriteLineInfo($"Connect serverUrl:{serverUrl} fail, the success times is 0");
  230. return -1;
  231. }
  232. else
  233. {
  234. var averageValue = totalTime / successTimes;
  235. Logger.WriteLineInfo($"Connect serverUrl:{serverUrl},successTimes:{successTimes},spend time averageValue:{averageValue}ms");
  236. return averageValue;
  237. }
  238. }
  239. }
  240. catch (Exception e)
  241. {
  242. Logger.WriteLineError($"Connect Server:{serverUrl}, SpeedTest exception:{e}");
  243. return -1;
  244. }
  245. }
  246. /// 多个服务器会报错同时用Jsonrpc会报错
  247. /// <summary>
  248. /// test connet server speed
  249. /// </summary>
  250. /// <param name="serverUrl"></param>
  251. /// <returns></returns>
  252. private static int OldGetConnectServerSpeed(string serverUrl, bool isUseHttps = true)
  253. {
  254. try
  255. {
  256. var prefix = "http://";
  257. if (isUseHttps)
  258. {
  259. prefix = "https://";
  260. }
  261. using (var client = new JsonRpcClient())
  262. {
  263. var clientEngine = new JsonRpcHttpClientEngine($"{prefix}{serverUrl}");
  264. client.UseEngine(clientEngine);
  265. var vinnoServerService = client.CreateProxy<IVinnoServerService>(5000);
  266. var stopWatch = new Stopwatch();
  267. var successTimes = 0;
  268. var totalTime = 0;
  269. for (var i = 0; i < 4; i++)//第一次为预热
  270. {
  271. if (i > 0)
  272. {
  273. Thread.Sleep(1000);
  274. }
  275. stopWatch.Restart();
  276. try
  277. {
  278. var result = JsonRpcHelper.Echo(vinnoServerService);
  279. stopWatch.Stop();
  280. if (result != null && result.Code == 0 && i > 0)
  281. {
  282. Logger.WriteLineInfo($"Connect serverUrl:{serverUrl}, spend time:{stopWatch.ElapsedMilliseconds}ms");
  283. totalTime += (int)stopWatch.ElapsedMilliseconds;
  284. successTimes++;
  285. }
  286. }
  287. catch (Exception ex)
  288. {
  289. stopWatch.Stop();
  290. Logger.WriteLineError($"Connect serverUrl:{serverUrl} error, Exception:{ex}");
  291. continue;
  292. }
  293. }
  294. if (successTimes == 0)
  295. {
  296. Logger.WriteLineInfo($"Connect serverUrl:{serverUrl} fail, the success times is 0");
  297. return -1;
  298. }
  299. else
  300. {
  301. var averageValue = totalTime / successTimes;
  302. Logger.WriteLineInfo($"Connect serverUrl:{serverUrl},successTimes:{successTimes},spend time averageValue:{averageValue}ms");
  303. return averageValue;
  304. }
  305. }
  306. }
  307. catch (Exception e)
  308. {
  309. Logger.WriteLineError($"Connect Server:{serverUrl}, SpeedTest exception:{e}");
  310. return -1;
  311. }
  312. }
  313. }
  314. }