Пример #1
0
        private static void PeriodicBackup()
        {
            // Create the backup path if it doesn't exist
            (new FileInfo(StarMapStateManager.MapFileDirectory)).Directory.Create();

            // Save the map
            var    mapToSave = StarMapStateManager.Build();
            string mapAsJson = JsonConvert.SerializeObject(mapToSave);

            logger.Info("Saving StarMap");
            WriteBoth(StarMapStateManager.MapFileDirectory, mapAsJson);

            // Save faction inventories
            var    inventoriesToSave = FactionInventoryStateManager.Build();
            string inventoryAsJson   = JsonConvert.SerializeObject(inventoriesToSave);

            logger.Info("Saving Faction Inventories");
            WriteBoth(FactionInventoryStateManager.ShopFileDirectory, inventoryAsJson);

            // Save player histories
            var    historiesToSave = PlayerStateManager.Build();
            string historyAsJson   = JsonConvert.SerializeObject(historiesToSave);

            logger.Info("Saving player history");
            lastBackupTime = DateTime.UtcNow;
        }
Пример #2
0
        /*
         * Application that uses Windows Communications Foundation (WCF) to provide a RESTful API that allows persistence of Morphyum's WarTech.
         * If you're unfamiliar with WCF, checkout the following:
         *
         * http://dotnetmentors.com/wcf/overview-on-wcf-service-architecture.aspx
         * https://docs.microsoft.com/en-us/dotnet/framework/wcf/extending/extending-dispatchers
         *
         * The client is the PersistentMapClient, in this repository.
         * This PersistentMapServer is the server.
         *
         * Note that WCF is no longer the preferred solution for REST endpoints, which has become ASP.NET5 w/ MVC6.
         *  See https://blog.tonysneed.com/2016/01/06/wcf-is-dead-long-live-mvc-6/.
         */
        static void Main(string[] args)
        {
            try {
                // Start a heart-beat monitor to check the server status
                BackgroundWorker heartbeatWorker = new BackgroundWorker {
                    WorkerSupportsCancellation = true
                };
                heartbeatWorker.DoWork             += new DoWorkEventHandler(HeartBeatMonitor.DoWork);
                heartbeatWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(HeartBeatMonitor.RunWorkerCompleted);
                heartbeatWorker.RunWorkerAsync();

                SettingsFileMonitor monitor = new SettingsFileMonitor();
                monitor.enable();

                BackgroundWorker playerHistoryPruner = new BackgroundWorker {
                    WorkerSupportsCancellation = true
                };
                playerHistoryPruner.DoWork             += new DoWorkEventHandler(PlayerHistoryPruner.DoWork);
                playerHistoryPruner.RunWorkerCompleted += new RunWorkerCompletedEventHandler(PlayerHistoryPruner.RunWorkerCompleted);
                playerHistoryPruner.RunWorkerAsync();

                BackgroundWorker backupWorker = new BackgroundWorker {
                    WorkerSupportsCancellation = true
                };

                backupWorker.DoWork             += new DoWorkEventHandler(BackupWorker.DoWork);
                backupWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackupWorker.RunWorkerCompleted);
                backupWorker.RunWorkerAsync();

                // Preload the data to allow any necessary initialization to happen
                StarMapStateManager.Build();
                FactionInventoryStateManager.Build();
                PlayerStateManager.Build();

                WarServices warServices = new WarServices();
                // Create an AOP proxy object that we can hang Castle.DynamicProxies upon. These are useful for operations across the whole
                //   of the service, or for when we need to fail a message in a reasonable way.
                var proxy = new Castle.DynamicProxy.ProxyGenerator()
                            .CreateClassProxyWithTarget <WarServices>(warServices, new Castle.DynamicProxy.IInterceptor[] {
                    new UserQuotaInterceptor(), new AdminKeyRequiredInterceptor()
                });

                // Create a RESTful service host. The service instance is automatically, through
                //   the WarServiceInstanceProviderBehaviorAttribute. We create the singleton this way to give
                //   us the chance to customize the binding
                WebServiceHost _serviceHost = new WebServiceHost(typeof(WarServices), new Uri(ServiceUrl));
                AddServiceBehaviors(_serviceHost);

                // Create a binding that wraps the default WebMessageEncodingBindingElement with a BindingElement
                //   that can GZip compress responses when a client requests it.
                WebMessageEncodingBindingElement innerEncoding = new WebMessageEncodingBindingElement {
                    ContentTypeMapper = new ForceJsonWebContentMapper()
                };
                GZipMessageEncodingBindingElement encodingWrapper = new GZipMessageEncodingBindingElement(innerEncoding);

                var transport = new HttpTransportBindingElement {
                    ManualAddressing = true,
                    KeepAliveEnabled = false,
                    AllowCookies     = false
                };

                var customBinding = new CustomBinding(encodingWrapper, transport);

                // Create a default endpoint with the JSON/XML behaviors and the behavior to check the incoming headers for GZIP requests
                var endpoint = _serviceHost.AddServiceEndpoint(typeof(IWarServices), customBinding, "");
                endpoint.Behaviors.Add(new WebHttpBehavior());
                endpoint.Behaviors.Add(new GZipBehavior());

                _serviceHost.Open();

                Console.WriteLine("Open Press Key to close");
                Console.ReadKey();

                _serviceHost.Close();
                Console.WriteLine("Connection Closed");

                // Cleanup any outstanding processes
                monitor.disable();

                heartbeatWorker.CancelAsync();

                playerHistoryPruner.CancelAsync();
                PlayerHistoryPruner.PruneOnExit();

                backupWorker.CancelAsync();
                BackupWorker.BackupOnExit();
            } catch (Exception e) {
                Console.WriteLine(e);
                Console.ReadKey();
            }
        }