Пример #1
0
        public static void WriteMessage(NegotiationMessage negotiationMessage, Stream output)
        {
            using (var writer = new JsonTextWriter(new StreamWriter(output, _utf8NoBom, 1024, leaveOpen: true)))
            {
                writer.WriteStartObject();
                writer.WritePropertyName(ProtocolPropertyName);
                writer.WriteValue(negotiationMessage.Protocol);
                writer.WriteEndObject();
            }

            TextMessageFormatter.WriteRecordSeparator(output);
        }
Пример #2
0
        public static void WriteMessage(NegotiationMessage negotiationMessage, Stream output)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var writer = new JsonTextWriter(new StreamWriter(memoryStream)))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName(ProtocolPropertyName);
                    writer.WriteValue(negotiationMessage.Protocol);
                    writer.WriteEndObject();
                }

                TextMessageFormatter.WriteMessage(memoryStream.ToArray(), output);
            }
        }
Пример #3
0
        public static bool TryParseMessage(ReadOnlySequence <byte> buffer, out NegotiationMessage negotiationMessage, out SequencePosition consumed, out SequencePosition examined)
        {
            var separator = buffer.PositionOf(TextMessageFormatter.RecordSeparator);

            if (separator == null)
            {
                // Haven't seen the entire negotiate message so bail
                consumed           = buffer.Start;
                examined           = buffer.End;
                negotiationMessage = null;
                return(false);
            }
            else
            {
                consumed = buffer.GetPosition(1, separator.Value);
                examined = consumed;
            }

            var memory = buffer.IsSingleSegment ? buffer.First : buffer.ToArray();

            return(TryParseMessage(memory.Span, out negotiationMessage));
        }
Пример #4
0
        public static bool TryParseMessage(ReadOnlySpan <byte> input, out NegotiationMessage negotiationMessage)
        {
            if (!TextMessageParser.TryParseMessage(ref input, out var payload))
            {
                throw new InvalidDataException("Unable to parse payload as a negotiation message.");
            }

            using (var memoryStream = new MemoryStream(payload.ToArray()))
            {
                using (var reader = new JsonTextReader(new StreamReader(memoryStream)))
                {
                    var token = JToken.ReadFrom(reader);
                    if (token == null || token.Type != JTokenType.Object)
                    {
                        throw new InvalidDataException($"Unexpected JSON Token Type '{token?.Type}'. Expected a JSON Object.");
                    }

                    var negotiationJObject = (JObject)token;
                    var protocol           = JsonUtils.GetRequiredProperty <string>(negotiationJObject, ProtocolPropertyName);
                    negotiationMessage = new NegotiationMessage(protocol);
                }
            }
            return(true);
        }