private static void WriteContent(MyRequestMessage message, IBufferWriter <byte> stream)
        {
            var reusableWriter = ReusableUtf8JsonWriter.Get(stream);

            try
            {
                var writer = reusableWriter.GetJsonWriter();

                writer.WriteStartObject();

                switch (message)
                {
                case InitMessage m:
                    WriteInitMessage(m, writer);
                    break;

                case PayloadMessage m:
                    WritePayLoadMessage(m, writer);
                    break;

                default:
                    throw new InvalidOperationException($"Unsupported message type: {message.GetType().FullName}");
                }

                writer.WriteEndObject();
                writer.Flush();
                Debug.Assert(writer.CurrentDepth == 0);
            }
            finally
            {
                ReusableUtf8JsonWriter.Return(reusableWriter);
            }
        }
        /// <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);
        }
Esempio n. 3
0
        private static void WriteContent(SixtyNineSendibleMessage message, ReusableUtf8JsonWriter reusableWriter)
        {
            try
            {
                var writer = reusableWriter.GetJsonWriter();

                writer.WriteStartObject();

                switch (message)
                {
                case ErrorMessage m:
                    WriteErrorMessage(m, writer);
                    break;

                case InitResponseMessage m:
                    WriteInitResponseMessage(m, writer);
                    break;

                case PayloadMessage m:
                    WritePayLoadMessage(m, writer);
                    break;

                default:
                    throw new InvalidOperationException($"Unsupported message type: {message.GetType().FullName}");
                }

                writer.WriteEndObject();
                writer.Flush();
                Debug.Assert(writer.CurrentDepth == 0);
            }
            finally
            {
                ReusableUtf8JsonWriter.Return(reusableWriter);
            }
        }
Esempio n. 4
0
    public static void Return(ReusableUtf8JsonWriter writer)
    {
        _cachedInstance = writer;

        writer._writer.Reset();

#if DEBUG
        writer._inUse = false;
#endif
    }
Esempio n. 5
0
        private void WriteMessageCore(HubMessage message, IBufferWriter<byte> stream)
        {
            var reusableWriter = ReusableUtf8JsonWriter.Get(stream);

            try
            {
                var writer = reusableWriter.GetJsonWriter();
                writer.WriteStartObject();
                switch (message)
                {
                    case InvocationMessage m:
                        WriteMessageType(writer, HubProtocolConstants.InvocationMessageType);
                        WriteHeaders(writer, m);
                        WriteInvocationMessage(m, writer);
                        break;
                    case StreamInvocationMessage m:
                        WriteMessageType(writer, HubProtocolConstants.StreamInvocationMessageType);
                        WriteHeaders(writer, m);
                        WriteStreamInvocationMessage(m, writer);
                        break;
                    case StreamItemMessage m:
                        WriteMessageType(writer, HubProtocolConstants.StreamItemMessageType);
                        WriteHeaders(writer, m);
                        WriteStreamItemMessage(m, writer);
                        break;
                    case CompletionMessage m:
                        WriteMessageType(writer, HubProtocolConstants.CompletionMessageType);
                        WriteHeaders(writer, m);
                        WriteCompletionMessage(m, writer);
                        break;
                    case CancelInvocationMessage m:
                        WriteMessageType(writer, HubProtocolConstants.CancelInvocationMessageType);
                        WriteHeaders(writer, m);
                        WriteCancelInvocationMessage(m, writer);
                        break;
                    case PingMessage _:
                        WriteMessageType(writer, HubProtocolConstants.PingMessageType);
                        break;
                    case CloseMessage m:
                        WriteMessageType(writer, HubProtocolConstants.CloseMessageType);
                        WriteCloseMessage(m, writer);
                        break;
                    default:
                        throw new InvalidOperationException($"Unsupported message type: {message.GetType().FullName}");
                }
                writer.WriteEndObject();
                writer.Flush();
                Debug.Assert(writer.CurrentDepth == 0);
            }
            finally
            {
                ReusableUtf8JsonWriter.Return(reusableWriter);
            }
        }
        /// <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);
        }
