/// <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); }
/// <summary> /// Initializes a new instance of the <see cref="NetworkManagerAsyncSettings"/> class /// with the specified number of receive operation <see cref="SocketAsyncEventArgs"/> objects /// and the specified number of send operation <see cref="SocketAsyncEventArgs"/> objects with /// the specified buffer size of each <see cref="SocketAsyncEventArgs"/> object. /// </summary> /// <param name="receiveCount">Number of receive operation <see cref="SocketAsyncEventArgs"/> objects.</param> /// <param name="sendCount">Number of send operation <see cref="SocketAsyncEventArgs"/> objects.</param> /// <param name="bufferSize">Buffer size of each <see cref="SocketAsyncEventArgs"/> object.</param> /// <exception cref="ArgumentOutOfRangeException"><paramref name="bufferSize"/> less than 1.</exception> public NetworkManagerAsyncSettings(int receiveCount, int sendCount, int bufferSize) { if (bufferSize < 1) { throw new ArgumentOutOfRangeException(nameof(bufferSize), "Buffer size cannot be less than 1."); } _bufferSize = bufferSize; _statistics = new NetworkManagerAsyncStatistics(); // Don't waste IO threads. var concurrentOpsCount = Environment.ProcessorCount * 2; _concurrentOps = new Semaphore(concurrentOpsCount, concurrentOpsCount); _receivePool = new SocketAsyncEventArgsPool(receiveCount); _sendPool = new SocketAsyncEventArgsPool(sendCount); _bufferManager = new MessageBufferManager(receiveCount, sendCount, bufferSize); for (int i = 0; i < ReceiveCount; i++) { var args = new SocketAsyncEventArgs(); _bufferManager.SetBuffer(args); MessageReceiveToken.Create(args); _receivePool.Push(args); } for (int i = 0; i < SendCount; i++) { var args = new SocketAsyncEventArgs(); _bufferManager.SetBuffer(args); MessageSendToken.Create(args); _sendPool.Push(args); } _buffers = new Pool <byte[]>(64); _args = new Pool <SocketAsyncEventArgs>(receiveCount + sendCount); }