Example #1
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="socket"></param>
        internal UserToken(Socket socket, ITcpServerLog iLog = null, UserTokenPool tokenPool = null)
        {
            _iLog = iLog;
            _tokenPool = tokenPool;
            _socket = socket;
            RemoteEndPoint = _socket.RemoteEndPoint as IPEndPoint;
            buffer = new byte[1024];
            bufferList = new List<byte>(1024);

            BeginReceive();

            //socket.ReceiveTimeout = TcpServerConfig.RECEIVE_TIMEOUT;
            //var networkStream = new NetworkStream(_socket);

            //_writer.Flush();
            //_writer.AutoFlush = true;
        }
Example #2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="tcpServerConfig"></param>
        /// <param name="accreditHandle"></param>
        /// <param name="iLog"></param>
        public TcpServerHost(TcpServerConfig tcpServerConfig, ServerHandle accreditHandle, ITcpServerLog iLog = null)
        {
            _isRun = false;

            _iLog = iLog;
            _accreditHandle = accreditHandle;
            //SessionFactory = sessionFactory;
            _tcpServerConfig = tcpServerConfig;
            _tokenPool = new UserTokenPool();

            var period = 60;

            recordThreads = new Thread(() => 
            {
                while (true)
                {
                    foreach (var _ut in _tokenPool.userTokens)
                    {
                        var _token = _ut.Value;
                        if (_token == null || _token.Socket == null || !_token.Socket.Connected
                            || _token.ActivityTime.AddHours(5) < DateTime.Now)
                        {
                            _tokenPool.RemoveUserToken(_token.Identity);
                            _token.Close();
                        }
                    }
                    //_tokenPool.RemoveUserToken(_token.Identity);
                    //_token.Close();

                    string msg = string.Format("bits per second: {0:N2}\r\nsend count per second: {1:N2}\r\nreceive count per second: {2:N2}\r\nsession count: {3}",
                       totalBytes / (double)period,
                       sendCount / (double)period,
                       receiveCount / (double)period, UserTokenCount());
                    totalBytes = 0;
                    sendCount = 0;
                    receiveCount = 0;
                    _iLog.LogInfo(msg, "record.txt");

                    Thread.Sleep(period * 1000);
                }
            });
            recordThreads.IsBackground = true;
            recordThreads.Start();

        }