/// <summary>
        /// 处理断开连接
        /// </summary>
        /// <param name="e"></param>
        private void ProcessDisconnect(SocketAsyncEventArgs e)
        {
            SocketToken sToken = e.UserToken as SocketToken;

            if (sToken == null)
            {
                e.Dispose();
                return;
            }

            try
            {
                sToken.Close();

                Interlocked.Decrement(ref numberOfConnections);
                //递减信号量
                maxNumberAcceptedClients.Release();

                //将断开的对象重新放回复用队列
                acceptTokenManager.Set(e);

                if (DisconnectedCallback != null)
                {
                    DisconnectedCallback(sToken);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #2
0
        private void ProcessDisconnectCallback(SocketAsyncEventArgs e)
        {
            SocketToken sToken = e.UserToken as SocketToken;

            if (sToken == null)
            {
                return;// throw new Exception("空异常");
            }

            try
            {
                sToken.Close();
                //递减信号量
                maxNumberAcceptedClients.Release();

                Interlocked.Decrement(ref numberOfConnections);
                if (sToken.TokenId != 0)
                {
                    //将断开的对象重新放回复用队列
                    acceptTokenManager.Set(e);
                }

                DisconnectedCallback?.Invoke(sToken);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void DisposeSocketArgs(SocketAsyncEventArgs e)
        {
            SocketToken s = e.UserToken as SocketToken;

            if (s != null)
            {
                s.Close();           // if (e.UserToken is SocketToken s) --新语法
            }
            e.Dispose();
        }
Пример #4
0
 /// <summary>
 /// 内部使用的释放连接对象方法
 /// </summary>
 /// <param name="sToken"></param>
 private void CloseClientSocket(SocketToken sToken)
 {
     try
     {
         if (sToken != null)
         {
             sToken.Close();
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }