public static void WriteResponseMessage(HandshakeResponseMessage responseMessage, IBufferWriter <byte> output)
        {
            var textWriter = Utf8BufferTextWriter.Get(output);

            try
            {
                using (var writer = JsonUtils.CreateJsonTextWriter(textWriter))
                {
                    writer.WriteStartObject();
                    if (!string.IsNullOrEmpty(responseMessage.Error))
                    {
                        writer.WritePropertyName(ErrorPropertyName);
                        writer.WriteValue(responseMessage.Error);
                    }

                    writer.WriteEndObject();
                    writer.Flush();
                }
            }
            finally
            {
                Utf8BufferTextWriter.Return(textWriter);
            }

            TextMessageFormatter.WriteRecordSeparator(output);
        }
        /// <summary>
        /// Writes the serialized representation of a <see cref="HandshakeResponseMessage"/> to the specified writer.
        /// </summary>
        /// <param name="responseMessage">The message to write.</param>
        /// <param name="output">The output writer.</param>
        public static void WriteResponseMessage(HandshakeResponseMessage responseMessage, IBufferWriter <byte> output)
        {
            var reusableWriter = ReusableUtf8JsonWriter.Get(output);

            try
            {
                var writer = reusableWriter.GetJsonWriter();

                writer.WriteStartObject();
                if (!string.IsNullOrEmpty(responseMessage.Error))
                {
                    writer.WriteString(ErrorPropertyNameBytes, responseMessage.Error);
                }

                writer.WriteEndObject();
                writer.Flush();
                Debug.Assert(writer.CurrentDepth == 0);
            }
            finally
            {
                ReusableUtf8JsonWriter.Return(reusableWriter);
            }

            TextMessageFormatter.WriteRecordSeparator(output);
        }
Example #3
0
    public void Setup()
    {
        var buffer = new byte[MessageLength];

        Random.Shared.NextBytes(buffer);
        var writer = MemoryBufferWriter.Get();

        try
        {
            BinaryMessageFormatter.WriteLengthPrefix(buffer.Length, writer);
            writer.Write(buffer);
            _binaryInput = writer.ToArray();
        }
        finally
        {
            MemoryBufferWriter.Return(writer);
        }

        buffer = new byte[MessageLength];
        Random.Shared.NextBytes(buffer);
        writer = MemoryBufferWriter.Get();
        try
        {
            writer.Write(buffer);
            TextMessageFormatter.WriteRecordSeparator(writer);

            _textInput = writer.ToArray();
        }
        finally
        {
            MemoryBufferWriter.Return(writer);
        }
    }
        private static byte[] FormatMessageToArray(byte[] message)
        {
            var output = new MemoryStream();

            output.Write(message, 0, message.Length);
            TextMessageFormatter.WriteRecordSeparator(output);
            return(output.ToArray());
        }
Example #5
0
 public void WriteMessage()
 {
     using (var ms = new MemoryStream())
     {
         var buffer = Encoding.UTF8.GetBytes("ABC");
         ms.Write(buffer, 0, buffer.Length);
         TextMessageFormatter.WriteRecordSeparator(ms);
         Assert.Equal("ABC\u001e", Encoding.UTF8.GetString(ms.ToArray()));
     }
 }
Example #6
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);
        }
Example #7
0
        public static void WriteRequestMessage(HandshakeRequestMessage requestMessage, Stream output)
        {
            using (var writer = CreateJsonTextWriter(output))
            {
                writer.WriteStartObject();
                writer.WritePropertyName(ProtocolPropertyName);
                writer.WriteValue(requestMessage.Protocol);
                writer.WriteEndObject();
            }

            TextMessageFormatter.WriteRecordSeparator(output);
        }
        /// <summary>
        /// Writes the serialized representation of a <see cref="HandshakeRequestMessage"/> to the specified writer.
        /// </summary>
        /// <param name="requestMessage">The message to write.</param>
        /// <param name="output">The output writer.</param>
        public static void WriteRequestMessage(HandshakeRequestMessage requestMessage, IBufferWriter <byte> output)
        {
            var writer = new Utf8JsonWriter(output, new JsonWriterState(new JsonWriterOptions()
            {
                SkipValidation = true
            }));

            writer.WriteStartObject();
            writer.WriteString(ProtocolPropertyNameBytes, requestMessage.Protocol, escape: false);
            writer.WriteNumber(ProtocolVersionPropertyNameBytes, requestMessage.Version, escape: false);
            writer.WriteEndObject();
            writer.Flush(isFinalBlock: true);

            TextMessageFormatter.WriteRecordSeparator(output);
        }
Example #9
0
        public static void WriteResponseMessage(HandshakeResponseMessage responseMessage, Stream output)
        {
            using (var writer = CreateJsonTextWriter(output))
            {
                writer.WriteStartObject();
                if (!string.IsNullOrEmpty(responseMessage.Error))
                {
                    writer.WritePropertyName(ErrorPropertyName);
                    writer.WriteValue(responseMessage.Error);
                }
                writer.WriteEndObject();
            }

            TextMessageFormatter.WriteRecordSeparator(output);
        }
Example #10
0
        /// <summary>
        /// Writes the serialized representation of a <see cref="HandshakeRequestMessage"/> to the specified writer.
        /// </summary>
        /// <param name="requestMessage">The message to write.</param>
        /// <param name="output">The output writer.</param>
        public static void WriteRequestMessage(HandshakeRequestMessage requestMessage, IBufferWriter <byte> output)
        {
            using var writer = new Utf8JsonWriter(output, new JsonWriterOptions()
            {
                SkipValidation = true
            });

            writer.WriteStartObject();
            writer.WriteString(ProtocolPropertyNameBytes, requestMessage.Protocol);
            writer.WriteNumber(ProtocolVersionPropertyNameBytes, requestMessage.Version);
            writer.WriteEndObject();
            writer.Flush();
            Debug.Assert(writer.CurrentDepth == 0);

            TextMessageFormatter.WriteRecordSeparator(output);
        }
