Example #1
0
        public ProtocolMessage ParseRealtimeData(RealtimeTransportData data)
        {
            ProtocolMessage protocolMessage;

            if (_protocol == Protocol.MsgPack)
            {
                protocolMessage = (ProtocolMessage)MsgPackHelper.Deserialise(data.Data, typeof(ProtocolMessage));
            }
            else
            {
                protocolMessage = JsonHelper.Deserialize <ProtocolMessage>(data.Text);
            }

            //Populate presence and message object timestamps
            if (protocolMessage != null)
            {
                foreach (var presenceMessage in protocolMessage.Presence)
                {
                    presenceMessage.Timestamp = protocolMessage.Timestamp;
                }

                foreach (var message in protocolMessage.Messages)
                {
                    message.Timestamp = protocolMessage.Timestamp;
                }
            }

            return(protocolMessage);
        }
Example #2
0
        public ProtocolMessage ParseRealtimeData(RealtimeTransportData data)
        {
            ProtocolMessage protocolMessage;

            // ReSharper disable once ConditionIsAlwaysTrueOrFalse
            if (IsMsgPack() && Config.MsgPackEnabled)
            {
#if MSGPACK
                protocolMessage = (ProtocolMessage)MsgPackHelper.Deserialise(data.Data, typeof(ProtocolMessage));
#endif
            }
            else
            {
                protocolMessage = JsonHelper.Deserialize <ProtocolMessage>(data.Text);
            }

            if (protocolMessage != null)
            {
                foreach (var presenceMessage in protocolMessage.Presence)
                {
                    presenceMessage.Timestamp = protocolMessage.Timestamp;
                }

                foreach (var message in protocolMessage.Messages)
                {
                    message.Timestamp = protocolMessage.Timestamp;
                }
            }

            return(protocolMessage);
        }
            private Message GetPayload()
            {
#if MSGPACK
                var messages = MsgPackHelper.Deserialise(LastRequest.RequestBody, typeof(List <Message>)) as List <Message>;
                return(messages.First());
#else
                return(null);
#endif
            }
Example #4
0
        private IEnumerable <Stats> ParseStatsResponse(AblyResponse response)
        {
            var body = response.TextResponse;

            if (_protocol == Protocol.MsgPack)
            {
                return((List <Stats>)MsgPackHelper.Deserialise(response.Body, typeof(List <Stats>)));
            }
            return(JsonHelper.Deserialize <List <Stats> >(body));
        }
Example #5
0
        public void CanDeserialiseConnectionDetailsMessages()
        {
            var connectionDetails = new ConnectionDetails()
            {
                ClientId = "123", ConnectionStateTtl = TimeSpan.FromSeconds(60)
            };
            var serialized   = MsgPackHelper.Serialise(connectionDetails);
            var deserialized = MsgPackHelper.Deserialise(serialized, typeof(ConnectionDetails));

            deserialized.ShouldBeEquivalentTo(connectionDetails);
        }
Example #6
0
        public T ParseResponse <T>(AblyRequest request, AblyResponse response) where T : class
        {
            LogResponse(response);

            var responseText = response.TextResponse;

            if (_protocol == Protocol.MsgPack)
            {
                return((T)MsgPackHelper.Deserialise(response.Body, typeof(T)));
            }
            return(JsonHelper.Deserialize <T>(responseText));
        }
Example #7
0
        public void CanSerialiseAndDeserialiseStatsCorrectly()
        {
            var bytes =
                "kYqqaW50ZXJ2YWxJZLAyMDE1LTAyLTAzOjE1OjA1pHVuaXSmbWludXRlo2FsbIKjYWxsgqVjb3VudG6kZGF0Yc0q+KhtZXNzYWdlc4KlY291bnRupGRhdGHNKvinaW5ib3VuZIKjYWxsgqNhbGyCpWNvdW50RqRkYXRhzRtYqG1lc3NhZ2VzgqVjb3VudEakZGF0Yc0bWKhyZWFsdGltZYKjYWxsgqVjb3VudEakZGF0Yc0bWKhtZXNzYWdlc4KlY291bnRGpGRhdGHNG1iob3V0Ym91bmSCo2FsbIKjYWxsgqVjb3VudCikZGF0Yc0PoKhtZXNzYWdlc4KlY291bnQopGRhdGHND6CocmVhbHRpbWWCo2FsbIKlY291bnQopGRhdGHND6CobWVzc2FnZXOCpWNvdW50KKRkYXRhzQ+gqXBlcnNpc3RlZIKjYWxsgqVjb3VudBSkZGF0Yc0H0KhwcmVzZW5jZYKlY291bnQUpGRhdGHNB9CrY29ubmVjdGlvbnOCo2FsbIOkcGVhaxSjbWluAKZvcGVuZWQKo3Rsc4KkcGVhaxSmb3BlbmVkCqhjaGFubmVsc4KkcGVhazKmb3BlbmVkHqthcGlSZXF1ZXN0c4Kpc3VjY2VlZGVkMqZmYWlsZWQKrXRva2VuUmVxdWVzdHOCqXN1Y2NlZWRlZDymZmFpbGVkFA=="
                .FromBase64();

            var expected = JsonHelper.Deserialize <List <Stats> >(ResourceHelper.GetResource("MsgPackStatsTest.json"));

            var unpacked = (List <Stats>)MsgPackHelper.Deserialise(bytes, typeof(List <Stats>));

            unpacked.ShouldBeEquivalentTo(expected);
        }
            public void CanSerialiseAndDeserializeProtocolMessage()
            {
                var message = new ProtocolMessage(ProtocolMessage.MessageAction.Presence, "boo");

                message.Presence = new[] { new PresenceMessage(PresenceAction.Enter, "123", "my data") };

                var data   = MsgPackHelper.Serialise(message);
                var result = MsgPackHelper.Deserialise(data, typeof(ProtocolMessage)) as ProtocolMessage;

                result.Action.Should().Be(message.Action);
                result.Presence.First().Data.Should().Be(message.Presence[0].Data);
            }
