public Server(int expectedNumberOfConnections, int receiveBufferSize) { _listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _expectedNumberOfConnections = expectedNumberOfConnections; _receiveBufferSize = receiveBufferSize; _bufferManager = new BufferManager(receiveBufferSize * expectedNumberOfConnections * OpsToPreAlloc, receiveBufferSize, true); _bufferManager.InitBuffer(); _acceptEvtArgsPool = new ThreadSafePool<SocketAsyncEventArgs>(10); _readWriteEvtArgsPool = new ThreadSafePool<SocketAsyncEventArgs>(expectedNumberOfConnections); for (int i = 0; i < _expectedNumberOfConnections; i++) { SocketAsyncEventArgs readWriteEvtArg = new SocketAsyncEventArgs(); _bufferManager.SetBuffer(readWriteEvtArg); readWriteEvtArg.Completed += IOCompleted; _readWriteEvtArgsPool.Push(readWriteEvtArg); } for (int i = 0; i < 10; i++) { SocketAsyncEventArgs acceptEvtArg = new SocketAsyncEventArgs(); acceptEvtArg.Completed += AcceptCompleted; _acceptEvtArgsPool.Push(acceptEvtArg); } }
public static void Init(int count, int blocksize, bool autoGrowth) { _autoGrowth = autoGrowth; bufferStack = new ThreadSafePool<SafeBuffer>(count); for (int i = 0; i < count; i++) { byte[] buf = new byte[blocksize]; bufferStack.Push(new SafeBuffer(buf)); } }