private void OnLogOut(object sender, LogOutEventArgs e) { //UpdateUI(sender, e); // 直接更新可能影响服务器性能,先加入队列缓冲一下,让专门线程去更新 _logOutQueue.Enqueue(e); _logRecvEvt.Set(); }
private void Log4Debug(string msg) { //LogOutEventArgs logArgs = _logOutEvtArgsPool.Pop(); //logArgs.SetArgs(msg, _clientCount, _totalBytesRead); LogOutEventArgs logArgs = new LogOutEventArgs(msg, _clientCount, _totalBytesRead); if (LogOut != null) { LogOut(this, logArgs); _logOutEvtArgsPool.Push(logArgs); } //Console.WriteLine("[" + DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss.fff") + "] " + msg); //Program.Logger.WriteLine(msg); }
private void UpdateUI(object sender, LogOutEventArgs e) { if (this.InvokeRequired) { Invoke(new EventHandler <LogOutEventArgs>(UpdateUI), new object[] { sender, e }); } else { txtClientCnt.Text = e.ClientCount.ToString(); txtRecvBytes.Text = e.RecvBytes.ToString(); if (txtLog.Text.Length > txtLog.MaxLength / 2) { txtLog.Clear(); } txtLog.AppendText(e.Msg + "\r\n"); txtLog.ScrollToCaret(); } }
/// <summary> /// 初始化函数 /// </summary> public void Init() { _totalBytesRead = 0; _clientCount = 0; _bufferManager = new BufferManager(_bufferSize * _maxClient * (opsToPreAlloc), _bufferSize); _userTokenPool = new AsyncSocketUserTokenPool(_maxClient); _clientList = new AsyncSocketUserTokenList(); _maxAcceptClientSem = new Semaphore(_maxClient, _maxClient); _stopWaitSem = new Semaphore(1, 1); _logOutEvtArgsPool = new Stack <LogOutEventArgs>(_maxClient); // preallocate pool of AsyncSocketUserToken and LogOutEventArgs AsyncSocketUserToken userToken; LogOutEventArgs logArgs; for (int i = 0; i < _maxClient; i++) { //Pre-allocate a set of reusable AsyncSocketUserToken userToken = new AsyncSocketUserToken(); userToken.SendEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnIOCompleted); userToken.RecvEventArgs.Completed += new EventHandler <SocketAsyncEventArgs>(OnIOCompleted); // assign a byte buffer from the buffer pool to the SocketAsyncEventArg object _bufferManager.SetBuffer(userToken.RecvEventArgs); _bufferManager.SetBuffer(userToken.SendEventArgs); _userTokenPool.Push(userToken); // Pre-allocate a set of reusable LogOutEventArgs logArgs = new LogOutEventArgs("", 0, 0); _logOutEvtArgsPool.Push(logArgs); } }