Start.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include <csignal>
  2. #include <cstdlib>
  3. #include <Datei.h>
  4. #include <fstream>
  5. #include <Globals.h>
  6. #include <iostream>
  7. #include <Klient.h>
  8. #include <sys/resource.h>
  9. #include <Text.h>
  10. #include <Zeit.h>
  11. #include <AsynchronCall.h>
  12. #include "Server.h"
  13. FactoryCraftServer* mserver = 0;
  14. void exit()
  15. {
  16. if (mserver)
  17. {
  18. std::cout << "The server terminated unexpectedly. Trying to save game progress.\n";
  19. mserver->close();
  20. }
  21. }
  22. int main()
  23. {
  24. initializeBlockTypes();
  25. initializeItemTypes();
  26. initializeEntityTypes();
  27. initializeDimensions();
  28. initializeMultiblockTypes();
  29. struct rlimit core_limits;
  30. core_limits.rlim_cur = core_limits.rlim_max = RLIM_INFINITY;
  31. setrlimit(RLIMIT_CORE, &core_limits);
  32. Framework::initFramework();
  33. Zeit* z = getZeit();
  34. Text* pfad = new Text("log/");
  35. pfad->append(z->getZeit("y-m-d_h-i-s.log"));
  36. z->release();
  37. DateiPfadErstellen(pfad->getText());
  38. std::ofstream file;
  39. file.open(pfad->getText());
  40. std::streambuf* sbuf = std::cout.rdbuf();
  41. // std::cout.rdbuf( file.rdbuf() );
  42. pfad->release();
  43. std::cout << "Starting...\n";
  44. std::cout << "Loading config file fcInit.ini ...\n";
  45. InitDatei* dat = new InitDatei("fcInit.ini");
  46. if (!dat->laden())
  47. {
  48. std::cout << "error: Datei konnte nicht gelesen werden. Das Programm "
  49. "wird geschlossen.\n";
  50. dat->release();
  51. file.close();
  52. std::cout.rdbuf(sbuf);
  53. exit(1);
  54. }
  55. const char* wichtig[]
  56. = {"SSLPort", "SSLCert", "SSLKey", "SSLPasswort", "Port"};
  57. for (const char* w : wichtig)
  58. {
  59. if (!dat->wertExistiert(w))
  60. {
  61. std::cout
  62. << "error: The value '" << w
  63. << "' was not specified. The Server can not start.\n";
  64. dat->release();
  65. file.close();
  66. std::cout.rdbuf(sbuf);
  67. exit(1);
  68. }
  69. }
  70. mserver = new FactoryCraftServer(dat);
  71. std::atexit(exit);
  72. signal(SIGTERM, exit);
  73. signal(SIGSEGV, exit);
  74. signal(SIGILL, exit);
  75. signal(SIGABRT, exit);
  76. signal(SIGFPE, exit);
  77. signal(SIGINT, exit);
  78. new Framework::AsynchronCall("Commander", []() {
  79. while (mserver)
  80. {
  81. std::string line;
  82. std::getline(std::cin, line);
  83. if (Text(line.c_str()) == Text("exit"))
  84. {
  85. std::cout << "The server will be terminated and the game progress will be saved.\n";
  86. mserver->close();
  87. return;
  88. }
  89. }
  90. });
  91. std::cout << "The Server is now running.\n";
  92. mserver->run();
  93. mserver->release();
  94. mserver = 0;
  95. if (Game::INSTANCE)
  96. {
  97. Game::INSTANCE->warteAufThread(100000000);
  98. }
  99. dat->release();
  100. std::cout << "The server was shut down successfully.\n";
  101. file.close();
  102. std::cout.rdbuf(sbuf);
  103. Framework::releaseFramework();
  104. return 0;
  105. }