Exemple #1
0
        /// <summary>
        /// 初始化发送对象池
        /// </summary>
        /// <param name="maxNumber"></param>
        private void InitializePool(int maxNumberOfConnections)
        {
            if (sendTokenManager != null)
            {
                sendTokenManager.Clear();
            }
            if (sBufferManager != null)
            {
                sBufferManager.Clear();
            }

            sendTokenManager = new SocketTokenManager <SocketAsyncEventArgs>(maxNumberOfConnections);
            sBufferManager   = new SocketBufferManager(maxNumberOfConnections, cliRecBufferSize);

            for (int i = 0; i < maxNumberOfConnections; ++i)
            {
                SocketAsyncEventArgs tArgs = new SocketAsyncEventArgs()
                {
                    DisconnectReuseSocket = true
                };
                tArgs.Completed += IO_Completed;
                tArgs.UserToken  = new SocketToken(i)
                {
                    TokenSocket = cliSocket,
                    TokenId     = i
                };
                sBufferManager.SetBuffer(tArgs);
                sendTokenManager.Set(tArgs);
            }
        }
        private void InitializePool(int maxNumberOfConnections)
        {
            if (sendTokenManager != null)
            {
                sendTokenManager.Clear();
            }
            if (sBufferManager != null)
            {
                sBufferManager.Clear();
            }
            int cnt = maxNumberOfConnections + offsetNumber;

            sendTokenManager = new SocketTokenManager <SocketAsyncEventArgs>(cnt);
            sBufferManager   = new SocketBufferManager(cnt, receiveChunkSize);

            for (int i = 1; i <= cnt; ++i)
            {
                SocketAsyncEventArgs tArgs = new SocketAsyncEventArgs()
                {
                    DisconnectReuseSocket = true
                };
                tArgs.Completed += IO_Completed;
                tArgs.UserToken  = new SocketToken(i)
                {
                    TokenSocket = socket,
                    TokenId     = i
                };
                sBufferManager.SetBuffer(tArgs);
                sendTokenManager.Set(tArgs);
            }
        }
 /// <summary>
 /// 构造
 /// </summary>
 /// <param name="blockSize"></param>
 /// <param name="maxNumberOfConnections"></param>
 public TcpClientProvider(int blockSize = 4096, int maxNumberOfConnections = 8)
 {
     this.maxBufferNumber = maxNumberOfConnections;
     this.blockSize       = blockSize;
     this.receiveBuffer   = new byte[blockSize];
     tokenPool            = new SocketTokenManager <SocketAsyncEventArgs>(maxNumberOfConnections);
     sendBufferPool       = new SocketBufferManager(maxNumberOfConnections, blockSize);
     InitializePool(maxNumberOfConnections);
 }
Exemple #4
0
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="maxConnections">最大连接数</param>
        /// <param name="blockSize">接收块缓冲区</param>
        public TcpServerProvider(int maxConnections = 32, int blockSize = 4096)
        {
            this.maxNumber = maxConnections;

            acceptSemphoreClients = new Semaphore(maxConnections, maxConnections);

            acceptPool = new SocketTokenManager <SocketAsyncEventArgs>(maxConnections);
            sendPool   = new SocketTokenManager <SocketAsyncEventArgs>(maxConnections);

            recvBuffer = new SocketBufferManager(maxConnections, blockSize);

            sendBuffer = new SocketBufferManager(maxConnections, blockSize);
        }
Exemple #5
0
        public void Initialize(int maxNumberOfConnections, int bufferSize = 2048)
        {
            receiveBufferPool = new SocketBufferManager(maxNumberOfConnections, bufferSize);
            receivePool       = new SocketTokenManager <SocketAsyncEventArgs>(maxNumberOfConnections);

            for (int i = 0; i < maxNumberOfConnections; ++i)
            {
                SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
                socketArgs.Completed += SocketArgs_Completed;
                receiveBufferPool.SetBuffer(socketArgs);
                receivePool.Push(socketArgs);
            }
        }
        /// <summary>
        /// 初始化客户端发送对象池
        /// </summary>
        public void Initialize(int maxCountClient, int blockSize = 4096)
        {
            this.maxCount  = maxCountClient;
            this.blocksize = blockSize;
            tokenPool      = new SocketTokenManager <SocketAsyncEventArgs>(maxCountClient);
            sentBufferPool = new SocketBufferManager(maxCountClient, blockSize);

            for (int i = 0; i < maxCount; ++i)
            {
                SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
                socketArgs.Completed += new EventHandler <SocketAsyncEventArgs>(ClientSocket_Completed);
                sentBufferPool.SetBuffer(socketArgs);
                tokenPool.Push(socketArgs);
            }
        }
        /// <summary>
        /// 初始化对象
        /// </summary>
        /// <param name="recBufferSize"></param>
        /// <param name="port"></param>
        private void Initialize()
        {
            sendTokenManager  = new SocketTokenManager <SocketAsyncEventArgs>(maxNumberOfConnections);
            sendBufferManager = new SocketBufferManager(maxNumberOfConnections, bufferSizeByConnection);

            //初始化发送接收对象池
            for (int i = 0; i < maxNumberOfConnections; ++i)
            {
                SocketAsyncEventArgs sendArgs = new SocketAsyncEventArgs();
                sendArgs.Completed += IO_Completed;
                sendArgs.UserToken  = cliSocket;
                sendBufferManager.SetBuffer(sendArgs);
                sendTokenManager.Set(sendArgs);
            }
        }
