Ejemplo n.º 1
0
        // Initializes the server by preallocating reusable buffers and context objects.
        private void Init()
        {
            // Allocates one large byte buffer which all I/O operations use a piece of.  This gaurds
            // against memory fragmentation
            _bufferManager.InitBuffer();

            // preallocate pool of SocketAsyncEventArgs objects
            for (int i = 0; i < _maxNumConnections * OpsToPreAlloc; i++)
            {
                //Pre-allocate a set of reusable SocketAsyncEventArgs
                SocketAsyncEventArgs readWriteArg = new SocketAsyncEventArgs();
                readWriteArg.Completed += OnSendReceiveCompleted;

                // assign a byte buffer from the buffer pool to the SocketAsyncEventArg objects
                _bufferManager.SetBuffer(readWriteArg);

                // add SocketAsyncEventArg to the pool
                _argsReadWritePool.Push(readWriteArg);
            }
        }
Ejemplo n.º 2
0
        // Initializes the server by preallocating reusable buffers and context objects.
        public void Init()
        {
            // Allocates one large byte buffer which all I/O operations use a piece of.  This gaurds
            // against memory fragmentation
            bufferManager.InitBuffer();

            // preallocate pool of SocketAsyncEventArgs objects
            SocketAsyncEventArgs readWriteArg;

            for (int i = 0; i < maxNumConnections; i++)
            {
                //Pre-allocate a set of reusable SocketAsyncEventArgs
                readWriteArg            = new SocketAsyncEventArgs();
                readWriteArg.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                readWriteArg.UserToken  = new HostAsyncUserToken();

                // assign a byte buffer from the buffer pool to the SocketAsyncEventArg object
                bufferManager.SetBuffer(readWriteArg);

                // add SocketAsyncEventArg to the pool
                argsReadWritePool.Push(readWriteArg);
            }
        }