public void IsBigEndian()
        {
            var serializer = new IntSerializer();
            var bytes      = serializer.Serialize(42);

            Assert.Equal(bytes.Length, 4);
            // most significant byte in smallest address.
            Assert.Equal(bytes[0], 0);
            Assert.Equal(bytes[3], 42);
        }
        public void CanReconstruct()
        {
            var serializer   = new IntSerializer();
            var deserializer = new IntDeserializer();

            foreach (int theInt in toTest)
            {
                var reconstructed = deserializer.Deserialize(serializer.Serialize(theInt));
                Assert.Equal(theInt, reconstructed);
            }
        }
        public void SerializationAgreesWithSystemNetHostToNetworkOrder()
        {
            foreach (int theInt in toTest)
            {
                int networkOrder = System.Net.IPAddress.HostToNetworkOrder(theInt);
                var bytes1       = BitConverter.GetBytes(networkOrder);

                var serializer = new IntSerializer();
                var bytes2     = serializer.Serialize(theInt);

                Assert.Equal(bytes1.Length, bytes2.Length);

                for (int i = 0; i < bytes1.Length; ++i)
                {
                    Assert.Equal(bytes1[i], bytes2[i]);
                }
            }
        }
Beispiel #4
0
        private Dictionary <Type, ISerializer> GetDefaultSerializers()
        {
            Dictionary <Type, ISerializer> tmp = new Dictionary <Type, ISerializer>();

            tmp[typeof(byte)]   = new ByteSerializer();
            tmp[typeof(sbyte)]  = new SByteSerializer();
            tmp[typeof(bool)]   = new BooleanSerializer();
            tmp[typeof(short)]  = new ShortSerializer();
            tmp[typeof(ushort)] = new UShortSerializer();
            tmp[typeof(int)]    = new IntSerializer();
            tmp[typeof(uint)]   = new UIntSerializer();
            tmp[typeof(long)]   = new LongSerializer();
            tmp[typeof(ulong)]  = new ULongSerializer();
            tmp[typeof(float)]  = new FloatSerializer();
            tmp[typeof(double)] = new DoubleSerializer();
            tmp[typeof(string)] = new StringSerializer(this);
            tmp[typeof(Array)]  = new ArraySerializer(this);

            return(tmp);
        }
        private ClassInfo <T> RegisterInternal <T>()
        {
            if (ClassInfo <T> .Instance != null)
            {
                return(ClassInfo <T> .Instance);
            }

            var t     = typeof(T);
            var props = t.GetProperties(
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.GetProperty |
                BindingFlags.SetProperty);
            var serializers = new List <FastCall <T> >();

            for (var i = 0; i < props.Length; i++)
            {
                var property     = props[i];
                var propertyType = property.PropertyType;

                var elementType = propertyType.IsArray ? propertyType.GetElementType() : propertyType;
                var callType    = propertyType.IsArray ? CallType.Array : CallType.Basic;

                if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    elementType = propertyType.GetGenericArguments()[0];
                    callType    = CallType.List;
                }

                var getMethod = property.GetGetMethod();
                var setMethod = property.GetSetMethod();
                if (getMethod == null || setMethod == null)
                {
                    continue;
                }

                FastCall <T> serialzer = null;
                if (propertyType.IsEnum)
                {
                    var underlyingType = Enum.GetUnderlyingType(propertyType);
                    if (underlyingType == typeof(byte))
                    {
                        serialzer = new EnumByteSerializer <T>(property, propertyType);
                    }
                    else if (underlyingType == typeof(int))
                    {
                        serialzer = new EnumIntSerializer <T>(property, propertyType);
                    }
                    else
                    {
                        throw new InvalidTypeException("Not supported enum underlying type: " + underlyingType.Name);
                    }
                }
                else if (elementType == typeof(string))
                {
                    serialzer = new StringSerializer <T>(_maxStringLength);
                }
                else if (elementType == typeof(bool))
                {
                    serialzer = new BoolSerializer <T>();
                }
                else if (elementType == typeof(byte))
                {
                    serialzer = new ByteSerializer <T>();
                }
                else if (elementType == typeof(sbyte))
                {
                    serialzer = new SByteSerializer <T>();
                }
                else if (elementType == typeof(short))
                {
                    serialzer = new ShortSerializer <T>();
                }
                else if (elementType == typeof(ushort))
                {
                    serialzer = new UShortSerializer <T>();
                }
                else if (elementType == typeof(int))
                {
                    serialzer = new IntSerializer <T>();
                }
                else if (elementType == typeof(uint))
                {
                    serialzer = new UIntSerializer <T>();
                }
                else if (elementType == typeof(long))
                {
                    serialzer = new LongSerializer <T>();
                }
                else if (elementType == typeof(ulong))
                {
                    serialzer = new ULongSerializer <T>();
                }
                else if (elementType == typeof(float))
                {
                    serialzer = new FloatSerializer <T>();
                }
                else if (elementType == typeof(double))
                {
                    serialzer = new DoubleSerializer <T>();
                }
                else if (elementType == typeof(char))
                {
                    serialzer = new CharSerializer <T>();
                }
                else if (elementType == typeof(IPEndPoint))
                {
                    serialzer = new IPEndPointSerializer <T>();
                }
                else
                {
                    CustomType customType;
                    _registeredTypes.TryGetValue(elementType, out customType);
                    if (customType != null)
                    {
                        serialzer = customType.Get <T>();
                    }
                }

                if (serialzer != null)
                {
                    serialzer.Init(getMethod, setMethod, callType);
                    serializers.Add(serialzer);
                }
                else
                {
                    throw new InvalidTypeException("Unknown property type: " + propertyType.FullName);
                }
            }

            ClassInfo <T> .Instance = new ClassInfo <T>(serializers);
            return(ClassInfo <T> .Instance);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="IntSchema"/> class.
 /// </summary>
 public IntSchema()
 {
     MinValue   = Int32.MinValue;
     MaxValue   = Int32.MaxValue;
     Serializer = new IntSerializer();
 }
