Exemplo n.º 1
0
        private static async Task ChangeStatus(FactorioServerStatus newStatus)
        {
            var oldStatus = status;

            if (newStatus != status)
            {
                status = newStatus;
                Log.Information("Factorio status changed from {oldStatus} to {newStatus}", oldStatus, newStatus);
            }
            // Even if the status hasn't changed, still send it to the Server as this method is used to poll the status for reconnected processes.

            if (!connected)
            {
                return;
            }

            try
            {
                Log.Information("Sending Factorio status changed from {oldStatus} to {newStatus}", oldStatus, newStatus);
                await connection.SendAsync(nameof(IFactorioProcessServerMethods.StatusChanged), newStatus, oldStatus);
            }
            catch (Exception e)
            {
                Log.Error(e, "Error sending factorio status data");
            }
        }
Exemplo n.º 2
0
        private static void BuildConenction()
        {
            Log.Information("Building connection");

            connection = new HubConnectionBuilder()
                         .WithUrl(url, options =>
            {
                options.AccessTokenProvider = () => Task.FromResult(token);
            })
                         .Build();

            connection.Closed += async(error) =>
            {
                connected = false;

                if (exit)
                {
                    return;
                }

                Log.Information("Lost connection");
                await Reconnect();
            };

            connection.On <string>(nameof(IFactorioProcessClientMethods.SendToFactorio), async data =>
            {
                await SendToFactorio(data);
            });

            connection.On(nameof(IFactorioProcessClientMethods.Stop), async() =>
            {
                try
                {
                    await factorioProcessLock.WaitAsync();

                    Log.Information("Stopping factorio server.");

                    var p = factorioProcess;
                    if (p != null && !p.HasExited)
                    {
                        Process.Start("kill", $"-sigterm {factorioProcess.Id}");
                    }

                    await ChangeStatus(FactorioServerStatus.Stopping);
                }
                catch (Exception e)
                {
                    Log.Error(e, "Error stopping factorio process");
                    await SendWrapperData("Error stopping factorio process");
                }
                finally
                {
                    // If an error is throw above the status wont be changed.
                    // This changes the status in case the factorio process hasn't started yet, to make sure it doesn't start.
                    status = FactorioServerStatus.Stopping;
                    factorioProcessLock.Release();
                }
            });

            connection.On(nameof(IFactorioProcessClientMethods.ForceStop), async() =>
            {
                try
                {
                    await factorioProcessLock.WaitAsync();

                    Log.Information("Killing factorio server.");

                    var p = factorioProcess;
                    if (p != null && !p.HasExited)
                    {
                        p.Kill();
                    }

                    await ChangeStatus(FactorioServerStatus.Killing);
                }
                catch (Exception e)
                {
                    Log.Error(e, "Error force killing factorio process");
                    await SendWrapperData("Error killing factorio process");
                }
                finally
                {
                    // If an error is throw above the status wont be changed.
                    // This changes the status in case the factorio process hasn't started yet, to make sure it doesn't start.
                    status = FactorioServerStatus.Killing;
                    factorioProcessLock.Release();
                }
            });

            // This is so the Server Control can get the status if a connection was lost.
            connection.On(nameof(IFactorioProcessClientMethods.GetStatus), async() =>
            {
                Log.Information("Status requested");
                await ChangeStatus(status);
            });
        }