Server.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "network/Server.h"
  2. #include "data/HashMap.h"
  3. #include "network/ENet.h"
  4. #include "utils/Logger.h"
  5. static_assert(sizeof(enet_uint16) == sizeof(Server::Port),
  6. "client port has wrong type");
  7. static ENetHost* server;
  8. static HashMap<Server::Client, ENetPeer*> clients;
  9. static Server::Client idCounter = 1;
  10. static Server::OnConnect onConnect = [](Server::Client) {};
  11. static Server::OnDisconnect onDisconnect = [](Server::Client) {};
  12. static Server::OnPacket onPacket = [](Server::Client, InPacket&) {};
  13. Error Server::start(Port port, int maxClients) {
  14. if(maxClients <= 0) {
  15. return {"invalid max client amount"};
  16. } else if(server != nullptr) {
  17. return {"already started"};
  18. } else if(ENet::add()) {
  19. return {"cannot initialize enet"};
  20. }
  21. ENetAddress address;
  22. memset(&address, 0, sizeof(ENetAddress));
  23. address.host = ENET_HOST_ANY;
  24. address.port = port;
  25. server = enet_host_create(&address, static_cast<unsigned int>(maxClients),
  26. 3, 0, 0);
  27. if(server == nullptr) {
  28. ENet::remove();
  29. return {"cannot create enet server host"};
  30. }
  31. return {};
  32. }
  33. void Server::stop() {
  34. if(server == nullptr) {
  35. return;
  36. }
  37. for(ENetPeer* peer : clients.values()) {
  38. enet_peer_reset(peer);
  39. }
  40. enet_host_destroy(server);
  41. server = nullptr;
  42. ENet::remove();
  43. }
  44. static void writeId(ENetPeer* peer, Server::Client id) {
  45. static_assert(sizeof(peer->data) >= sizeof(id),
  46. "private data not big enough for id");
  47. memcpy(&(peer->data), &id, sizeof(id));
  48. }
  49. static Server::Client getId(ENetPeer* peer) {
  50. Server::Client id = -1;
  51. memcpy(&id, &(peer->data), sizeof(id));
  52. return id;
  53. }
  54. static void handleConnect(ENetEvent& e) {
  55. Server::Client id = idCounter++;
  56. if(clients.tryEmplace(id, e.peer)) {
  57. LOG_WARNING("id is connected twice");
  58. return;
  59. }
  60. writeId(e.peer, id);
  61. onConnect(id);
  62. }
  63. static void handlePacket(ENetEvent& e) {
  64. if(e.peer->data == nullptr) {
  65. LOG_WARNING("client without data sent package");
  66. return;
  67. }
  68. Server::Client id = getId(e.peer);
  69. InPacket in(e.packet->data, static_cast<int>(e.packet->dataLength));
  70. onPacket(id, in);
  71. }
  72. static void handleDisconnect(ENetEvent& e) {
  73. if(e.peer->data == nullptr) {
  74. LOG_WARNING("client without data disconnected");
  75. return;
  76. }
  77. Server::Client id = getId(e.peer);
  78. onDisconnect(id);
  79. if(clients.remove(id)) {
  80. LOG_WARNING("removed non existing client");
  81. }
  82. }
  83. void Server::tick() {
  84. if(server == nullptr) {
  85. return;
  86. }
  87. ENetEvent e;
  88. while(enet_host_service(server, &e, 0) > 0) {
  89. switch(e.type) {
  90. case ENET_EVENT_TYPE_CONNECT: handleConnect(e); break;
  91. case ENET_EVENT_TYPE_RECEIVE:
  92. handlePacket(e);
  93. enet_packet_destroy(e.packet);
  94. break;
  95. case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
  96. case ENET_EVENT_TYPE_DISCONNECT: handleDisconnect(e); break;
  97. case ENET_EVENT_TYPE_NONE: return;
  98. }
  99. }
  100. }
  101. static ENetPacket* fromBuffer(const Buffer& buffer, int index) {
  102. constexpr enet_uint32 flags[] = {ENET_PACKET_FLAG_RELIABLE, 0,
  103. ENET_PACKET_FLAG_UNSEQUENCED};
  104. return enet_packet_create(
  105. buffer, static_cast<unsigned int>(buffer.getLength()), flags[index]);
  106. }
  107. void Server::send(const OutPacket& p, PacketSendMode mode) {
  108. if(server != nullptr) {
  109. int index = static_cast<int>(mode);
  110. enet_host_broadcast(server, static_cast<enet_uint8>(index),
  111. fromBuffer(p.buffer, index));
  112. }
  113. }
  114. void Server::send(Server::Client client, const OutPacket& p,
  115. PacketSendMode mode) {
  116. if(server == nullptr) {
  117. return;
  118. }
  119. ENetPeer** peer = clients.search(client);
  120. if(peer != nullptr) {
  121. int index = static_cast<int>(mode);
  122. enet_peer_send(*peer, static_cast<enet_uint8>(index),
  123. fromBuffer(p.buffer, index));
  124. }
  125. }
  126. void Server::disconnect(Client client) {
  127. ENetPeer** peer = clients.search(client);
  128. if(peer != nullptr) {
  129. enet_peer_disconnect(*peer, 0);
  130. }
  131. }
  132. void Server::setConnectHandler(OnConnect oc) {
  133. onConnect = oc;
  134. }
  135. void Server::setDisconnectHandler(OnDisconnect od) {
  136. onDisconnect = od;
  137. }
  138. void Server::setPacketHandler(OnPacket op) {
  139. onPacket = op;
  140. }
  141. void Server::resetHandler() {
  142. onConnect = [](Server::Client) {};
  143. onDisconnect = [](Server::Client) {};
  144. onPacket = [](Server::Client, InPacket&) {};
  145. }