Example #1
0
        //private ConcurrentQueue<Socket> socketQueue = new ConcurrentQueue<Socket>();

        /// <summary>
        /// 添加用户token
        /// </summary>
        /// <param name="token"></param>
        public void AddUserToken(UserToken token)
        {
            token = userTokens.AddOrUpdate(token.Identity, token, (key, val) =>
            {
                val.Close();
                val = null;
                return token;
            });
        }
Example #2
0
 /// <summary>
 /// 授权回调
 /// </summary>
 /// <param name="token"></param>
 /// <param name="session"></param>
 public void AccreditCallback(UserToken token, SessionHandle session)
 {
     token.Session = session;
     AcceptEnd(token);
 }
Example #3
0
        /// <summary>
        /// 授权完成
        /// </summary>
        /// <param name="token"></param>
        public void AcceptEnd(UserToken token)
        {
            if (token.Session == null)
            {
                if (_iLog != null)
                {
                    _iLog.LogError(string.Format("授权失败\r\n{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")), null);
                }

                token.Close();
                return;
            }
            else
            {
                if (_iLog != null)
                {
                    _iLog.LogDebug(string.Format("授权成功\r\nIdentity:{0}\r\n{1}", token.Session.Identity, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                }
            }

            string identity = token.Session.Identity;

            if (string.IsNullOrWhiteSpace(identity))
            {
                token.Close();
                token = null;
            }
            else
            {
                token.Identity = identity;
                _tokenPool.AddUserToken(token);

                if (AcceptSuccess != null)
                {
                    AcceptSuccess(token);
                }

            }
        }
Example #4
0
        /// <summary>
        /// 接入socket
        /// </summary>
        /// <param name="clientSocket"></param>
        private void AcceptSocket(Socket clientSocket)
        {
            UserToken token = new UserToken(clientSocket, _iLog, _tokenPool);
            token.SocketErrorAction = (_token, ex) =>
            {
                _tokenPool.RemoveUserToken(_token.Identity);
                _token.Close();
            };

            token.SendSuccessAction = x =>
            {
                System.Threading.Interlocked.Increment(ref sendCount);
                System.Threading.Interlocked.Add(ref totalBytes, x);
            };

            token.ReceiveSuccessAction = x =>
            {
                System.Threading.Interlocked.Increment(ref receiveCount);
                System.Threading.Interlocked.Add(ref totalBytes, x);
            };

            //_token防止函数闭包
            _accreditHandle.Accredit(token, AccreditCallback);
        }