aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChaplain <aionthefirst@gmail.com>2014-07-15 17:46:09 +0200
committerleak <leak@bitmx.net>2014-07-15 18:15:55 +0200
commit68398a559e2264b85d8765949989f44e39ce364d (patch)
tree5524aa83c45922dcb5a05e5cc78e162233361093 /src
parentecde28d1c114a151c9828fff8898def5e74de4e1 (diff)
[Auth\Worldserver] Use boost to load console arguments. (Added a few style changes and cmake fix)
Conflicts: src/server/worldserver/Main.cpp
Diffstat (limited to 'src')
-rw-r--r--src/server/authserver/Main.cpp60
-rw-r--r--src/server/shared/Configuration/Config.cpp12
-rw-r--r--src/server/shared/Configuration/Config.h10
-rw-r--r--src/server/shared/Utilities/ServiceWin32.cpp6
-rw-r--r--src/server/worldserver/Main.cpp118
5 files changed, 100 insertions, 106 deletions
diff --git a/src/server/authserver/Main.cpp b/src/server/authserver/Main.cpp
index 1286e261a47..701f65c0c14 100644
--- a/src/server/authserver/Main.cpp
+++ b/src/server/authserver/Main.cpp
@@ -26,6 +26,7 @@
#include <cstdlib>
#include <boost/date_time/posix_time/posix_time.hpp>
+#include <boost/program_options.hpp>
#include <iostream>
#include <openssl/opensslv.h>
#include <openssl/crypto.h>
@@ -42,6 +43,7 @@
#include "Util.h"
using boost::asio::ip::tcp;
+using namespace boost::program_options;
#ifndef _TRINITY_REALM_CONFIG
# define _TRINITY_REALM_CONFIG "authserver.conf"
@@ -51,7 +53,7 @@ bool StartDB();
void StopDB();
void SignalHandler(const boost::system::error_code& error, int signalNumber);
void KeepDatabaseAliveHandler(const boost::system::error_code& error);
-void usage(const char* prog);
+variables_map GetConsoleArguments(int argc, char** argv, std::string& configFile);
boost::asio::io_service _ioService;
boost::asio::deadline_timer _dbPingTimer(_ioService);
@@ -60,36 +62,24 @@ LoginDatabaseWorkerPool LoginDatabase;
int main(int argc, char** argv)
{
- // Command line parsing to get the configuration file name
- char const* configFile = _TRINITY_REALM_CONFIG;
- int count = 1;
- while (count < argc)
- {
- if (strcmp(argv[count], "-c") == 0)
- {
- if (++count >= argc)
- {
- printf("Runtime-Error: -c option requires an input argument\n");
- usage(argv[0]);
- return 1;
- }
- else
- configFile = argv[count];
- }
- ++count;
- }
+ std::string configFile = _TRINITY_REALM_CONFIG;
+ auto vm = GetConsoleArguments(argc, argv, configFile);
+ // exit if help is enabled
+ if (vm.count("help"))
+ return 0;
if (!sConfigMgr->LoadInitial(configFile))
{
- printf("Invalid or missing configuration file : %s\n", configFile);
+ printf("Invalid or missing configuration file : %s\n", configFile.c_str());
printf("Verify that the file exists and has \'[authserver]\' written in the top of the file!\n");
return 1;
}
TC_LOG_INFO("server.authserver", "%s (authserver)", _FULLVERSION);
TC_LOG_INFO("server.authserver", "<Ctrl-C> to stop.\n");
- TC_LOG_INFO("server.authserver", "Using configuration file %s.", configFile);
- TC_LOG_INFO("server.authserver", "%s (Library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
+ TC_LOG_INFO("server.authserver", "Using configuration file %s.", configFile.c_str());
+ TC_LOG_INFO("server.worldserver", "Using SSL version: %s (library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
+ TC_LOG_INFO("server.worldserver", "Using Boost version: %i.%i.%i", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
// authserver PID file creation
std::string pidFile = sConfigMgr->GetStringDefault("PidFile", "");
@@ -222,10 +212,26 @@ void KeepDatabaseAliveHandler(const boost::system::error_code& error)
}
}
-/// Print out the usage string for this program on the console.
-void usage(const char* prog)
+variables_map GetConsoleArguments(int argc, char** argv, std::string& configFile)
{
- TC_LOG_INFO("server.authserver", "Usage: \n %s [<options>]\n"
- " -c config_file use config_file as configuration file\n\r",
- prog);
+ options_description all("Allowed options");
+ all.add_options()
+ ("help,h", "print usage message")
+ ("config,c", value<std::string>(&configFile)->default_value(_TRINITY_REALM_CONFIG), "use <arg> as configuration file")
+ ;
+ variables_map variablesMap;
+ try
+ {
+ store(command_line_parser(argc, argv).options(all).allow_unregistered().run(), variablesMap);
+ notify(variablesMap);
+ }
+ catch (std::exception& e) {
+ std::cerr << e.what() << "\n";
+ }
+
+ if (variablesMap.count("help")) {
+ std::cout << all << "\n";
+ }
+
+ return variablesMap;
}
diff --git a/src/server/shared/Configuration/Config.cpp b/src/server/shared/Configuration/Config.cpp
index fe61cde5594..5cd7ef52f82 100644
--- a/src/server/shared/Configuration/Config.cpp
+++ b/src/server/shared/Configuration/Config.cpp
@@ -25,10 +25,8 @@
using namespace boost::property_tree;
-bool ConfigMgr::LoadInitial(char const* file)
+bool ConfigMgr::LoadInitial(std::string const& file)
{
- ASSERT(file);
-
std::lock_guard<std::mutex> lock(_configLock);
_filename = file;
@@ -57,7 +55,7 @@ bool ConfigMgr::Reload()
return LoadInitial(_filename.c_str());
}
-std::string ConfigMgr::GetStringDefault(const char* name, const std::string& def)
+std::string ConfigMgr::GetStringDefault(std::string const& name, const std::string& def)
{
std::string value = _config.get<std::string>(ptree::path_type(name, '/'), def);
@@ -66,7 +64,7 @@ std::string ConfigMgr::GetStringDefault(const char* name, const std::string& def
return value;
}
-bool ConfigMgr::GetBoolDefault(const char* name, bool def)
+bool ConfigMgr::GetBoolDefault(std::string const& name, bool def)
{
try
{
@@ -80,12 +78,12 @@ bool ConfigMgr::GetBoolDefault(const char* name, bool def)
}
}
-int ConfigMgr::GetIntDefault(const char* name, int def)
+int ConfigMgr::GetIntDefault(std::string const& name, int def)
{
return _config.get<int>(ptree::path_type(name, '/'), def);
}
-float ConfigMgr::GetFloatDefault(const char* name, float def)
+float ConfigMgr::GetFloatDefault(std::string const& name, float def)
{
return _config.get<float>(ptree::path_type(name, '/'), def);
}
diff --git a/src/server/shared/Configuration/Config.h b/src/server/shared/Configuration/Config.h
index d05a083d166..68daca5440f 100644
--- a/src/server/shared/Configuration/Config.h
+++ b/src/server/shared/Configuration/Config.h
@@ -31,7 +31,7 @@ class ConfigMgr
public:
/// Method used only for loading main configuration files (authserver.conf and worldserver.conf)
- bool LoadInitial(char const* file);
+ bool LoadInitial(std::string const& file);
static ConfigMgr* instance()
{
@@ -41,10 +41,10 @@ public:
bool Reload();
- std::string GetStringDefault(const char* name, const std::string& def);
- bool GetBoolDefault(const char* name, bool def);
- int GetIntDefault(const char* name, int def);
- float GetFloatDefault(const char* name, float def);
+ std::string GetStringDefault(std::string const& name, const std::string& def);
+ bool GetBoolDefault(std::string const& name, bool def);
+ int GetIntDefault(std::string const& name, int def);
+ float GetFloatDefault(std::string const& name, float def);
std::string const& GetFilename();
std::list<std::string> GetKeysByString(std::string const& name);
diff --git a/src/server/shared/Utilities/ServiceWin32.cpp b/src/server/shared/Utilities/ServiceWin32.cpp
index ec472b33f40..f4a0339d9e6 100644
--- a/src/server/shared/Utilities/ServiceWin32.cpp
+++ b/src/server/shared/Utilities/ServiceWin32.cpp
@@ -60,7 +60,7 @@ bool WinServiceInstall()
if (GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]) ) > 0)
{
SC_HANDLE service;
- std::strcat(path, " --service");
+ std::strcat(path, " --service run");
service = CreateService(serviceControlManager,
serviceName, // name of service
serviceLongName, // service name to display
@@ -119,6 +119,8 @@ bool WinServiceInstall()
}
CloseServiceHandle(serviceControlManager);
}
+
+ printf("Service installed\n");
return true;
}
@@ -143,6 +145,8 @@ bool WinServiceUninstall()
CloseServiceHandle(serviceControlManager);
}
+
+ printf("Service uninstalled\n");
return true;
}
diff --git a/src/server/worldserver/Main.cpp b/src/server/worldserver/Main.cpp
index 967a31579fa..c28adbc7c21 100644
--- a/src/server/worldserver/Main.cpp
+++ b/src/server/worldserver/Main.cpp
@@ -24,6 +24,7 @@
#include <openssl/crypto.h>
#include <boost/asio/io_service.hpp>
#include <boost/asio/deadline_timer.hpp>
+#include <boost/program_options.hpp>
#include "Common.h"
#include "DatabaseEnv.h"
@@ -45,6 +46,8 @@
#include "SystemConfig.h"
#include "WorldSocket.h"
+using namespace boost::program_options;
+
#ifndef _TRINITY_CORE_CONFIG
#define _TRINITY_CORE_CONFIG "worldserver.conf"
#endif
@@ -76,7 +79,6 @@ CharacterDatabaseWorkerPool CharacterDatabase; ///< Accessor to the
LoginDatabaseWorkerPool LoginDatabase; ///< Accessor to the realm/login database
uint32 realmID; ///< Id of the realm
-void usage(const char* prog);
void SignalHandler(const boost::system::error_code& error, int signalNumber);
void FreezeDetectorHandler(const boost::system::error_code& error);
AsyncAcceptor<RASession>* StartRaSocketAcceptor(boost::asio::io_service& ioService);
@@ -84,66 +86,32 @@ bool StartDB();
void StopDB();
void WorldUpdateLoop();
void ClearOnlineAccounts();
+variables_map GetConsoleArguments(int argc, char** argv, std::string& cfg_file, std::string& cfg_service);
/// Launch the Trinity server
extern int main(int argc, char** argv)
{
- ///- Command line parsing to get the configuration file name
- char const* cfg_file = _TRINITY_CORE_CONFIG;
- int c = 1;
- while (c < argc)
- {
- if (!strcmp(argv[c], "-c"))
- {
- if (++c >= argc)
- {
- printf("Runtime-Error: -c option requires an input argument");
- usage(argv[0]);
- return 1;
- }
- else
- cfg_file = argv[c];
- }
+ std::string configFile = _TRINITY_CORE_CONFIG;
+ std::string configService;
-#ifdef _WIN32
- if (strcmp(argv[c], "-s") == 0) // Services
- {
- if (++c >= argc)
- {
- printf("Runtime-Error: -s option requires an input argument");
- usage(argv[0]);
- return 1;
- }
-
- if (strcmp(argv[c], "install") == 0)
- {
- if (WinServiceInstall())
- printf("Installing service\n");
- return 1;
- }
- else if (strcmp(argv[c], "uninstall") == 0)
- {
- if (WinServiceUninstall())
- printf("Uninstalling service\n");
- return 1;
- }
- else
- {
- printf("Runtime-Error: unsupported option %s", argv[c]);
- usage(argv[0]);
- return 1;
- }
- }
+ auto vm = GetConsoleArguments(argc, argv, configFile, configService);
+ // exit if help is enabled
+ if (vm.count("help"))
+ return 0;
- if (strcmp(argv[c], "--service") == 0)
+ if (!configService.empty())
+ {
+ if (configService.compare("install") == 0)
+ return WinServiceInstall() == true ? 0 : 1;
+ else if (configService.compare("uninstall") == 0)
+ return WinServiceUninstall() == true ? 0 : 1;
+ else if (configService.compare("run") == 0)
WinServiceRun();
-#endif
- ++c;
}
- if (!sConfigMgr->LoadInitial(cfg_file))
+ if (!sConfigMgr->LoadInitial(configFile))
{
- printf("Invalid or missing configuration file : %s\n", cfg_file);
+ printf("Invalid or missing configuration file : %s\n", configFile.c_str());
printf("Verify that the file exists and has \'[worldserver]' written in the top of the file!\n");
return 1;
}
@@ -165,7 +133,7 @@ extern int main(int argc, char** argv)
TC_LOG_INFO("server.worldserver", " \\/_/\\/_/ \\/_/\\/_/\\/_/\\/_/\\/__/ `/___/> \\");
TC_LOG_INFO("server.worldserver", " C O R E /\\___/");
TC_LOG_INFO("server.worldserver", "http://TrinityCore.org \\/__/\n");
- TC_LOG_INFO("server.worldserver", "Using configuration file %s.", cfg_file);
+ TC_LOG_INFO("server.worldserver", "Using configuration file %s.", configFile.c_str());
TC_LOG_INFO("server.worldserver", "Using SSL version: %s (library: %s)", OPENSSL_VERSION_TEXT, SSLeay_version(SSLEAY_VERSION));
TC_LOG_INFO("server.worldserver", "Using Boost version: %i.%i.%i", BOOST_VERSION / 100000, BOOST_VERSION / 100 % 1000, BOOST_VERSION % 100);
@@ -403,20 +371,6 @@ void WorldUpdateLoop()
}
}
-/// Print out the usage string for this program on the console.
-void usage(const char* prog)
-{
- printf("Usage:\n");
- printf(" %s [<options>]\n", prog);
- printf(" -c config_file use config_file as configuration file\n");
-#ifdef _WIN32
- printf(" Running as service functions:\n");
- printf(" --service run as service\n");
- printf(" -s install install service\n");
- printf(" -s uninstall uninstall service\n");
-#endif
-}
-
void SignalHandler(const boost::system::error_code& error, int signalNumber)
{
if (!error)
@@ -587,3 +541,35 @@ void ClearOnlineAccounts()
}
/// @}
+
+variables_map GetConsoleArguments(int argc, char** argv, std::string& configFile, std::string& configService)
+{
+ options_description all("Allowed options");
+ all.add_options()
+ ("help,h", "print usage message")
+ ("config,c", value<std::string>(&configFile)->default_value(_TRINITY_CORE_CONFIG), "use <arg> as configuration file")
+ ;
+#ifdef _WIN32
+ options_description win("Windows platform specific options");
+ win.add_options()
+ ("service,s", value<std::string>(&configService)->default_value(""), "Windows service options: [install | uninstall]")
+ ;
+
+ all.add(win);
+#endif
+ variables_map vm;
+ try
+ {
+ store(command_line_parser(argc, argv).options(all).allow_unregistered().run(), vm);
+ notify(vm);
+ }
+ catch (std::exception& e) {
+ std::cerr << e.what() << "\n";
+ }
+
+ if (vm.count("help")) {
+ std::cout << all << "\n";
+ }
+
+ return vm;
+}