Exemplo n.º 1
0
        private static void OnFileChange(object source, FileSystemEventArgs e)
        {
            Console.WriteLine("File: " + e.FullPath + " was changed: " + e.ChangeType);

            try {
                bool sleepAgain = true;
                if (updateThread != null && updateThread.IsAlive)
                {
                    sleepAgain = true;
                    return;
                }


                updateThread = new Thread(() => {
                    try {
                        while (sleepAgain)
                        {
                            sleepAgain = false;
                            Thread.Sleep(15000);
                        }

                        sleepAgain = true;
                        Console.WriteLine("Generating new file hashes..");
                        di = new DynatreeItem(new DirectoryInfo(modFolder), true);

                        Console.WriteLine("\nNew hashes generated...");
                    }
                    catch { }
                });
                updateThread.Start();
            } catch (Exception ex) {
                Console.WriteLine("File changed exception: " + ex);
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            Settings settings = Settings.ReadSettings();

            modFolder = settings.ModFolder;


            if (!File.Exists("repo.json"))
            {
                Console.WriteLine("Repo.json is missing, setting defaults...");
                Repo.WriteDefaults();
            }

            if (!Directory.Exists(modFolder))
            {
                Console.WriteLine("Unable to start Hasty server, mod folder is not configured/does not exist");
                Console.ReadLine();
                return;
            }

            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path = modFolder;
            watcher.IncludeSubdirectories = true;

            watcher.Changed += new FileSystemEventHandler(OnFileChange);
            watcher.Created += new FileSystemEventHandler(OnFileChange);

            watcher.EnableRaisingEvents = true;


            Console.WriteLine("Generating file hashes...");

            DirectoryInfo dirInfo = new DirectoryInfo(modFolder);

            di = new DynatreeItem(dirInfo);

            Console.WriteLine("\nHashes generated...");

            //new Thread(TcpCommunication.StartListener).Start();
            if (!Ftp.Init())
            {
                Console.WriteLine("Failed to init FTP");
            }

            if (!Ftp.Start())
            {
                Console.WriteLine("Failed to start FTP");
            }

            HttpServer server = new HttpServer();

            server.RequestReceived += (s, e) => {
                string path = e.Request.Path;

                using (var writer = new StreamWriter(e.Response.OutputStream)) {
                    if (path == "/" || path == "/repo.json")
                    {
                        JObject json = JObject.Parse(File.ReadAllText("repo.json"));

                        writer.Write(json.ToString());
                    }
                    else if (path == "/mod")
                    {
                        Console.WriteLine($"{DateTime.Now.ToShortTimeString()} Request for files from: " + e.Request.UserHostAddress);

                        int files = Directory.GetFiles(modFolder, "*", SearchOption.AllDirectories).Length;
                        di.totalFiles = files;
                        string result = di.DynatreeToJson();
                        writer.Write(result);
                    }
                    else
                    {
                        writer.Write("404 not found");
                    }
                }
            };

            server.EndPoint = new IPEndPoint(IPAddress.Any, 8090);

            server.Start();


            Console.WriteLine("Server running...");
            Console.ReadLine();
        }