Esempio n. 7
0
    public static ReusableUtf8JsonWriter Get(IBufferWriter <byte> stream)
    {
        var writer = _cachedInstance;

        if (writer == null)
        {
            writer = new ReusableUtf8JsonWriter(stream);
        }

        // Taken off the thread static
        _cachedInstance = null;
#if DEBUG
        if (writer._inUse)
        {
            throw new InvalidOperationException("The writer wasn't returned!");
        }

        writer._inUse = true;
#endif
        writer._writer.Reset(stream);
        return(writer);
    }
        public static void WriteResponse(NegotiationResponse response, IBufferWriter <byte> output)
        {
            var reusableWriter = ReusableUtf8JsonWriter.Get(output);

            try
            {
                var writer = reusableWriter.GetJsonWriter();
                writer.WriteStartObject();

                if (!string.IsNullOrEmpty(response.Url))
                {
                    writer.WriteString(UrlPropertyNameBytes, response.Url);
                }

                if (!string.IsNullOrEmpty(response.AccessToken))
                {
                    writer.WriteString(AccessTokenPropertyNameBytes, response.AccessToken);
                }

                if (!string.IsNullOrEmpty(response.ConnectionId))
                {
                    writer.WriteString(ConnectionIdPropertyNameBytes, response.ConnectionId);
                }

                writer.WriteStartArray(AvailableTransportsPropertyNameBytes);

                if (response.AvailableTransports != null)
                {
                    var transportCount = response.AvailableTransports.Count;
                    for (var i = 0; i < transportCount; ++i)
                    {
                        var availableTransport = response.AvailableTransports[i];
                        writer.WriteStartObject();
                        if (availableTransport.Transport != null)
                        {
                            writer.WriteString(TransportPropertyNameBytes, availableTransport.Transport);
                        }
                        else
                        {
                            // Might be able to remove this after https://github.com/dotnet/corefx/issues/34632 is resolved
                            writer.WriteNull(TransportPropertyNameBytes);
                        }
                        writer.WriteStartArray(TransferFormatsPropertyNameBytes);

                        if (availableTransport.TransferFormats != null)
                        {
                            var formatCount = availableTransport.TransferFormats.Count;
                            for (var j = 0; j < formatCount; ++j)
                            {
                                writer.WriteStringValue(availableTransport.TransferFormats[j]);
                            }
                        }

                        writer.WriteEndArray();
                        writer.WriteEndObject();
                    }
                }

                writer.WriteEndArray();
                writer.WriteEndObject();

                writer.Flush();
                Debug.Assert(writer.CurrentDepth == 0);
            }
            finally
            {
                ReusableUtf8JsonWriter.Return(reusableWriter);
            }
        }
Esempio n. 9
0
    /// <summary>
    /// Writes the <paramref name="response"/> to the <paramref name="output"/>.
    /// </summary>
    /// <param name="response">The negotiation response generated in response to a negotiation request.</param>
    /// <param name="output">Where the <paramref name="response"/> is written to as Json.</param>
    public static void WriteResponse(NegotiationResponse response, IBufferWriter <byte> output)
    {
        var reusableWriter = ReusableUtf8JsonWriter.Get(output);

        try
        {
            var writer = reusableWriter.GetJsonWriter();
            writer.WriteStartObject();

            // If we already have an error its due to a protocol version incompatibility.
            // We can just write the error and complete the JSON object and return.
            if (!string.IsNullOrEmpty(response.Error))
            {
                writer.WriteString(ErrorPropertyNameBytes, response.Error);
                writer.WriteEndObject();
                writer.Flush();
                Debug.Assert(writer.CurrentDepth == 0);
                return;
            }

            writer.WriteNumber(NegotiateVersionPropertyNameBytes, response.Version);

            if (!string.IsNullOrEmpty(response.Url))
            {
                writer.WriteString(UrlPropertyNameBytes, response.Url);
            }

            if (!string.IsNullOrEmpty(response.AccessToken))
            {
                writer.WriteString(AccessTokenPropertyNameBytes, response.AccessToken);
            }

            if (!string.IsNullOrEmpty(response.ConnectionId))
            {
                writer.WriteString(ConnectionIdPropertyNameBytes, response.ConnectionId);
            }

            if (response.Version > 0 && !string.IsNullOrEmpty(response.ConnectionToken))
            {
                writer.WriteString(ConnectionTokenPropertyNameBytes, response.ConnectionToken);
            }

            writer.WriteStartArray(AvailableTransportsPropertyNameBytes);

            if (response.AvailableTransports != null)
            {
                var transportCount = response.AvailableTransports.Count;
                for (var i = 0; i < transportCount; ++i)
                {
                    var availableTransport = response.AvailableTransports[i];
                    writer.WriteStartObject();
                    if (availableTransport.Transport != null)
                    {
                        writer.WriteString(TransportPropertyNameBytes, availableTransport.Transport);
                    }
                    else
                    {
                        // Might be able to remove this after https://github.com/dotnet/corefx/issues/34632 is resolved
                        writer.WriteNull(TransportPropertyNameBytes);
                    }
                    writer.WriteStartArray(TransferFormatsPropertyNameBytes);

                    if (availableTransport.TransferFormats != null)
                    {
                        var formatCount = availableTransport.TransferFormats.Count;
                        for (var j = 0; j < formatCount; ++j)
                        {
                            writer.WriteStringValue(availableTransport.TransferFormats[j]);
                        }
                    }

                    writer.WriteEndArray();
                    writer.WriteEndObject();
                }
            }

            writer.WriteEndArray();
            writer.WriteEndObject();

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