Example #9
0
        private static IEnumerable <T> ParseOther <T>(AblyResponse response)
        {
            var body = response.TextResponse;

#if MSGPACK
            if (_protocol == Protocol.MsgPack)
            {
                return((List <Stats>)MsgPackHelper.Deserialise(response.Body, typeof(List <Stats>)));
            }
#endif
            return(JsonHelper.Deserialize <List <T> >(body) ?? new List <T>());
        }
Example #10
0
        public void CanSerialiseListOfMessagesAndDeserialiseThem()
        {
            var message    = new Message("example", "The quick brown fox jumped over the lazy dog");
            var serialised = MsgPackHelper.Serialise(new List <Message> {
                message
            });

            var result        = MsgPackHelper.Deserialise(serialised, typeof(List <Message>)) as List <Message>;
            var resultMessage = result.First();

            resultMessage.Data.Should().Be(message.Data);
            resultMessage.Name.Should().Be(message.Name);
        }
Example #11
0
        private void LogRequestBody(byte[] requestBody)
        {
            try
            {
                var body = MsgPackHelper.Deserialise(requestBody, typeof(MessagePackObject)).ToString();

                Logger.Debug("RequestBody: " + body);
            }
            catch (Exception ex)
            {
                Logger.Error("Error while logging request body.", ex);
            }
        }
Example #12
0
        public IEnumerable <PresenceMessage> ParsePresenceMessages(AblyResponse response, ChannelOptions options)
        {
            if (response.Type == ResponseType.Json)
            {
                var messages = JsonHelper.Deserialize <List <PresenceMessage> >(response.TextResponse);
                ProcessMessages(messages, options);
                return(messages);
            }

            var payloads = MsgPackHelper.Deserialise(response.Body, typeof(List <PresenceMessage>)) as List <PresenceMessage>;

            ProcessMessages(payloads, options);
            return(payloads);
        }
Example #13
0
        public void CanSerialiseAndDeserialiseBase64ByteArray()
        {
            var message = new Message()
            {
                Name = "example", Data = "AAECAwQFBgcICQoLDA0ODw==".FromBase64()
            };
            var serialised = MsgPackHelper.Serialise(new List <Message> {
                message
            });
            var resultMessage = MsgPackHelper.Deserialise(serialised, typeof(List <Message>)) as List <Message>;
            var data          = resultMessage.First().Data as byte[];

            data.Should().BeEquivalentTo(message.Data as byte[]);
            resultMessage.First().Name.Should().Be(message.Name);
        }
Example #14
0
        public void CanSerialiseAndDeserialiseTokenDetailsWithEmptyCapability()
        {
            var details = new TokenDetails()
            {
                Token      = "DaC_fA.DzqwNZFHNIl_GpUR6ENpeqewYnzf5LtI2L2RCP0eDSs597_6OXEW5vumSHiBn2pdxP7ge420UNbrqpKaS_W4aWE5oc15OriGL_8hELLGpgDEoNsnUixWIizGvhsnKVFYT",
                KeyName    = "DaC_fA.ChPHsQ",
                Issued     = 1462827574141.FromUnixTimeInMilliseconds(),
                Capability = new Capability(),
            };

            var packed   = MsgPackHelper.Serialise(details);
            var unpacked = (TokenDetails)MsgPackHelper.Deserialise(packed, typeof(TokenDetails));

            unpacked.ShouldBeEquivalentTo(details);
        }
