Ejemplo n.º 1
0
        public Command HandleFrame(Frame f)
        {
            switch (m_state)
            {
            case AssemblyState.ExpectingMethod:
            {
                if (f.Type != Constants.FrameMethod)
                {
                    throw new UnexpectedFrameException(f);
                }
                m_command.Method = m_protocol.DecodeMethodFrom(f.GetReader());
                m_state          = m_command.Method.HasContent
                        ? AssemblyState.ExpectingContentHeader
                        : AssemblyState.Complete;
                return(CompletedCommand());
            }

            case AssemblyState.ExpectingContentHeader:
            {
                if (f.Type != Constants.FrameHeader)
                {
                    throw new UnexpectedFrameException(f);
                }
                NetworkBinaryReader reader = f.GetReader();
                m_command.Header     = m_protocol.DecodeContentHeaderFrom(reader);
                m_remainingBodyBytes = m_command.Header.ReadFrom(reader);
                UpdateContentBodyState();
                return(CompletedCommand());
            }

            case AssemblyState.ExpectingContentBody:
            {
                if (f.Type != Constants.FrameBody)
                {
                    throw new UnexpectedFrameException(f);
                }
                byte[] fragment = f.Payload;
                m_command.AppendBodyFragment(fragment);
                if ((ulong)fragment.Length > m_remainingBodyBytes)
                {
                    throw new MalformedFrameException
                              (string.Format("Overlong content body received - {0} bytes remaining, {1} bytes received",
                                             m_remainingBodyBytes,
                                             fragment.Length));
                }
                m_remainingBodyBytes -= (ulong)fragment.Length;
                UpdateContentBodyState();
                return(CompletedCommand());
            }

            case AssemblyState.Complete:
            default:
#if NETFX_CORE
                Debug.WriteLine("Received frame in invalid state {0}; {1}", m_state, f);
#else
                Trace.Fail(string.Format("Received frame in invalid state {0}; {1}", m_state, f));
#endif
                return(null);
            }
        }
Ejemplo n.º 2
0
        public Command HandleFrame(InboundFrame f)
        {
            switch (m_state)
            {
            case AssemblyState.ExpectingMethod:
            {
                if (!f.IsMethod())
                {
                    throw new UnexpectedFrameException(f);
                }
                m_command.Method = m_protocol.DecodeMethodFrom(f.GetReader());
                m_state          = m_command.Method.HasContent
                            ? AssemblyState.ExpectingContentHeader
                            : AssemblyState.Complete;
                return(CompletedCommand());
            }

            case AssemblyState.ExpectingContentHeader:
            {
                if (!f.IsHeader())
                {
                    throw new UnexpectedFrameException(f);
                }
                NetworkBinaryReader reader = f.GetReader();
                m_command.Header     = m_protocol.DecodeContentHeaderFrom(reader);
                m_remainingBodyBytes = m_command.Header.ReadFrom(reader);
                UpdateContentBodyState();
                return(CompletedCommand());
            }

            case AssemblyState.ExpectingContentBody:
            {
                if (!f.IsBody())
                {
                    throw new UnexpectedFrameException(f);
                }
                m_command.AppendBodyFragment(f.Payload);
                if ((ulong)f.Payload.Length > m_remainingBodyBytes)
                {
                    throw new MalformedFrameException
                              (string.Format("Overlong content body received - {0} bytes remaining, {1} bytes received",
                                             m_remainingBodyBytes,
                                             f.Payload.Length));
                }
                m_remainingBodyBytes -= (ulong)f.Payload.Length;
                UpdateContentBodyState();
                return(CompletedCommand());
            }

            case AssemblyState.Complete:
            default:
                return(null);
            }
        }
Ejemplo n.º 3
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);
            }
        }
        public Command HandleFrame(InboundFrame f)
        {
            switch (m_state)
            {
            case AssemblyState.ExpectingMethod:
            {
                if (!f.IsMethod())
                {
                    throw new UnexpectedFrameException(f);
                }
                m_method = m_protocol.DecodeMethodFrom(f.GetReader());
                m_state  = m_method.HasContent
                        ? AssemblyState.ExpectingContentHeader
                        : AssemblyState.Complete;
                return(CompletedCommand());
            }

            case AssemblyState.ExpectingContentHeader:
            {
                if (!f.IsHeader())
                {
                    throw new UnexpectedFrameException(f);
                }
                NetworkBinaryReader reader = f.GetReader();
                m_header = m_protocol.DecodeContentHeaderFrom(reader);
                var totalBodyBytes = m_header.ReadFrom(reader);
                if (totalBodyBytes > MaxArrayOfBytesSize)
                {
                    throw new UnexpectedFrameException(f);
                }
                m_remainingBodyBytes = (int)totalBodyBytes;
                m_body       = new byte[m_remainingBodyBytes];
                m_bodyStream = new MemoryStream(m_body, true);
                UpdateContentBodyState();
                return(CompletedCommand());
            }

            case AssemblyState.ExpectingContentBody:
            {
                if (!f.IsBody())
                {
                    throw new UnexpectedFrameException(f);
                }
                if (f.Payload.Length > m_remainingBodyBytes)
                {
                    throw new MalformedFrameException
                              (string.Format("Overlong content body received - {0} bytes remaining, {1} bytes received",
                                             m_remainingBodyBytes,
                                             f.Payload.Length));
                }
                m_bodyStream.Write(f.Payload, 0, f.Payload.Length);
                m_remainingBodyBytes -= f.Payload.Length;
                UpdateContentBodyState();
                return(CompletedCommand());
            }

            case AssemblyState.Complete:
            default:
#if NETFX_CORE
                Debug.WriteLine("Received frame in invalid state {0}; {1}", m_state, f);
#else
                //Trace.Fail(string.Format("Received frame in invalid state {0}; {1}", m_state, f));
#endif
                return(null);
            }
        }