示例#1
0
        private static PacketDefinition Deserialize(string packetContent, PacketDefinition deserializedPacket, KeyValuePair <Tuple <Type, string>,
                                                                                                                             Dictionary <PacketIndexAttribute, PropertyInfo> > serializationInformation)
        {
            MatchCollection matches = Regex.Matches(packetContent, @"([^\040]+[\.][^\040]+[\040]?)+((?=\040)|$)|([^\040]+)((?=\040)|$)");

            if (matches.Count <= 0)
            {
                return(deserializedPacket);
            }

            foreach (KeyValuePair <PacketIndexAttribute, PropertyInfo> packetBasePropertyInfo in serializationInformation.Value)
            {
                int currentIndex = packetBasePropertyInfo.Key.Index + 1; // adding 2 because we need to skip incrementing number and packet header

                if (currentIndex < matches.Count)
                {
                    if (packetBasePropertyInfo.Key.SerializeToEnd)
                    {
                        // get the value to the end and stop deserialization
                        string valueToEnd = packetContent.Substring(matches[currentIndex].Index, packetContent.Length - matches[currentIndex].Index);
                        packetBasePropertyInfo.Value.SetValue(deserializedPacket,
                                                              DeserializeValue(packetBasePropertyInfo.Value.PropertyType, valueToEnd, packetBasePropertyInfo.Key, matches));
                        break;
                    }


                    string currentValue = matches[currentIndex].Value;

                    if (packetBasePropertyInfo.Value.PropertyType == typeof(string) && string.IsNullOrEmpty(currentValue))
                    {
                        throw new NullReferenceException();
                    }

                    // set the value & convert currentValue
                    packetBasePropertyInfo.Value.SetValue(deserializedPacket,
                                                          DeserializeValue(packetBasePropertyInfo.Value.PropertyType, currentValue, packetBasePropertyInfo.Key, matches));
                }
                else
                {
                    break;
                }
            }

            return(deserializedPacket);
        }
示例#2
0
 private static void SetDeserializationInformations(PacketDefinition packetDefinition, string packetContent, string packetHeader)
 {
     packetDefinition.OriginalContent = packetContent;
     packetDefinition.OriginalHeader  = packetHeader;
 }