/// <summary> /// 重要:连接服务端 /// </summary> /// <returns></returns> public async Task ConnectToProvider() { var appIdIpPortConfig = ClientConfig.Clients; ConnnectionManager = ServerConnnectionManager.Create(); ConnnectionManager.ClientGroupConnected += ServerConnnectionManager_ClientGroupConnected; var clientModel = await ConnnectionManager.InitConfig(); int counter = 0; //appid为0时说明没有分配appid,所以需要分配一个 foreach (var app in appIdIpPortConfig) { if (app.AppId == 0) { app.AppId = clientModel.AppList[counter].AppId; counter++; } } Console.WriteLine("****************port list*************"); foreach (var ap in clientModel.AppList) { var cApp = appIdIpPortConfig.First(obj => obj.AppId == ap.AppId); Console.WriteLine(ap.AppId.ToString() + ": " + ClientConfig.ProviderAddress + ":" + ap.Port.ToString() + "=>" + cApp.IP + ":" + cApp.TargetServicePort); } Console.WriteLine("**************************************"); await ConnnectionManager.PollingToProvider(); }
/// <summary> /// 重要:连接服务端 /// </summary> /// <returns></returns> public async Task ConnectToProvider() { var appIdIpPortConfig = ClientConfig.Clients; ConnnectionManager = ServerConnnectionManager.Create(); ConnnectionManager.ClientGroupConnected += ServerConnnectionManager_ClientGroupConnected; var clientModel = await ConnnectionManager.InitConfig(); int counter = 0; //appid为0时说明没有分配appid,所以需要分配一个 foreach (var app in appIdIpPortConfig) { if (app.AppId == 0) { app.AppId = clientModel.AppList[counter].AppId; counter++; } } Logger.Debug("****************port list*************"); foreach (var ap in clientModel.AppList) { var cApp = appIdIpPortConfig.First(obj => obj.AppId == ap.AppId); Logger.Debug(ap.AppId.ToString() + ": " + ClientConfig.ProviderAddress + ":" + ap.Port.ToString() + "=>" + cApp.IP + ":" + cApp.TargetServicePort); } Logger.Debug("**************************************"); Task pollingTask = ConnnectionManager.PollingToProvider(); try { await pollingTask; } catch (Exception ex) { Logger.Error("Thread:" + Thread.CurrentThread.ManagedThreadId + " crashed.\n", ex); throw; } await Task.Delay(TimeSpan.FromHours(24), CANCEL_TOKEN.Token); }
/// <summary> /// 重要:连接服务端,一般做为入口方法 /// 该方法主要操作一些配置和心跳 /// </summary> /// <returns></returns> public async Task Start() { var oneLiveToken = ONE_LIVE_TOKEN_SRC.Token; while (!oneLiveToken.IsCancellationRequested) { CANCEL_TOKEN_SRC = new CancellationTokenSource(); TRANSFERING_TOKEN_SRC = new CancellationTokenSource(); HEARTBEAT_TOKEN_SRC = new CancellationTokenSource(); _waiter = new TaskCompletionSource <object>(); var appIdIpPortConfig = ClientConfig.Clients; int clientId = 0; //0.5 如果有文件,取出缓存中的clientid try { if (File.Exists(NSMART_CLIENT_CACHE_PATH)) { using (var stream = File.OpenRead(NSMART_CLIENT_CACHE_PATH)) { byte[] bytes = new byte[2]; stream.Read(bytes, 0, bytes.Length); clientId = StringUtil.DoubleBytesToInt(bytes); } } } catch (Exception ex) { Logger.Error(ex.Message, ex); } //1.获取配置 ConnectionManager = ServerConnnectionManager.Create(clientId); ConnectionManager.ClientGroupConnected += ServerConnnectionManager_ClientGroupConnected; ConnectionManager.ServerNoResponse = DoServerNoResponse; //下钻事件 ClientModel clientModel = null; // try { //非第一次则算作重连,发送clientid过去 clientModel = await ConnectionManager.InitConfig(this.ClientConfig).ConfigureAwait(false); } catch (Exception ex) { //TODO 状态码:连接失败 Router.Logger.Error("连接失败:" + ex.Message, ex); //throw; } //HasConnected = true; if (clientModel != null) { int counter = 0; //1.5 写入缓存 File.WriteAllBytes(NSMART_CLIENT_CACHE_PATH, StringUtil.IntTo2Bytes(clientModel.ClientId)); //2.分配配置:appid为0时说明没有分配appid,所以需要分配一个 foreach (var app in appIdIpPortConfig) { if (app.AppId == 0) { app.AppId = clientModel.AppList[counter].AppId; counter++; } } Logger.Debug("****************port list*************"); List <string> tunnelstrs = new List <string>(); foreach (var ap in clientModel.AppList) { var cApp = appIdIpPortConfig.First(obj => obj.AppId == ap.AppId); var tunnelStr = ap.AppId.ToString() + ": " + ClientConfig.ProviderAddress + ":" + ap.Port.ToString() + "=>" + cApp.IP + ":" + cApp.TargetServicePort; Logger.Debug(tunnelStr); tunnelstrs.Add(tunnelStr); } Logger.Debug("**************************************"); ConnectionManager.PollingToProvider(StatusChanged, tunnelstrs); //3.创建心跳连接 ConnectionManager.StartHeartBeats(Global.HeartbeatInterval, HEARTBEAT_TOKEN_SRC.Token); //try //{ // await pollingTask.ConfigureAwait(false); //} //catch (Exception ex) //{ // Logger.Error("Thread:" + Thread.CurrentThread.ManagedThreadId + " crashed.\n", ex); // throw; //} IsStarted = true; Exception exception = await _waiter.Task.ConfigureAwait(false) as Exception; Router.Logger.Debug($"程序异常终止:{exception.Message}。"); } else { Router.Logger.Debug($"程序启动失败。"); //如果程序从未启动过就出错,则终止程序,否则重试。 if (IsStarted == false) { StatusChanged(ClientStatus.Stopped, null); return; } } //出错重试 await Task.Delay(3000, ONE_LIVE_TOKEN_SRC.Token); //TODO 返回错误码 //await Task.Delay(TimeSpan.FromHours(24), CANCEL_TOKEN.Token).ConfigureAwait(false); } //正常终止 }