PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Common/Server.cpp

http://github.com/swganh/mmoserver
C++ | 148 lines | 86 code | 18 blank | 44 comment | 5 complexity | 5ed9f6f98b1d8b874c8d3f98810551c2 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. ---------------------------------------------------------------------------------------
  3. This source file is part of SWG:ANH (Star Wars Galaxies - A New Hope - Server Emulator)
  4. For more information, visit http://www.swganh.com
  5. Copyright (c) 2006 - 2010 The SWG:ANH Team
  6. ---------------------------------------------------------------------------------------
  7. Use of this source code is governed by the GPL v3 license that can be found
  8. in the COPYING file or at http://www.gnu.org/licenses/gpl-3.0.html
  9. This library is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU Lesser General Public
  11. License as published by the Free Software Foundation; either
  12. version 2.1 of the License, or (at your option) any later version.
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. Lesser General Public License for more details.
  17. You should have received a copy of the GNU Lesser General Public
  18. License along with this library; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. ---------------------------------------------------------------------------------------
  21. */
  22. #include "Common/Server.h"
  23. #include <iostream>
  24. #include <fstream>
  25. /*! \brief Common is a catch-all library containing primarily base classes and
  26. * classes used for maintaining application lifetimes.
  27. */
  28. namespace common {
  29. BaseServer::BaseServer()
  30. : configuration_options_description_("Configuration Options")
  31. {
  32. configuration_options_description_.add_options()
  33. ("help", "Displays this help dialog.")
  34. ("BindAddress", boost::program_options::value<std::string>()->default_value("127.0.0.1"), "Network listen address.")
  35. ("BindPort", boost::program_options::value<uint16_t>(), "Port the server listens for messages on.")
  36. ("ServiceMessageHeap", boost::program_options::value<uint32_t>()->default_value(8192), "")
  37. ("GlobalMessageHeap", boost::program_options::value<uint32_t>()->default_value(8192), "")
  38. ("DBServer", boost::program_options::value<std::string>()->default_value("localhost"), "Address of the MySQL Server.")
  39. ("DBPort", boost::program_options::value<uint16_t>()->default_value(3306), "Port of the MySQL Server.")
  40. ("DBName", boost::program_options::value<std::string>()->default_value("swganh"), "Name of the MySQL database schema.")
  41. ("DBUser", boost::program_options::value<std::string>()->default_value("root"), "Username of the database user account.")
  42. ("DBPass", boost::program_options::value<std::string>()->default_value(""), "Password of the database user account.")
  43. ("DBMinThreads", boost::program_options::value<uint32_t>()->default_value(4), "Minimum number of threads used for database work.")
  44. ("DBMaxThreads", boost::program_options::value<uint32_t>()->default_value(16), "Maximum number of threads used for database work.")
  45. ("ReliablePacketSizeServerToServer", boost::program_options::value<uint16_t>()->default_value(1400), "size of Packets for server server communication")
  46. ("UnreliablePacketSizeServerToServer", boost::program_options::value<uint16_t>()->default_value(1400), "size of Packets for server server communication")
  47. ("ReliablePacketSizeServerToClient", boost::program_options::value<uint16_t>()->default_value(496), "size of Packets for server client communication")
  48. ("UnreliablePacketSizeServerToClient", boost::program_options::value<uint16_t>()->default_value(496), "size of Packets for server client communication")
  49. ("ServerPacketWindowSize", boost::program_options::value<uint32_t>()->default_value(800), "")
  50. ("ClientPacketWindowSize", boost::program_options::value<uint32_t>()->default_value(80), "")
  51. ("UdpBufferSize", boost::program_options::value<uint32_t>()->default_value(4096), "Kernel UDP Buffer")
  52. ("DBGlobalSchema", boost::program_options::value<std::string>()->default_value("swganh_static"), "")
  53. ("DBGalaxySchema", boost::program_options::value<std::string>()->default_value("swganh"), "")
  54. ("DBConfigSchema", boost::program_options::value<std::string>()->default_value("swganh_config"), "")
  55. ;
  56. }
  57. BaseServer::~BaseServer()
  58. {
  59. }
  60. bool BaseServer::Startup()
  61. {
  62. return true;
  63. }
  64. void BaseServer::Shutdown()
  65. {
  66. }
  67. void BaseServer::LoadOptions_(uint32_t argc, char* argv[])
  68. {
  69. boost::program_options::store(boost::program_options::parse_command_line(argc, argv, configuration_options_description_), configuration_variables_map_);
  70. boost::program_options::notify(configuration_variables_map_);
  71. // The help argument has been flagged, display the
  72. // server options and throw a runtime_error exception
  73. // to stop server startup.
  74. if(configuration_variables_map_.count("help"))
  75. {
  76. std::cout << configuration_options_description_ << std::endl;
  77. throw std::runtime_error("Help option flagged.");
  78. }
  79. }
  80. void BaseServer::LoadOptions_(std::list<std::string> config_files)
  81. {
  82. // Iterate through the configuration files
  83. // that are to be loaded. If a configuration file
  84. // is missing, throw a runtime_error.
  85. std::for_each(config_files.begin(), config_files.end(), [=] (const std::string& filename) {
  86. std::ifstream config_file(filename);
  87. if(!config_file)
  88. throw std::runtime_error("Could not open configuration file.");
  89. else
  90. boost::program_options::store(boost::program_options::parse_config_file(config_file, configuration_options_description_, true), configuration_variables_map_);
  91. });
  92. boost::program_options::notify(configuration_variables_map_);
  93. // The help argument has been flagged, display the
  94. // server options and throw a runtime_error exception
  95. // to stop server startup.
  96. if(configuration_variables_map_.count("help"))
  97. {
  98. std::cout << configuration_options_description_ << std::endl;
  99. throw std::runtime_error("Help option flagged.");
  100. }
  101. }
  102. void BaseServer::LoadOptions_(uint32_t argc, char* argv[], std::list<std::string> config_files)
  103. {
  104. boost::program_options::store(boost::program_options::parse_command_line(argc, argv, configuration_options_description_), configuration_variables_map_);
  105. // Iterate through the configuration files
  106. // that are to be loaded. If a configuration file
  107. // is missing, throw a runtime_error.
  108. std::for_each(config_files.begin(), config_files.end(), [=] (const std::string& filename) {
  109. std::ifstream config_file(filename);
  110. if(!config_file)
  111. throw std::runtime_error("Could not open configuration file.");
  112. else
  113. boost::program_options::store(boost::program_options::parse_config_file(config_file, configuration_options_description_, true), configuration_variables_map_);
  114. });
  115. boost::program_options::notify(configuration_variables_map_);
  116. // The help argument has been flagged, display the
  117. // server options and throw a runtime_error exception
  118. // to stop server startup.
  119. if(configuration_variables_map_.count("help"))
  120. {
  121. std::cout << configuration_options_description_ << std::endl;
  122. throw std::runtime_error("Help option flagged.");
  123. }
  124. }
  125. }