Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
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();
            }
        }
Exemplo n.º 3
0
        public async Task Send_CloseOk()
        {
            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.CloseOk);

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

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemplo n.º 4
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();
            }
        }
Exemplo n.º 5
0
        public async Task Send_Qos(uint prefetchSize, ushort prefetchCount, bool global)
        {
            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.Qos);
                writer.Write(prefetchSize);
                writer.Write(prefetchCount);
                writer.WriteBits(global);

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

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemplo n.º 6
0
        public async Task Send_Ack(ulong deliveryTag, bool multiple)
        {
            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.Ack);
                writer.Write(deliveryTag);
                writer.WriteBits(multiple);

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

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemplo n.º 7
0
        public async Task Send_Heartbeat()
        {
            var buffer = await socket.GetWriteBuffer();

            try
            {
                if (socket.HeartbeatNeeded)
                {
                    WritePayload();
                }

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

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

                writer.Write(FrameType.Heartbeat);
                writer.Write(connectionChannelNumber);
                writer.Write((uint)0);
                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemplo n.º 8
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();
            }
        }
Exemplo n.º 9
0
        public async Task Send_TuneOk(ushort channelMax, uint frameMax, ushort heartbeat)
        {
            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.TuneOk);
                writer.Write(channelMax);
                writer.Write(frameMax);
                writer.Write(heartbeat);

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

                writer.Write(FrameEnd);

                writer.Commit();
            }
        }
Exemplo n.º 10
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();
            }
        }
Exemplo n.º 11
0
        void WriteContentBodyFrame(ref CustomBufferWriter <PipeWriter> writer, Span <byte> body)
        {
            var payloadSizeHeader = writer.WriteFrameHeader(FrameType.ContentBody, channelNumber);

            writer.Commit();
            var bytesWrittenBefore = (uint)writer.BytesCommitted;

            writer.Write(body);

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

            writer.Write(FrameEnd);
        }
Exemplo n.º 12
0
        void WriteContentHeaderFrame(ref CustomBufferWriter <PipeWriter> writer, MessageProperties properties, ulong length)
        {
            var payloadSizeHeader = writer.WriteFrameHeader(FrameType.ContentHeader, channelNumber);

            writer.Commit();
            var bytesWrittenBefore = (uint)writer.BytesCommitted;

            writer.Write(ClassId.Basic);
            writer.Write(Reserved);
            writer.Write(Reserved);
            writer.Write(length);
            writer.WriteBasicProperties(properties);

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

            writer.Write(FrameEnd);
        }
Exemplo n.º 13
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();
            }
        }
Exemplo n.º 14
0
        public async Task Send_ProtocolHeader()
        {
            var buffer = await socket.GetWriteBuffer();

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

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

                writer.Write(protocolHeader);

                writer.Commit();
            }
        }
Exemplo n.º 15
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();
            }
        }