ip.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*************************************************************************/
  2. /* ip.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "ip.h"
  31. #include "core/hash_map.h"
  32. #include "core/os/semaphore.h"
  33. #include "core/os/thread.h"
  34. VARIANT_ENUM_CAST(IP::ResolverStatus);
  35. /************* RESOLVER ******************/
  36. struct _IP_ResolverPrivate {
  37. struct QueueItem {
  38. volatile IP::ResolverStatus status;
  39. IP_Address response;
  40. String hostname;
  41. IP::Type type;
  42. void clear() {
  43. status = IP::RESOLVER_STATUS_NONE;
  44. response = IP_Address();
  45. type = IP::TYPE_NONE;
  46. hostname = "";
  47. };
  48. QueueItem() {
  49. clear();
  50. };
  51. };
  52. QueueItem queue[IP::RESOLVER_MAX_QUERIES];
  53. IP::ResolverID find_empty_id() const {
  54. for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) {
  55. if (queue[i].status == IP::RESOLVER_STATUS_NONE)
  56. return i;
  57. }
  58. return IP::RESOLVER_INVALID_ID;
  59. }
  60. Mutex *mutex;
  61. Semaphore *sem;
  62. Thread *thread;
  63. //Semaphore* semaphore;
  64. bool thread_abort;
  65. void resolve_queues() {
  66. for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) {
  67. if (queue[i].status != IP::RESOLVER_STATUS_WAITING)
  68. continue;
  69. queue[i].response = IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type);
  70. if (!queue[i].response.is_valid())
  71. queue[i].status = IP::RESOLVER_STATUS_ERROR;
  72. else
  73. queue[i].status = IP::RESOLVER_STATUS_DONE;
  74. }
  75. }
  76. static void _thread_function(void *self) {
  77. _IP_ResolverPrivate *ipr = (_IP_ResolverPrivate *)self;
  78. while (!ipr->thread_abort) {
  79. ipr->sem->wait();
  80. ipr->mutex->lock();
  81. ipr->resolve_queues();
  82. ipr->mutex->unlock();
  83. }
  84. }
  85. HashMap<String, IP_Address> cache;
  86. static String get_cache_key(String p_hostname, IP::Type p_type) {
  87. return itos(p_type) + p_hostname;
  88. }
  89. };
  90. IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
  91. resolver->mutex->lock();
  92. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  93. if (resolver->cache.has(key) && resolver->cache[key].is_valid()) {
  94. IP_Address res = resolver->cache[key];
  95. resolver->mutex->unlock();
  96. return res;
  97. }
  98. IP_Address res = _resolve_hostname(p_hostname, p_type);
  99. resolver->cache[key] = res;
  100. resolver->mutex->unlock();
  101. return res;
  102. }
  103. IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) {
  104. resolver->mutex->lock();
  105. ResolverID id = resolver->find_empty_id();
  106. if (id == RESOLVER_INVALID_ID) {
  107. WARN_PRINT("Out of resolver queries");
  108. resolver->mutex->unlock();
  109. return id;
  110. }
  111. String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
  112. resolver->queue[id].hostname = p_hostname;
  113. resolver->queue[id].type = p_type;
  114. if (resolver->cache.has(key) && resolver->cache[key].is_valid()) {
  115. resolver->queue[id].response = resolver->cache[key];
  116. resolver->queue[id].status = IP::RESOLVER_STATUS_DONE;
  117. } else {
  118. resolver->queue[id].response = IP_Address();
  119. resolver->queue[id].status = IP::RESOLVER_STATUS_WAITING;
  120. if (resolver->thread)
  121. resolver->sem->post();
  122. else
  123. resolver->resolve_queues();
  124. }
  125. resolver->mutex->unlock();
  126. return id;
  127. }
  128. IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const {
  129. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE);
  130. resolver->mutex->lock();
  131. if (resolver->queue[p_id].status == IP::RESOLVER_STATUS_NONE) {
  132. ERR_PRINT("Condition status == IP::RESOLVER_STATUS_NONE");
  133. resolver->mutex->unlock();
  134. return IP::RESOLVER_STATUS_NONE;
  135. }
  136. IP::ResolverStatus res = resolver->queue[p_id].status;
  137. resolver->mutex->unlock();
  138. return res;
  139. }
  140. IP_Address IP::get_resolve_item_address(ResolverID p_id) const {
  141. ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address());
  142. resolver->mutex->lock();
  143. if (resolver->queue[p_id].status != IP::RESOLVER_STATUS_DONE) {
  144. ERR_PRINTS("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet.");
  145. resolver->mutex->unlock();
  146. return IP_Address();
  147. }
  148. IP_Address res = resolver->queue[p_id].response;
  149. resolver->mutex->unlock();
  150. return res;
  151. }
  152. void IP::erase_resolve_item(ResolverID p_id) {
  153. ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES);
  154. resolver->mutex->lock();
  155. resolver->queue[p_id].status = IP::RESOLVER_STATUS_NONE;
  156. resolver->mutex->unlock();
  157. }
  158. void IP::clear_cache(const String &p_hostname) {
  159. resolver->mutex->lock();
  160. if (p_hostname.empty()) {
  161. resolver->cache.clear();
  162. } else {
  163. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE));
  164. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV4));
  165. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6));
  166. resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY));
  167. }
  168. resolver->mutex->unlock();
  169. }
  170. Array IP::_get_local_addresses() const {
  171. Array addresses;
  172. List<IP_Address> ip_addresses;
  173. get_local_addresses(&ip_addresses);
  174. for (List<IP_Address>::Element *E = ip_addresses.front(); E; E = E->next()) {
  175. addresses.push_back(E->get());
  176. }
  177. return addresses;
  178. }
  179. void IP::_bind_methods() {
  180. ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY));
  181. ClassDB::bind_method(D_METHOD("resolve_hostname_queue_item", "host", "ip_type"), &IP::resolve_hostname_queue_item, DEFVAL(IP::TYPE_ANY));
  182. ClassDB::bind_method(D_METHOD("get_resolve_item_status", "id"), &IP::get_resolve_item_status);
  183. ClassDB::bind_method(D_METHOD("get_resolve_item_address", "id"), &IP::get_resolve_item_address);
  184. ClassDB::bind_method(D_METHOD("erase_resolve_item", "id"), &IP::erase_resolve_item);
  185. ClassDB::bind_method(D_METHOD("get_local_addresses"), &IP::_get_local_addresses);
  186. ClassDB::bind_method(D_METHOD("clear_cache", "hostname"), &IP::clear_cache, DEFVAL(""));
  187. BIND_ENUM_CONSTANT(RESOLVER_STATUS_NONE);
  188. BIND_ENUM_CONSTANT(RESOLVER_STATUS_WAITING);
  189. BIND_ENUM_CONSTANT(RESOLVER_STATUS_DONE);
  190. BIND_ENUM_CONSTANT(RESOLVER_STATUS_ERROR);
  191. BIND_CONSTANT(RESOLVER_MAX_QUERIES);
  192. BIND_CONSTANT(RESOLVER_INVALID_ID);
  193. BIND_ENUM_CONSTANT(TYPE_NONE);
  194. BIND_ENUM_CONSTANT(TYPE_IPV4);
  195. BIND_ENUM_CONSTANT(TYPE_IPV6);
  196. BIND_ENUM_CONSTANT(TYPE_ANY);
  197. }
  198. IP *IP::singleton = NULL;
  199. IP *IP::get_singleton() {
  200. return singleton;
  201. }
  202. IP *(*IP::_create)() = NULL;
  203. IP *IP::create() {
  204. ERR_FAIL_COND_V(singleton, NULL);
  205. ERR_FAIL_COND_V(!_create, NULL);
  206. return _create();
  207. }
  208. IP::IP() {
  209. singleton = this;
  210. resolver = memnew(_IP_ResolverPrivate);
  211. resolver->sem = NULL;
  212. resolver->mutex = Mutex::create();
  213. #ifndef NO_THREADS
  214. resolver->sem = Semaphore::create();
  215. if (resolver->sem) {
  216. resolver->thread_abort = false;
  217. resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver);
  218. if (!resolver->thread)
  219. memdelete(resolver->sem); //wtf
  220. } else {
  221. resolver->thread = NULL;
  222. }
  223. #else
  224. resolver->sem = NULL;
  225. resolver->thread = NULL;
  226. #endif
  227. }
  228. IP::~IP() {
  229. #ifndef NO_THREADS
  230. if (resolver->thread) {
  231. resolver->thread_abort = true;
  232. resolver->sem->post();
  233. Thread::wait_to_finish(resolver->thread);
  234. memdelete(resolver->thread);
  235. memdelete(resolver->sem);
  236. }
  237. #endif
  238. memdelete(resolver->mutex);
  239. memdelete(resolver);
  240. }