/// <summary> /// Helper method that builds list of message envelopes from list of multipart data in binary form. /// Returns list of Message Envelopes. /// </summary> private IList <IPacketMessageEnvelope> EnvelopePartsToEnvelope(PacketSerializer serializer, IList <byte[]> envelopeParts) { // Check that number of parts is even if (envelopeParts.Count % 2 != 0) { throw new Exception("Incorrect number of envelope parts"); } // One message envelope consists of two binary parts - metadata part and message part var list = new List <IPacketMessageEnvelope>(envelopeParts.Count / 2); // Build message envelope for (int i = 0; i < envelopeParts.Count / 2; i++) { var envelope = new PacketMessageEnvelope( serializer, envelopeParts[i * 2 + 1], envelopeParts[i * 2]); list.Add(envelope); } // Prevent modifications of this list. // If you want to modify packet's list of envelopes - create new packet. return(list.AsReadOnly()); }
/// <summary> /// Creates MessageEnvelope from message and metadata in binary form /// </summary> public PacketMessageEnvelope(PacketSerializer packetSerializer, byte[] message, byte[] metadata) { if (packetSerializer == null) { throw new ArgumentNullException("packetSerializer"); } if (message == null) { throw new ArgumentNullException("message"); } if (metadata == null) { throw new ArgumentNullException("metadata"); } _serializer = packetSerializer; _metadataBinary = metadata; _messageBinary = message; }
/// <summary> /// Creates envelope with default and empty metadata /// </summary> public static IPacketMessageEnvelope CreateEnvelope(PacketSerializer serializer, IMessage message) { if (message == null) { throw new ArgumentNullException("message"); } IMessageMetadata metadata; if (message is IEvent) { metadata = new EventMetadata(); } else if (message is ICommand) { metadata = new CommandMetadata(); } else { metadata = new MessageMetadata(); } return(new PacketMessageEnvelope(serializer, message, metadata)); }
/// <summary> /// Creates packet with specified header and list of message envelopes /// </summary> public Packet(PacketSerializer serializer, IPacketHeaders headers, IList <IPacketMessageEnvelope> envelopes) { _serializer = serializer; _headers = headers; _envelopes = envelopes; }
/// <summary> /// Creates packet from the list binary data (multipart message) /// </summary> public Packet(PacketSerializer serializer, IList <byte[]> parts) { _serializer = serializer; _headersBinary = parts[0]; _envelopes = EnvelopePartsToEnvelope(serializer, parts.Skip(1).ToList()); }
/// <summary> /// Boring code that enforce correct message and metadata pairs for events, commands and ordinary messages /// </summary> public static IPacketMessageEnvelope CreateEnvelope(PacketSerializer serializer, IMessage message, IMessageMetadata messageMetadata) { return(new PacketMessageEnvelope(serializer, message, messageMetadata)); }
public PacketBuilder(Func <Type, Guid> typeToTagResolver, PacketSerializer serializer) { _typeToTagResolver = typeToTagResolver; _serializer = serializer; }