public bool TryParseMessage(ref ReadOnlySequence <byte> input, IInvocationBinder binder, out HubMessage message) { if (!BinaryMessageParser.TryParseMessage(ref input, out var payload)) { message = null; return(false); } var arraySegment = GetArraySegment(payload); message = ParseMessage(arraySegment.Array, arraySegment.Offset, binder); return(message != null); }
public byte[] GetMessageBytes(HubMessage message) { return(HubProtocolExtensions.GetMessageBytes(this, message)); }
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); }
public void WriteMessage(HubMessage message, IBufferWriter <byte> output) { WriteMessageCore(message, output); TextMessageFormatter.WriteRecordSeparator(output); }
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; 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(); } } finally { Utf8BufferTextWriter.Return(textWriter); } }