Server.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. 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. Error error;
  29. HashMap<int, Client> clients;
  30. int idCounter;
  31. public:
  32. Server(Port port, int maxClients);
  33. Server(const Server&) = delete;
  34. Server(Server&&) = delete;
  35. ~Server();
  36. Server& operator=(const Server&) = delete;
  37. Server& operator=(Server&&) = delete;
  38. bool hasError() const;
  39. const Error& getError() const;
  40. template<typename T>
  41. void consumeEvents(T& consumer) {
  42. ENetEvent e;
  43. while(!hasError() && enet_host_service(server, &e, 0) > 0) {
  44. switch(e.type) {
  45. case ENET_EVENT_TYPE_CONNECT: onConnect(e, consumer); break;
  46. case ENET_EVENT_TYPE_RECEIVE:
  47. onPacket(e, consumer);
  48. enet_packet_destroy(e.packet);
  49. break;
  50. case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
  51. case ENET_EVENT_TYPE_DISCONNECT:
  52. onDisconnect(e, consumer);
  53. break;
  54. case ENET_EVENT_TYPE_NONE: return;
  55. }
  56. }
  57. }
  58. void send(OutPacket& p);
  59. void disconnect(Client& client);
  60. private:
  61. template<typename T>
  62. void onConnect(ENetEvent& e, T& consumer) {
  63. int id = idCounter++;
  64. if(clients.tryEmplace(id, e.peer, id)) {
  65. error.clear().append("id is connected twice");
  66. return;
  67. }
  68. static_assert(sizeof(e.peer->data) >= sizeof(id),
  69. "private data not big enough for id");
  70. memcpy(&(e.peer->data), &id, sizeof(id));
  71. Client* client = clients.search(id);
  72. if(client != nullptr) {
  73. consumer.onConnect(*client);
  74. } else {
  75. error.clear().append("cannot find added client");
  76. }
  77. }
  78. template<typename T>
  79. void onPacket(ENetEvent& e, T& consumer) {
  80. if(e.peer->data == nullptr) {
  81. error.clear().append("client without data sent package");
  82. return;
  83. }
  84. int id = -1;
  85. memcpy(&id, &(e.peer->data), sizeof(id));
  86. Client* client = clients.search(id);
  87. if(client != nullptr) {
  88. InPacket in(e.packet);
  89. consumer.onPacket(*client, in);
  90. } else {
  91. error.clear().append("client with invalid id sent package");
  92. }
  93. }
  94. template<typename T>
  95. void onDisconnect(ENetEvent& e, T& consumer) {
  96. if(e.peer->data == nullptr) {
  97. error.clear().append("client without data disconnected");
  98. return;
  99. }
  100. int id = -1;
  101. memcpy(&id, &(e.peer->data), sizeof(id));
  102. Client* client = clients.search(id);
  103. if(client != nullptr) {
  104. consumer.onDisconnect(*client);
  105. clients.remove(id);
  106. } else {
  107. error.clear().append("client has invalid id");
  108. }
  109. }
  110. };
  111. #endif