Example #15
0
        public void DeserializesMessageCorrectly_Count(int count)
        {
            // Arrange
            byte[] expectedMessage = MsgPackHelper.Serialise(new ProtocolMessage()
            {
                Count = count
            }) as byte[];

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage);

            // Assert
            Assert.NotNull(target);
            Assert.Equal(count, target.Count.Value);
        }
Example #16
0
        public void DeserializesMessageCorrectly_Id(string id)
        {
            // Arrange
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x81);
            expectedMessage.AddRange(SerializeString("id"));
            expectedMessage.AddRange(SerializeString(id));

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage.ToArray());

            // Assert
            Assert.NotNull(target);
            Assert.Equal <string>(id, target.Id);
        }
Example #17
0
        public void DeserializesMessageCorrectly_Action(ProtocolMessage.MessageAction action)
        {
            // Arrange
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x81);
            expectedMessage.AddRange(SerializeString("action"));
            expectedMessage.Add((byte)action);

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage.ToArray());

            // Assert
            Assert.NotNull(target);
            Assert.Equal(action, target.Action);
        }
Example #18
0
        public IEnumerable <PresenceMessage> ParsePresenceMessages(AblyResponse response, ChannelOptions options)
        {
            if (response.Type == ResponseType.Json)
            {
                var messages = JsonHelper.Deserialize <List <PresenceMessage> >(response.TextResponse);
                ProcessMessages(messages, options);
                return(messages);
            }
#if MSGPACK
            var payloads = MsgPackHelper.Deserialise(response.Body, typeof(List <PresenceMessage>)) as List <PresenceMessage>;
            ProcessMessages(payloads, options);
            return(payloads);
#else
            throw new AblyException($"Response of type '{response.Type}' is invalid because MsgPack support was not enabled for this build.");
#endif
        }
        public void DeserializesMessageCorrectly_ConnectionId(string connectionId)
        {
            // Arrange
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x81);
            expectedMessage.AddRange(SerializeString("connectionId"));
            expectedMessage.AddRange(SerializeString(connectionId));

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage.ToArray());

            // Assert
            target.Should().NotBeNull();
            Assert.Equal(connectionId, target.ConnectionId);
        }
        public void DeserializesMessageCorrectly_ConnectionSerial(long connectionSerial)
        {
            // Arrange
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x81);
            expectedMessage.AddRange(SerializeString("connectionSerial"));
            expectedMessage.Add(BitConverter.GetBytes(connectionSerial).First());

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage.ToArray());

            // Assert
            target.Should().NotBeNull();
            Assert.Equal <long>(connectionSerial, target.ConnectionSerial.Value);
        }
Example #21
0
        public void CanDeserialiseTokenResponse()
        {
            var value =
                "gaxhY2Nlc3NfdG9rZW6GpXRva2Vu2YhnNFg2UVEuRHlCYzlMZUdvdy1saWVEcG4zTXRsd09uUEhoN2VtN3MyQ3JTZ1pLM2NUNkRvZUo1dlQxWXRwNDFvaTVWUUtNUkxuSVdDckFadHVOb3F5Q0lvVFphQjFfb1FFX0Utb3c2Y3hKX1EwcFUyZ3lpb2xRNGp1VDM1TjI0Qzgzd0p6aUI5p2tleU5hbWWtZzRYNlFRLnV0ekdsZ6Zpc3N1ZWTOVMEP1qdleHBpcmVzzlTBHeaqY2FwYWJpbGl0eYGhKpGhKqhjbGllbnRJZKMxMjM=";

            var decodedMessagePack = MsgPackHelper.Deserialise(value.FromBase64(), typeof(MessagePackObject)).ToString();

            var response = JsonHelper.Deserialize <TokenResponse>(decodedMessagePack);

            response.AccessToken.Should().NotBeNull();
            response.AccessToken.Capability.ToJson().Should().Be("{\"*\":[\"*\"]}");
            response.AccessToken.ClientId.Should().Be("123");
            response.AccessToken.Token.Should().Be("g4X6QQ.DyBc9LeGow-lieDpn3MtlwOnPHh7em7s2CrSgZK3cT6DoeJ5vT1Ytp41oi5VQKMRLnIWCrAZtuNoqyCIoTZaB1_oQE_E-ow6cxJ_Q0pU2gyiolQ4juT35N24C83wJziB9");
            response.AccessToken.Issued.Should().Be(((long)1421938646).FromUnixTimeInMilliseconds());
            response.AccessToken.Expires.Should().Be(((long)1421942246).FromUnixTimeInMilliseconds());
        }
Example #22
0
        public void DeserializesMessageCorrectly_MsgSerial(long serial)
        {
            // Arrange
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x81);
            expectedMessage.AddRange(SerializeString("msgSerial"));
            expectedMessage.Add(BitConverter.GetBytes(serial).First());

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage.ToArray());

            // Assert
            Assert.NotNull(target);
            Assert.Equal <long>(serial, target.MsgSerial);
        }
