EnetService.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. //
  2. // EnetService.cpp
  3. // vcmi
  4. //
  5. // Created by nordsoft on 17.01.2023.
  6. //
  7. #include "EnetService.h"
  8. #include "StdInc.h"
  9. #include "CThreadHelper.h"
  10. #include <thread>
  11. EnetConnection::EnetConnection(ENetPeer * _peer):
  12. peer(_peer)
  13. {
  14. connected = false;
  15. }
  16. EnetConnection::EnetConnection(ENetHost * client, const std::string & host, ui16 port)
  17. {
  18. connected = false;
  19. ENetAddress address;
  20. enet_address_set_host(&address, host.c_str());
  21. address.port = port;
  22. peer = enet_host_connect(client, &address, 2, 0);
  23. if(!peer)
  24. {
  25. throw std::runtime_error("Can't establish connection :(");
  26. }
  27. }
  28. EnetConnection::~EnetConnection()
  29. {
  30. close();
  31. kill();
  32. }
  33. bool EnetConnection::isOpen() const
  34. {
  35. return connected;
  36. }
  37. void EnetConnection::open()
  38. {
  39. connected = true;
  40. }
  41. void EnetConnection::close()
  42. {
  43. std::lock_guard<std::mutex> guard(mutexWrite);
  44. connected = false;
  45. enet_peer_disconnect(peer, 0);
  46. }
  47. void EnetConnection::kill()
  48. {
  49. connected = false;
  50. if(peer)
  51. enet_peer_reset(peer);
  52. peer = nullptr;
  53. }
  54. const ENetPeer * EnetConnection::getPeer() const
  55. {
  56. return peer;
  57. }
  58. void EnetConnection::dispatch(ENetPacket * packet)
  59. {
  60. std::lock_guard<std::mutex> guard(mutexRead);
  61. packets.push_back(packet);
  62. }
  63. void EnetConnection::write(const void * data, unsigned size)
  64. {
  65. if(size == 0 || !isOpen())
  66. return;
  67. std::lock_guard<std::mutex> guard(mutexWrite);
  68. ENetPacket * packet = enet_packet_create(data, size, ENET_PACKET_FLAG_RELIABLE);
  69. enet_peer_send(peer, channel, packet);
  70. }
  71. void EnetConnection::read(void * data, unsigned size)
  72. {
  73. if(!size)
  74. return;
  75. while(packets.empty())
  76. {
  77. if(!isOpen())
  78. return;
  79. std::this_thread::sleep_for(std::chrono::milliseconds(READ_REFRESH));
  80. }
  81. std::lock_guard<std::mutex> guard(mutexRead);
  82. auto * packet = packets.front();
  83. packets.pop_front();
  84. if(packet->dataLength > 0)
  85. memcpy(data, packet->data, packet->dataLength);
  86. assert(size == packet->dataLength);
  87. enet_packet_destroy(packet);
  88. }
  89. EnetService::EnetService():
  90. service(nullptr), flagValid(false)
  91. {
  92. if(enet_initialize() != 0)
  93. throw std::runtime_error("Cannot initialize enet");
  94. doMonitoring = true;
  95. threadMonitoring = std::make_unique<std::thread>(&EnetService::monitor, this);
  96. threadMonitoring->detach();
  97. }
  98. EnetService::~EnetService()
  99. {
  100. stop();
  101. doMonitoring = false;
  102. if(threadMonitoring)
  103. threadMonitoring->join();
  104. enet_deinitialize();
  105. }
  106. void EnetService::init(short port, const std::string & host)
  107. {
  108. init(0);
  109. active.insert(std::make_shared<EnetConnection>(service, host, port));
  110. }
  111. void EnetService::init(short port)
  112. {
  113. ENetAddress address;
  114. address.host = ENET_HOST_ANY;
  115. address.port = port;
  116. service = enet_host_create(port ? &address : nullptr, port ? CONNECTIONS : 1, CHANNELS, 0, 0);
  117. if(service)
  118. flagValid = true;
  119. start();
  120. }
  121. void EnetService::start()
  122. {
  123. stop();
  124. doPolling = true;
  125. if(service)
  126. {
  127. threadPolling = std::make_unique<std::thread>(&EnetService::poll, this);
  128. threadPolling->detach();
  129. }
  130. }
  131. void EnetService::monitor()
  132. {
  133. setThreadName("EnetService::monitor");
  134. while(doMonitoring)
  135. {
  136. while(!disconnecting.empty())
  137. {
  138. std::lock_guard<std::mutex> guard(mutex);
  139. if(disconnecting.front()->isOpen())
  140. disconnecting.front()->kill();
  141. handleDisconnection(disconnecting.front());
  142. disconnecting.pop_front();
  143. }
  144. while(!connecting.empty())
  145. {
  146. std::lock_guard<std::mutex> guard(mutex);
  147. connecting.front()->open();
  148. handleConnection(connecting.front());
  149. connecting.pop_front();
  150. }
  151. if(service)
  152. enet_host_flush(service);
  153. std::this_thread::sleep_for(std::chrono::milliseconds(MONITOR_INTERVAL));
  154. }
  155. }
  156. void EnetService::stop()
  157. {
  158. doPolling = false;
  159. if(threadPolling)
  160. threadPolling->join();
  161. threadPolling.reset();
  162. }
  163. bool EnetService::valid() const
  164. {
  165. return flagValid;
  166. }
  167. void EnetService::poll()
  168. {
  169. setThreadName("EnetService::poll");
  170. ENetEvent event;
  171. while(doPolling)
  172. {
  173. if(enet_host_service(service, &event, POLL_INTERVAL) > 0)
  174. {
  175. switch(event.type)
  176. {
  177. case ENET_EVENT_TYPE_CONNECT: {
  178. bool receiverFound = false;
  179. for(auto & c : active)
  180. {
  181. if(c->getPeer() == event.peer)
  182. {
  183. std::lock_guard<std::mutex> guard(mutex);
  184. connecting.push_back(c);
  185. receiverFound = true;
  186. break;
  187. }
  188. }
  189. if(!receiverFound)
  190. {
  191. auto c = std::make_shared<EnetConnection>(event.peer);
  192. active.insert(c);
  193. std::lock_guard<std::mutex> guard(mutex);
  194. connecting.push_back(c);
  195. }
  196. enet_packet_destroy(event.packet);
  197. break;
  198. }
  199. case ENET_EVENT_TYPE_RECEIVE: {
  200. bool receiverFound = false;
  201. for(auto & c : active)
  202. {
  203. if(c->getPeer() == event.peer)
  204. {
  205. c->dispatch(event.packet);
  206. receiverFound = true;
  207. break;
  208. }
  209. }
  210. if(!receiverFound)
  211. enet_packet_destroy(event.packet);
  212. break;
  213. }
  214. case ENET_EVENT_TYPE_DISCONNECT: {
  215. for(auto c : active)
  216. {
  217. if(c->getPeer() == event.peer)
  218. {
  219. std::lock_guard<std::mutex> guard(mutex);
  220. disconnecting.push_back(c);
  221. active.erase(c);
  222. break;
  223. }
  224. }
  225. enet_packet_destroy(event.packet);
  226. break;
  227. }
  228. }
  229. }
  230. }
  231. }