Beispiel #7
0
        private ClassInfo <T> RegisterInternal <T>()
        {
            if (ClassInfo <T> .Instance != null)
            {
                return(ClassInfo <T> .Instance);
            }

            Type t = typeof(T);

            PropertyInfo[] props = t.GetProperties(
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.GetProperty |
                BindingFlags.SetProperty);
            List <FastCall <T> > serializers = new List <FastCall <T> >();

            for (int i = 0; i < props.Length; i++)
            {
                PropertyInfo property     = props[i];
                Type         propertyType = property.PropertyType;

                Type     elementType = propertyType.IsArray ? propertyType.GetElementType() : propertyType;
                CallType callType    = propertyType.IsArray ? CallType.Array : CallType.Basic;

                if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    elementType = propertyType.GetGenericArguments()[0];
                    callType    = CallType.List;
                }

                // Note from Cod: Required to get it to build
                // TODO: Fix this

                /*if (Attribute.IsDefined(property, typeof(IgnoreDataMemberAttribute)))
                 *  continue;*/

                MethodInfo getMethod = property.GetGetMethod();
                MethodInfo setMethod = property.GetSetMethod();
                if (getMethod == null || setMethod == null)
                {
                    continue;
                }

                FastCall <T> serialzer = null;
                if (propertyType.IsEnum)
                {
                    Type underlyingType = Enum.GetUnderlyingType(propertyType);
                    if (underlyingType == typeof(byte))
                    {
                        serialzer = new EnumByteSerializer <T>(property, propertyType);
                    }
                    else if (underlyingType == typeof(int))
                    {
                        serialzer = new EnumIntSerializer <T>(property, propertyType);
                    }
                    else
                    {
                        throw new InvalidTypeException("Not supported enum underlying type: " + underlyingType.Name);
                    }
                }
                else if (elementType == typeof(string))
                {
                    serialzer = new StringSerializer <T>(_maxStringLength);
                }
                else if (elementType == typeof(bool))
                {
                    serialzer = new BoolSerializer <T>();
                }
                else if (elementType == typeof(byte))
                {
                    serialzer = new ByteSerializer <T>();
                }
                else if (elementType == typeof(sbyte))
                {
                    serialzer = new SByteSerializer <T>();
                }
                else if (elementType == typeof(short))
                {
                    serialzer = new ShortSerializer <T>();
                }
                else if (elementType == typeof(ushort))
                {
                    serialzer = new UShortSerializer <T>();
                }
                else if (elementType == typeof(int))
                {
                    serialzer = new IntSerializer <T>();
                }
                else if (elementType == typeof(uint))
                {
                    serialzer = new UIntSerializer <T>();
                }
                else if (elementType == typeof(long))
                {
                    serialzer = new LongSerializer <T>();
                }
                else if (elementType == typeof(ulong))
                {
                    serialzer = new ULongSerializer <T>();
                }
                else if (elementType == typeof(float))
                {
                    serialzer = new FloatSerializer <T>();
                }
                else if (elementType == typeof(double))
                {
                    serialzer = new DoubleSerializer <T>();
                }
                else if (elementType == typeof(char))
                {
                    serialzer = new CharSerializer <T>();
                }
                else if (elementType == typeof(IPEndPoint))
                {
                    serialzer = new IPEndPointSerializer <T>();
                }
                else
                {
                    _registeredTypes.TryGetValue(elementType, out CustomType customType);
                    if (customType != null)
                    {
                        serialzer = customType.Get <T>();
                    }
                }

                if (serialzer != null)
                {
                    serialzer.Init(getMethod, setMethod, callType);
                    serializers.Add(serialzer);
                }
                else
                {
                    throw new InvalidTypeException("Unknown property type: " + propertyType.FullName);
                }
            }
            ClassInfo <T> .Instance = new ClassInfo <T>(serializers);
            return(ClassInfo <T> .Instance);
        }
