Esempio n. 1
0
        private static ReadOnlyMemory <byte> ReadPayload(Stream source, int payloadSize)
        {
            if (payloadSize == 0)
            {
                return(ReadOnlyMemory <byte> .Empty);
            }

            var payload = new Memory <byte>(new byte[payloadSize]);

            var consumed = 0;

            while (true)
            {
                var read = source.Read(payload.Slice(consumed).Span);
                if (read < 1)
                {
                    throw NatsException.OpParserOpParsingError(MsgOp.Name, "Could not read payload from stream.");
                }

                consumed += read;

                if (consumed == payloadSize)
                {
                    break;
                }

                if (consumed > payloadSize)
                {
                    throw NatsException.OpParserOpParsingError(MsgOp.Name, "Read to many bytes for the payload.");
                }
            }

            return(payload);
        }
Esempio n. 2
0
        private static ErrOp ParseErrorOp(Stream source, Stream workspace)
        {
            while (true)
            {
                var b = (byte)source.ReadByte();
                if (b == Cr)
                {
                    b = (byte)source.ReadByte();
                    if (b != Lf)
                    {
                        throw NatsException.OpParserOpParsingError(ErrOp.Name, Lf, b);
                    }
                    break;
                }

                workspace.WriteByte(b);
            }

            var buff = new Memory <byte>(new byte[workspace.Position]);

            workspace.Position = 0;
            workspace.Read(buff.Span);

            return(new ErrOp(string.Create(buff.Length, buff, (t, s) =>
            {
                var x = s.Span;
                for (var i = 0; i < t.Length; i++)
                {
                    t[i] = (char)x[i];
                }
            })));
        }
Esempio n. 3
0
        private static PingOp ParsePingOp(Stream source)
        {
            var c = (byte)source.ReadByte();

            if (c != Lf)
            {
                throw NatsException.OpParserOpParsingError(PingOp.Name, Lf, c);
            }

            return(PingOp.Instance);
        }
Esempio n. 4
0
        private static InfoOp ParseInfoOp(Stream source, Stream workspace)
        {
            while (true)
            {
                var b = (byte)source.ReadByte();
                if (b == Cr)
                {
                    b = (byte)source.ReadByte();
                    if (b != Lf)
                    {
                        throw NatsException.OpParserOpParsingError(InfoOp.Name, Lf, b);
                    }
                    break;
                }

                workspace.WriteByte(b);
            }

            var m = ToChars(workspace);

            return(new InfoOp(m));
        }
Esempio n. 5
0
        private static MsgOp ParseMsgOp(Stream source, Stream workspace)
        {
            var  sub     = ReadOnlySpan <char> .Empty;
            var  sid     = ReadOnlySpan <char> .Empty;
            var  replyTo = ReadOnlySpan <char> .Empty;
            int  payloadSize;
            byte b;

            while (true)
            {
                b = (byte)source.ReadByte();
                if (b == Cr)
                {
                    payloadSize = int.Parse(ToChars(workspace).Span);

                    b = (byte)source.ReadByte();
                    if (b != Lf)
                    {
                        throw NatsException.OpParserOpParsingError(MsgOp.Name, Lf, b);
                    }
                    break;
                }

                if (!IsDelimMarker(b))
                {
                    workspace.WriteByte(b);
                }
                else
                {
                    if (sub.IsEmpty)
                    {
                        sub = ToChars(workspace).Span;
                        workspace.Position = 0;
                        continue;
                    }

                    if (sid.IsEmpty)
                    {
                        sid = ToChars(workspace).Span;
                        workspace.Position = 0;
                        continue;
                    }

                    if (replyTo.IsEmpty)
                    {
                        replyTo            = ToChars(workspace).Span;
                        workspace.Position = 0;
                        continue;
                    }

                    throw NatsException.OpParserOpParsingError(MsgOp.Name, "Message does not conform to expected protocol format.");
                }
            }

            var payload = ReadPayload(source, payloadSize);

            b = (byte)source.ReadByte();
            if (b != Cr)
            {
                throw NatsException.OpParserOpParsingError(MsgOp.Name, Cr, b);
            }

            b = (byte)source.ReadByte();
            if (b != Lf)
            {
                throw NatsException.OpParserOpParsingError(MsgOp.Name, Lf, b);
            }

            return(new MsgOp(
                       sub,
                       sid,
                       replyTo,
                       payload));
        }