/// %Thread start void CliRunnable::run() { ///- Init new SQL thread for the world database (one connection call enough) WorldDatabase.ThreadStart(); // let thread do safe mySQL requests char commandbuf[256]; ///- Display the list of available CLI functions then beep sLog.outString(); if (sConfig.GetBoolDefault("BeepAtStart", true)) printf("\a"); // \a = Alert // print this here the first time // later it will be printed after command queue updates printf("mangos>"); ///- As long as the World is running (no World::m_stopEvent), get the command line and handle it while (!World::IsStopped()) { fflush(stdout); #ifdef linux while (!kb_hit_return() && !World::IsStopped()) // With this, we limit CLI to 10commands/second usleep(100); if (World::IsStopped()) break; #endif char *command_str = fgets(commandbuf,sizeof(commandbuf),stdin); if (command_str != NULL) { for(int x=0;command_str[x];x++) if(command_str[x]=='\r'||command_str[x]=='\n') { command_str[x]=0; break; } if(!*command_str) { printf("mangos>"); continue; } std::string command; if(!consoleToUtf8(command_str,command)) // convert from console encoding to utf8 { printf("mangos>"); continue; } sWorld.QueueCliCommand(new CliCommandHolder(0, SEC_CONSOLE, NULL, command.c_str(), &utf8print, &commandFinished)); } else if (feof(stdin)) { World::StopNow(SHUTDOWN_EXIT_CODE); } } ///- End the database thread WorldDatabase.ThreadEnd(); // free mySQL thread resources }
/// %Thread start void CliRunnable::run() { ///- Init new SQL thread for the world database (one connection call enough) WorldDatabase.ThreadStart(); // let thread do safe mySQL requests char commandbuf[256]; ///- Display the list of available CLI functions then beep sLog.outString(); if (sConfig.GetBoolDefault("BeepAtStart", true)) printf("\a"); // \a = Alert // print this here the first time // later it will be printed after command queue updates printf("mangos>"); #ifdef __unix__ //Set stdin IO to nonblocking - prevent Server from hanging in shutdown process till enter is pressed int fd = fileno(stdin); int flags = fcntl(fd, F_GETFL, 0); flags |= O_NONBLOCK; fcntl(fd, F_SETFL, flags); #endif ///- As long as the World is running (no World::m_stopEvent), get the command line and handle it while (!World::IsStopped()) { fflush(stdout); #ifdef __unix__ while (!kb_hit_return() && !World::IsStopped()) { // With this, we limit CLI to 10commands/second std::this_thread::sleep_for(std::chrono::nanoseconds(100000)); // Check for world stoppage after each sleep interval if (World::IsStopped()) break; } #endif char* command_str = fgets(commandbuf, sizeof(commandbuf), stdin); if (command_str != nullptr) { for (int x = 0; command_str[x]; ++x) if (command_str[x] == '\r' || command_str[x] == '\n') { command_str[x] = 0; break; } if (!*command_str) { printf("mangos>"); continue; } std::string command; if (!consoleToUtf8(command_str, command)) // convert from console encoding to utf8 { printf("mangos>"); continue; } sWorld.QueueCliCommand(new CliCommandHolder(0, SEC_CONSOLE, command.c_str(), &utf8print, &commandFinished)); } else if (feof(stdin)) { World::StopNow(SHUTDOWN_EXIT_CODE); } } ///- End the database thread WorldDatabase.ThreadEnd(); // free mySQL thread resources }