Exemple #1
0
        /// <summary>
        /// Serialize the NsMessage.
        /// </summary>
        /// <param name="obj">The object to serialize</param>
        /// <returns>The serialized object in byte array form</returns>
        public byte[] Encode(NsMessage <T> obj)
        {
            NsMessageProto proto = NsMessageProto.Create(obj, _codec);

            using (var stream = new MemoryStream())
            {
                Serializer.Serialize(stream, proto);
                return(stream.ToArray());
            }
        }
Exemple #2
0
        public static NsMessageProto Create <T>(NsMessage <T> message, ICodec <T> codec)
        {
            NsMessageProto proto = new NsMessageProto();

            proto.SourceId = message.SourceId.ToString();
            proto.DestId   = message.DestId.ToString();

            foreach (T item in message.Data)
            {
                proto.Data.Add(codec.Encode(item));
            }

            return(proto);
        }
Exemple #3
0
        /// <summary>
        /// Deserialize the byte array into NsMessage.
        /// </summary>
        /// <param name="data">The serialized byte array</param>
        /// <returns>The deserialized NsMessage</returns>
        public NsMessage <T> Decode(byte[] data)
        {
            using (var stream = new MemoryStream(data))
            {
                NsMessageProto proto = Serializer.Deserialize <NsMessageProto>(stream);

                IIdentifier   sourceId = _idFactory.Create(proto.SourceId);
                IIdentifier   destId   = _idFactory.Create(proto.DestId);
                NsMessage <T> message  = new NsMessage <T>(sourceId, destId);

                var messages = proto.Data.Select(byteArr => _codec.Decode(byteArr));
                message.Data.AddRange(messages);
                return(message);
            }
        }