Beispiel #8
0
        public object executeStatement(string strSql)
        {
            string strErr = string.Empty;

            try
            {
                // We'll have already checked for "CREATE INDEX". Go ahead and remove that.
                strSql = strSql.Substring("CREATE INDEX ".Length);

                string[] astrTokes = strSql.StringToNonWhitespaceTokens2();

                foreach (string str in astrTokes)
                {
                    Console.WriteLine(str);
                }

                string strIndexName = astrTokes[0];
                string strTableName = astrTokes[2];
                string strColName   = astrTokes[3];

                TableContext table  = _database.getTableByName(strTableName);
                Column       column = table.getColumnByName(strColName);

                if (null == column)
                {
                    throw new Exception("Column " + strColName + " not found in table " + strTableName);
                }

                Dictionary <int, byte[]> dictRows = new Dictionary <int, byte[]>();

                string strIndexLoc = table.strTableFileLoc.Substring(0, table.strTableFileLoc.Length - 5) + ".mdbi";

                Column        colFauxIndex  = new Column("rowNum", COLUMN_TYPES.INT, column.intColLength, 4);
                IntSerializer intSerializer = new IntSerializer(colFauxIndex);

                using (BinaryReader b = new BinaryReader(File.Open(table.strTableFileLoc, FileMode.Open)))
                {
                    int intRowCount = table.intFileLength / table.intRowLength;
                    b.BaseStream.Seek(2 * table.intRowLength, SeekOrigin.Begin);  // TODO: Code more defensively in case it's somehow not the right/minimum length

                    for (int i = 2; i < intRowCount; i++)
                    {
                        byte[] abytRow = b.ReadBytes(table.intRowLength);

                        // Check and make sure this is an active row, and has
                        // the standard row lead byte, 0x11.  If not, the row
                        // should not be read.
                        switch (abytRow[0])
                        {
                        case 0x88:
                            // DELETED
                            break;

                        case 0x11:
                            // ACTIVE
                            byte[] abytCol = new byte[column.intColLength];
                            Array.Copy(abytRow, column.intColStart, abytCol, 0, column.intColLength);
                            dictRows.Add(i, abytCol);
                            break;

                        default:
                            throw new Exception("Unexpected row state in CREATE INDEX: " + abytRow[0]);
                        }
                    }
                }

                // now write the index to a file.
                using (FileStream stream = new FileStream(strIndexLoc, FileMode.Create))
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        foreach (KeyValuePair <int, byte[]> kvp in dictRows.OrderBy(kvp => kvp.Value, new ByteComparer()))
                        {
                            writer.Write(kvp.Value);
                            writer.Write(intSerializer.toByteArray(kvp.Key.ToString()));
                        }
                        writer.Flush();
                    }
                }
            }
            catch (Exception e)
            {
                strErr = e.ToString();
            }

            return(string.IsNullOrWhiteSpace(strErr) ? "Index created" : strErr);
        }
