示例#1
0
        public virtual void CloseSocket(AsyncUserToken iSocketToken)
        {
            if (iSocketToken.Socket == null)
            {
                return;
            }
            string socketInfo = string.Format("Local Address: {0} Remote Address: {1}", iSocketToken.Socket.LocalEndPoint,
                                              iSocketToken.SendEventArgs.RemoteEndPoint);

            App.Logger.Info("Connection disconnected. {0}", socketInfo);
            try
            {
                iSocketToken.Socket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception e)
            {
                App.Logger.Info("CloseSocket Disconnect {0} error, message: {1}", socketInfo, e.Message);
            }
            iSocketToken.Socket.Close();
            iSocketToken.Socket = null; //释放引用,并清理缓存,包括释放协议对象等资源
            iSocketToken.Clear();
            AsyncSocketUserTokenUsed.Remove(iSocketToken.OwnerID1);
            AsyncSocketUserTokenPool.Push(iSocketToken);

            MaxNumberAccepted.Release();
        }
示例#2
0
        public virtual void CloseSocket(AsyncUserToken socketToken)
        {
            if (socketToken.Socket == null)
            {
                return;
            }
            string socketInfo = string.Format("本地地址: {0} 远程地址: {1}", socketToken.Socket.LocalEndPoint,
                                              socketToken.Socket.RemoteEndPoint);

            try
            {
                socketToken.Socket.Shutdown(SocketShutdown.Both);
                App.Info("关闭连接. {0}", socketInfo);
            }
            catch (Exception e)
            {
                App.Info("关闭连接 {0} 错误, 消息: {1}", socketInfo, e.Message);
            }
            socketToken.Socket.Close();
            socketToken.Socket = null; //释放引用,并清理缓存,包括释放协议对象等资源
            socketToken.Clear();

            AsyncSocketUserTokenUsed.Remove(socketToken);
            AsyncSocketUserTokenPool.Push(socketToken);

            MaxNumberAccepted.Release();

            Closed?.Invoke(socketToken); // 连接关闭回调
        }