//public ClientRequestHandler ClientRequestHandler { get; set; }//* #region Construcotr /// <summary> /// Socket Server Constructor /// </summary> /// <param name="port">socket listen port</param> /// <param name="start">是否直接啟動</param> /// <param name="serviceName">要啟動的服務名稱,看ServiceFactory.cs的enum ServiceSelect列表</param> /// <param name="listenIP">socket listen ip [null=AnyIP]</param> public AsyncMultiSocketServer(int port, bool start, string serviceName, string listenIP = null) { this.port = port; this.serviceName = serviceName; this.AcceptConnectEvent = new ManualResetEvent(false); this.clientManager = new ClientRequestManager(); //log.Debug(">> Port Number: " + this.port); //log.Debug(">> State name: " + this.stateName); //log.Debug(">> Start : " + (start ? "true" : "false")); if (start) { this.Start(); } //是否只監聽本機 if (listenIP != null) { IPAddress ip = null; if (IPAddress.TryParse(listenIP, out ip)) { this.listenIP = ip; } else { //log.Error("IP Convert Failed => Listen AnyIP"); this.listenIP = IPAddress.Any; } } else { this.listenIP = IPAddress.Any; } }
/// <summary> /// 停止服務 /// </summary> public void Stop() { if (this.KeepSocketServerAlive) { this.KeepSocketServerAlive = false; this.AcceptConnectEvent.Set(); this.AcceptConnectEvent.Dispose(); log.Info(m => m(">> Set Flag ==> False and dispose Main ManualResetEvent")); //Console.WriteLine(">> Set Flag ==> False "); } //Console.WriteLine(">> Service Stop ...{0}", this.serviceName); log.Info(m => m(">> Service Stop ...{0}", this.serviceName)); if (this.mainSocket != null) { try { this.mainSocket.Close(); //log.Debug(">> Server Stop ..."); //Console.WriteLine(">> {0} Server Stop ...",this.stateName); } catch (SocketException ex) { //Console.WriteLine(">> Main Socket Close Failed : " + ex.Message + ":" + ex.ErrorCode); //log.Error(">> Main Socket Close Failed : " + ex.Message + ":" + ex.ErrorCode); if (this.mainSocket.Connected) { this.mainSocket.Shutdown(SocketShutdown.Both); } this.mainSocket.Dispose(); log.Error(m => m(">> Main Socket Dispose ...Error:{0}", ex.Message)); } finally { this.mainSocket = null; //log.Debug(">> Main Socket Set Null"); } } //清除Client Manager的所有數據 this.clientManager.ClearAll(); this.clientManager = null; }
public void init() { this.manager = new ClientRequestManager(); }