private void WriteCompletionMessage(CompletionMessage completionMessage, IBufferWriter <byte> output)
        {
            var protobufCompletionMessage = new CompletionMessageProtobuf
            {
                Error        = completionMessage.Error ?? "",
                InvocationId = completionMessage.InvocationId
            };

            if (completionMessage.Headers != null)
            {
                protobufCompletionMessage.Headers.Add(completionMessage.Headers);
            }

            var result = new ArgumentDescriptor(0, Array.Empty <byte>());

            if (completionMessage.Result != null)
            {
                result = _argumentSerializer.SerializeArgument(completionMessage.Result);
            }

            var packedMessage = MessageDescriptor.PackMessage(HubProtocolConstants.CompletionMessageType, protobufCompletionMessage.ToByteArray(), new List <ArgumentDescriptor> {
                result
            });

            output.Write(packedMessage);
        }
        private HubMessage CreateHubCompletionMessage(ReadOnlySpan <byte> protobufMessage, object[] arguments)
        {
            var protobufCompletionMessage = new CompletionMessageProtobuf();

            protobufCompletionMessage.MergeFrom(protobufMessage.ToArray());

            if (!string.IsNullOrEmpty(protobufCompletionMessage.Error) || arguments.FirstOrDefault() == null)
            {
                return(new CompletionMessage(protobufCompletionMessage.InvocationId, protobufCompletionMessage.Error, null, false)
                {
                    Headers = protobufCompletionMessage.Headers
                });
            }
            return(new CompletionMessage(protobufCompletionMessage.InvocationId, null, arguments.FirstOrDefault(), true)
            {
                Headers = protobufCompletionMessage.Headers
            });
        }