Example #1
0
        /// <summary>
        /// Initializes the server by preallocating reusable buffers and
        /// context objects.  These objects do not need to be preallocated
        /// or reused, but it is done this way to illustrate how the API can
        /// easily be used to create reusable objects to increase server performance.
        /// </summary>
        public virtual void Init(SocketSettings settings)
        {
            _settings = settings;

            // Allocates one large byte buffer which all I/O operations use a piece of.  This guards against memory fragmentation
            IBufferManager bufferManager = _bufferManagerFactory.Create();

            bufferManager.InitBuffer(_settings.BufferSize * _settings.MaxConnections * 2, _settings.BufferSize);
            _communicationChannelsPool = new GenericPool <ICommunicationChannel>(_settings.MaxConnections);

            for (int i = 0; i < _settings.MaxConnections; i++)
            {
                ICommunicationChannel communicationChannel = _applicationContext.Container.Resolve <ICommunicationChannel>();
                communicationChannel.SocketClosedEvent += ClientHandler_SocketClosedEvent;
                communicationChannel.Init(bufferManager, _packetsHandler);
                _communicationChannelsPool.Push(communicationChannel);
            }
        }