0

I'm coding a restart feature into my newest Crysis Wars server modification that remotely reboots the server. This is useful if the server has a problem and a simple system reload does not fix it, also is useful to tell the server to restart at a specified time in order to free up memory.

I have coded the required functions in order to achieve this, and the application itself has no problem restarting. The issue is that the port is not closed quickly enough, resulting in a new instance of the application that cannot function properly.

I am looking for an ideal solution to this, that the program is shut down and launches two seconds later, instead of immediately. Doing this will give Windows enough time to free the port that the server was using, and clean up any existing memory.

Please Note: I have removed my other (related) question since apparently closing program ports is impossible without telling it to do so when it it assigned the port, which is something I cannot do since I don't have access to the sourcecode of the code that binds to the port.

The code, if it's required

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();
}
AStopher
  • 4,207
  • 11
  • 50
  • 75

1 Answers1

0

What I usually do is add a command-line parameter, 'StartupDelay'. When the server/whatever starts up, before attempting to run the listener etc, it checks for that parameter. If no param, it runs up normally, if it finds 'StartupDelay=2000', it sleeps for 2 seconds before attempting to start the listener etc.

Result - if started from desktop icon, it starts immediately. If it needs to 'restart itself' it sets the parameter to instruct the new instance of itself to wait as directed.

Martin James
  • 24,453
  • 3
  • 36
  • 60
  • Although this wasn't the exact answer, it did help me to find the solution. In the end I just made it execute a batch file which had a delay to execute the code to start the server application up again. – AStopher Nov 20 '13 at 13:39