/// <summary> /// 关闭所有会话 /// </summary> protected void StopCreators() { CreatorManage[] pCreators = this.GetSocketCreators(); if (pCreators != null) { int iLoopCount = 0; this.pCreatorsResetEvent.Reset(); foreach (CreatorManage pCreator in pCreators) { try { pCreator.CreatorStop(); } finally { this.RemoveCreator(pCreator); pCreator.Dispose(); ThreadLoop.LoopSleep(ref iLoopCount); } } if (pCreators.Length > 0) { this.pCreatorsResetEvent.WaitOne(SocketConstant.DEF_RESETEVENT_TIMEOUT, false); } } }
internal override void CreatorStart() { if (!this.Disposed) { try { int iLoopCount = 101; this.pSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); this.pSocket.Bind(this.EndPoint); this.pSocket.Listen(this._iMaxConnection); for (int i = 1; i <= this._iAcceptThread; i++) { this.pSocket.BeginAccept(new AsyncCallback(SocketAcceptCallback), this); ThreadLoop.LoopSleep(ref iLoopCount); } } catch { throw new Exception("网络地址重复绑定,服务中断"); } } }
internal override void SendTo(ServerConnect pServerConnect, byte[] pBuffer, bool bAllConnection) { if (!this.Disposed) { ConnectionManage[] pConnectionItmes = this.GetConnections(); if (pConnectionItmes != null) { int iLoopSleep = 0; foreach (ConnectionManage pConnection in pConnectionItmes) { if (this.Disposed) { break; } try { if (bAllConnection || pServerConnect != pConnection) { byte[] pSendBuffer = new byte[pBuffer.Length]; Buffer.BlockCopy(pBuffer, 0, pSendBuffer, 0, pBuffer.Length); this.SendTo(pConnection, pSendBuffer); } } finally { ThreadLoop.LoopSleep(ref iLoopSleep); } //end try...finally... } //end foreach(...) } //end if(...) } //end if(...) }
private void CheckSocketConnections(Object stateInfo) { if (!this.Disposed) { //Console.WriteLine(1000); this.pIdleCheckTimer.Change(Timeout.Infinite, Timeout.Infinite); try { ConnectionManage[] pConnectionItems = this.GetConnections(); if (pConnectionItems != null) { int iLoopSleep = 101; foreach (ConnectionManage pConnection in pConnectionItems) { if (this.Disposed) { break; } try { if (pConnection != null) { if (DateTime.Now > (pConnection.LastAction.AddMilliseconds(this._iIdleTimeOut))) { pConnection.OnDisconnect(); } } } finally { ThreadLoop.LoopSleep(ref iLoopSleep); } } } } finally { if (!Disposed) { this.pIdleCheckTimer.Change(this._iIdleCheckInterval, this._iIdleCheckInterval); } } } }
/// <summary> /// 启动服务 /// </summary> public void ServiceStart() { if (!this.Disposed) { int iLoopSleep = 0; foreach (CreatorManage pCreator in this.arrCreators) { pCreator.CreatorStart(); ThreadLoop.LoopSleep(ref iLoopSleep); } if (this.pIdleCheckTimer != null) { this.pIdleCheckTimer.Change(this._iIdleTimeOut, this._iIdleTimeOut); } this.Active = true; } }
/// <summary> /// 关闭所有连接 /// </summary> protected void StopConnections() { if (!this.Disposed) { ConnectionManage[] pConnections = this.GetConnections(); if (pConnections != null) { int iLoopSleep = 0; this.pConnectionsResetEvent.Reset(); foreach (ConnectionManage pConnection in pConnections) { pConnection.OnDisconnect(); ThreadLoop.LoopSleep(ref iLoopSleep); } if (pConnections.Length > 0) { this.pConnectionsResetEvent.WaitOne(SocketConstant.DEF_RESETEVENT_TIMEOUT, false); } } } }