public void TestBufferPool()
        {
            Assert.IsTrue(2 == pool5Bytes.CurrentPoolSize());
            Assert.IsTrue(3 == pool6Bytes.CurrentPoolSize());

            var buf1 = pool5Bytes.Rent();

            Assert.AreEqual(5, buf1.Memory.Length);
            Assert.AreEqual(0, buf1.SignificantLength);
            Assert.AreEqual(buf1.Pool, pool5Bytes);
            Assert.AreEqual(1, pool5Bytes.CurrentPoolSize());

            //------------
            buf1.Memory[0] = 1;
            buf1.Memory[1] = 2;

            Assert.AreEqual(2, buf1.Memory[1]);
            buf1.SignificantLength = 2;
            buf1.EraseData();
            Assert.AreEqual(0, buf1.Memory[1]);
            Assert.AreEqual(0, buf1.SignificantLength);

            buf1.Pool.Return(buf1);
            Assert.AreEqual(2, pool5Bytes.CurrentPoolSize());
            Assert.IsNull(buf1.Memory);
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <returns>usually null.</returns>
        public override async Task <UdpClient2> Accept()//TODO MaxNumClient limit. //TODO blocklist.
        {
            if (null == _listenerClient)
            {
                return(null);
            }
            try
            {
                #region receive packet
                FixedSizeBuffer     buff = _bufferPool.Rent();
                ArraySegment <byte> seg  = new ArraySegment <byte>(buff.Memory, buff.Offset, buff.Memory.Length);

                //TODO cache
                IPEndPoint clientEndPoint = new IPEndPoint(_listenerClient.Client.LocalEndPoint.AddressFamily == AddressFamily.InterNetworkV6 ?
                                                           IPAddress.IPv6Any : IPAddress.Any, 0);
                var result = await _listenerClient.Client.ReceiveFromAsync(seg, SocketFlags.None, clientEndPoint);// _listenerClient.Client.LocalEndPoint);

                buff.SignificantLength = result.ReceivedBytes;
                #endregion

                //////var result = await _listenerClient.ReceiveAsync();
                var client = _clientManager.Get(result.RemoteEndPoint);
                if (null != client)//
                {
                    #region delivery packet
                    if (client.Active)
                    {
                        client.PostReceived(buff);
                        return(null);
                    }
                    else//inactive
                    {
                        DeleteClient(result.RemoteEndPoint);
                        buff.Pool.Return(buff);//drop packet.
                        return(null);
                    }
                    #endregion
                }
                else
                {
                    #region create client
                    client = CreateClient(_listenerClient.Client, result.RemoteEndPoint as IPEndPoint);
                    client.PostReceived(buff);
                    KeepClient(client);

                    _logger?.LogInformation($"UdpServer New client:[{client.EndPoint.ToString()}].");
                    return(client);

                    #endregion
                }
            }
            catch (SocketException se)
            {
                _logger?.LogError(se, "UdpServer ReceiveFromAsync error.");
                return(null);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <returns>usually null.</returns>
        public override async Task <UdpClient2> Accept()//TODO MaxNumClient limit. //TODO blocklist.
        {
            if (null == _listenerClient)
            {
                return(null);
            }
            try
            {
                #region receive packet
                FixedSizeBuffer     buff = _bufferPool.Rent();
                ArraySegment <byte> seg  = new ArraySegment <byte>(buff.Memory, buff.Offset, buff.Memory.Length);

                //TODO cache
                IPEndPoint clientEndPoint = new IPEndPoint(_listenerClient.Client.LocalEndPoint.AddressFamily == AddressFamily.InterNetworkV6 ?
                                                           IPAddress.IPv6Any : IPAddress.Any, 0);
                var received = await _listenerClient.Client.ReceiveFromAsync(seg, SocketFlags.None, clientEndPoint);// _listenerClient.Client.LocalEndPoint);

                buff.SignificantLength = received.ReceivedBytes;
                #endregion

                //////var received = await _listenerClient.ReceiveAsync();
                var locker = _clientLockers.Get(received.RemoteEndPoint);
                if (null != locker)//
                {
                    #region delivery packet
                    if ((DateTime.Now - locker.Owner.LastActive).TotalMilliseconds <= CLIENT_ACTIVE_TIMEOUT)
                    {
                        locker.PutPacket(buff);
                        return(null);
                    }
                    else//inactive
                    {
                        DeleteLocker(received.RemoteEndPoint);
                        buff.Pool.Return(buff);//drop packet.
                        return(null);
                    }
                    #endregion
                }
                else
                {
                    #region create client
                    locker = new Locker <UdpClient2>(received.RemoteEndPoint as IPEndPoint);
                    locker.PutPacket(buff);
                    KeepLocker(locker);

                    _logger?.LogInformation($"UdpServer New client:[{locker.Number.ToString()}].");

                    return(CreateClient(_listenerClient.Client, locker));

                    #endregion
                }
            }
            catch (SocketException se)
            {
                _logger?.LogError($"UdpServer ReceiveFromAsync error {se.SocketErrorCode}, {se.Message}.");
                return(null);
            }
            catch (Exception ex)
            {
                _logger?.LogError($"UdpServer ReceiveFromAsync error {ex.Message}.");
                return(null);
            }
        }