static async Task Main(string[] args)
        {
            var address = Dns.GetHostAddresses("centos0.mshome.net")[0];
            RabbitMQConnectionBuilder builder = new RabbitMQConnectionBuilder(new IPEndPoint(address, 5672));
            var connection = builder.ConnectionInfo("gamover", "gam2106", "/")
                             .Heartbeat(60 * 10)
                             .ProductName("AMQP.Client.RabbitMQ")
                             .ProductVersion("0.0.1")
                             .ConnectionName("AMQP.Client.RabbitMQ:Test")
                             .ClientInformation("TEST TEST TEST")
                             .ClientCopyright("©")
                             .Build();
            await connection.StartAsync();

            var channel = await connection.CreateChannel();

            var publisher = channel.CreatePublisher();
            ContentHeaderProperties properties = new ContentHeaderProperties();

            properties.AppId         = "testapp";
            properties.CorrelationId = Guid.NewGuid().ToString();
            while (true)
            {
                properties.CorrelationId = Guid.NewGuid().ToString();
                await publisher.Publish("TestExchange", string.Empty, false, false, properties, new byte[32]);
            }
        }
Example #2
0
        async Task StartFloodAsync(RabbitMQChannel channel, string queue, byte[] body, int count)
        {
            var propertiesConsume = new ContentHeaderProperties();

            for (int i = 0; i < count; i++)
            {
                await channel.Publish(string.Empty, queue, false, false, propertiesConsume, body);
            }
        }
        private static async Task RunDefault()
        {
            var address = Dns.GetHostAddresses("centos0.mshome.net")[0];
            RabbitMQConnectionBuilder builder = new RabbitMQConnectionBuilder(new IPEndPoint(address, 5672));
            var connection = builder.ConnectionInfo("gamover", "gam2106", "/")
                             .Heartbeat(60 * 10)
                             .ProductName("AMQP.Client.RabbitMQ")
                             .ProductVersion("0.0.1")
                             .ConnectionName("AMQP.Client.RabbitMQ:Test")
                             .ClientInformation("TEST TEST TEST")
                             .ClientCopyright("©")
                             .Build();
            await connection.StartAsync();

            var channel = await connection.CreateChannel();

            await channel.ExchangeDeclareAsync("TestExchange", ExchangeType.Direct, arguments : new Dictionary <string, object> {
                { "TEST_ARGUMENT", true }
            });

            var queueOk = await channel.QueueDeclareAsync("TestQueue", false, false, false, new Dictionary <string, object> {
                { "TEST_ARGUMENT", true }
            });

            await channel.QueueBindAsync("TestQueue", "TestExchange");

            var publisher = channel.CreatePublisher();
            ContentHeaderProperties properties = new ContentHeaderProperties();

            properties.AppId         = "testapp";
            properties.CorrelationId = Guid.NewGuid().ToString();
            for (var i = 0; i < 500000; i++)
            {
                properties.CorrelationId = Guid.NewGuid().ToString();
                await publisher.Publish("TestExchange", string.Empty, false, false, properties, new byte[32]);
            }

            var consumer = await channel.CreateConsumer("TestQueue", "TestConsumer", noAck : true);

            consumer.Received += async(deliver, result) =>
            {
                //await deliver.Ack();
            };
            await connection.WaitEndReading();
        }
Example #4
0
        private static async Task StartPublisher()
        {
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                builder.AddConsole();
                builder.SetMinimumLevel(LogLevel.Debug);
            });
            var builder = new RabbitMQConnectionFactoryBuilder(new DnsEndPoint(Host, 5672));
            var factory = builder.AddLogger(loggerFactory.CreateLogger(string.Empty))
                          .ConnectionTimeout(TimeSpan.FromSeconds(30))
                          .ConnectionAttempts(100)
                          .Build();
            var connection = factory.CreateConnection();

            await connection.StartAsync();

            var channel = await connection.OpenChannel();

            await channel.ExchangeDeclareAsync(ExchangeDeclare.Create("TestExchange", ExchangeType.Direct));

            await channel.QueueDeclareAsync(QueueDeclare.Create("TestQueue"));

            await channel.QueueBindAsync(QueueBind.Create("TestQueue", "TestExchange"));

            var properties = new ContentHeaderProperties();
            //properties.AppId = "testapp";
            var body = new byte[Size];
            int i    = 0;

            while (true /*!channel.IsClosed*/)
            {
                //properties.CorrelationId = Guid.NewGuid().ToString();
                var result = await channel.Publish("TestExchange", string.Empty, false, false, properties, body);

                //if (!result)
                //{
                //    break;
                //}
            }

            //await Task.Delay(TimeSpan.FromHours(1));
        }
