Пример #1
0
        public async Task PumpAsync(Func <ProtocolHeader, bool> onHeader, Func <ByteBuffer, bool> onBuffer)
        {
            byte[] header = new byte[FixedWidth.ULong];

            if (onHeader != null)
            {
                // header
                await this.ReceiveBufferAsync(header, 0, FixedWidth.ULong);

                if (!onHeader(ProtocolHeader.Create(header, 0)))
                {
                    return;
                }
            }

            // frames
            while (true)
            {
                await this.ReceiveBufferAsync(header, 0, FixedWidth.UInt);

                int frameSize = AmqpBitConverter.ReadInt(header, 0);

                byte[] buffer = new byte[frameSize];
                Buffer.BlockCopy(header, 0, buffer, 0, FixedWidth.UInt);

                await this.ReceiveBufferAsync(buffer, FixedWidth.UInt, frameSize - FixedWidth.UInt);

                if (!onBuffer(new ByteBuffer(buffer, 0, frameSize, frameSize)))
                {
                    break;
                }
            }
        }
Пример #2
0
        void ReadFrame(out byte frameType, out ushort channel, out ulong code, out List fields, out ByteBuffer payload)
        {
            byte[] headerBuffer = this.ReadFixedSizeBuffer(8);
            int    size         = AmqpBitConverter.ReadInt(headerBuffer, 0);

            frameType = headerBuffer[5];    // TOOD: header EXT
            channel   = (ushort)(headerBuffer[6] << 8 | headerBuffer[7]);

            size -= 8;
            if (size > 0)
            {
                byte[]     frameBuffer = this.ReadFixedSizeBuffer(size);
                ByteBuffer buffer      = new ByteBuffer(frameBuffer, 0, size, size);
                Fx.AssertAndThrow(ErrorCode.ClientInvalidFormatCodeRead, Encoder.ReadFormatCode(buffer) == FormatCode.Described);

                code   = Encoder.ReadULong(buffer, Encoder.ReadFormatCode(buffer));
                fields = Encoder.ReadList(buffer, Encoder.ReadFormatCode(buffer));
                if (buffer.Length > 0)
                {
                    payload = new ByteBuffer(buffer.Buffer, buffer.Offset, buffer.Length, buffer.Length);
                }
                else
                {
                    payload = null;
                }
            }
            else
            {
                code    = 0;
                fields  = null;
                payload = null;
            }
        }
Пример #3
0
        public async Task PumpAsync(uint maxFrameSize, Func <ProtocolHeader, bool> onHeader, Func <ByteBuffer, bool> onBuffer)
        {
            byte[] header = new byte[FixedWidth.ULong];

            if (onHeader != null)
            {
                // header
                await this.ReceiveBufferAsync(header, 0, FixedWidth.ULong).ConfigureAwait(false);

                Trace.WriteBuffer("RECV {0}", header, 0, header.Length);
                if (!onHeader(ProtocolHeader.Create(header, 0)))
                {
                    return;
                }
            }

            // frames
            while (true)
            {
                await this.ReceiveBufferAsync(header, 0, FixedWidth.UInt).ConfigureAwait(false);

                int frameSize = AmqpBitConverter.ReadInt(header, 0);
                if ((uint)frameSize > maxFrameSize)
                {
                    throw new AmqpException(ErrorCode.InvalidField,
                                            Fx.Format(SRAmqp.InvalidFrameSize, frameSize, maxFrameSize));
                }

                ByteBuffer buffer = this.bufferManager.GetByteBuffer(frameSize);

                try
                {
                    Buffer.BlockCopy(header, 0, buffer.Buffer, buffer.Offset, FixedWidth.UInt);
                    await this.ReceiveBufferAsync(buffer.Buffer, buffer.Offset + FixedWidth.UInt, frameSize - FixedWidth.UInt).ConfigureAwait(false);

                    buffer.Append(frameSize);
                    Trace.WriteBuffer("RECV {0}", buffer.Buffer, buffer.Offset, buffer.Length);

                    if (!onBuffer(buffer))
                    {
                        break;
                    }
                }
                finally
                {
                    buffer.ReleaseReference();
                }
            }
        }
Пример #4
0
        public async Task PumpAsync(Func <ProtocolHeader, bool> onHeader, Func <ByteBuffer, bool> onBuffer)
        {
            byte[] header = new byte[FixedWidth.ULong];

            if (onHeader != null)
            {
                // header
                await this.ReceiveBufferAsync(header, 0, FixedWidth.ULong);

                if (!onHeader(ProtocolHeader.Create(header, 0)))
                {
                    return;
                }
            }

            // frames
            while (true)
            {
                await this.ReceiveBufferAsync(header, 0, FixedWidth.UInt);

                int        frameSize = AmqpBitConverter.ReadInt(header, 0);
                ByteBuffer buffer    = this.bufferManager.GetByteBuffer(frameSize);

                try
                {
                    Buffer.BlockCopy(header, 0, buffer.Buffer, buffer.Offset, FixedWidth.UInt);
                    await this.ReceiveBufferAsync(buffer.Buffer, buffer.Offset + FixedWidth.UInt, frameSize - FixedWidth.UInt);

                    buffer.Append(frameSize);

                    if (!onBuffer(buffer))
                    {
                        break;
                    }
                }
                finally
                {
                    buffer.ReleaseReference();
                }
            }
        }