diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-05-24 18:18:51 +0200 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2009-05-24 18:18:51 +0200 |
commit | 509d0a5cd64e37c5f6397ce20b4aef877b604e18 (patch) | |
tree | 0c98b29338211001afdb4b49e41c19fcc4234688 | |
parent | 5a401a833d9807a257331f49a8a8ba9768a12655 (diff) | |
download | manaserv-509d0a5cd64e37c5f6397ce20b4aef877b604e18.tar.gz manaserv-509d0a5cd64e37c5f6397ce20b4aef877b604e18.tar.xz manaserv-509d0a5cd64e37c5f6397ce20b4aef877b604e18.zip |
Stopped tmwserv from writing to its configuration file
This was confusing, since changes made to the config file while the
server is running are lost once the server exists. Also, XML comments
were being stripped by the server.
The command line option to set the port now only applies to a single
run, and isn't saved in the configuration. There shouldn't be a need for
the server to write to its configuration file, it might not even have
the rights to do so.
-rw-r--r-- | src/account-server/main-account.cpp | 67 | ||||
-rw-r--r-- | src/common/configuration.cpp | 37 | ||||
-rw-r--r-- | src/common/configuration.hpp | 17 | ||||
-rw-r--r-- | src/game-server/main-game.cpp | 50 |
4 files changed, 71 insertions, 100 deletions
diff --git a/src/account-server/main-account.cpp b/src/account-server/main-account.cpp index b207ae0..4aa171b 100644 --- a/src/account-server/main-account.cpp +++ b/src/account-server/main-account.cpp @@ -46,6 +46,8 @@ #include "utils/stringfilter.h" #include "utils/timer.h" +using utils::Logger; + // Default options that automake should be able to override. #define DEFAULT_LOG_FILE "tmwserv-account.log" #define DEFAULT_STATS_FILE "tmwserv.stats" @@ -84,7 +86,6 @@ static void closeGracefully(int) */ static void initialize() { - // Reset to default segmentation fault handling for debugging purposes signal(SIGSEGV, SIG_DFL); @@ -253,26 +254,39 @@ static void printHelp() exit(0); } +struct CommandLineOptions +{ + CommandLineOptions(): + verbosity(Logger::INFO), + port(Configuration::getValue("net_accountServerPort", + DEFAULT_SERVER_PORT)) + {} + + Logger::Level verbosity; + int port; +}; + /** * Parse the command line arguments */ -static void parseOptions(int argc, char *argv[]) +static void parseOptions(int argc, char *argv[], CommandLineOptions &options) { const char *optstring = "h"; - const struct option long_options[] = { + const struct option long_options[] = + { { "help", no_argument, 0, 'h' }, { "verbosity", required_argument, 0, 'v' }, { "port", required_argument, 0, 'p' }, { 0, 0, 0, 0 } }; - while (optind < argc) { + while (optind < argc) + { int result = getopt_long(argc, argv, optstring, long_options, NULL); - if (result == -1) { + if (result == -1) break; - } switch (result) { default: // Unknown option @@ -281,18 +295,11 @@ static void parseOptions(int argc, char *argv[]) printHelp(); break; case 'v': - // Set Verbosity to level - unsigned short verbosityLevel; - verbosityLevel = atoi(optarg); - utils::Logger::setVerbosity(utils::Logger::Level(verbosityLevel)); - LOG_INFO("Setting Log Verbosity Level to " << verbosityLevel); + options.verbosity = static_cast<Logger::Level>(atoi(optarg)); + LOG_INFO("Using log verbosity level " << options.verbosity); break; case 'p': - // Change the port to listen on. - unsigned short portToListenOn; - portToListenOn = atoi(optarg); - Configuration::setValue("ListenOnPort", portToListenOn); - LOG_INFO("Setting Default Port to " << portToListenOn); + options.port = atoi(optarg); break; } } @@ -308,17 +315,18 @@ int main(int argc, char *argv[]) LOG_INFO("The Mana World Account+Chat Server v" << PACKAGE_VERSION); #endif - // Parse Command Line Options - parseOptions(argc, argv); + // Parse command line options + CommandLineOptions options; + parseOptions(argc, argv, options); + Logger::setVerbosity(options.verbosity); - // General Initialization + // General initialization initialize(); - int port = Configuration::getValue("net_accountServerPort", DEFAULT_SERVER_PORT); std::string host = Configuration::getValue("net_listenHost", std::string()); - if (!AccountClientHandler::initialize(port, host) || - !GameServerHandler::initialize(port + 1, host) || - !chatHandler->startListen(port + 2, host)) + if (!AccountClientHandler::initialize(options.port, host) || + !GameServerHandler::initialize(options.port + 1, host) || + !chatHandler->startListen(options.port + 2, host)) { LOG_FATAL("Unable to create an ENet server host."); return 3; @@ -341,15 +349,22 @@ int main(int argc, char *argv[]) storage->setWorldStateVar("accountserver_version", revision); // ------------------------------------------------------------------------- - while (running) { + while (running) + { AccountClientHandler::process(); GameServerHandler::process(); chatHandler->process(50); - if (statTimer.poll()) dumpStatistics(); - if (banTimer.poll()) storage->checkBannedAccounts(); + + if (statTimer.poll()) + dumpStatistics(); + + if (banTimer.poll()) + storage->checkBannedAccounts(); } LOG_INFO("Received: Quit signal, closing down..."); chatHandler->stopListen(); deinitialize(); + + return 0; } diff --git a/src/common/configuration.cpp b/src/common/configuration.cpp index 4b52b65..e749af8 100644 --- a/src/common/configuration.cpp +++ b/src/common/configuration.cpp @@ -21,7 +21,7 @@ #include <cmath> #include <map> -#include <libxml/xmlwriter.h> +#include <libxml/xmlreader.h> #include "common/configuration.hpp" @@ -67,41 +67,6 @@ void Configuration::initialize(const std::string &filename) void Configuration::deinitialize() { - xmlTextWriterPtr writer = xmlNewTextWriterFilename(configPath.c_str(), 0); - - if (writer) - { - xmlTextWriterSetIndent(writer, 1); - xmlTextWriterStartDocument(writer, NULL, NULL, NULL); - xmlTextWriterStartElement(writer, BAD_CAST "configuration"); - - std::map<std::string, std::string>::iterator iter; - - for (iter = options.begin(); iter != options.end(); iter++) - { - xmlTextWriterStartElement(writer, BAD_CAST "option"); - xmlTextWriterWriteAttribute(writer, - BAD_CAST "name", BAD_CAST iter->first.c_str()); - xmlTextWriterWriteAttribute(writer, - BAD_CAST "value", BAD_CAST iter->second.c_str()); - xmlTextWriterEndElement(writer); - } - - xmlTextWriterEndDocument(writer); - xmlFreeTextWriter(writer); - } -} - -void Configuration::setValue(const std::string &key, const std::string &value) -{ - options[key] = value; -} - -void Configuration::setValue(const std::string &key, int value) -{ - std::ostringstream ss; - ss << value; - setValue(key, ss.str()); } const std::string &Configuration::getValue(const std::string &key, diff --git a/src/common/configuration.hpp b/src/common/configuration.hpp index 1c1a9ce..b7c296f 100644 --- a/src/common/configuration.hpp +++ b/src/common/configuration.hpp @@ -32,26 +32,9 @@ namespace Configuration */ void initialize(const std::string &filename); - /** - * Writes the current settings back to the configuration file. - */ void deinitialize(); /** - * Sets an option to a given value. - * @param key option identifier. - * @param value Value. - */ - void setValue(const std::string &key, const std::string &value); - - /** - * Sets an option to a given value. - * @param key option identifier. - * @param value value. - */ - void setValue(const std::string &key, int value); - - /** * Gets an option as a string. * @param key option identifier. * @param deflt default value. diff --git a/src/game-server/main-game.cpp b/src/game-server/main-game.cpp index 0824a8d..03c6aa7 100644 --- a/src/game-server/main-game.cpp +++ b/src/game-server/main-game.cpp @@ -47,6 +47,8 @@ #include "utils/timer.h" #include "utils/mathutils.h" +using utils::Logger; + // Default options that automake should be able to override. #define DEFAULT_LOG_FILE "tmwserv-game.log" #define DEFAULT_CONFIG_FILE "tmwserv.xml" @@ -217,26 +219,39 @@ void printHelp() exit(0); } +struct CommandLineOptions +{ + CommandLineOptions(): + verbosity(Logger::INFO), + port(Configuration::getValue("net_gameServerPort", + DEFAULT_SERVER_PORT + 3)) + {} + + Logger::Level verbosity; + int port; +}; + /** * Parse the command line arguments */ -void parseOptions(int argc, char *argv[]) +void parseOptions(int argc, char *argv[], CommandLineOptions &options) { const char *optstring = "h"; - const struct option long_options[] = { + const struct option long_options[] = + { { "help", no_argument, 0, 'h' }, { "verbosity", required_argument, 0, 'v' }, { "port", required_argument, 0, 'p' }, { 0 } }; - while (optind < argc) { + while (optind < argc) + { int result = getopt_long(argc, argv, optstring, long_options, NULL); - if (result == -1) { + if (result == -1) break; - } switch (result) { default: // Unknown option @@ -245,18 +260,11 @@ void parseOptions(int argc, char *argv[]) printHelp(); break; case 'v': - // Set Verbosity to level - unsigned short verbosityLevel; - verbosityLevel = atoi(optarg); - utils::Logger::setVerbosity(utils::Logger::Level(verbosityLevel)); - LOG_INFO("Setting log verbosity level to " << verbosityLevel); + options.verbosity = static_cast<Logger::Level>(atoi(optarg)); + LOG_INFO("Using log verbosity level " << options.verbosity); break; case 'p': - // Change the port to listen on. - unsigned short portToListenOn; - portToListenOn = atoi(optarg); - Configuration::setValue("net_gameServerPort", portToListenOn); - LOG_INFO("Setting default port to " << portToListenOn); + options.port = atoi(optarg); break; } } @@ -274,7 +282,9 @@ int main(int argc, char *argv[]) #endif // Parse command line options - parseOptions(argc, argv); + CommandLineOptions options; + parseOptions(argc, argv, options); + Logger::setVerbosity(options.verbosity); // General initialization initialize(); @@ -282,10 +292,7 @@ int main(int argc, char *argv[]) // Make an initial attempt to connect to the account server accountHandler->start(); - int gameServerPort = - Configuration::getValue("net_gameServerPort", DEFAULT_SERVER_PORT + 3); - - if (!gameHandler->startListen(gameServerPort)) + if (!gameHandler->startListen(options.port)) { LOG_FATAL("Unable to create an ENet server host."); return 3; @@ -294,7 +301,8 @@ int main(int argc, char *argv[]) // Initialize world timer worldTimer.start(); - while (running) { + while (running) + { elapsedWorldTicks = worldTimer.poll(); if (elapsedWorldTicks > 0) { |