예제 #1
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);
        }
예제 #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; and <see cref="MessageProcessor"/> that is going to process incoming and outgoing messages.
 /// </summary>
 ///
 /// <remarks>
 /// The <see cref="NetworkManagerAsync"/> will use the <see cref="NetworkManagerAsyncSettings.DefaultSettings"/> to acquire a
 /// receive and send pool.
 /// </remarks>
 ///
 /// <param name="socket">
 ///     <see cref="System.Net.Sockets.Socket"/> instance from which messages will be received and sent.
 /// </param>
 /// <param name="processor">
 ///     <see cref="MessageProcessor"/> instance that is going to process incoming and outgoing messages.
 /// </param>
 ///
 /// <exception cref="ArgumentNullException">
 ///     <paramref name="socket"/> is null.
 /// </exception>
 /// <exception cref="ArgumentNullException">
 ///     <paramref name="processor"/> is null.
 /// </exception>
 public NetworkManagerAsync(Socket socket, MessageProcessor processor)
     : this(socket, NetworkManagerAsyncSettings.DefaultSettings, processor)
 {
     // Space
 }