/// <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("bufferSize", "Argument bufferSize cannot be less than 1."); BufferSize = bufferSize; 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); } }
/// <summary> /// Initializes a new instance of the <see cref="NetworkManagerAsync"/> class. /// </summary> /// <param name="connection"><see cref="Socket"/> to wrap.</param> /// <exception cref="ArgumentNullException"/> public NetworkManagerAsync(Socket connection) { if (connection == null) throw new ArgumentNullException("connection"); Connection = connection; Settings = new NetworkManagerAsyncSettings(); // default settings CoCCrypto = new CoCCrypto(); ReceiveEventPool = new SocketAsyncEventArgsPool(25); SendEventPool = new SocketAsyncEventArgsPool(25); SetAsyncOperationPools(); StartReceive(ReceiveEventPool.Pop()); }
/// <summary> /// Initializes a new instance of the <see cref="NetworkManagerAsync"/> class. /// </summary> /// <param name="connection"><see cref="Socket"/> to wrap.</param> /// <param name="networkHandler"><see cref="PacketReceivedHandler"/> that will handle incoming packet.</param> /// <param name="packetReceivedFailedHandler"><see cref="PacketReceivedFailedHandler"/> that will handle incoming packet that failed to read.</param> /// <exception cref="System.ArgumentNullException"/> public NetworkManagerAsync(Socket connection, PacketReceivedHandler packetReceivedHandler, PacketReceivedFailedHandler packetReceivedFailedHandler) { if (PacketDictionary == null) // could a use a static constructor? InitializePacketDictionary(); if (connection == null) throw new ArgumentNullException("connection"); if (packetReceivedHandler == null) throw new ArgumentNullException("packetReceivedHandler"); Connection = connection; PacketReceived = packetReceivedHandler; PacketReceivedFailed = packetReceivedFailedHandler; CoCCrypto = new CoCCrypto(); ReceiveEventPool = new SocketAsyncEventArgsPool(25); SendEventPool = new SocketAsyncEventArgsPool(25); StartReceive(ReceiveEventPool.Pop()); }
/// <summary> /// Initializes a new instance of the <see cref="NetworkManagerAsync"/> class with /// the specified <see cref="NetworkManagerAsyncSettings"/>. /// </summary> /// <param name="connection"><see cref="Socket"/> to wrap.</param> /// <param name="settings"><see cref="NetworkManagerAsyncSettings"/> to use.</param> /// <exception cref="ArgumentNullException"/> public NetworkManagerAsync(Socket connection, NetworkManagerAsyncSettings settings) { if (connection == null) { throw new ArgumentNullException("connection"); } if (settings == null) { throw new ArgumentNullException("settings"); } Connection = connection; Settings = settings; CoCCrypto = new CoCCrypto(); ReceiveEventPool = new SocketAsyncEventArgsPool(25); SendEventPool = new SocketAsyncEventArgsPool(25); SetAsyncOperationPools(); StartReceive(ReceiveEventPool.Pop()); }
/// <summary> /// Initializes a new instance of the <see cref="NetworkManagerAsync"/> class. /// </summary> /// <param name="connection"><see cref="Socket"/> to wrap.</param> /// <param name="networkHandler"><see cref="PacketReceivedHandler"/> that will handle incoming packet.</param> /// <param name="packetReceivedFailedHandler"><see cref="PacketReceivedFailedHandler"/> that will handle incoming packet that failed to read.</param> /// <exception cref="System.ArgumentNullException"/> public NetworkManagerAsync(Socket connection, PacketReceivedHandler packetReceivedHandler, PacketReceivedFailedHandler packetReceivedFailedHandler) { if (PacketDictionary == null) // could a use a static constructor? InitializePacketDictionary(); if (connection == null) throw new ArgumentNullException("connection"); if (packetReceivedHandler == null) throw new ArgumentNullException("packetReceivedHandler"); Connection = connection; PacketReceived = packetReceivedHandler; PacketReceivedFailed = packetReceivedFailedHandler; Seed = MathHelper.Random.Next(); // must make this moar flexible CoCCrypto = new CoCCrypto(); ReceiveEventPool = new SocketAsyncEventArgsPool(25); SendEventPool = new SocketAsyncEventArgsPool(25); StartReceive(); }