public void Init() { AsyncUserToken userToken; for (int i = 0; i < MaxConnections; i++) //按照连接数建立读写对象 { userToken = new AsyncUserToken(BufferSize); userToken.SetComplete(IO_Completed); userToken.Completed(); TokenPool.Push(userToken); } }
/// <summary> /// Socket 断开处理 /// </summary> private void CloseClientSocket(AsyncUserToken userToken) { try { if (userToken.ConnectSocket == null) { return; } ServerDataManager.instance.SetOffLineByState(userToken); Log4Debug(String.Format("客户 {0} 清理链接!", userToken.ConnectSocket.RemoteEndPoint.ToString())); // userToken.ConnectSocket.Shutdown(SocketShutdown.Both); userToken.ConnectSocket.Close(); Log4Debug("Free Client total:" + userTokenPool.Count()); } catch { } finally { userTokenPool.RemoveUsed(userToken); //清除在线 userToken.Init(); //清除该变量 userTokenPool.Push(userToken); //复存该变量 } }
private void Init() { bufferManager = new BufferManager(maxClient * 4 * bufferSize, bufferSize); userTokenPool = new AsyncUserTokenPool(maxClient); for (int i = 0; i < maxClient; i++) //填充SocketAsyncEventArgs池 { AsyncUserToken userToken = new AsyncUserToken(bufferSize); userToken.SAEA_Receive.UserToken = userToken; userToken.SAEA_Receive.Completed += new EventHandler <SocketAsyncEventArgs>(OnIOCompleted); bufferManager.SetBuffer(userToken.SAEA_Receive); userTokenPool.Push(userToken); } }