Beispiel #9
0
        public static void Main(string[] args)
        {
            Log.CreateInstance(true);
            AngryWasp.Serializer.Serializer.Initialize();

            CommandLineParser cmd = CommandLineParser.Parse(args);

            NodeMapDataStore ds = new NodeMapDataStore();

            if (File.Exists("NodeMap.xml"))
            {
                Log.Instance.Write("Loading node map info");
                ds = new ObjectSerializer().Deserialize <NodeMapDataStore>(XDocument.Load("NodeMap.xml"));
                Log.Instance.Write($"Node map loaded {ds.NodeMap.Count} items from file");

                if (!File.Exists("/var/www/html/nodemap.json"))
                {
                    File.WriteAllText("/var/www/html/nodemap.json", $"{{\"status\":\"OK\",\"result\":{ds.FetchAll()}}}\r\n");
                }
            }

            string        apiKey      = null;
            List <string> allowedKeys = new List <string>();

            if (cmd["geo-api-key"] != null)
            {
                Log.Instance.Write("Geolocation API key loaded from command line");
                apiKey = cmd["geo-api-key"].Value;
            }

            if (apiKey == null && cmd["geo-api-key-file"] != null)
            {
                //todo: check file exists
                Log.Instance.Write("Geolocation API key loaded from file");
                apiKey = File.ReadAllText(cmd["geo-api-key-file"].Value);
            }

            if (apiKey == null)
            {
                Log.Instance.Write(Log_Severity.Fatal, "Geolocation API key not provided!");
                Environment.Exit(0);
            }
            else
            {
                Log.Instance.Write($"Loaded Geolocation API key {apiKey}");
            }

            if (cmd["access-keys"] != null)
            {
                //todo: check file exists
                Log.Instance.Write("Access keys loaded from file");
                Config.AllowedKeys = File.ReadAllLines(cmd["access-keys"].Value).ToList();
            }

            GeoLocator.ApiKey = apiKey;

            int port = DEFAULT_PORT;

            if (cmd["port"] != null)
            {
                port = new IntSerializer().Deserialize(cmd["port"].Value);
            }

            Log.Instance.Write($"Listening on port {port}");

            RpcListener r = new RpcListener();

            bool mapDataChanged = false;

            r.MapDataChanged += () => {
                mapDataChanged = true;
            };

            //run once every 5 minutes
            Timer t = new Timer(1000 * 60 * 5);

            t.Elapsed += (s, e) =>
            {
                if (mapDataChanged)
                {
                    Task.Run(() =>
                    {
                        new ObjectSerializer().Serialize(r.DataStore, "NodeMap.xml");
                        Log.Instance.Write("Node map data saved");

                        Log.Instance.Write("Saving node map data to json");
                        File.WriteAllText("/var/www/html/nodemap.json", $"{{\"status\":\"OK\",\"result\":{r.DataStore.FetchAll()}}}\r\n");

                        mapDataChanged = false;
                    });
                }
            };

            t.Start();

            r.Start(ds, port);

            Application.Start();
        }