Client.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "network/Client.h"
  2. #include "network/ENet.h"
  3. static_assert(sizeof(enet_uint16) == sizeof(Client::Port),
  4. "client port has wrong type");
  5. static ENetHost* client = nullptr;
  6. static ENetPeer* connection = nullptr;
  7. static int connectTicks = 0;
  8. static int connectTimeoutTicks = 0;
  9. static int disconnectTicks = 0;
  10. static int disconnectTimeoutTicks = 0;
  11. static Client::OnConnect onConnect = []() {};
  12. static Client::OnDisconnect onDisconnect = []() {};
  13. static Client::OnPacket onPacket = [](InPacket&) {};
  14. Error Client::start() {
  15. if(client != nullptr) {
  16. return {"already started"};
  17. } else if(ENet::add()) {
  18. return {"cannot initialize enet"};
  19. }
  20. client = enet_host_create(nullptr, 1, 2, 0, 0);
  21. if(client == nullptr) {
  22. ENet::remove();
  23. return {"cannot create enet client host"};
  24. }
  25. return {};
  26. }
  27. void Client::stop() {
  28. if(connection != nullptr) {
  29. onDisconnect();
  30. enet_peer_disconnect_now(connection, 0);
  31. connection = nullptr;
  32. }
  33. if(client != nullptr) {
  34. enet_host_destroy(client);
  35. ENet::remove();
  36. client = nullptr;
  37. }
  38. connectTicks = 0;
  39. disconnectTicks = 0;
  40. }
  41. Error Client::connect(const char* server, Port port, int timeoutTicks) {
  42. if(client == nullptr) {
  43. return {"client not started"};
  44. } else if(connection != nullptr) {
  45. return {"connection already exists"};
  46. }
  47. ENetAddress address;
  48. memset(&address, 0, sizeof(ENetAddress));
  49. enet_address_set_host(&address, server);
  50. address.port = port;
  51. connection = enet_host_connect(client, &address, 3, 0);
  52. if(connection == nullptr) {
  53. return {"cannot create connection"};
  54. }
  55. connectTicks = 1;
  56. connectTimeoutTicks = timeoutTicks;
  57. return {};
  58. }
  59. void Client::disconnect(int timeoutTicks) {
  60. if(connection == nullptr) {
  61. return;
  62. }
  63. connectTicks = 0;
  64. enet_peer_disconnect(connection, 0);
  65. disconnectTicks = 1;
  66. disconnectTimeoutTicks = timeoutTicks;
  67. }
  68. void Client::send(OutPacket& p, PacketSendMode mode) {
  69. if(client != nullptr && connection != nullptr && connectTicks < 0) {
  70. constexpr enet_uint32 flags[] = {ENET_PACKET_FLAG_RELIABLE, 0,
  71. ENET_PACKET_FLAG_UNSEQUENCED};
  72. enet_uint8 index = static_cast<enet_uint8>(mode);
  73. enet_peer_send(connection, index,
  74. enet_packet_create(
  75. p.buffer, static_cast<size_t>(p.buffer.getLength()),
  76. flags[index]));
  77. }
  78. }
  79. void Client::tick() {
  80. if(client == nullptr) {
  81. return;
  82. }
  83. ENetEvent e;
  84. while(enet_host_service(client, &e, 0) > 0) {
  85. switch(e.type) {
  86. case ENET_EVENT_TYPE_CONNECT:
  87. connectTicks = -1;
  88. onConnect();
  89. break;
  90. case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
  91. case ENET_EVENT_TYPE_DISCONNECT:
  92. disconnectTicks = 0;
  93. onDisconnect();
  94. connection = nullptr;
  95. break;
  96. case ENET_EVENT_TYPE_NONE: return;
  97. case ENET_EVENT_TYPE_RECEIVE:
  98. InPacket in(e.packet->data,
  99. static_cast<int>(e.packet->dataLength));
  100. onPacket(in);
  101. enet_packet_destroy(e.packet);
  102. break;
  103. }
  104. }
  105. if(connectTicks >= 1 && ++connectTicks > connectTimeoutTicks) {
  106. connectTicks = 0;
  107. disconnect(connectTimeoutTicks);
  108. }
  109. if(disconnectTicks >= 1 && ++disconnectTicks > disconnectTimeoutTicks) {
  110. disconnectTicks = 0;
  111. onDisconnect();
  112. if(connection != nullptr) {
  113. enet_peer_reset(connection);
  114. connection = nullptr;
  115. }
  116. }
  117. }
  118. void Client::setConnectHandler(OnConnect oc) {
  119. onConnect = oc;
  120. }
  121. void Client::setDisconnectHandler(OnDisconnect od) {
  122. onDisconnect = od;
  123. }
  124. void Client::setPacketHandler(OnPacket op) {
  125. onPacket = op;
  126. }
  127. void Client::resetHandler() {
  128. onConnect = []() {};
  129. onDisconnect = []() {};
  130. onPacket = [](InPacket&) {};
  131. }
  132. bool Client::isConnecting() {
  133. return connectTicks >= 1;
  134. }
  135. bool Client::isConnected() {
  136. return connectTicks < 0;
  137. }