/// <summary> /// 服务启动 /// </summary> /// <returns></returns> internal Task <ThriftyServer> StartAsync() { Contract.Assert(this._config != null && this._config.Port != 0, "swifty server port is null"); Contract.Assert(this._serviceTypes != null && this._serviceTypes.Any(), "No service can be used to start swifty server."); Contract.Assert( this._state == ThriftyServerStatus.Init, "swifty server can not start, the state is out of control"); this._state = ThriftyServerStatus.Starting; Task task; try { ThriftCodecManager thriftCodecManager; thriftCodecManager = this._codescs == null ? new ThriftCodecManager() : new ThriftCodecManager(this._codescs); INiftyProcessor process = new ThriftServiceProcessor( this._serviceLocator, codecManager: thriftCodecManager, eventHandlers: this._handlers, serviceTypes: this._serviceTypes, loggerFactory: this._loggerFactory); this._server = new ThriftServer(process, this._config, _sslConfig, this._loggerFactory); String addres = String.IsNullOrWhiteSpace(_config.BindingAddress) ? "0.0.0.0" : _config.BindingAddress; this._logger.LogDebug($"server is ready for starting: {addres}:{_config.Port}"); task = this._server.StartAsync(); return(task.ContinueWith(t => { if (t.Exception == null) { this._state = ThriftyServerStatus.Running; } this._state = (t.Exception == null) ? ThriftyServerStatus.Running : ThriftyServerStatus.Error; return this; })); } catch (Exception e) { this._state = ThriftyServerStatus.Error; throw new ThriftyException(e.Message); } }
/// <summary> /// 服务关闭 /// </summary> internal Task <ThriftyServer> ShutdownAsync() { this._logger.LogDebug("server is ready for stopping"); if (this._state != ThriftyServerStatus.Running || this._server == null) { return(Task.FromResult(this)); } this._state = ThriftyServerStatus.Shutingdown; return(this._server.CloseAsync() .ContinueWith( x => { ThriftyRegistryService.ThriftyRegistryServiceInstance.Shutdown(); this._state = (x.Exception != null) ? ThriftyServerStatus.Shutingdown : ThriftyServerStatus.Error; return this; })); }