Example #11
0
        /// <summary>
        /// Writes the serialized representation of a <see cref="HandshakeResponseMessage"/> to the specified writer.
        /// </summary>
        /// <param name="responseMessage">The message to write.</param>
        /// <param name="output">The output writer.</param>
        public static void WriteResponseMessage(HandshakeResponseMessage responseMessage, IBufferWriter <byte> output)
        {
            var writer = new Utf8JsonWriter(output, new JsonWriterState(new JsonWriterOptions()
            {
                SkipValidation = true
            }));

            writer.WriteStartObject();
            if (!string.IsNullOrEmpty(responseMessage.Error))
            {
                writer.WriteString(ErrorPropertyNameBytes, responseMessage.Error);
            }

            writer.WriteNumber(MinorVersionPropertyNameBytes, responseMessage.MinorVersion, escape: false);

            writer.WriteEndObject();
            writer.Flush(isFinalBlock: true);

            TextMessageFormatter.WriteRecordSeparator(output);
        }
Example #12
0
        public void Setup()
        {
            var buffer = new byte[MessageLength];

            Random.NextBytes(buffer);
            var output = new MemoryStream();

            BinaryMessageFormatter.WriteLengthPrefix(buffer.Length, output);
            output.Write(buffer, 0, buffer.Length);

            _binaryInput = output.ToArray();

            buffer = new byte[MessageLength];
            Random.NextBytes(buffer);
            output = new MemoryStream();
            output.Write(buffer, 0, buffer.Length);
            TextMessageFormatter.WriteRecordSeparator(output);

            _textInput = output.ToArray();
        }
Example #13
0
        /// <summary>
        /// Writes the serialized representation of a <see cref="HandshakeResponseMessage"/> to the specified writer.
        /// </summary>
        /// <param name="responseMessage">The message to write.</param>
        /// <param name="output">The output writer.</param>
        public static void WriteResponseMessage(HandshakeResponseMessage responseMessage, IBufferWriter <byte> output)
        {
            using var writer = new Utf8JsonWriter(output, new JsonWriterOptions()
            {
                SkipValidation = true
            });

            writer.WriteStartObject();
            if (!string.IsNullOrEmpty(responseMessage.Error))
            {
                writer.WriteString(ErrorPropertyNameBytes, responseMessage.Error);
            }

            writer.WriteNumber(MinorVersionPropertyNameBytes, responseMessage.MinorVersion);

            writer.WriteEndObject();
            writer.Flush();
            Debug.Assert(writer.CurrentDepth == 0);

            TextMessageFormatter.WriteRecordSeparator(output);
        }
        /// <summary>
        /// Writes the serialized representation of a <see cref="HandshakeRequestMessage"/> to the specified writer.
        /// </summary>
        /// <param name="requestMessage">The message to write.</param>
        /// <param name="output">The output writer.</param>
        public static void WriteRequestMessage(HandshakeRequestMessage requestMessage, IBufferWriter <byte> output)
        {
            var reusableWriter = ReusableUtf8JsonWriter.Get(output);

            try
            {
                var writer = reusableWriter.GetJsonWriter();

                writer.WriteStartObject();
                writer.WriteString(ProtocolPropertyNameBytes, requestMessage.Protocol);
                writer.WriteNumber(ProtocolVersionPropertyNameBytes, requestMessage.Version);
                writer.WriteEndObject();
                writer.Flush();
                Debug.Assert(writer.CurrentDepth == 0);
            }
            finally
            {
                ReusableUtf8JsonWriter.Return(reusableWriter);
            }

            TextMessageFormatter.WriteRecordSeparator(output);
        }
Example #15
0
        public void Setup()
        {
            var buffer = new byte[MessageLength];

            Random.NextBytes(buffer);
            using (var writer = new MemoryBufferWriter())
            {
                BinaryMessageFormatter.WriteLengthPrefix(buffer.Length, writer);
                writer.Write(buffer);
                _binaryInput = writer.ToArray();
            }

            buffer = new byte[MessageLength];
            Random.NextBytes(buffer);
            using (var writer = new MemoryBufferWriter())
            {
                writer.Write(buffer);
                TextMessageFormatter.WriteRecordSeparator(writer);

                _textInput = writer.ToArray();
            }
        }
Example #16
0
        public static void WriteRequestMessage(HandshakeRequestMessage requestMessage, IBufferWriter <byte> output)
        {
            var textWriter = Utf8BufferTextWriter.Get(output);

            try
            {
                using (var writer = JsonUtils.CreateJsonTextWriter(textWriter))
                {
                    writer.WriteStartObject();
                    writer.WritePropertyName(ProtocolPropertyName);
                    writer.WriteValue(requestMessage.Protocol);
                    writer.WritePropertyName(ProtocolVersionPropertyName);
                    writer.WriteValue(requestMessage.Version);
                    writer.WriteEndObject();
                    writer.Flush();
                }
            }
            finally
            {
                Utf8BufferTextWriter.Return(textWriter);
            }

            TextMessageFormatter.WriteRecordSeparator(output);
        }
Example #17
0
 /// <inheritdoc />
 public void WriteMessage(HubMessage message, IBufferWriter <byte> output)
 {
     WriteMessageCore(message, output);
     TextMessageFormatter.WriteRecordSeparator(output);
 }
Example #18
0
 public void WriteMessage(HubMessage message, Stream output)
 {
     WriteMessageCore(message, output);
     TextMessageFormatter.WriteRecordSeparator(output);
 }