public void MessageDescriptor_Should_Properly_Pack_A_Message_Proto(int messageType, string data, params string[] arguments) { var messageDescriptor = new MessageDescriptor(); var protobufMessage = new TestMessage { Data = data }; var protobufMessageSerialized = protobufMessage.ToByteArray(); var argumentsDescriptors = GetArgumentsDescriptors(arguments); var totalLength = protobufMessageSerialized.Length + argumentsDescriptors.Sum(argument => argument.Argument.Length + ProtobufHubProtocolConstants.ARGUMENT_HEADER_LENGTH) + 4; //Int => // ProtobufMessageLength ReadOnlySpan <byte> messagePacked = messageDescriptor.PackMessage(messageType, protobufMessageSerialized, argumentsDescriptors); Assert.Equal(messageType, messagePacked[0]); Assert.Equal(totalLength, BitConverter.ToInt32(messagePacked.Slice(1, 4))); Assert.Equal(protobufMessageSerialized.Length, BitConverter.ToInt32(messagePacked.Slice(5, 4))); var protobufObject = new TestMessage(); protobufObject.MergeFrom(messagePacked.Slice(ProtobufHubProtocolConstants.MESSAGE_HEADER_LENGTH, protobufMessageSerialized.Length).ToArray()); Assert.Equal(protobufMessage, protobufObject); var serializedArguments = messagePacked.Slice(ProtobufHubProtocolConstants.MESSAGE_HEADER_LENGTH + protobufMessageSerialized.Length); var i = 0; while (!serializedArguments.IsEmpty) { var argumentType = BitConverter.ToInt32(serializedArguments.Slice(0, 4)); var argumentLength = BitConverter.ToInt32(serializedArguments.Slice(4, 4)); var argument = serializedArguments.Slice(ProtobufHubProtocolConstants.ARGUMENT_HEADER_LENGTH, argumentLength).ToArray(); protobufObject.MergeFrom(argument); Assert.Equal(ARGUMENT_TYPE, argumentType); Assert.Equal(arguments[i++], protobufObject.Data); serializedArguments = serializedArguments.Slice(ProtobufHubProtocolConstants.ARGUMENT_HEADER_LENGTH + argumentLength); } }
public void MessageDescriptor_Should_Retrieve_ProtobufMessage_From_A_PackedMessage(string data) { var protobufMessageSerialized = new TestMessage { Data = data }.ToByteArray(); var argumentsDescriptors = GetArgumentsDescriptors("arg"); ReadOnlySpan <byte> messagePacked = MessageDescriptor.PackMessage(1, protobufMessageSerialized, argumentsDescriptors); var protobufMessage = MessageDescriptor.GetProtobufMessage(messagePacked); var protobufObject = new TestMessage(); protobufObject.MergeFrom(protobufMessage.ToArray()); Assert.Equal(data, protobufObject.Data); }
public void MessageDescriptor_Should_Retrieve_Arguments_From_A_PackedMessage(params string[] arguments) { var protobufMessageSerialized = new TestMessage { Data = "FooBar" }.ToByteArray(); var argumentsDescriptors = GetArgumentsDescriptors(arguments); ReadOnlySpan <byte> messagePacked = MessageDescriptor.PackMessage(1, protobufMessageSerialized, argumentsDescriptors); var descriptors = MessageDescriptor.GetArguments(messagePacked); var i = 0; foreach (var descriptor in descriptors) { var protobufObject = new TestMessage(); protobufObject.MergeFrom(descriptor.Argument); Assert.Equal(ARGUMENT_TYPE, descriptor.Type); Assert.Equal(arguments[i++], protobufObject.Data); } }