Exemplo n.º 1
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 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.WritePropertyName(MinorVersionPropertyName);
                    writer.WriteValue(responseMessage.MinorVersion);

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

            TextMessageFormatter.WriteRecordSeparator(output);
        }
Exemplo n.º 2
0
        private void WriteMessageCore(HubMessage message, IBufferWriter <byte> stream)
        {
            var textWriter = Utf8BufferTextWriter.Get(stream);

            try
            {
                using (var writer = JsonUtils.CreateJsonTextWriter(textWriter))
                {
                    writer.WriteStartObject();
                    switch (message)
                    {
                    case InvocationMessage m:
                        WriteMessageType(writer, HubProtocolConstants.InvocationMessageType);
                        WriteHeaders(writer, m);
                        WriteInvocationMessage(m, writer);
                        break;

                    default:
                        throw new InvalidOperationException($"Unsupported message type: {message.GetType().FullName}");
                    }
                    writer.WriteEndObject();
                    writer.Flush();
                }
            }
            finally
            {
                Utf8BufferTextWriter.Return(textWriter);
            }
        }
Exemplo n.º 3
0
        public static void WriteResponse(NegotiationResponse response, IBufferWriter <byte> output)
        {
            var textWriter = Utf8BufferTextWriter.Get(output);

            try
            {
                using (var jsonWriter = JsonUtils.CreateJsonTextWriter(textWriter))
                {
                    jsonWriter.WriteStartObject();

                    if (!string.IsNullOrEmpty(response.Url))
                    {
                        jsonWriter.WritePropertyName(UrlPropertyName);
                        jsonWriter.WriteValue(response.Url);
                    }

                    if (!string.IsNullOrEmpty(response.AccessToken))
                    {
                        jsonWriter.WritePropertyName(AccessTokenPropertyName);
                        jsonWriter.WriteValue(response.AccessToken);
                    }

                    if (!string.IsNullOrEmpty(response.ConnectionId))
                    {
                        jsonWriter.WritePropertyName(ConnectionIdPropertyName);
                        jsonWriter.WriteValue(response.ConnectionId);
                    }

                    jsonWriter.WritePropertyName(AvailableTransportsPropertyName);
                    jsonWriter.WriteStartArray();

                    foreach (var availableTransport in response.AvailableTransports)
                    {
                        jsonWriter.WriteStartObject();
                        jsonWriter.WritePropertyName(TransportPropertyName);
                        jsonWriter.WriteValue(availableTransport.Transport);
                        jsonWriter.WritePropertyName(TransferFormatsPropertyName);
                        jsonWriter.WriteStartArray();

                        foreach (var transferFormat in availableTransport.TransferFormats)
                        {
                            jsonWriter.WriteValue(transferFormat);
                        }

                        jsonWriter.WriteEndArray();
                        jsonWriter.WriteEndObject();
                    }

                    jsonWriter.WriteEndArray();
                    jsonWriter.WriteEndObject();

                    jsonWriter.Flush();
                }
            }
            finally
            {
                Utf8BufferTextWriter.Return(textWriter);
            }
        }
        public void GetAndReturnCachedBufferTextWriter()
        {
            var bufferWriter1 = new TestMemoryBufferWriter();

            var textWriter1 = Utf8BufferTextWriter.Get(bufferWriter1);

            textWriter1.Write("Hello");
            textWriter1.Flush();
            Utf8BufferTextWriter.Return(textWriter1);

            Assert.Equal("Hello", Encoding.UTF8.GetString(bufferWriter1.ToArray()));

            var bufferWriter2 = new TestMemoryBufferWriter();

            var textWriter2 = Utf8BufferTextWriter.Get(bufferWriter2);

            textWriter2.Write("World");
            textWriter2.Flush();
            Utf8BufferTextWriter.Return(textWriter2);

            Assert.Equal("World", Encoding.UTF8.GetString(bufferWriter2.ToArray()));

            Assert.Same(textWriter1, textWriter2);
        }
Exemplo n.º 5
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)
        {
            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);
        }