public SocketListener(int maxConnectionCount, int bufferSize) { _maxConnectionCount = maxConnectionCount; _bufferSize = bufferSize; _socketAsyncReceiveEventArgsPool = new SocketAsyncEventArgsPool(maxConnectionCount); _socketAsyncSendEventArgsPool = new SocketAsyncEventArgsPool(maxConnectionCount); _acceptedClientsSemaphore = new Semaphore(maxConnectionCount, maxConnectionCount); sendingQueue = new BlockingCollection <MessageData>(); sendMessageWorker = new Thread(new ThreadStart(SendQueueMessage)); for (int i = 0; i < maxConnectionCount; i++) { SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs(); socketAsyncEventArgs.Completed += OnIOCompleted; socketAsyncEventArgs.SetBuffer(new Byte[bufferSize], 0, bufferSize); _socketAsyncReceiveEventArgsPool.Push(socketAsyncEventArgs); } for (int i = 0; i < maxConnectionCount; i++) { SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs(); socketAsyncEventArgs.Completed += OnIOCompleted; socketAsyncEventArgs.SetBuffer(new Byte[bufferSize], 0, bufferSize); _socketAsyncSendEventArgsPool.Push(socketAsyncEventArgs); } waitSendEvent = new AutoResetEvent(false); }
private Stopwatch _watch; //for testing #endregion Fields #region Constructors public SocketListener(int maxConnectionCount, int bufferSize) { _maxConnectionCount = maxConnectionCount; _bufferSize = bufferSize; _socketAsyncReceiveEventArgsPool = new SocketAsyncEventArgsPool(maxConnectionCount); _socketAsyncSendEventArgsPool = new SocketAsyncEventArgsPool(maxConnectionCount); _acceptedClientsSemaphore = new Semaphore(maxConnectionCount, maxConnectionCount); sendingQueue = new BlockingCollection<MessageData>(); sendMessageWorker = new Thread(new ThreadStart(SendQueueMessage)); for (int i = 0; i < maxConnectionCount; i++) { SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs(); socketAsyncEventArgs.Completed += OnIOCompleted; socketAsyncEventArgs.SetBuffer(new Byte[bufferSize], 0, bufferSize); _socketAsyncReceiveEventArgsPool.Push(socketAsyncEventArgs); } for (int i = 0; i < maxConnectionCount; i++) { SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs(); socketAsyncEventArgs.Completed += OnIOCompleted; socketAsyncEventArgs.SetBuffer(new Byte[bufferSize], 0, bufferSize); _socketAsyncSendEventArgsPool.Push(socketAsyncEventArgs); } waitSendEvent = new AutoResetEvent(false); }
private void CloseClientSocket(SocketAsyncEventArgs e) { var token = e.UserToken as AsyncUserToken; token.Dispose(); _acceptedClientsSemaphore.Release(); Interlocked.Decrement(ref _connectedSocketCount); Console.WriteLine("A client has been disconnected from the server. There are {0} clients connected to the server", _connectedSocketCount); _socketAsyncReceiveEventArgsPool.Push(e); }
//____________________________________________________________________________ private void HandleBadAccept(SocketAsyncEventArgs acceptEventArgs) { var acceptOpToken = (acceptEventArgs.UserToken as AcceptOpUserToken); Program.testWriter.WriteLine("Closing socket of accept id " + acceptOpToken.TokenId); //This method closes the socket and releases all resources, both //managed and unmanaged. It internally calls Dispose. acceptEventArgs.AcceptSocket.Close(); //Put the SAEA back in the pool. poolOfAcceptEventArgs.Push(acceptEventArgs); }
/// <summary> /// Initializes the server by preallocating reusable buffers and context objects. These objects do not /// need to be preallocated or reused, by is done this way to illustrate how the API can easily be used /// to create reusable objects to increase server performance. /// </summary> public void Init() { // Allocates one large byte buffer which all I/O operations use a piece of. This gaurds // against memory fragmentation m_bufferManager.InitBuffer(); // preallocate pool of SocketAsyncEventArgs objects SocketAsyncEventArgs readWriteEventArg; for (int i = 0; i < m_numConnections; i++) { //Pre-allocate a set of reusable SocketAsyncEventArgs readWriteEventArg = new SocketAsyncEventArgs(); readWriteEventArg.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed); readWriteEventArg.UserToken = new AsyncUserToken(); // assign a byte buffer from the buffer pool to the SocketAsyncEventArg object m_bufferManager.SetBuffer(readWriteEventArg); // add SocketAsyncEventArg to the pool m_readWritePool.Push(readWriteEventArg); } }
private void ProcessSend(SocketAsyncEventArgs e) { _socketAsyncSendEventArgsPool.Push(e); waitSendEvent.Set(); }