ip.cpp 8.3 KB

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