/// <summary> /// Creates a RawMessageContainer from the parameters. /// </summary> /// <param name="type">Type of message to pass to container.</param> /// <param name="payload">Raw, serialized payload in bytes.</param> /// <returns>Serialized bytes.</returns> public byte[] SerializeMessageContainer(TodoMessageTypes type, byte[] payload) { var messageContainer = new RawMessageContainer(type, payload); var rawProtoContainer = Mapper.Map <RawMessageContainer, MessageContainerProto>(messageContainer); return(GetBytesFromProto <MessageContainerProto>(rawProtoContainer)); }
/// <summary> /// Deserializes and maps Protos to Payloads. /// Note: You have to register new message types with the Server router in ServerBootstrapper.cs /// </summary> /// <param name="container">RawMessageContainer to read from.</param> /// <returns>Deserialized ICommMessage object.</returns> public virtual ICommMessage DeserializePayload(RawMessageContainer container) { // We need to keep this switch here because we can't deserialize the Protobuf without a type. // Well, actually... We can. Protobuf.Serializer.NonGeneric provides a set of methods that read // The attribute tags from a Protobuf object and deserializes it. But it's slow. // And this is networking. So optimizing for speed is the best idea for us. Reflection ain't good, yo. // The primary reason this switch is here is so that ServerNode doesn't have to keep a reference to // The proto objects. Since they require a reference to Protobuf.Net // and we don't want to force ourselves to keep reference that in every project. switch (container.PayloadType) { case TodoMessageTypes.PositionUpdateData: return(DeserializeProtobufAndMap <PositionUpdateProto, PositionUpdate>(container.Data)); } // Todo: Throw custom exceptions. throw new Exception("Deserialization exception"); }