Server.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #ifndef SERVER_H
  2. #define SERVER_H
  3. #include "network/ENet.h"
  4. #include "network/Packet.h"
  5. #include "utils/HashMap.h"
  6. #include "utils/StringBuffer.h"
  7. #include "utils/Types.h"
  8. struct Server final {
  9. typedef enet_uint16 Port;
  10. typedef StringBuffer<256> Error;
  11. class Client final {
  12. ENetPeer* peer;
  13. int id;
  14. friend HashMap<int, Client>;
  15. Client(ENetPeer* peer, int id);
  16. Client(const Client&) = delete;
  17. Client(Client&& other);
  18. Client& operator=(const Client&) = delete;
  19. Client& operator=(Client&& other);
  20. public:
  21. ~Client();
  22. int getId() const;
  23. };
  24. private:
  25. ENetHost* server;
  26. Error error;
  27. HashMap<int, Client> clients;
  28. int idCounter;
  29. public:
  30. Server(Port port, int maxClients);
  31. Server(const Server&) = delete;
  32. Server(Server&&) = delete;
  33. ~Server();
  34. Server& operator=(const Server&) = delete;
  35. Server& operator=(Server&&) = delete;
  36. bool hasError() const;
  37. const Error& getError() const;
  38. template<typename T>
  39. void consumeEvents(T& consumer) {
  40. ENetEvent e;
  41. while(!hasError() && 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. onPackage(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. private:
  57. template<typename T>
  58. void onConnect(ENetEvent& e, T& consumer) {
  59. int id = idCounter++;
  60. if(clients.tryEmplace(id, e.peer, id)) {
  61. error.clear().append("id is connected twice");
  62. return;
  63. }
  64. static_assert(sizeof(e.peer->data) >= sizeof(id),
  65. "private data not big enough for id");
  66. memcpy(&(e.peer->data), &id, sizeof(id));
  67. Client* client = clients.search(id);
  68. if(client != nullptr) {
  69. consumer.onConnection(*client);
  70. } else {
  71. error.clear().append("cannot find added client");
  72. }
  73. }
  74. template<typename T>
  75. void onPackage(ENetEvent& e, T& consumer) {
  76. if(e.peer->data == nullptr) {
  77. error.clear().append("client without data sent package");
  78. return;
  79. }
  80. int id = -1;
  81. memcpy(&id, &(e.peer->data), sizeof(id));
  82. Client* client = clients.search(id);
  83. if(client != nullptr) {
  84. InPacket in(e.packet);
  85. consumer.onPackage(*client, in);
  86. } else {
  87. error.clear().append("client with invalid id sent package");
  88. }
  89. }
  90. template<typename T>
  91. void onDisconnect(ENetEvent& e, T& consumer) {
  92. if(e.peer->data == nullptr) {
  93. error.clear().append("client without data disconnected");
  94. return;
  95. }
  96. int id = -1;
  97. memcpy(&id, &(e.peer->data), sizeof(id));
  98. Client* client = clients.search(id);
  99. if(client != nullptr) {
  100. consumer.onDisconnect(*client);
  101. clients.remove(id);
  102. } else {
  103. error.clear().append("client has invalid id");
  104. }
  105. }
  106. };
  107. #endif