Пример #1
0
        //关闭SOCKET连接
        private void CloseClientSocket(MySocketAsyncEventArgs msaea, CloseReason reason)
        {
            SocketSession session = (SocketSession)msaea.UserToken;

            CloseSessionEvent?.Invoke(null, new SessionEventArgs {
                Type = SessionType.CLOSED, Reason = reason, Endpoint = session.Client.RemoteEndPoint.ToString()
            });
            try { _dic.TryRemove(session.SessionId, out SocketSession outValue); _argsDic.TryRemove(session.SessionId, out MySocketAsyncEventArgs eventArgs); }//移除关闭的连接
            catch (Exception e) { Log.WriteLog(e.TargetSite + "->" + e.Message, LogType.ERROR); }
            try
            {
                session.Client.Shutdown(SocketShutdown.Send);
            }
            catch (Exception e) { }
            try
            {
                session.Client.Close();
            }
            catch (Exception e) { }

            Interlocked.Decrement(ref _CurConn); //当前连接数自减
            msaea.UserToken = null;
            Buffer.FreeSocketEvent(msaea);       //把关闭的事件对象,放入集合中
        }