public void bson_serialize_message_test()
        {
            var          prop1 = "prop1";
            var          prop2 = "prop2";
            var          msg   = new WoftamEvent(prop1, prop2);
            MemoryStream ms    = new MemoryStream();
            var          bs    = new BsonDataWriter(ms);

            bs.WriteStartObject();
            bs.WritePropertyName(msg.GetType().FullName);
            bs.WriteValue(JsonConvert.SerializeObject(msg));
            bs.WriteEnd();
             
            ms.Seek(0, SeekOrigin.Begin);

            Message msg2;
            var     reader = new BsonDataReader(ms);

            // read object
            reader.Read();
            // read type name
            reader.Read();
            var messageType = MessageHierarchy.GetTypeByFullName((string)reader.Value);

            reader.Read(); //property value
            msg2 = (Message)JsonConvert.DeserializeObject((string)reader.Value, messageType);
            Assert.IsType <WoftamEvent>(msg2);
            Assert.Equal(prop1, ((WoftamEvent)msg2).Property1);
        }
Exemplo n.º 2
0
        public void TestFullNameLookup()
        {
            var messageType = MessageHierarchy.GetTypeByFullName("ReactiveDomain.Messaging.Message");

            Assert.Equal(typeof(Message), messageType);
            var childMessageType = MessageHierarchy.GetTypeByFullName("ReactiveDomain.Messaging.Event");

            Assert.Equal(typeof(Event), childMessageType);
        }
        public Tuple <string, Type> FromBytes(ArraySegment <byte> data)
        {
            if (data.Array == null || data.Count < sizeof(int))
            {
                throw new ArgumentException($"ArraySegment null or too short, length: {data.Count}", nameof(data));
            }

            var offset = data.Offset;

            offset += ReadBytes(data.Array, offset, out var typeNameByteCount);
            offset += ReadBytes(data.Array, offset, typeNameByteCount, out var typeName);
            offset += ReadBytes(data.Array, offset, out var jsonByteCount);
            ReadBytes(data.Array, offset, jsonByteCount, out var json);

            var messageType = MessageHierarchy.GetTypeByFullName(typeName);

            return(new Tuple <string, Type>(json, messageType));
        }
        public void can_create_tcp_message_from_byte_array()
        {
            var tcpMsg = new TcpMessage(_testEvent);

            Assert.NotNull(tcpMsg.Data.Array);
            // ReSharper disable once AssignNullToNotNullAttribute
            var reader = new BsonDataReader(new MemoryStream(tcpMsg.Data.Array));

            // read object
            reader.Read();
            // read type name
            reader.Read();
            var messageType = MessageHierarchy.GetTypeByFullName((string)reader.Value);

            reader.Read(); //read json value
            var msg2 = (Message)JsonConvert.DeserializeObject((string)reader.Value, messageType, Json.JsonSettings);

            Assert.IsType <WoftamEvent>(msg2);
            Assert.Equal(Prop1, ((WoftamEvent)msg2).Property1);
        }
Exemplo n.º 5
0
        public IMessage FromBytes(ArraySegment <byte> data)
        {
            if (data.Array == null || data.Count < sizeof(int))
            {
                throw new ArgumentException($"ArraySegment null or too short, length: {data.Count}", nameof(data));
            }

            var offset = data.Offset;

            offset += ReadBytes(data.Array, offset, out var typeNameByteCount);
            offset += ReadBytes(data.Array, offset, typeNameByteCount, out var typeName);
            offset += ReadBytes(data.Array, offset, out var jsonByteCount);
            ReadBytes(data.Array, offset, jsonByteCount, out var json);

            var messageType = MessageHierarchy.GetTypeByFullName(typeName);
            var msg         = DeserializeMessage(json, messageType);

            Log.Debug("Deserialized Message MsgId=" + msg.MsgId + " MsgType" + msg.GetType().Name);
            return(msg);
        }
Exemplo n.º 6
0
 public bool TryResovleType()
 {
     if (!isType)
     {
         return(false);
     }
     if (PermissionType != null)
     {
         return(true);
     }
     try
     {
         var type = MessageHierarchy.GetTypeByFullName(PermissionName);
         if (type != null)
         {
             PermissionType = type;
             return(true);
         }
     }
     catch { /*ignore and return false */ }
     return(false);
 }
Exemplo n.º 7
0
        /// <summary>
        /// Builds a tcp messages from a wrapped data array
        /// used by the tcp processing system to deserialize recieved data
        /// </summary>
        /// <param name="data">The wrapped memory array.</param>
        /// <returns>a new tcp message built from the array segment</returns>
        /// <exception cref="UnregisteredMessageException"> If the message type is not found this will throw UnregisteredMessageException</exception>
        public static TcpMessage FromArraySegment(ArraySegment <byte> data)
        {
            if (data.Array == null || data.Count < sizeof(int))
            {
                throw new ArgumentException($"ArraySegment null or too short, length: {data.Count}", nameof(data));
            }

            Message msg;

#pragma warning disable 618
            using (var reader = new BsonDataReader(new MemoryStream(data.Array)))
#pragma warning restore 618
            {
                reader.Read(); //object
                reader.Read(); //property name

                var messageType = MessageHierarchy.GetTypeByFullName((string)reader.Value);
                reader.Read(); //property value
                msg = (Message)JsonConvert.DeserializeObject((string)reader.Value, messageType, Settings.JsonSettings);
            }
            Log.Debug("Deserialized Message MsgId=" + msg.MsgId + " MsgType" + msg.GetType().Name);
            return(new TcpMessage(msg, data));
        }