Exemple #1
0
        static void Main(string[] args)
        {
            Init();

            string configPath = Path.Combine(AmoebaEnvironment.Paths.ConfigPath, "Service");

            if (!Directory.Exists(configPath))
            {
                Directory.CreateDirectory(configPath);
            }

            using (var bufferManager = new BufferManager(1024 * 1024 * 256, 1024 * 1024 * 256))
                using (var serviceManager = new ServiceManager(configPath, AmoebaEnvironment.Config.Cache.BlocksPath, bufferManager))
                {
                    serviceManager.Load();
                    serviceManager.Start();

                    try
                    {
                        using (var server = new AmoebaDaemonManager())
                        {
                            var info     = UriUtils.Parse(AmoebaEnvironment.Config.Daemon.ListenUri);
                            var endpoint = new IPEndPoint(IPAddress.Parse(info.GetValue <string>("Address")), info.GetValue <int>("Port"));

                            server.Watch(serviceManager, endpoint).Wait();
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }

                    serviceManager.Stop();
                    serviceManager.Save();
                }
        }
Exemple #2
0
        public void Run()
        {
            // カレントディレクトリをexeと同じディレクトリパスへ変更。
            Directory.SetCurrentDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));

            // ハンドルしていない例外をログ出力させる。
            AppDomain.CurrentDomain.UnhandledException += this.Program_UnhandledException;
            Thread.GetDomain().UnhandledException += this.Program_UnhandledException;

            // コマンドライン引数を解析。
            var options = CommandLine.Parser.Default.ParseArguments <AmoebaDaemonOptions>(Environment.GetCommandLineArgs())
                          .MapResult(
                (AmoebaDaemonOptions x) => x,
                errs => null);

            if (options == null)
            {
                return;
            }

            // Tomlファイルを読み込み。
            DaemonConfig config = null;
            {
#if !DEBUG
                if (File.Exists(options.ConfigFilePath))
                {
                    var tomlSettings = TomlSettings.Create(builder => builder
                                                           .ConfigureType <Version>(type => type
                                                                                    .WithConversionFor <TomlString>(convert => convert
                                                                                                                    .ToToml(tt => tt.ToString())
                                                                                                                    .FromToml(ft => Version.Parse(ft.Value)))));

                    config = Toml.ReadFile <DaemonConfig>(options.ConfigFilePath, tomlSettings);
                }
#else
                var basePath = "../../";

                config = new DaemonConfig(
                    new Version(0, 0, 0),
                    new DaemonConfig.CommunicationConfig("tcp:127.0.0.1:4040"),
                    new DaemonConfig.CacheConfig(Path.Combine("E:", "Test", "Cache.blocks")),
                    new DaemonConfig.PathsConfig(
                        Path.Combine(basePath, "Temp"),
                        Path.Combine(basePath, "Config", "Service"),
                        Path.Combine(basePath, "Log")));
#endif
            }
            if (config == null)
            {
                return;
            }

            // 既定のフォルダを作成する。
            {
                foreach (var propertyInfo in typeof(DaemonConfig.PathsConfig).GetProperties())
                {
                    string path = propertyInfo.GetValue(config.Paths) as string;
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                }
            }

            // Tempフォルダを環境変数に登録。
            {
                // Tempフォルダ内を掃除。
                try
                {
                    foreach (string path in Directory.GetFiles(config.Paths.TempDirectoryPath, "*", SearchOption.AllDirectories))
                    {
                        File.Delete(path);
                    }

                    foreach (string path in Directory.GetDirectories(config.Paths.TempDirectoryPath, "*", SearchOption.AllDirectories))
                    {
                        Directory.Delete(path, true);
                    }
                }
                catch (Exception)
                {
                }

                Environment.SetEnvironmentVariable("TMP", Path.GetFullPath(config.Paths.TempDirectoryPath), EnvironmentVariableTarget.Process);
                Environment.SetEnvironmentVariable("TEMP", Path.GetFullPath(config.Paths.TempDirectoryPath), EnvironmentVariableTarget.Process);
            }

            // ログファイルを設定する。
            this.Setting_Log(config);

            _timer = new WatchTimer(() =>
            {
                GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
                GC.Collect();
            });
            _timer.Start(new TimeSpan(0, 30, 0));

            // サービス開始。
            try
            {
                using (var bufferManager = new BufferManager(1024 * 1024 * 1024))
                    using (var serviceManager = new ServiceManager(config.Paths.ConfigDirectoryPath, config.Cache.BlocksFilePath, bufferManager))
                    {
                        IPEndPoint endpoint;
                        {
                            var info = UriUtils.Parse(config.Communication.ListenUri);
                            endpoint = new IPEndPoint(IPAddress.Parse(info.GetValue <string>("Address")), info.GetValue <int>("Port"));
                        }

                        var tcpListener = new TcpListener(endpoint);
                        tcpListener.Start();

                        using (var socket = tcpListener.AcceptSocket())
                            using (var server = new AmoebaDaemonManager <ServiceManager>(socket, serviceManager, bufferManager))
                            {
                                try
                                {
                                    server.Watch();
                                }
                                catch (Exception e)
                                {
                                    Log.Error(e);

                                    Console.WriteLine(e.Message);
                                }
                            }

                        tcpListener.Stop();
                        tcpListener.Server.Dispose();
                    }
            }
            catch (Exception e)
            {
                Log.Error(e);

                Console.WriteLine(e.Message);
            }
        }