GameServer.cpp 2.9 KB

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