Server.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef SERVER_H
  2. #define SERVER_H
  3. #include "network/ENet.h"
  4. #include "network/Packet.h"
  5. #include "utils/Error.h"
  6. #include "utils/HashMap.h"
  7. #include "utils/Logger.h"
  8. #include "utils/StringBuffer.h"
  9. struct Server final {
  10. typedef enet_uint16 Port;
  11. class Client final {
  12. friend class Server;
  13. ENetPeer* peer;
  14. int id;
  15. friend HashMap<int, Client>;
  16. Client(ENetPeer* peer, int id);
  17. Client(const Client&) = delete;
  18. Client(Client&& other);
  19. Client& operator=(const Client&) = delete;
  20. Client& operator=(Client&& other);
  21. public:
  22. ~Client();
  23. int getId() const;
  24. void send(OutPacket& p);
  25. };
  26. private:
  27. ENetHost* server;
  28. HashMap<int, Client> clients;
  29. int idCounter;
  30. public:
  31. Server();
  32. Server(const Server&) = delete;
  33. Server(Server&&) = delete;
  34. ~Server();
  35. Server& operator=(const Server&) = delete;
  36. Server& operator=(Server&&) = delete;
  37. Error start(Port port, int maxClients);
  38. template<typename T>
  39. void consumeEvents(T& consumer) {
  40. ENetEvent e;
  41. while(enet_host_service(server, &e, 0) > 0) {
  42. switch(e.type) {
  43. case ENET_EVENT_TYPE_CONNECT: onConnect(e, consumer); break;
  44. case ENET_EVENT_TYPE_RECEIVE:
  45. onPacket(e, consumer);
  46. enet_packet_destroy(e.packet);
  47. break;
  48. case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
  49. case ENET_EVENT_TYPE_DISCONNECT:
  50. onDisconnect(e, consumer);
  51. break;
  52. case ENET_EVENT_TYPE_NONE: return;
  53. }
  54. }
  55. }
  56. void send(OutPacket& p);
  57. void disconnect(Client& client);
  58. private:
  59. template<typename T>
  60. void onConnect(ENetEvent& e, T& consumer) {
  61. int id = idCounter++;
  62. if(clients.tryEmplace(id, e.peer, id)) {
  63. LOG_WARNING("id is connected twice");
  64. return;
  65. }
  66. static_assert(sizeof(e.peer->data) >= sizeof(id),
  67. "private data not big enough for id");
  68. memcpy(&(e.peer->data), &id, sizeof(id));
  69. Client* client = clients.search(id);
  70. if(client != nullptr) {
  71. consumer.onConnect(*client);
  72. } else {
  73. LOG_WARNING("cannot find added client");
  74. }
  75. }
  76. template<typename T>
  77. void onPacket(ENetEvent& e, T& consumer) {
  78. if(e.peer->data == nullptr) {
  79. LOG_WARNING("client without data sent package");
  80. return;
  81. }
  82. int id = -1;
  83. memcpy(&id, &(e.peer->data), sizeof(id));
  84. Client* client = clients.search(id);
  85. if(client != nullptr) {
  86. InPacket in(e.packet);
  87. consumer.onPacket(*client, in);
  88. } else {
  89. LOG_WARNING("client with invalid id sent package");
  90. }
  91. }
  92. template<typename T>
  93. void onDisconnect(ENetEvent& e, T& consumer) {
  94. if(e.peer->data == nullptr) {
  95. LOG_WARNING("client without data disconnected");
  96. return;
  97. }
  98. int id = -1;
  99. memcpy(&id, &(e.peer->data), sizeof(id));
  100. Client* client = clients.search(id);
  101. if(client != nullptr) {
  102. consumer.onDisconnect(*client);
  103. clients.remove(id);
  104. } else {
  105. LOG_WARNING("client has invalid id");
  106. }
  107. }
  108. };
  109. #endif