Example #23
0
        public void DeserializesMessageCorrectly_ConnectionKey(string connectionKey)
        {
            // Arrange
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x81);
            expectedMessage.AddRange(SerializeString("connectionKey"));
            expectedMessage.AddRange(SerializeString(connectionKey));

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage.ToArray());

            // Assert
            Assert.NotNull(target);
            Assert.Equal <string>(connectionKey, target.ConnectionKey);
        }
Example #24
0
        private void socket_DataReceived(object sender, DataReceivedEventArgs e)
        {
            if (Logger.IsDebug)
            {
                try
                {
                    var message = MsgPackHelper.Deserialise(e.Data, typeof(MessagePackObject)).ToString();
                    Logger.Debug("Websocket data message received. Raw: " + message);
                }
                catch (Exception)
                {
                    Logger.Debug("Error parsing message as MsgPack.");
                }
            }

            Listener?.OnTransportDataReceived(new RealtimeTransportData(e.Data));
        }
Example #25
0
        public void DeserializesMessageCorrectly_Flags(int flags)
        {
            // Arrange

            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x81);
            expectedMessage.AddRange(SerializeString("flags"));
            expectedMessage.Add((byte)flags);

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage.ToArray());

            // Assert
            Assert.NotNull(target);
            Assert.Equal <byte>((byte)flags, (byte)target.Flags);
        }
Example #26
0
        public void WithMsgPackEncoding_CanSerialiseAndDeserializeProtocolMessage()
        {
#if MSGPACK
            if (!Defaults.MsgPackEnabled)
            {
                return;
            }

            var message = new ProtocolMessage(ProtocolMessage.MessageAction.Presence, "boo");
            message.Presence = new[] { new PresenceMessage(PresenceAction.Enter, "123", "my data") };

            var data   = MsgPackHelper.Serialise(message);
            var result = MsgPackHelper.Deserialise(data, typeof(ProtocolMessage)) as ProtocolMessage;

            result.Action.Should().Be(message.Action);
            result.Presence.First().Data.Should().Be(message.Presence[0].Data);
#endif
        }
Example #27
0
 private void LogResponse(AblyResponse response)
 {
     if (Logger.IsDebug)
     {
         Logger.Debug("Protocol:" + _protocol);
         try
         {
             var responseBody = response.TextResponse;
             if (_protocol == Protocol.MsgPack && response.Body != null)
             {
                 responseBody = MsgPackHelper.Deserialise(response.Body, typeof(MessagePackObject)).ToString();
             }
             Logger.Debug("Response: " + responseBody);
         }
         catch (Exception ex)
         {
             Logger.Error("Error while logging response body.", ex);
         }
     }
 }
Example #28
0
        public void CanSerializeAndDeserialiseCapabilityObject()
        {
            var allAllowed      = Capability.AllowAll;
            var withOneResource = new Capability();

            withOneResource.AddResource("test").AllowPresence().AllowPublish().AllowSubscribe();
            var withTwoResources = new Capability();

            withTwoResources.AddResource("one").AllowAll();
            withTwoResources.AddResource("two").AllowPublish().AllowSubscribe();

            var list = new[] { allAllowed, withOneResource, withTwoResources };

            foreach (var item in list)
            {
                var data     = MsgPackHelper.Serialise(item);
                var unpacked = MsgPackHelper.Deserialise(data, typeof(Capability));
                Assert.Equal(item, unpacked);
            }
        }
Example #29
0
        public void DeserializesMessageCorrectly_Messages(byte[] messageBin, params Message[] expectedMessages)
        {
            // Arrange

            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x81);
            expectedMessage.AddRange(SerializeString("messages"));
            expectedMessage.AddRange(messageBin);

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage.ToArray());

            // Assert
            Assert.NotNull(target);
            Assert.NotNull(target.Messages);
            Assert.Equal <int>(expectedMessages.Length, target.Messages.Length);
            for (int i = 0; i < expectedMessages.Length; i++)
            {
                Assert.Equal <string>(expectedMessages[i].Name, target.Messages[i].Name);
                Assert.Equal(expectedMessages[i].Data, target.Messages[i].Data);
            }
        }
Example #30
0
        public void DeserializesMessageCorrectly_ChannelSerial(string serial)
        {
            // Arrange
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x81);
            expectedMessage.AddRange(SerializeString("channelSerial"));
            if (serial != null)
            {
                expectedMessage.AddRange(SerializeString(serial));
            }
            else
            {
                expectedMessage.Add(0xc0);
            }

            // Act
            ProtocolMessage target = MsgPackHelper.Deserialise <ProtocolMessage>(expectedMessage.ToArray());

            // Assert
            Assert.NotNull(target);
            Assert.Equal <string>(serial, target.ChannelSerial);
        }