public ushort ReadShort()
        {
            ushort result = NetworkOrderDeserializer.ReadUInt16(Span);

            _offset += 2;
            return(result);
        }
        public ushort ReadShort()
        {
            ushort result = NetworkOrderDeserializer.ReadUInt16(_memory.Slice(_memoryOffset));

            _memoryOffset += 2;
            return(result);
        }
 public void ReadFlagWord()
 {
     if (!ContinuationBitSet)
     {
         throw new MalformedFrameException("Attempted to read flag word when none advertised");
     }
     m_flagWord     = NetworkOrderDeserializer.ReadUInt16(_memory.Slice(_memoryOffset));
     _memoryOffset += 2;
     m_bitCount     = 0;
 }
 private void ReadBits()
 {
     if (!ContinuationBitSet)
     {
         throw new MalformedFrameException("Attempted to read flag word when none advertised");
     }
     _bits    = NetworkOrderDeserializer.ReadUInt16(Span);
     _offset += 2;
     _bitMask = StartBitMask;
 }
示例#5
0
        public Command HandleFrame(InboundFrame f)
        {
            switch (m_state)
            {
            case AssemblyState.ExpectingMethod:
                if (!f.IsMethod())
                {
                    throw new UnexpectedFrameException(f);
                }
                m_method = m_protocol.DecodeMethodFrom(f.Payload);
                m_state  = m_method.HasContent ? AssemblyState.ExpectingContentHeader : AssemblyState.Complete;
                return(CompletedCommand());

            case AssemblyState.ExpectingContentHeader:
                if (!f.IsHeader())
                {
                    throw new UnexpectedFrameException(f);
                }
                m_header = m_protocol.DecodeContentHeaderFrom(NetworkOrderDeserializer.ReadUInt16(f.Payload));
                ulong totalBodyBytes = m_header.ReadFrom(f.Payload.Slice(2));
                if (totalBodyBytes > MaxArrayOfBytesSize)
                {
                    throw new UnexpectedFrameException(f);
                }

                m_remainingBodyBytes = (int)totalBodyBytes;
                m_body = MemoryPool <byte> .Shared.Rent(m_remainingBodyBytes);

                UpdateContentBodyState();
                return(CompletedCommand());

            case AssemblyState.ExpectingContentBody:
                if (!f.IsBody())
                {
                    throw new UnexpectedFrameException(f);
                }

                if (f.Payload.Length > m_remainingBodyBytes)
                {
                    throw new MalformedFrameException($"Overlong content body received - {m_remainingBodyBytes} bytes remaining, {f.Payload.Length} bytes received");
                }

                f.Payload.CopyTo(m_body.Memory.Slice(_offset));
                m_remainingBodyBytes -= f.Payload.Length;
                _offset += f.Payload.Length;
                UpdateContentBodyState();
                return(CompletedCommand());

            case AssemblyState.Complete:
            default:
                return(null);
            }
        }