Example #5
0
        private static async Task Main(string[] args)
        {
            var builder    = new RabbitMQConnectionFactoryBuilder(new DnsEndPoint(Host, 5672));
            var factory    = builder.Build();
            var connection = factory.CreateConnection();

            await connection.StartAsync();

            var channel = await connection.OpenChannel();

            await channel.ExchangeDeclareAsync(ExchangeDeclare.Create("TestExchange", ExchangeType.Direct));

            await channel.QueueDeclareAsync(QueueDeclare.Create("TestQueue"));

            await channel.QueueBindAsync(QueueBind.Create("TestQueue", "TestExchange"));

            var properties = new ContentHeaderProperties();

            properties.AppId = "testapp";
            //var body = new byte[16 * 1024 * 1024 + 1];
            var body = new byte[32];

            //var body = new byte[16*1024];
            //var body = new byte[128*1024];
            //var body = new byte[512 * 1024];
            //var body = new byte[1 * 1024 * 1024];
            //var body = new byte[1024];

            while (true /*!channel.IsClosed*/)
            {
                properties.CorrelationId = Guid.NewGuid().ToString();
                await channel.Publish("TestExchange", string.Empty, false, false, properties, body);
            }

            //await Task.Delay(TimeSpan.FromHours(1));
        }
Example #6
0
        public static async Task ChannelTest()
        {
            var loggerFactory = LoggerFactory.Create(builder =>
            {
                //builder.AddConsole();
                builder.SetMinimumLevel(LogLevel.Information);
            });

            var builder = new RabbitMQConnectionFactoryBuilder(new DnsEndPoint(Host, 5672));
            var factory = builder.AddLogger(loggerFactory.CreateLogger(string.Empty))
                          .ConnectionTimeout(TimeSpan.FromSeconds(30))
                          .ConnectionAttempts(100000)
                          .Build();
            var connection = factory.CreateConnection();
            await connection.StartAsync();

            var channel1 = await connection.OpenChannel();

            var channel2 = await connection.OpenChannel();

            await channel1.ExchangeDeclareAsync(ExchangeDeclare.Create("TestExchange", ExchangeType.Direct));

            await channel1.ExchangeDeclareAsync(ExchangeDeclare.CreateNoWait("TestExchange2", ExchangeType.Direct));

            var declareOk = await channel1.QueueDeclareAsync(QueueDeclare.Create("TestQueue"));

            await channel1.QueueDeclareNoWaitAsync(QueueDeclare.Create("TestQueue2"));

            var purgeOk = await channel1.QueuePurgeAsync(QueuePurge.Create("TestQueue"));

            await channel1.QueuePurgeNoWaitAsync(QueuePurge.Create("TestQueue2"));

            await channel1.QueueBindAsync(QueueBind.Create("TestQueue", "TestExchange"));

            await channel1.QueueBindAsync(QueueBind.CreateNoWait("TestQueue2", "TestExchange2"));

            var body1 = new byte[Size];


            var consumer1 = new RabbitMQConsumer(channel1, ConsumeConf.Create("TestQueue", "TestConsumer", true));

            consumer1.Received += async(sender, result) =>
            {
                //await channel1.Ack(deliver.DeliveryTag, true);
                var propertiesConsume = new ContentHeaderProperties();
                propertiesConsume.AppId = "testapp2";
                var published = await channel2.Publish("TestExchange2", string.Empty, false, false, propertiesConsume, body1);
            };

            var consumer2 = new RabbitMQConsumer(channel2, ConsumeConf.Create("TestQueue2", "TestConsumer2", true));

            consumer2.Received += async(sender, result) =>
            {
                //await channel2.Ack(deliver.DeliveryTag, true);
                var propertiesConsume = new ContentHeaderProperties();
                propertiesConsume.AppId = "testapp1";
                var published = await channel1.Publish("TestExchange", string.Empty, false, false, propertiesConsume, body1);
            };
            await channel1.ConsumerStartAsync(consumer1);

            await channel2.ConsumerStartAsync(consumer2);

            var firtsTask = Task.Run(async() =>
            {
                var properties   = new ContentHeaderProperties();
                properties.AppId = "testapp1";
                while (true /*!channel1.IsClosed*/)
                {
                    var published = await channel1.Publish("TestExchange", string.Empty, false, false, properties, body1);
                }
            });
            var secondTask = Task.Run(async() =>
            {
                var properties   = new ContentHeaderProperties();
                properties.AppId = "testapp2";
                while (true /*!channel2.IsClosed*/)
                {
                    var published = await channel2.Publish("TestExchange2", string.Empty, false, false, properties, body1);
                }
            });

            await Task.Delay(TimeSpan.FromHours(1));
        }
