GameServer.cpp 3.5 KB

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