Beispiel #1
0
        private async Task <string> ReadConnectionMetadataAsync(ConnectionContext connection)
        {
            var result = await connection.Transport.Input.ReadAsync();

            Utf8BufferTextReader reader = null;

            try
            {
                reader = Utf8BufferTextReader.Get(result.Buffer);

                return(await reader.ReadToEndAsync());
            }
            finally
            {
                Utf8BufferTextReader.Return(reader);

                connection.Transport.Input.AdvanceTo(result.Buffer.End);
            }
        }
Beispiel #2
0
        /// <inheritdoc />
        public bool TryParseMessage(ref ReadOnlySequence <byte> input, IInvocationBinder binder, out HubMessage message)
        {
            if (!TextMessageParser.TryParseMessage(ref input, out var payload))
            {
                message = null;
                return(false);
            }

            var textReader = Utf8BufferTextReader.Get(payload);

            try
            {
                message = ParseMessage(textReader, binder);
            }
            finally
            {
                Utf8BufferTextReader.Return(textReader);
            }

            return(message != null);
        }
        /// <summary>
        /// Creates a new <see cref="HandshakeRequestMessage"/> from the specified serialized representation.
        /// </summary>
        /// <param name="buffer">The serialized representation of the message.</param>
        /// <param name="requestMessage">When this method returns, contains the parsed message.</param>
        /// <returns>A value that is <c>true</c> if the <see cref="HandshakeRequestMessage"/> was successfully parsed; otherwise, <c>false</c>.</returns>
        public static bool TryParseRequestMessage(ref ReadOnlySequence <byte> buffer, out HandshakeRequestMessage requestMessage)
        {
            if (!TextMessageParser.TryParseMessage(ref buffer, out var payload))
            {
                requestMessage = null;
                return(false);
            }

            var textReader = Utf8BufferTextReader.Get(payload);

            try
            {
                using (var reader = JsonUtils.CreateJsonTextReader(textReader))
                {
                    JsonUtils.CheckRead(reader);
                    JsonUtils.EnsureObjectStart(reader);

                    string protocol        = null;
                    int?   protocolVersion = null;

                    var completed = false;
                    while (!completed && JsonUtils.CheckRead(reader))
                    {
                        switch (reader.TokenType)
                        {
                        case JsonToken.PropertyName:
                            var memberName = reader.Value.ToString();

                            switch (memberName)
                            {
                            case ProtocolPropertyName:
                                protocol = JsonUtils.ReadAsString(reader, ProtocolPropertyName);
                                break;

                            case ProtocolVersionPropertyName:
                                protocolVersion = JsonUtils.ReadAsInt32(reader, ProtocolVersionPropertyName);
                                break;

                            default:
                                reader.Skip();
                                break;
                            }
                            break;

                        case JsonToken.EndObject:
                            completed = true;
                            break;

                        default:
                            throw new InvalidDataException($"Unexpected token '{reader.TokenType}' when reading handshake request JSON. Message content: {GetPayloadAsString()}");
                        }
                    }

                    if (protocol == null)
                    {
                        throw new InvalidDataException($"Missing required property '{ProtocolPropertyName}'. Message content: {GetPayloadAsString()}");
                    }
                    if (protocolVersion == null)
                    {
                        throw new InvalidDataException($"Missing required property '{ProtocolVersionPropertyName}'. Message content: {GetPayloadAsString()}");
                    }

                    requestMessage = new HandshakeRequestMessage(protocol, protocolVersion.Value);
                }
            }
            finally
            {
                Utf8BufferTextReader.Return(textReader);
            }

            // For error messages, we want to print the payload as text
            string GetPayloadAsString()
            {
                // REVIEW: Should we show hex for binary charaters?
                return(Encoding.UTF8.GetString(payload.ToArray()));
            }

            return(true);
        }
        /// <summary>
        /// Creates a new <see cref="HandshakeResponseMessage"/> from the specified serialized representation.
        /// </summary>
        /// <param name="buffer">The serialized representation of the message.</param>
        /// <param name="responseMessage">When this method returns, contains the parsed message.</param>
        /// <returns>A value that is <c>true</c> if the <see cref="HandshakeResponseMessage"/> was successfully parsed; otherwise, <c>false</c>.</returns>
        public static bool TryParseResponseMessage(ref ReadOnlySequence <byte> buffer, out HandshakeResponseMessage responseMessage)
        {
            if (!TextMessageParser.TryParseMessage(ref buffer, out var payload))
            {
                responseMessage = null;
                return(false);
            }

            var textReader = Utf8BufferTextReader.Get(payload);

            try
            {
                using (var reader = JsonUtils.CreateJsonTextReader(textReader))
                {
                    JsonUtils.CheckRead(reader);
                    JsonUtils.EnsureObjectStart(reader);

                    int?   minorVersion = null;
                    string error        = null;

                    var completed = false;
                    while (!completed && JsonUtils.CheckRead(reader))
                    {
                        switch (reader.TokenType)
                        {
                        case JsonToken.PropertyName:
                            var memberName = reader.Value.ToString();

                            switch (memberName)
                            {
                            case TypePropertyName:
                                // a handshake response does not have a type
                                // check the incoming message was not any other type of message
                                throw new InvalidDataException("Expected a handshake response from the server.");

                            case ErrorPropertyName:
                                error = JsonUtils.ReadAsString(reader, ErrorPropertyName);
                                break;

                            case MinorVersionPropertyName:
                                minorVersion = JsonUtils.ReadAsInt32(reader, MinorVersionPropertyName);
                                break;

                            default:
                                reader.Skip();
                                break;
                            }
                            break;

                        case JsonToken.EndObject:
                            completed = true;
                            break;

                        default:
                            throw new InvalidDataException($"Unexpected token '{reader.TokenType}' when reading handshake response JSON.");
                        }
                    }
                    ;

                    responseMessage = new HandshakeResponseMessage(minorVersion, error);
                    return(true);
                }
            }
            finally
            {
                Utf8BufferTextReader.Return(textReader);
            }
        }