Beispiel #1
0
        private void RegisterFileSystemWatcher()
        {
            fileSystemWatcher.Changed += (sender, args) =>
            {
                ID = new ID();
                ProcessFile(args.FullPath);

                if (args.FullPath.EndsWith(".exe") || args.FullPath.EndsWith(".dll"))
                {
                    ServerHandler.LogMessage("[lwshost] [Updated File] " + args.FullPath);
                }
            };

            fileSystemWatcher.Created += (sender, args) =>
            {
                ProcessFile(args.FullPath);

                if (args.FullPath.EndsWith(".exe") || args.FullPath.EndsWith(".dll"))
                {
                    ServerHandler.LogMessage("[lwshost] [Added File] " + args.FullPath);
                }
            };

            fileSystemWatcher.Renamed += (sender, args) =>
            {
                if (TypesPerFile.ContainsKey(args.OldFullPath))
                {
                    TypesPerFile[args.FullPath] = TypesPerFile[args.OldFullPath];
                    TypesPerFile.Remove(args.OldFullPath);
                }

                ServerHandler.LogMessage("[lwshost] [Renamed File] " + args.FullPath);
            };

            fileSystemWatcher.EnableRaisingEvents = true;
        }
        private static void UserCleanup()
        {
            if (UserInfos.Count > MaxUsers)
            {
                DateTime oldestTime  = DateTime.UtcNow;
                string   oldestIndex = null;

                foreach (var userInfoPair in UserInfos)
                {
                    if (userInfoPair.Value.lastPullUtcTime < oldestTime)
                    {
                        oldestTime  = userInfoPair.Value.lastPullUtcTime;
                        oldestIndex = userInfoPair.Key;
                    }
                }

                if (oldestIndex != null)
                {
                    UserInfosByName.Remove(UserInfos[oldestIndex].UserName);
                    UserInfos.Remove(oldestIndex);
                }
            }
        }