Exemple #8
0
        /// <summary>
        /// 初始化对象
        /// </summary>
        private void Initialize()
        {
            _sendTokenManager  = new SocketTokenManager <SocketAsyncEventArgs>(_maxNumberOfConnections);
            _sendBufferManager = new SocketBufferManager(_maxNumberOfConnections, _bufferSizeByConnection);

            //初始化发送接收对象池
            for (var i = 0; i < _maxNumberOfConnections; ++i)
            {
                var sendArgs = new SocketAsyncEventArgs();
                sendArgs.Completed += IO_Completed;
                sendArgs.UserToken  = Socket;
                _sendBufferManager.SetBuffer(sendArgs);
                _sendTokenManager.Set(sendArgs);
            }
        }
        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="maxConnections">最大连接数</param>
        /// <param name="chunkBufferSize">接收块缓冲区</param>
        public TcpServerProvider(int maxConnections = 32, int chunkBufferSize = 4096)
        {
            if (maxConnections < 2)
            {
                maxConnections = 2;
            }
            this.maxNumberOfConnections = maxConnections;

            maxNumberAcceptedClients = new Semaphore(maxConnections, maxConnections);

            recvBufferManager  = new SocketBufferManager(maxConnections, chunkBufferSize);
            acceptTokenManager = new SocketTokenManager <SocketAsyncEventArgs>(maxConnections);

            sendTokenManager  = new SocketTokenManager <SocketAsyncEventArgs>(maxConnections);
            sendBufferManager = new SocketBufferManager(maxConnections, chunkBufferSize);
        }
        /// <summary>
        /// 初始化客户端发送对象池
        /// </summary>
        /// <param name="maxCountClient"></param>
        /// <param name="blockSize"></param>
        public void Initialize(int maxCountClient, int blockSize = 4096)
        {
            this.maxCount     = maxCountClient;
            this.blockSize    = blockSize;
            sendTokenManager  = new SocketTokenManager <SocketAsyncEventArgs>(maxCountClient);
            sendBufferManager = new SocketBufferManager(maxCountClient, blockSize);

            for (int i = 0; i < maxCount; ++i)
            {
                SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs
                {
                    UserToken = sendSocket
                };
                socketArgs.Completed += ClientSocket_Completed;
                sendBufferManager.SetBuffer(socketArgs);
                sendTokenManager.Set(socketArgs);
            }
        }
Exemple #11
0
        public SocketSend(Socket socket, int maxCountClient, int blockSize = 4096)
            : base(blockSize)
        {
            this.maxCount = maxCountClient;
            base.socket   = socket;

            sendTokenManager  = new SocketTokenManager <SocketAsyncEventArgs>(maxCountClient);
            sendBufferManager = new SocketBufferManager(maxCountClient, blockSize);

            for (int i = 0; i < maxCount; ++i)
            {
                SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs
                {
                    UserToken = base.socket
                };
                socketArgs.Completed += ClientSocket_Completed;
                sendBufferManager.SetBuffer(socketArgs);
                sendTokenManager.Set(socketArgs);
            }
        }
Exemple #12
0
        /// <summary>
        /// 初始化发送对象
        /// </summary>
        /// <param name="maxCountClient">客户端最大数</param>
        public SocketSend(int maxCountClient, int blockSize = 4096)
            : base(blockSize)
        {
            this.maxCount         = maxCountClient;
            socket                = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            socket.ReceiveTimeout = receiveTimeout;
            socket.SendTimeout    = sendTimeout;

            sendTokenManager  = new SocketTokenManager <SocketAsyncEventArgs>(maxCountClient);
            sendBufferManager = new SocketBufferManager(maxCountClient, blockSize);

            for (int i = 0; i < maxCount; ++i)
            {
                SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs
                {
                    UserToken = socket
                };
                socketArgs.Completed += ClientSocket_Completed;
                sendBufferManager.SetBuffer(socketArgs);
                sendTokenManager.Set(socketArgs);
            }
        }
Exemple #13
0
        private void InitializePool(int maxNumberOfConnections)
        {
            _sendTokenManager?.Clear();
            _sBufferManager?.Clear();

            _sendTokenManager = new SocketTokenManager <SocketAsyncEventArgs>(maxNumberOfConnections);
            _sBufferManager   = new SocketBufferManager(maxNumberOfConnections, receiveChunkSize);

            for (var i = 0; i < maxNumberOfConnections; ++i)
            {
                var tArgs = new SocketAsyncEventArgs()
                {
                    DisconnectReuseSocket = true
                };
                tArgs.Completed += IO_Completed;
                tArgs.UserToken  = new SocketToken(i)
                {
                    TokenSocket = socket,
                    TokenId     = i
                };
                _sBufferManager.SetBuffer(tArgs);
                _sendTokenManager.Set(tArgs);
            }
        }
Exemple #14
0
        /// <summary>
        /// 初始化发送对象
        /// </summary>
        /// <param name="maxCountClient">客户端最大数</param>
        /// <param name="blockSize"></param>
        public SocketSend(int maxCountClient, int blockSize = 4096)
            : base(blockSize)
        {
            _maxCount = maxCountClient;
            Socket    = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
            {
                ReceiveTimeout = ReceiveTimeout,
                SendTimeout    = SendTimeout
            };

            _sendTokenManager  = new SocketTokenManager <SocketAsyncEventArgs>(maxCountClient);
            _sendBufferManager = new SocketBufferManager(maxCountClient, blockSize);

            for (var i = 0; i < _maxCount; ++i)
            {
                var socketArgs = new SocketAsyncEventArgs
                {
                    UserToken = Socket
                };
                socketArgs.Completed += ClientSocket_Completed;
                _sendBufferManager.SetBuffer(socketArgs);
                _sendTokenManager.Set(socketArgs);
            }
        }