internal BufferStream(NetworkManagerAsyncSettings settings, Pool <byte[]> buffers)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (buffers == null)
            {
                throw new ArgumentNullException(nameof(buffers));
            }

            _settings = settings;
            _buffers  = buffers;
            _bufIndex = -1;

            TakeNewBuffer();
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="NetworkManagerAsync"/> class with the specified <see cref="System.Net.Sockets.Socket"/>
        /// from which messages will be received and sent; <see cref="NetworkManagerAsyncSettings"/> and
        /// <see cref="MessageProcessor"/> that is going to process incoming and outgoing messages.
        /// </summary>
        ///
        /// <param name="socket">
        ///     <see cref="System.Net.Sockets.Socket"/> instance from which messages will be received and sent.
        /// </param>
        ///
        /// <param name="settings">
        ///     <see cref="NetworkManagerAsyncSettings"/> instance that is going to be used.
        /// </param>
        ///
        /// <param name="processor">
        ///     <see cref="MessageProcessor"/> instance that is going to process incoming and outgoing messages.
        /// </param>
        public NetworkManagerAsync(Socket socket, NetworkManagerAsyncSettings settings, MessageProcessor processor)
        {
            if (socket == null)
            {
                throw new ArgumentNullException(nameof(socket));
            }
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }
            if (processor == null)
            {
                throw new ArgumentNullException(nameof(processor));
            }

            _processor = processor;
            _socket    = socket;
            _settings  = settings;
            _stats     = new NetworkManagerAsyncStatistics();
#if DEBUG
            _ident = Guid.NewGuid().ToString();
#endif

            _settingsStats = Settings.Statistics;

            _receivePool = Settings._receivePool;
            _sendPool    = Settings._sendPool;

            _usedIncomingBuffers = new Pool <byte[]>(4);
            _incomingBuffer      = new BufferStream(_settings, _usedIncomingBuffers);

            // Take a SocketAsyncEventArgs object from the pool or create new instance
            // if empty.
            var args = _settings.GetArgs();

            // Set SocketAsyncEventArgs to write directly
            // into the stream buffer.
            args.SetBuffer(_incomingBuffer._buf, 0, _settings.BufferSize);

            // Begin receiving on the args.
            StartReceive(args);
        }