123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- using JsonRpcLite.Network;
- using JsonRpcLite.Rpc;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Net.NetworkInformation;
- using System.Threading;
- using System.Threading.Tasks;
- using Vinno.IUS.Common.Log;
- using Vinno.vCloud.Common.FIS.Helper;
- using WingInterfaceLibrary.DTO.ServerInfo;
- using WingInterfaceLibrary.Interface;
- using WingInterfaceLibrary.Request.FastestServer;
- namespace Vinno.vCloud.Common.FIS
- {
- public class vCloudTerminalBuilderV2
- {
- /// <summary>
- /// connect default server before get terminal connection faster address info from address info list
- /// if get null ,can not connected to internet
- /// </summary>
- /// <returns></returns>
- public static ServerInfo GetFasterServer(string filePath = null)
- {
- try
- {
- string settingFilePath = AppDomain.CurrentDomain.BaseDirectory;
- if (string.IsNullOrEmpty(settingFilePath))
- {
- if (!string.IsNullOrEmpty(filePath))
- {
- settingFilePath = filePath;
- }
- else
- {
- return null;
- }
- }
- ServerInfo fasterServer = null;
- var speedResults = new ConcurrentDictionary<ServerInfo, int>();
- var serverFilePath = Path.Combine(settingFilePath, "FISServerSettingsV2.conf");
- DefaultServerSettings serverSettings = null;
- //serverSettings = GetFasterServerList(true);
- if (serverSettings == null)
- {
- if (File.Exists(serverFilePath))
- {
- var stringValue = File.ReadAllText(serverFilePath);
- serverSettings = JsonConvert.DeserializeObject<DefaultServerSettings>(stringValue);
- }
- if (serverSettings == null)
- {
- serverSettings = new DefaultServerSettings
- {
- Servers = new List<ServerInfo>
- {
- new ServerInfo { Host = "bj.flyinsono.com", Port = 443 },
- new ServerInfo { Host = "hk.flyinsono.com", Port = 443 },
- new ServerInfo { Host = "fra.flyinsono.com", Port = 443 },
- },
- MasterServer = new ServerInfo { Host = "hk.flyinsono.com", Port = 443 }
- };
- }
- }
- Parallel.ForEach(serverSettings.Servers, serverInfo =>
- {
- var speed = GetConnectServerSpeed(serverInfo.Host);
- if (speed != -1)
- {
- speedResults.TryAdd(serverInfo, speed);
- }
- });
- if (speedResults.Count == 0)
- {
- fasterServer = serverSettings.MasterServer;
- }
- else
- {
- foreach (var result in speedResults)
- {
- if (fasterServer == null)
- {
- fasterServer = result.Key;
- }
- else
- {
- if (speedResults[fasterServer] > result.Value)
- {
- fasterServer = result.Key;
- }
- }
- }
- }
- Logger.WriteLineInfo($"Get faster server is {fasterServer.Host}:{fasterServer.Port}");
- return fasterServer;
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Get faster server error: {ex}");
- }
- return new ServerInfo { Host = "hk.flyinsono.com", Port = 443 };
- }
- private static DefaultServerSettings GetFasterServerList(bool isUseHttps = true)
- {
- try
- {
- string prefix;
- if (isUseHttps)
- {
- prefix = "https://";
- }
- else
- {
- prefix = "http://";
- }
- using (var client = new JsonRpcClient())
- {
- var clientEngine = new JsonRpcHttpClientEngine($"{prefix}hk.flyinsono.com:443");
- client.UseEngine(clientEngine);
- var vinnoServerService = client.CreateProxy<IVinnoServerService>(6000);
- var queryServerInfoRequest = new QueryServerInfoRequest();
- List<ServerInfoDTO> result = JsonRpcHelper.GetServerInfoList(vinnoServerService, queryServerInfoRequest);
- if (result == null || result.Count == 0)
- {
- return null;
- }
- string host;
- int port = 443;
- var serverSetting = new DefaultServerSettings() { Servers = new List<ServerInfo>() };
- foreach (var server in result)
- {
- var serverArray = server.Host.Split(':');
- if (serverArray.Length == 1)
- {
- host = serverArray[0];
- }
- else if (serverArray.Length == 2)
- {
- host = serverArray[0];
- int.TryParse(serverArray[1], out port);
- }
- else
- {
- throw new Exception($"Server Info Format is invalid:{server.Host}");
- }
- var serverInfo = new ServerInfo() { Host = host, Port = port };
- if (serverSetting.MasterServer == null)
- {
- serverSetting.MasterServer = serverInfo;
- }
- serverSetting.Servers.Add(serverInfo);
- }
- return serverSetting;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"vCloudTerminalBuilder2 GetFasterServerList Error:{ex}");
- return null;
- }
- }
- /// <summary>
- /// Connect to server
- /// </summary>
- /// <param name="connectionInfo">The connection Info</param>
- /// <returns></returns>
- public IvCloudTerminalV2 Connect(ConnectionInfo connectionInfo, bool isUseHttps, bool isUseOldTerminalSdk, string uniqueId, string terminalId)
- {
- if (string.IsNullOrEmpty(connectionInfo?.Account?.Name) || string.IsNullOrEmpty(connectionInfo?.Account?.Password))
- {
- return null;
- }
- var terminalV2 = new vCloudTerminalV2(connectionInfo, isUseHttps, isUseOldTerminalSdk);
- try
- {
- terminalV2.Connect(uniqueId, terminalId);
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Termnial Login ex:{e}");
- }
- return terminalV2;
- }
- /// <summary>
- /// Register Account
- /// </summary>
- /// <param name="connectionInfo"></param>
- /// <returns></returns>
- public bool Register(ConnectionInfo connectionInfo)
- {
- return false;
- }
- /// <summary>
- /// test connet server speed
- /// </summary>
- /// <param name="serverUrl"></param>
- /// <returns></returns>
- private static int GetConnectServerSpeed(string serverUrl)
- {
- try
- {
- using (var ping = new Ping())
- {
- var successTimes = 0;
- var totalTime = 0;
- for (var i = 0; i < 4; i++)//第一次为预热
- {
- try
- {
- var reply = ping.Send(serverUrl, 3000);
- if (reply.Status == IPStatus.Success && i > 0)
- {
- Logger.WriteLineInfo($"Connect serverUrl:{serverUrl}, spend time:{reply.RoundtripTime}ms");
- totalTime += (int)reply.RoundtripTime;
- successTimes++;
- }
- }
- catch (Exception ex)
- {
- Logger.WriteLineError($"Connect serverUrl:{serverUrl} error, Exception:{ex}");
- continue;
- }
- }
- if (successTimes == 0)
- {
- Logger.WriteLineInfo($"Connect serverUrl:{serverUrl} fail, the success times is 0");
- return -1;
- }
- else
- {
- var averageValue = totalTime / successTimes;
- Logger.WriteLineInfo($"Connect serverUrl:{serverUrl},successTimes:{successTimes},spend time averageValue:{averageValue}ms");
- return averageValue;
- }
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Connect Server:{serverUrl}, SpeedTest exception:{e}");
- return -1;
- }
- }
- /// 多个服务器会报错同时用Jsonrpc会报错
- /// <summary>
- /// test connet server speed
- /// </summary>
- /// <param name="serverUrl"></param>
- /// <returns></returns>
- private static int OldGetConnectServerSpeed(string serverUrl, bool isUseHttps = true)
- {
- try
- {
- var prefix = "http://";
- if (isUseHttps)
- {
- prefix = "https://";
- }
- using (var client = new JsonRpcClient())
- {
- var clientEngine = new JsonRpcHttpClientEngine($"{prefix}{serverUrl}");
- client.UseEngine(clientEngine);
- var vinnoServerService = client.CreateProxy<IVinnoServerService>(5000);
- var stopWatch = new Stopwatch();
- var successTimes = 0;
- var totalTime = 0;
- for (var i = 0; i < 4; i++)//第一次为预热
- {
- if (i > 0)
- {
- Thread.Sleep(1000);
- }
- stopWatch.Restart();
- try
- {
- var result = JsonRpcHelper.Echo(vinnoServerService);
- stopWatch.Stop();
- if (result != null && result.Code == 0 && i > 0)
- {
- Logger.WriteLineInfo($"Connect serverUrl:{serverUrl}, spend time:{stopWatch.ElapsedMilliseconds}ms");
- totalTime += (int)stopWatch.ElapsedMilliseconds;
- successTimes++;
- }
- }
- catch (Exception ex)
- {
- stopWatch.Stop();
- Logger.WriteLineError($"Connect serverUrl:{serverUrl} error, Exception:{ex}");
- continue;
- }
- }
- if (successTimes == 0)
- {
- Logger.WriteLineInfo($"Connect serverUrl:{serverUrl} fail, the success times is 0");
- return -1;
- }
- else
- {
- var averageValue = totalTime / successTimes;
- Logger.WriteLineInfo($"Connect serverUrl:{serverUrl},successTimes:{successTimes},spend time averageValue:{averageValue}ms");
- return averageValue;
- }
- }
- }
- catch (Exception e)
- {
- Logger.WriteLineError($"Connect Server:{serverUrl}, SpeedTest exception:{e}");
- return -1;
- }
- }
- }
- }
|