- added new option to headless modedisable reading from local console:

--headless-server-mode=vps

can be comgined with exit option:

--headless-server-mode=exit,vps
This commit is contained in:
Mark Vejvoda 2011-11-16 05:20:35 +00:00
parent 3e3bcb6af8
commit 54e4d7604c
2 changed files with 61 additions and 35 deletions

View File

@ -93,6 +93,7 @@ using namespace Shared;
namespace Glest{ namespace Game{ namespace Glest{ namespace Game{
bool disableheadless_console = false;
bool disableBacktrace = false; bool disableBacktrace = false;
bool gameInitialized = false; bool gameInitialized = false;
//static string application_binary=""; //static string application_binary="";
@ -2457,9 +2458,22 @@ int glestMain(int argc, char** argv) {
vector<string> paramPartTokens; vector<string> paramPartTokens;
Tokenize(paramValue,paramPartTokens,"="); Tokenize(paramValue,paramPartTokens,"=");
if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) {
string exitHeadless = paramPartTokens[1]; string headless_command_list = paramPartTokens[1];
printf("Forcing quit after game has compelted [%s]\n",exitHeadless.c_str());
Program::setWantShutdownApplicationAfterGame(true); vector<string> paramHeadlessCommandList;
Tokenize(headless_command_list,paramHeadlessCommandList,",");
for(unsigned int i = 0; i < paramHeadlessCommandList.size(); ++i) {
string headless_command = paramHeadlessCommandList[i];
if(headless_command == "exit") {
printf("Forcing quit after game has compelted [%s]\n",headless_command.c_str());
Program::setWantShutdownApplicationAfterGame(true);
}
else if(headless_command == "vps") {
printf("Disabled reading from console [%s]\n",headless_command.c_str());
disableheadless_console = true;
}
}
} }
} }
@ -3424,15 +3438,22 @@ int glestMain(int argc, char** argv) {
// Check for commands being input from stdin // Check for commands being input from stdin
string command=""; string command="";
#ifndef WIN32
#ifndef WIN32
pollfd cinfd[1]; pollfd cinfd[1];
// Theoretically this should always be 0, but one fileno call isn't going to hurt, and if #else
// we try to run somewhere that stdin isn't fd 0 then it will still just work HANDLE h = 0;
cinfd[0].fd = fileno(stdin); #endif
cinfd[0].events = POLLIN; if(disableheadless_console == false) {
#else #ifndef WIN32
HANDLE h = GetStdHandle(STD_INPUT_HANDLE); // Theoretically this should always be 0, but one fileno call isn't going to hurt, and if
#endif // we try to run somewhere that stdin isn't fd 0 then it will still just work
cinfd[0].fd = fileno(stdin);
cinfd[0].events = POLLIN;
#else
h = GetStdHandle(STD_INPUT_HANDLE);
#endif
}
if(isMasterServerModeEnabled == true) { if(isMasterServerModeEnabled == true) {
printf("Headless server is now running...\n"); printf("Headless server is now running...\n");
@ -3443,37 +3464,40 @@ int glestMain(int argc, char** argv) {
//main loop //main loop
while(program->isShutdownApplicationEnabled() == false && Window::handleEvent()) { while(program->isShutdownApplicationEnabled() == false && Window::handleEvent()) {
if(isMasterServerModeEnabled == true) { if(isMasterServerModeEnabled == true) {
if(disableheadless_console == false) {
#ifndef WIN32 #ifndef WIN32
int pollresult = poll(cinfd, 1, 0); int pollresult = poll(cinfd, 1, 0);
int pollerror = errno; int pollerror = errno;
if(pollresult) if(pollresult)
#else #else
// This is problematic because input on Windows is not line-buffered so this will return // This is problematic because input on Windows is not line-buffered so this will return
// even if getline may block. I haven't found a good way to fix it, so for the moment // even if getline may block. I haven't found a good way to fix it, so for the moment
// I just strongly suggest only running the server from the Python frontend, which does // I just strongly suggest only running the server from the Python frontend, which does
// line buffer input. This does work okay as long as the user doesn't enter characters // line buffer input. This does work okay as long as the user doesn't enter characters
// without pressing enter, and then try to end the server another way (say a remote // without pressing enter, and then try to end the server another way (say a remote
// console command), in which case we'll still be waiting for the stdin EOL and hang. // console command), in which case we'll still be waiting for the stdin EOL and hang.
if (WaitForSingleObject(h, 0) == WAIT_OBJECT_0) if (WaitForSingleObject(h, 0) == WAIT_OBJECT_0)
#endif #endif
{ {
getline(cin, command); getline(cin, command);
cin.clear(); cin.clear();
printf("server command [%s]\n",command.c_str()); printf("server command [%s]\n",command.c_str());
if(command == "quit") { if(command == "quit") {
break; break;
} }
#ifndef WIN32 #ifndef WIN32
if(pollresult < 0) { if(pollresult < 0) {
printf("pollresult = %d errno = %d [%s]\n",pollresult,pollerror,strerror(pollerror)); printf("pollresult = %d errno = %d [%s]\n",pollresult,pollerror,strerror(pollerror));
cinfd[0].fd = fileno(stdin); cinfd[0].fd = fileno(stdin);
cinfd[0].events = POLLIN; cinfd[0].events = POLLIN;
} }
#endif #endif
}
} }
//printf("looping\n"); //printf("looping\n");
} }

View File

@ -124,8 +124,10 @@ void printParameterHelp(const char *argv0, bool foundInvalidArgs) {
printf("\n%s=x\t\t\tAuto connects to a network server at IP or hostname x",GAME_ARGS[GAME_ARG_CLIENT]); printf("\n%s=x\t\t\tAuto connects to a network server at IP or hostname x",GAME_ARGS[GAME_ARG_CLIENT]);
printf("\n%s\t\t\tAuto creates a network server.",GAME_ARGS[GAME_ARG_SERVER]); printf("\n%s\t\t\tAuto creates a network server.",GAME_ARGS[GAME_ARG_SERVER]);
printf("\n%s=x\t\t\tRun as a headless server.",GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]); printf("\n%s=x,x\tRun as a headless server.",GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]);
printf("\n \t\tWhere x is an option command: exit which quits the application after a game has no more connected players."); printf("\n \t\tWhere x is an optional comma delimited command list of one or more of the following: ");
printf("\n \t\texit - which quits the application after a game has no more connected players.");
printf("\n \t\tvps - which does NOT read commands from the local console (required for some vps's).");
printf("\n%s=x\t\tAuto loads the specified scenario by scenario name.",GAME_ARGS[GAME_ARG_LOADSCENARIO]); printf("\n%s=x\t\tAuto loads the specified scenario by scenario name.",GAME_ARGS[GAME_ARG_LOADSCENARIO]);
printf("\n%s=x\t\tAuto Preview the specified map by map name.",GAME_ARGS[GAME_ARG_PREVIEW_MAP]); printf("\n%s=x\t\tAuto Preview the specified map by map name.",GAME_ARGS[GAME_ARG_PREVIEW_MAP]);