GameServer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #include <iostream>
  2. #include <cstring>
  3. #include <chrono>
  4. #include <mutex>
  5. #include <thread>
  6. #include <vector>
  7. #include <poll.h>
  8. #include <readline/readline.h>
  9. #include <readline/history.h>
  10. #include <unistd.h>
  11. #include "server/GameServer.h"
  12. #include "server/network/Server.h"
  13. #include "server/commands/ServerCommands.h"
  14. #include "server/commands/CommandManager.h"
  15. static ServerCommands serverCommands;
  16. static const std::chrono::nanoseconds NANOS_PER_TICK = std::chrono::nanoseconds(50000000);
  17. static const std::chrono::nanoseconds MIN_NANO_SLEEP = std::chrono::nanoseconds(300000);
  18. static std::vector<std::string> commandQueue;
  19. static std::mutex comandQueueMutex;
  20. static bool once = false;
  21. static void init()
  22. {
  23. rl_bind_key('\t', rl_insert);
  24. rl_event_hook = []()
  25. {
  26. if(!serverCommands.isRunning())
  27. {
  28. rl_stuff_char('\n');
  29. }
  30. return 0;
  31. };
  32. }
  33. static void readFromConsole()
  34. {
  35. while(serverCommands.isRunning())
  36. {
  37. char* test = readline("> ");
  38. if(test == nullptr)
  39. {
  40. continue;
  41. }
  42. std::string command(test);
  43. if(command.size() != 0)
  44. {
  45. add_history(test);
  46. std::lock_guard<std::mutex> lg(comandQueueMutex);
  47. commandQueue.push_back(command);
  48. }
  49. free(test);
  50. }
  51. }
  52. static void handleCommandQueue()
  53. {
  54. std::lock_guard<std::mutex> lg(comandQueueMutex);
  55. for(std::string& s : commandQueue)
  56. {
  57. CommandManager::execute(serverCommands, s);
  58. }
  59. commandQueue.clear();
  60. }
  61. static void tick()
  62. {
  63. rl_clear_visible_line();
  64. handleCommandQueue();
  65. std::cout << "tick\n";
  66. rl_forced_update_display();
  67. }
  68. static void onFullServerClientConnect(int socket)
  69. {
  70. Stream answer;
  71. answer.write("Sorry, the server is full");
  72. answer.sendToSocket(socket);
  73. }
  74. static void onClientConnect(int socket)
  75. {
  76. std::cout << socket << " has connected\n";
  77. Stream answer;
  78. answer.write("Welcome to the server.");
  79. answer.sendToSocket(socket);
  80. }
  81. static void onClientPackage(int socket, Stream& in)
  82. {
  83. std::string s = "";
  84. while(in.hasData())
  85. {
  86. char c;
  87. in.read(&c, 1);
  88. s = c + s;
  89. }
  90. Stream answer;
  91. answer.write(s.data(), s.length());
  92. answer.sendToSocket(socket);
  93. }
  94. static void onClientDisconnect(int socket)
  95. {
  96. std::cout << socket << " has disconnected\n";
  97. Stream answer;
  98. answer.write("Bye.");
  99. answer.sendToSocket(socket);
  100. }
  101. static void loop()
  102. {
  103. auto lastTime = std::chrono::steady_clock::now().time_since_epoch();
  104. auto lag = lastTime.zero();
  105. std::thread commandThread = std::thread(readFromConsole);
  106. while(serverCommands.isRunning())
  107. {
  108. auto time = std::chrono::steady_clock::now().time_since_epoch();
  109. lag += time - lastTime;
  110. lastTime = time;
  111. while(lag >= NANOS_PER_TICK)
  112. {
  113. lag -= NANOS_PER_TICK;
  114. tick();
  115. }
  116. auto waitTime = NANOS_PER_TICK - lag - MIN_NANO_SLEEP;
  117. if(waitTime > MIN_NANO_SLEEP)
  118. {
  119. std::this_thread::sleep_for(waitTime);
  120. }
  121. }
  122. commandThread.join();
  123. }
  124. void GameServer::start(u16 port, u16 maxClients)
  125. {
  126. if(once)
  127. {
  128. return;
  129. }
  130. once = true;
  131. init();
  132. Server::setFullServerClientConnectFunction(onFullServerClientConnect);
  133. Server::setClientConnectFunction(onClientConnect);
  134. Server::setClientPackageFunction(onClientPackage);
  135. Server::setClientDisconnectFunction(onClientDisconnect);
  136. if(!Server::start(port, maxClients))
  137. {
  138. return;
  139. }
  140. loop();
  141. }