Esempio n. 1
0
        // Create an uninitialized server instance.
        // To start the server listening for connection requests
        // call the Init method followed by Start method
        //
        // <param name="numConnections">the maximum number of connections the sample is designed to handle simultaneously</param>
        // <param name="receiveBufferSize">buffer size to use for each socket I/O operation</param>
        public TcpServerV1(int numConnections, int receiveBufferSize)
        {
            maxTotalBytesRead = 0;
            maxNumOfConnectedSockets = 0;
            maxNumOfConnections = numConnections;
            maxReceiveBufferSize = receiveBufferSize;
            // allocate buffers such that the maximum number of sockets can have one outstanding read and
            //write posted to the socket simultaneously
            bufferManager = new BufferManager(numConnections * operationPClient * receiveBufferSize, receiveBufferSize);

            readWritePool = new SocketAsyncEventArgsPool(numConnections);
            maxNumberOfAcceptedClients = new Semaphore(numConnections, numConnections);
        }
Esempio n. 2
0
        /// <summary>
        /// Create an uninitialized server instance.  
        /// To start the server listening for connection requests
        /// call the Init method followed by Start method.
        /// </summary>
        /// <param name="numConnections">Maximum number of connections to be handled simultaneously.</param>
        /// <param name="bufferSize">Buffer size to use for each socket I/O operation.</param>
        internal SocketListener(Int32 numConnections, Int32 bufferSize)
        {
            log.InfoFormat("{0}/SocketListener", _fileNm);

            // TODO
            // Manually handle client disconnect, donot want to wait connections in OS pool
            // numConnections = numConnections + 2;

            this.numConnectedSockets = 0;
            this.numConnections = numConnections;
            this.bufferSize = bufferSize;

            this.readWritePool = new SocketAsyncEventArgsPool(numConnections);
            this.semaphoreAcceptedClients = new Semaphore(numConnections, numConnections);

            // Preallocate pool of SocketAsyncEventArgs objects.
            for (Int32 i = 0; i < this.numConnections; i++)
            {
                SocketAsyncEventArgs readWriteEventArg = new SocketAsyncEventArgs();
                readWriteEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(OnIOCompleted);
                readWriteEventArg.SetBuffer(new Byte[this.bufferSize], 0, this.bufferSize);

                // Add SocketAsyncEventArg to the pool.
                this.readWritePool.Push(readWriteEventArg);
            }
            log.InfoFormat("{0}/SocketListener Finished", _fileNm);
        }