Exemplo n.º 1
0
        private static int GetFieldCount(ProtocolMessage message)
        {
            int fieldCount = 2; //action & msgSerial

            if (!string.IsNullOrEmpty(message.Channel))
            {
                fieldCount++;
            }
            if (message.Messages != null && message.Messages.Any(c => GetFieldCount(c) > 0))
            {
                fieldCount++;
            }
            if (message.Presence != null)
            {
                fieldCount++;
            }
            return(fieldCount);
        }
Exemplo n.º 2
0
        public ProtocolMessage DeserializeProtocolMessage(object value)
        {
            ProtocolMessage message = new ProtocolMessage();

            using (MemoryStream stream = new MemoryStream((byte[])value))
            {
                using (Unpacker unpacker = Unpacker.Create(stream))
                {
                    long fieldCount = 0;
                    unpacker.ReadMapLength(out fieldCount);
                    for (int i = 0; i < fieldCount; i++)
                    {
                        string fieldName;
                        unpacker.ReadString(out fieldName);
                        unpackActions[fieldName](unpacker, message);
                    }
                }
            }
            return(message);
        }
Exemplo n.º 3
0
        public ProtocolMessage DeserializeProtocolMessage(object value)
        {
            JObject         json    = JObject.Parse(value as string);
            ProtocolMessage message = new ProtocolMessage();

            JToken token;

            if (json.TryGetValue(ProtocolMessage.ActionPropertyName, out token))
            {
                message.Action = (ProtocolMessage.MessageAction)token.Value <int>();
            }
            if (json.TryGetValue(ProtocolMessage.FlagsPropertyName, out token))
            {
                message.Flags = (ProtocolMessage.MessageFlag)token.Value <int>();
            }
            if (json.TryGetValue(ProtocolMessage.CountPropertyName, out token))
            {
                message.Count = token.Value <int>();
            }
            if (json.TryGetValue(ProtocolMessage.MsgSerialPropertyName, out token))
            {
                message.MsgSerial = token.Value <long>();
            }
            if (json.TryGetValue(ProtocolMessage.ErrorPropertyName, out token))
            {
                JObject errorJObject = token.Value <JObject>();
                string  reason       = errorJObject.OptValue <string>(ErrorInfo.ReasonPropertyName);
                int     statusCode   = errorJObject.OptValue <int>(ErrorInfo.StatusCodePropertyName);
                int     code         = errorJObject.OptValue <int>(ErrorInfo.CodePropertyName);
                message.Error = new ErrorInfo(reason, code, statusCode == 0 ? null : (System.Net.HttpStatusCode?)statusCode);
            }
            if (json.TryGetValue(ProtocolMessage.IdPropertyName, out token))
            {
                message.Id = token.Value <string>();
            }
            if (json.TryGetValue(ProtocolMessage.ChannelPropertyName, out token))
            {
                message.Channel = token.Value <string>();
            }
            if (json.TryGetValue(ProtocolMessage.ChannelSerialPropertyName, out token))
            {
                message.ChannelSerial = token.Value <string>();
            }
            if (json.TryGetValue(ProtocolMessage.ConnectionIdPropertyName, out token))
            {
                message.ConnectionId = token.Value <string>();
            }
            if (json.TryGetValue(ProtocolMessage.ConnectionKeyPropertyName, out token))
            {
                message.ConnectionKey = token.Value <string>();
            }
            if (json.TryGetValue(ProtocolMessage.ConnectionSerialPropertyName, out token))
            {
                message.ConnectionSerial = token.Value <long>();
            }
            if (json.TryGetValue(ProtocolMessage.TimestampPropertyName, out token))
            {
                message.Timestamp = token.Value <long>().FromUnixTimeInMilliseconds();
            }
            if (json.TryGetValue(ProtocolMessage.MessagesPropertyName, out token))
            {
                JArray messagesArray = (JArray)token;
                message.Messages = messagesArray.Select(c => DeserializeMessage(c as JObject)).ToArray();
            }
            if (json.TryGetValue(ProtocolMessage.PresencePropertyName, out token))
            {
                JArray messagesArray = (JArray)token;
                message.Presence = messagesArray.Select(c => DeserializePresenceMessage(c as JObject)).ToArray();
            }

            return(message);
        }