Example #7
0
        public async ValueTask <bool> Publish(string exchangeName, string routingKey, bool mandatory, bool immediate, ContentHeaderProperties properties, ReadOnlyMemory <byte> message)
        {
            if (IsClosed)
            {
                return(false);
            }
            Session.LockEvent.Wait();
            var info    = new BasicPublishInfo(exchangeName, routingKey, mandatory, immediate);
            var content = new ContentHeader(60, message.Length, ref properties);

            if (message.Length <= Session.Tune.FrameMax)
            {
                var allinfo = new PublishAllInfo(ChannelId, ref message, ref info, content);
                try
                {
                    await Session.PublishAllAsync(allinfo).ConfigureAwait(false);
                }catch (Exception e)
                {
                    Debugger.Break();
                    var cts = new CancellationTokenSource(Session.Options.ConnectionTimeout);
                    using (var timeoutRegistratiuon = cts.Token.Register(() => cts.Cancel()))
                    {
                        return(await PublishAllContinuation(allinfo, cts.Token));
                    }
                }
            }


            await WriterSemaphore.WaitAsync().ConfigureAwait(false);

            var written     = 0;
            var partialInfo = new PublishPartialInfo(ref info, ref content);
            await Session.PublishPartialAsync(ChannelId, partialInfo).ConfigureAwait(false);

            while (written < content.BodySize)
            {
                var batchCnt = 0;
                while (batchCnt < _publishBatchSize && written < content.BodySize)
                {
                    var writable = Math.Min(Session.Tune.FrameMax, (int)content.BodySize - written);
                    _publishBatch[batchCnt] = message.Slice(written, writable);
                    batchCnt++;
                    written += writable;
                }
                await Session.PublishBodyAsync(ChannelId, _publishBatch).ConfigureAwait(false);

                _publishBatch.AsSpan().Fill(ReadOnlyMemory <byte> .Empty);
            }

            WriterSemaphore.Release();
            return(true);
        }
        private void WriteBitFlagsAndContinuation(ref ContentHeaderProperties properties, ref ValueWriter writer)
        {
            WritePresence(properties.ContentType != null);
            WritePresence(properties.ContentEncoding != null);
            WritePresence(properties.Headers != null);
            WritePresence(properties.DeliveryMode != 0);
            WritePresence(properties.Priority != 0);
            WritePresence(properties.CorrelationId != null);
            WritePresence(properties.ReplyTo != null);
            WritePresence(properties.Expiration != null);
            WritePresence(properties.MessageId != null);
            WritePresence(properties.Timestamp != 0);
            WritePresence(properties.Type != null);
            WritePresence(properties.UserId != null);
            WritePresence(properties.AppId != null);
            WritePresence(properties.ClusterId != null);
            writer.WriteShortInt(_flagWord);
            if (properties.ContentType != null)
            {
                writer.WriteShortStr(properties.ContentType);
            }

            if (properties.ContentEncoding != null)
            {
                writer.WriteShortStr(properties.ContentEncoding);
            }

            if (properties.Headers != null)
            {
                writer.WriteTable(properties.Headers);
            }

            if (properties.DeliveryMode != 0)
            {
                writer.WriteOctet(properties.DeliveryMode);
            }

            if (properties.Priority != 0)
            {
                writer.WriteOctet(properties.Priority);
            }

            if (properties.CorrelationId != null)
            {
                writer.WriteShortStr(properties.CorrelationId);
            }

            if (properties.ReplyTo != null)
            {
                writer.WriteShortStr(properties.ReplyTo);
            }

            if (properties.Expiration != null)
            {
                writer.WriteShortStr(properties.Expiration);
            }

            if (properties.MessageId != null)
            {
                writer.WriteShortStr(properties.MessageId);
            }

            if (properties.Timestamp != 0)
            {
                writer.WriteLongLong(properties.Timestamp);
            }

            if (properties.Type != null)
            {
                writer.WriteShortStr(properties.Type);
            }

            if (properties.UserId != null)
            {
                writer.WriteShortStr(properties.UserId);
            }

            if (properties.AppId != null)
            {
                writer.WriteShortStr(properties.AppId);
            }

            if (properties.ClusterId != null)
            {
                writer.WriteShortStr(properties.ClusterId);
            }
        }
        private bool ReadBitFlagsAndContinuation(ushort flags, ref ValueReader reader, ref ContentHeaderProperties properties)
        {
            var m_contentType_present     = ReadPresence(flags);
            var m_contentEncoding_present = ReadPresence(flags);
            var m_headers_present         = ReadPresence(flags);
            var m_deliveryMode_present    = ReadPresence(flags);
            var m_priority_present        = ReadPresence(flags);
            var m_correlationId_present   = ReadPresence(flags);
            var m_replyTo_present         = ReadPresence(flags);
            var m_expiration_present      = ReadPresence(flags);
            var m_messageId_present       = ReadPresence(flags);
            var m_timestamp_present       = ReadPresence(flags);
            var m_type_present            = ReadPresence(flags);
            var m_userId_present          = ReadPresence(flags);
            var m_appId_present           = ReadPresence(flags);
            var m_clusterId_present       = ReadPresence(flags);

            if (m_contentType_present)
            {
                if (!reader.ReadShortStr(out var m_contentType))
                {
                    return(false);
                }
                properties.ContentType = m_contentType;
            }
            if (m_contentEncoding_present)
            {
                if (!reader.ReadShortStr(out var m_contentEncoding))
                {
                    return(false);
                }
                properties.ContentEncoding = m_contentEncoding;
            }
            if (m_headers_present)
            {
                if (!reader.ReadTable(out var m_headers))
                {
                    return(false);
                }
                properties.Headers = m_headers;
            }
            if (m_deliveryMode_present)
            {
                if (!reader.ReadOctet(out var m_deliveryMode))
                {
                    return(false);
                }
                properties.DeliveryMode = m_deliveryMode;
            }
            if (m_priority_present)
            {
                if (!reader.ReadOctet(out var m_priority))
                {
                    return(false);
                }
                properties.Priority = m_priority;
            }
            if (m_correlationId_present)
            {
                if (!reader.ReadShortStr(out var m_correlationId))
                {
                    return(false);
                }
                properties.CorrelationId = m_correlationId;
            }
            if (m_replyTo_present)
            {
                if (!reader.ReadShortStr(out var m_replyTo))
                {
                    return(false);
                }
                properties.ReplyTo = m_replyTo;
            }
            if (m_expiration_present)
            {
                if (!reader.ReadShortStr(out var m_expiration))
                {
                    return(false);
                }
                properties.Expiration = m_expiration;
            }
            if (m_messageId_present)
            {
                if (!reader.ReadShortStr(out var m_messageId))
                {
                    return(false);
                }
                properties.MessageId = m_messageId;
            }
            if (m_timestamp_present)
            {
                if (!reader.ReadTimestamp(out var m_timestamp))
                {
                    return(false);
                }
                properties.Timestamp = m_timestamp;
            }
            if (m_type_present)
            {
                if (!reader.ReadShortStr(out var m_type))
                {
                    return(false);
                }
                properties.Type = m_type;
            }
            if (m_userId_present)
            {
                if (!reader.ReadShortStr(out var m_userId))
                {
                    return(false);
                }
                properties.UserId = m_userId;
            }
            if (m_appId_present)
            {
                if (!reader.ReadShortStr(out var m_appId))
                {
                    return(false);
                }
                properties.AppId = m_appId;
            }
            if (m_clusterId_present)
            {
                if (!reader.ReadShortStr(out var m_clusterId))
                {
                    return(false);
                }
                properties.ClusterId = m_clusterId;
            }

            return(true);
        }
        public async ValueTask Publish(string exchangeName, string routingKey, bool mandatory, bool immediate, ContentHeaderProperties properties, byte[] message)
        {
            var info    = new BasicPublishInfo(exchangeName, routingKey, mandatory, immediate);
            var content = new ContentHeader(60, message.Length, ref properties);
            await _protocol.Writer.WriteAsync(new BasicPublishWriter(_channelId), info).ConfigureAwait(false);

            await _protocol.Writer.WriteAsync(new ContentHeaderWriter(_channelId), content).ConfigureAwait(false);

            await _protocol.Writer.WriteAsync(new BodyFrameWriter(_channelId), message).ConfigureAwait(false);
        }
        public async ValueTask Publish(string exchangeName, string routingKey, bool mandatory, bool immediate, ContentHeaderProperties properties, Action <IBufferWriter <byte> > callback)
        {
            var info = new BasicPublishInfo(exchangeName, routingKey, mandatory, immediate);

            var writer = _protocol.Context.Transport.Output;
            await _protocol.Writer.WriteAsync(new BasicPublishWriter(_channelId), info).ConfigureAwait(false);

            callback(writer);
            var oneByte = writer.GetMemory(1);

            oneByte.Span[0] = Constants.FrameEnd;
            await writer.FlushAsync().ConfigureAwait(false);
        }