重新启动应用程序:延迟

Restarting Application: Delay

本文关键字:延迟 应用程序 重新启动      更新时间:2023-10-16

我正在将重启功能编码到我最新的孤岛危机服务器修改中,该功能可以远程重新启动服务器。如果服务器出现问题并且简单的系统重新加载无法修复它,这很有用,对于告诉服务器在指定时间重新启动以释放内存也很有用。

为了实现这一点,我已经编写了所需的函数,并且应用程序本身重新启动没有问题。问题是端口关闭速度不够快,导致应用程序的新实例无法正常运行。

我正在寻找一个理想的解决方案,即程序关闭并在秒钟后启动,而不是立即启动。这样做将使 Windows 有足够的时间来释放服务器正在使用的端口,并清理任何现有内存。

请注意:我已经删除了我的其他(相关)问题,因为显然关闭程序端口是不可能的,除非在分配端口时告诉它这样做,这是我无法做到的,因为我无法访问绑定到端口的代码的源代码。

代码(如果需要)

int CScriptBind_GameRules::Restart(IFunctionHandler *pH)
{
    bool arg1 = false;
    const char *arg2 = "";
    gEnv->pScriptSystem->BeginCall("Dynamic","GetValue");
    gEnv->pScriptSystem->PushFuncParam("r.enable");
    gEnv->pScriptSystem->EndCall(arg1); 
    gEnv->pScriptSystem->BeginCall("Dynamic","GetValue");
    gEnv->pScriptSystem->PushFuncParam("r.line");
    gEnv->pScriptSystem->EndCall(arg2); 
    if (arg1)
    {
        LogMsg(2, "System restart initiated.");
        if (arg2)
        {
            LogMsg(2, "System Reboot.");
            gEnv->pScriptSystem->BeginCall("os","execute");
            gEnv->pScriptSystem->PushFuncParam(arg2);
            gEnv->pScriptSystem->EndCall(), close((int)gEnv->pConsole->GetCVar("sv_port")->GetString());
            return pH->EndFunction();
        }
        else
        {
            LogMsg(2, "Internal Faliure.");
            return pH->EndFunction();
        }
        return pH->EndFunction();
    }
    LogMsg(2, "System restart cancelled: Feature is Disabled.");
    return pH->EndFunction();
}

我通常做的是添加一个命令行参数'StartupDelay'。 当服务器/任何东西启动时,在尝试运行侦听器等之前,它会检查该参数。如果没有参数,它正常运行,如果它找到"StartupDelay=2000",它会休眠 2 秒,然后尝试启动侦听器等。

结果 - 如果从桌面图标启动,它将立即启动。如果它需要"重新启动自身",它会设置参数以指示自身的新实例按照指示等待。