// Create an uninitialized server instance. // To start the server listening for connection requests // call the Init method followed by Start method // // <param name="numConnections">the maximum number of connections the sample is designed to handle simultaneously</param> // <param name="receiveBufferSize">buffer size to use for each socket I/O operation</param> public StateContext(int MaxConnections, int BufferSize) { TokenPool = new AsyncUserTokenPool(MaxConnections); TotalBytesRead = 0; ConnectedSockets = 0; this.MaxConnections = MaxConnections; this.BufferSize = BufferSize; // allocate buffers such that the maximum number of sockets can have one outstanding read and //write posted to the socket simultaneously semaphore = new Semaphore(MaxConnections, MaxConnections); }
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); } }