Exemple #1
0
        public async Task Send_Publish(string exchange, string routingKey, bool mandatory, MessageProperties properties, Memory <byte> bodyBytes)
        {
            var buffer = await socket.GetWriteBuffer();

            try
            {
                WritePayload();
                await buffer.FlushAsync();
            }
            finally
            {
                socket.ReleaseWriteBuffer();
            }

            void WritePayload()
            {
                var writer = new CustomBufferWriter <PipeWriter>(buffer);

                var payloadSizeHeader = writer.WriteFrameHeader(FrameType.Method, channelNumber);

                writer.Write(Method.Basic.Publish);
                writer.Write(Reserved);
                writer.Write(Reserved);
                writer.WriteShortString(exchange);
                writer.WriteShortString(routingKey);
                writer.WriteBits(mandatory);

                writer.Commit();
                BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - FrameHeaderSize));

                writer.Write(FrameEnd);

                var body = bodyBytes.Span;

                WriteContentHeaderFrame(ref writer, properties, (ulong)body.Length);

                var framesToWrite = body.Length > 0;

                while (framesToWrite)
                {
                    Span <byte> frame;

                    if (body.Length > maxContentBodySize)
                    {
                        frame = body.Slice(0, (int)maxContentBodySize);
                        body  = body.Slice((int)maxContentBodySize);
                    }
                    else
                    {
                        frame         = body;
                        framesToWrite = false;
                    }

                    WriteContentBodyFrame(ref writer, frame);
                }

                writer.Commit();
            }
        }
Exemple #2
0
        public async Task Send_Delete(string exchange, bool onlyIfUnused)
        {
            var buffer = await socket.GetWriteBuffer();

            try
            {
                WritePayload();
                await buffer.FlushAsync();
            }
            finally
            {
                socket.ReleaseWriteBuffer();
            }

            void WritePayload()
            {
                var writer = new CustomBufferWriter <PipeWriter>(buffer);

                var payloadSizeHeader = writer.WriteFrameHeader(FrameType.Method, channelNumber);

                writer.Write(Method.Exchange.Delete);
                writer.Write(Reserved);
                writer.Write(Reserved);
                writer.WriteShortString(exchange);
                writer.WriteBits(onlyIfUnused);

                writer.Commit();
                BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - FrameHeaderSize));

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemple #3
0
        public async Task Send_Close(ushort replyCode, string replyText, ushort failingClass, ushort failingMethod)
        {
            var buffer = await socket.GetWriteBuffer();

            try
            {
                WritePayload();
                await buffer.FlushAsync();
            }
            finally
            {
                socket.ReleaseWriteBuffer();
            }

            void WritePayload()
            {
                var writer = new CustomBufferWriter <PipeWriter>(buffer);

                var payloadSizeHeader = writer.WriteFrameHeader(FrameType.Method, channelNumber);

                writer.Write(Method.Channel.Close);
                writer.Write(replyCode);
                writer.WriteShortString(replyText);
                writer.Write(failingClass);
                writer.Write(failingMethod);

                writer.Commit();
                BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - FrameHeaderSize));

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemple #4
0
        public async Task Send_Cancel(string consumerTag)
        {
            var buffer = await socket.GetWriteBuffer();

            try
            {
                WritePayload();
                await buffer.FlushAsync();
            }
            finally
            {
                socket.ReleaseWriteBuffer();
            }

            void WritePayload()
            {
                var writer = new CustomBufferWriter <PipeWriter>(buffer);

                var payloadSizeHeader = writer.WriteFrameHeader(FrameType.Method, channelNumber);

                writer.Write(Method.Basic.Cancel);
                writer.WriteShortString(consumerTag);
                writer.WriteBits();

                writer.Commit();
                BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - FrameHeaderSize));

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemple #5
0
        public async Task Send_Declare(string queueName, bool passive, bool durable, bool exclusive, bool autoDelete, Dictionary <string, object> arguments)
        {
            var buffer = await socket.GetWriteBuffer();

            try
            {
                WritePayload();
                await buffer.FlushAsync();
            }
            finally
            {
                socket.ReleaseWriteBuffer();
            }

            void WritePayload()
            {
                var writer = new CustomBufferWriter <PipeWriter>(buffer);

                var payloadSizeHeader = writer.WriteFrameHeader(FrameType.Method, channelNumber);

                writer.Write(Method.Queue.Declare);
                writer.Write(Reserved);
                writer.Write(Reserved);
                writer.WriteShortString(queueName);
                writer.WriteBits(passive, durable, exclusive, autoDelete);
                writer.WriteTable(arguments);

                writer.Commit();
                BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - FrameHeaderSize));

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemple #6
0
        public async Task Send_Open(string virtualHost)
        {
            var buffer = await socket.GetWriteBuffer();

            try
            {
                WritePayload();
                await buffer.FlushAsync();
            }
            finally
            {
                socket.ReleaseWriteBuffer();
            }

            void WritePayload()
            {
                var writer = new CustomBufferWriter <PipeWriter>(buffer);

                var payloadSizeHeader = writer.WriteFrameHeader(FrameType.Method, connectionChannelNumber);

                writer.Write(Method.Connection.Open);
                writer.WriteShortString(virtualHost);
                writer.Write(Reserved);
                writer.Write(Reserved);

                writer.Commit();
                BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - FrameHeaderSize));

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemple #7
0
        public async Task Send_StartOk(string connectionName, string userName, string password, string mechanism = "PLAIN", string locale = "en_US")
        {
            var buffer = await socket.GetWriteBuffer();

            try
            {
                WritePayload();
                await buffer.FlushAsync();
            }
            finally
            {
                socket.ReleaseWriteBuffer();
            }

            void WritePayload()
            {
                var writer = new CustomBufferWriter <PipeWriter>(buffer);

                var payloadSizeHeader = writer.WriteFrameHeader(FrameType.Method, connectionChannelNumber);

                writer.Write(Method.Connection.StartOk);

                var clientProperties = new Dictionary <string, object>
                {
                    { "product", "Angora" },
                    { "capabilities", capabilities },
                    { "connection_name", connectionName }
                };

                writer.WriteTable(clientProperties);
                writer.WriteShortString(mechanism);
                writer.WriteLongString($"\0{userName}\0{password}"); //response
                writer.WriteShortString(locale);

                writer.Commit();
                BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - FrameHeaderSize));

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemple #8
0
        public async Task Send_Bind(string source, string destination, string routingKey, Dictionary <string, object> arguments)
        {
            var buffer = await socket.GetWriteBuffer();

            try
            {
                WritePayload();
                await buffer.FlushAsync();
            }
            finally
            {
                socket.ReleaseWriteBuffer();
            }

            void WritePayload()
            {
                var writer = new CustomBufferWriter <PipeWriter>(buffer);

                var payloadSizeHeader = writer.WriteFrameHeader(FrameType.Method, channelNumber);

                writer.Write(Method.Exchange.Bind);
                writer.Write(Reserved);
                writer.Write(Reserved);
                writer.WriteShortString(source);
                writer.WriteShortString(destination);
                writer.WriteShortString(routingKey);
                writer.WriteBits();
                writer.WriteTable(arguments);

                writer.Commit();
                BinaryPrimitives.WriteUInt32BigEndian(payloadSizeHeader, ((uint)writer.BytesCommitted - FrameHeaderSize));

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }