/// <summary> /// 关闭侦听的TMSKSocket /// </summary> public void Stop() { TMSKSocket s = this.listenSocket; this.listenSocket = null; s.Close(); }
public bool CloseSocket(TMSKSocket s, string reason = "") { bool result; if (!this.FindSocket(s)) { Global._SendBufferManager.Remove(s); result = false; } else { if (!string.IsNullOrEmpty(reason)) { s.CloseReason = reason; } this.RemoveSocket(s); if (null != this.SocketClosed) { this.SocketClosed(this, s); } Global._SendBufferManager.Remove(s); try { s.Shutdown(SocketShutdown.Both); } catch (Exception ex) { try { LogManager.WriteLog(LogTypes.Info, string.Format("CloseSocket s.Shutdown()异常: {0}, {1}", s.RemoteEndPoint, ex.Message), null, true); } catch (Exception) { } } try { s.Close(30); } catch (Exception ex) { try { LogManager.WriteLog(LogTypes.Info, string.Format("CloseSocket s.Close()异常: {0}, {1}", s.RemoteEndPoint, ex.Message), null, true); } catch (Exception) { } } result = true; } return(result); }
/// <summary> /// 关闭指定的TMSKSocket连接 /// </summary> /// <param name="s"></param> public void CloseSocket(TMSKSocket s) { //try //{ // s.Shutdown(SocketShutdown.Both); // // 需要Close 不然不会收到套接字关闭 ChenXiaojun // s.Close(); //} //catch (Exception ex) //{ // // Throws if client process has already closed. // try // { // LogManager.WriteLog(LogTypes.Info, string.Format("CloseSocket 异常: {0}, {1}", s.RemoteEndPoint, ex.Message)); // } // catch (Exception) // { // } //} if (!FindSocket(s)) //已经关闭了 { //已经关闭之后再调用一次 Global._SendBufferManager.Remove(s); return; } RemoveSocket(s); /// 断开成功通知函数 if (null != SocketClosed) { SocketClosed(this, s); } //在这个位置调用,避免遗漏,重复调用不会出错 Global._SendBufferManager.Remove(s); try { s.Shutdown(SocketShutdown.Both); } catch (Exception ex) { // Throws if client process has already closed. try { LogManager.WriteLog(LogTypes.Info, string.Format("CloseSocket s.Shutdown()异常: {0}, {1}", s.RemoteEndPoint, ex.Message)); } catch (Exception) { } } try { s.Close(); } catch (Exception ex) { // Throws if client process has already closed. try { LogManager.WriteLog(LogTypes.Info, string.Format("CloseSocket s.Close()异常: {0}, {1}", s.RemoteEndPoint, ex.Message)); } catch (Exception) { } } //由接收事件去释放处理 }
/// <summary> /// Process the accept for the socket listener. /// </summary> /// <param name="e">SocketAsyncEventArg associated with the completed accept operation.</param> private void ProcessAccept(SocketAsyncEventArgs e) { //Socket s = e.AcceptSocket; TMSKSocket s = new TMSKSocket(e.AcceptSocket); //检查IP名单 bool disableConnect = false; if (EnabledIPListFilter) { lock (IPWhiteList) { if (EnabledIPListFilter && null != s && null != s.RemoteEndPoint) { IPEndPoint remoteIPEndPoint = (s.RemoteEndPoint as IPEndPoint); if (null != remoteIPEndPoint && null != remoteIPEndPoint.Address) { string remoteIP = remoteIPEndPoint.Address.ToString(); if (!string.IsNullOrEmpty(remoteIP) && !IPWhiteList.ContainsKey(remoteIP)) { disableConnect = true; } } } } } //是否不再接受新的用户 if (DontAccept || disableConnect) { try { if (disableConnect) { LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是客户端IP处于IP过滤中,直接关闭连接:{1}", s.RemoteEndPoint, ConnectedSocketsCount)); } else if (DontAccept) { LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是服务器端处于不接受新连接状态,直接关闭连接:{1}", s.RemoteEndPoint, ConnectedSocketsCount)); } } catch (Exception) { } try { s.Shutdown(SocketShutdown.Both); } catch (Exception) { // Throws if client process has already closed. } try { s.Close(); } catch (Exception) { } // Accept the next connection request. this.StartAccept(e); return; } byte[] inOptionValues = new byte[sizeof(uint) * 3]; BitConverter.GetBytes((uint)1).CopyTo(inOptionValues, 0); BitConverter.GetBytes((uint)120000).CopyTo(inOptionValues, sizeof(uint)); BitConverter.GetBytes((uint)5000).CopyTo(inOptionValues, sizeof(uint) * 2); (s as TMSKSocket).IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); SocketAsyncEventArgs readEventArgs = null; if (GameManager.FlagOptimizeThreadPool3) { readEventArgs = s.PopReadSocketAsyncEventArgs(); //线程安全的操作 } else { readEventArgs = this.readPool.Pop(); } if (null == readEventArgs) { try { LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是readPool内的缓存不足,直接关闭连接:{1}", s.RemoteEndPoint, ConnectedSocketsCount)); } catch (Exception) { } try { s.Shutdown(SocketShutdown.Both); } catch (Exception) { // Throws if client process has already closed. } try { s.Close(); } catch (Exception) { } // Accept the next connection request. this.StartAccept(e); return; } //增加计数器 //Interlocked.Increment(ref this.numConnectedSockets); // Get the socket for the accepted client connection and put it into the // ReadEventArg object user token. (readEventArgs.UserToken as AsyncUserToken).CurrentSocket = s; Global._SendBufferManager.Add(s); AddSocket(s); try { LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 当前总共: {1}", s.RemoteEndPoint, ConnectedSocketsCount)); } catch (Exception) { } /// 连接成功通知函数 if (null != SocketConnected) { SocketConnected(this, readEventArgs); } // As soon as the client is connected, post a receive to the connection. Boolean willRaiseEvent = _ReceiveAsync(readEventArgs); if (!willRaiseEvent) { this.ProcessReceive(readEventArgs); } // Accept the next connection request. this.StartAccept(e); }
private void ProcessAccept(SocketAsyncEventArgs e) { TMSKSocket s = new TMSKSocket(e.AcceptSocket); s.SetAcceptIp(); bool disableConnect = false; bool?inIpWriteList = null; if (this.EnabledIPListFilter) { lock (this.IPWhiteList) { if (this.EnabledIPListFilter && s != null && null != s.RemoteEndPoint) { IPEndPoint remoteIPEndPoint = s.RemoteEndPoint as IPEndPoint; if (remoteIPEndPoint != null && null != remoteIPEndPoint.Address) { string remoteIP = remoteIPEndPoint.Address.ToString(); if (!string.IsNullOrEmpty(remoteIP) && !this.IPWhiteList.ContainsKey(remoteIP)) { LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是客户端IP处于IP过滤中:{1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true); inIpWriteList = new bool?(false); } else { inIpWriteList = new bool?(true); } } } } } if (IPStatisticsManager.getInstance().GetIPInBeOperation(s, IPOperaType.BanConnect)) { disableConnect = true; } if (this.DontAccept || disableConnect) { try { if (disableConnect) { LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是客户端IP处于IP过滤中,直接关闭连接:{1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true); } else if (this.DontAccept) { LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是服务器端处于不接受新连接状态,直接关闭连接:{1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true); } } catch (Exception) { } try { s.Shutdown(SocketShutdown.Both); } catch (Exception) { } try { s.Close(30); } catch (Exception) { } this.StartAccept(e); } else { byte[] inOptionValues = new byte[12]; BitConverter.GetBytes(1U).CopyTo(inOptionValues, 0); BitConverter.GetBytes(120000U).CopyTo(inOptionValues, 4); BitConverter.GetBytes(5000U).CopyTo(inOptionValues, 8); s.IOControl(IOControlCode.KeepAliveValues, inOptionValues, null); LingerOption lingerOption = new LingerOption(true, 10); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption); SocketAsyncEventArgs readEventArgs = null; readEventArgs = s.PopReadSocketAsyncEventArgs(); if (null == readEventArgs) { try { LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 但是readPool内的缓存不足,直接关闭连接:{1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true); } catch (Exception) { } try { s.Shutdown(SocketShutdown.Both); } catch (Exception) { } try { s.Close(30); } catch (Exception) { } this.StartAccept(e); } else { (readEventArgs.UserToken as AsyncUserToken).CurrentSocket = s; Global._SendBufferManager.Add(s); this.AddSocket(s); try { LogManager.WriteLog(LogTypes.Error, string.Format("新远程连接: {0}, 当前总共: {1}", s.RemoteEndPoint, this.ConnectedSocketsCount), null, true); } catch (Exception) { } if (null != this.SocketConnected) { this.SocketConnected(this, readEventArgs); } s.session.InIpWhiteList = inIpWriteList; s.session.SetSocketTime(0); if (!this._ReceiveAsync(readEventArgs)) { this.ProcessReceive(readEventArgs); } this.StartAccept(e); } } }