Beispiel #3
0
        public void ExecuteTestHashMap()
        {
            List <string> hashes = new List <string>();
            List <string> values = new List <string>();
            const int     size   = 1000;

            Console.WriteLine("Small Tests...");
            Assert.IsTrue(hashmap.Count == 0);
            hashmap.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(hashmap.ContainsKey("key"));
            Assert.IsFalse(hashmap.ContainsKey("value"));
            Assert.IsTrue(hashmap["key"] == "value");
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Validate();
            hashmap.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(hashmap.ContainsKey("key"));
            Assert.IsFalse(hashmap.ContainsKey("value"));
            Assert.IsTrue(hashmap["key"] == "value2");
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Validate();
            hashmap.Add(new KeyValuePair <string, string>("key", "avalue"));
            Assert.IsTrue(hashmap.ContainsKey("key"));
            Assert.IsFalse(hashmap.ContainsKey("value"));
            Assert.IsTrue(hashmap["key"] == "avalue");
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Validate();
            Assert.IsFalse(hashmap.Remove("value"));
            Assert.IsTrue(hashmap.Remove("key"));
            hashmap.Validate();
            Assert.IsTrue(hashmap.Count == 0);
            hashmap.Add(new KeyValuePair <string, string>("key", "value2"));
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Clear();
            Assert.IsTrue(hashmap.Count == 0);
            hashmap.Add(new KeyValuePair <string, string>("key", "value"));
            Assert.IsTrue(hashmap.ContainsKey("key"));
            Assert.IsFalse(hashmap.ContainsKey("value"));
            Assert.IsTrue(hashmap["key"] == "value");
            Assert.IsTrue(hashmap.Count == 1);
            hashmap.Validate();
            hashmap.Clear();
            Assert.IsFalse(hashmap.Remove(""));
            Assert.IsFalse(hashmap.Remove(new KeyValuePair <string, string>("", "")));

            Console.WriteLine("Adding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(hashmap.Count == i);
                hashes.Add(Hash.GetHash());
                values.Add(Hash.GetHash());
                hashmap[hashes[i]] = values[i];
                Assert.IsTrue(hashmap[hashes[i]] == values[i]);
                Assert.IsTrue(hashmap.Keys.Contains(hashes[i]));
                Assert.IsTrue(hashmap.Values.Contains(values[i]));
                hashmap.Validate();
            }

            Console.WriteLine("Overriding...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(hashmap[hashes[i]] == values[i]);
                values[i]          = Hash.GetHash();
                hashmap[hashes[i]] = values[i];
                Assert.IsTrue(hashmap.Count == size);
            }

            Console.WriteLine("Checking...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(hashmap[hashes[i]] == values[i]);
                Assert.IsTrue(hashmap.Keys.Contains(hashes[i]));
                Assert.IsTrue(hashmap.Values.Contains(values[i]));
            }

            Console.WriteLine("Validating...");

            hashmap.Validate();

            Serializer.WriteXmlData(hashmap, nameof(hashmap));
            hashmap = Serializer.ReadXmlData <AVLHashMap <string, string> >(nameof(hashmap));

            hashmap.Validate();

            Console.WriteLine("Deleting...");

            for (int i = 0; i < size; i++)
            {
                Assert.IsTrue(hashmap.Count == size - i);
                Assert.IsTrue(hashmap.ContainsKey(hashes[i]));
                Assert.IsTrue(hashmap[hashes[i]] != default(string));
                Assert.IsTrue(hashmap.Remove(hashes[i]));
                Assert.IsFalse(hashmap.Keys.Contains(hashes[i]));
                Assert.IsFalse(hashmap.Values.Contains(values[i]));

                if (true)
                {
                    for (int j = i + 1; j < size; j++)
                    {
                        Assert.IsFalse(hashmap[hashes[j]].Contains(hashes[j]));
                    }

                    for (int j = 0; j < i; j++)
                    {
                        Assert.IsFalse(hashmap.Remove(hashes[j]));
                    }
                }

                Assert.IsTrue(hashmap[hashes[i]] == default(string));
                hashmap.Validate();
            }

            Serializer.WriteXmlData(hashmap, nameof(hashmap));
            hashmap = Serializer.ReadXmlData <AVLHashMap <string, string> >(nameof(hashmap));

            hashmap.Validate();
        }
        /// <summary>
        /// This method also creates a user if none exists!
        /// </summary>
        /// <param name="user">the current user</param>
        /// <param name="isNewSSID">has a new ssid been created or are we reusing the old one</param>
        /// <param name="userInfo">the UserInfo of the retrieved User</param>
        /// <returns></returns>
        internal static string GetSSIDforUser(string user, out bool isNewSSID, out UserInfo userInfo)
        {
            string hash = UserInfosByName[user]?.ID;

            if (hash == null)
            {
                userInfo          = new UserInfo();
                userInfo.ID       = GenerateUnusedHash();
                hash              = userInfo.ID;
                userInfo.UserName = user;

                mutex.WaitOne();

                UserInfos.Add(userInfo.ID, userInfo);
                UserInfosByName.Add(userInfo.UserName, userInfo);

                UserCleanup();

                mutex.ReleaseMutex();

                isNewSSID = true;
            }
            else
            {
                isNewSSID = false;

                if (SessionIdRereferencingMode == ESessionIdRereferencingMode.AlwaysRenew)
                {
                    mutex.WaitOne();

                    userInfo = UserInfosByName[user];
                    UserInfos.Remove(userInfo.ID);
                    userInfo.ID            = GenerateUnusedHash();
                    UserInfos[userInfo.ID] = userInfo;

                    UserCleanup();

                    mutex.ReleaseMutex();

                    isNewSSID = true;
                    hash      = userInfo.ID;
                }
                else if (SessionIdRereferencingMode == ESessionIdRereferencingMode.Keep && UserInfosByName[user].ID == null)
                {
                    mutex.WaitOne();

                    userInfo = UserInfosByName[user];
                    UserInfos.Remove(userInfo.ID);
                    userInfo.ID            = GenerateUnusedHash();
                    UserInfos[userInfo.ID] = userInfo;

                    UserCleanup();

                    mutex.ReleaseMutex();

                    isNewSSID = true;
                    hash      = userInfo.ID;
                }
                else
                {
                    userInfo = UserInfosByName[user];

                    if (userInfo != null)
                    {
                        userInfo.lastPullUtcTime = DateTime.UtcNow;
                    }
                }
            }

            return(hash);
        }