示例#6
0
        public static InboundFrame ReadFrom(Stream reader)
        {
            int type;

            try
            {
                type = reader.ReadByte();
                if (type == -1)
                {
                    throw new EndOfStreamException("Reached the end of the stream. Possible authentication failure.");
                }
            }
            catch (IOException ioe) when
                (ioe.InnerException != null &&
                (ioe.InnerException is SocketException) &&
                ((SocketException)ioe.InnerException).SocketErrorCode == SocketError.TimedOut)
            {
                throw ioe.InnerException;
            }

            if (type == 'A')
            {
                // Probably an AMQP protocol header, otherwise meaningless
                ProcessProtocolHeader(reader);
            }

            using IMemoryOwner <byte> headerMemory = MemoryPool <byte> .Shared.Rent(6);

            Memory <byte> headerSlice = headerMemory.Memory.Slice(0, 6);

            reader.Read(headerSlice);
            int channel                 = NetworkOrderDeserializer.ReadUInt16(headerSlice);
            int payloadSize             = NetworkOrderDeserializer.ReadInt32(headerSlice.Slice(2)); // FIXME - throw exn on unreasonable value
            IMemoryOwner <byte> payload = MemoryPool <byte> .Shared.Rent(payloadSize);

            int bytesRead = 0;

            try
            {
                while (bytesRead < payloadSize)
                {
                    bytesRead += reader.Read(payload.Memory[bytesRead..payloadSize]);
 public void TestReadUInt16()
 {
     Assert.Equal(0x89AB, NetworkOrderDeserializer.ReadUInt16(new byte[] { 0x89, 0xAB }.AsSpan()));
 }
示例#8
0
        internal static InboundFrame ReadFrom(Stream reader)
        {
            int type;

            try
            {
                type = reader.ReadByte();
                if (type == -1)
                {
                    throw new EndOfStreamException("Reached the end of the stream. Possible authentication failure.");
                }
            }
            catch (IOException ioe)
            {
                // If it's a WSAETIMEDOUT SocketException, unwrap it.
                // This might happen when the limit of half-open connections is
                // reached.
                if (ioe.InnerException == null ||
                    !(ioe.InnerException is SocketException) ||
                    ((SocketException)ioe.InnerException).SocketErrorCode != SocketError.TimedOut)
                {
                    throw ioe;
                }
                throw ioe.InnerException;
            }

            if (type == 'A')
            {
                // Probably an AMQP protocol header, otherwise meaningless
                ProcessProtocolHeader(reader);
            }

            using (IMemoryOwner <byte> headerMemory = MemoryPool <byte> .Shared.Rent(6))
            {
                Memory <byte> headerSlice = headerMemory.Memory.Slice(0, 6);
                reader.Read(headerSlice);
                int channel                 = NetworkOrderDeserializer.ReadUInt16(headerSlice);
                int payloadSize             = NetworkOrderDeserializer.ReadInt32(headerSlice.Slice(2)); // FIXME - throw exn on unreasonable value
                IMemoryOwner <byte> payload = MemoryPool <byte> .Shared.Rent(payloadSize);

                int bytesRead = 0;
                try
                {
                    while (bytesRead < payloadSize)
                    {
                        bytesRead += reader.Read(payload.Memory.Slice(bytesRead, payloadSize - bytesRead));
                    }
                }
                catch (Exception)
                {
                    // Early EOF.
                    throw new MalformedFrameException($"Short frame - expected to read {payloadSize} bytes, only got {bytesRead} bytes");
                }

                int frameEndMarker = reader.ReadByte();
                if (frameEndMarker != Constants.FrameEnd)
                {
                    throw new MalformedFrameException("Bad frame end marker: " + frameEndMarker);
                }

                return(new InboundFrame((FrameType)type, channel, payload, payloadSize));
            }
        }
示例#9
0
 public static int ReadShort(ReadOnlySpan <byte> span, out ushort value)
 {
     value = NetworkOrderDeserializer.ReadUInt16(span);
     return(2);
 }
示例#10
0
        internal static InboundFrame ReadFrom(Stream reader, byte[] frameHeaderBuffer)
        {
            int type = default;

            try
            {
                type = reader.ReadByte();
            }
            catch (IOException ioe)
            {
                // If it's a WSAETIMEDOUT SocketException, unwrap it.
                // This might happen when the limit of half-open connections is
                // reached.
                if (ioe.InnerException == null ||
                    !(ioe.InnerException is SocketException) ||
                    ((SocketException)ioe.InnerException).SocketErrorCode != SocketError.TimedOut)
                {
                    throw;
                }

                ExceptionDispatchInfo.Capture(ioe.InnerException).Throw();
            }

            switch (type)
            {
            case -1:
                throw new EndOfStreamException("Reached the end of the stream. Possible authentication failure.");

            case 'A':
                // Probably an AMQP protocol header, otherwise meaningless
                ProcessProtocolHeader(reader);
                break;
            }

            reader.Read(frameHeaderBuffer, 0, frameHeaderBuffer.Length);
            int channel     = NetworkOrderDeserializer.ReadUInt16(new ReadOnlySpan <byte>(frameHeaderBuffer));
            int payloadSize = NetworkOrderDeserializer.ReadInt32(new ReadOnlySpan <byte>(frameHeaderBuffer, 2, 4)); // FIXME - throw exn on unreasonable value

            const int EndMarkerLength = 1;
            // Is returned by InboundFrame.ReturnPayload in Connection.MainLoopIteration
            var readSize = payloadSize + EndMarkerLength;

            byte[] payloadBytes = ArrayPool <byte> .Shared.Rent(readSize);

            int bytesRead = 0;

            try
            {
                while (bytesRead < readSize)
                {
                    bytesRead += reader.Read(payloadBytes, bytesRead, readSize - bytesRead);
                }
            }
            catch (Exception)
            {
                // Early EOF.
                ArrayPool <byte> .Shared.Return(payloadBytes);

                throw new MalformedFrameException($"Short frame - expected to read {readSize} bytes, only got {bytesRead} bytes");
            }

            if (payloadBytes[payloadSize] != Constants.FrameEnd)
            {
                ArrayPool <byte> .Shared.Return(payloadBytes);

                throw new MalformedFrameException($"Bad frame end marker: {payloadBytes[payloadSize]}");
            }

            return(new InboundFrame((FrameType)type, channel, new Memory <byte>(payloadBytes, 0, payloadSize), payloadBytes));
        }
        internal static InboundFrame ReadFrom(Stream reader)
        {
            int type = default;

            try
            {
                type = reader.ReadByte();
                if (type == -1)
                {
                    throw new EndOfStreamException("Reached the end of the stream. Possible authentication failure.");
                }
            }
            catch (IOException ioe)
            {
                // If it's a WSAETIMEDOUT SocketException, unwrap it.
                // This might happen when the limit of half-open connections is
                // reached.
                if (ioe.InnerException == null ||
                    !(ioe.InnerException is SocketException) ||
                    ((SocketException)ioe.InnerException).SocketErrorCode != SocketError.TimedOut)
                {
                    throw;
                }

                ExceptionDispatchInfo.Capture(ioe.InnerException).Throw();
            }

            if (type == 'A')
            {
                // Probably an AMQP protocol header, otherwise meaningless
                ProcessProtocolHeader(reader);
            }

            Span <byte> headerBytes = stackalloc byte[6];

            reader.Read(headerBytes);
            int channel     = NetworkOrderDeserializer.ReadUInt16(headerBytes);
            int payloadSize = NetworkOrderDeserializer.ReadInt32(headerBytes.Slice(2)); // FIXME - throw exn on unreasonable value

            // Is returned by InboundFrame.Dispose in Connection.MainLoopIteration
            byte[] payloadBytes = ArrayPool <byte> .Shared.Rent(payloadSize);

            Memory <byte> payload   = new Memory <byte>(payloadBytes, 0, payloadSize);
            int           bytesRead = 0;

            try
            {
                while (bytesRead < payloadSize)
                {
                    bytesRead += reader.Read(payload.Slice(bytesRead, payloadSize - bytesRead));
                }
            }
            catch (Exception)
            {
                // Early EOF.
                ArrayPool <byte> .Shared.Return(payloadBytes);

                throw new MalformedFrameException($"Short frame - expected to read {payloadSize} bytes, only got {bytesRead} bytes");
            }

            int frameEndMarker = reader.ReadByte();

            if (frameEndMarker != Constants.FrameEnd)
            {
                ArrayPool <byte> .Shared.Return(payloadBytes);

                throw new MalformedFrameException($"Bad frame end marker: {frameEndMarker}");
            }

            return(new InboundFrame((FrameType)type, channel, payload));
        }