ip.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*************************************************************************/
  2. /* ip.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 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 "os/thread.h"
  31. #include "os/semaphore.h"
  32. #include "hash_map.h"
  33. VARIANT_ENUM_CAST(IP::ResolverStatus);
  34. VARIANT_ENUM_CAST(IP_Address::AddrType);
  35. /************* RESOLVER ******************/
  36. struct _IP_ResolverPrivate {
  37. struct QueueItem {
  38. volatile IP::ResolverStatus status;
  39. IP_Address response;
  40. String hostname;
  41. IP_Address::AddrType type;
  42. void clear() {
  43. status = IP::RESOLVER_STATUS_NONE;
  44. response = IP_Address();
  45. type = IP_Address::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. Semaphore *sem;
  61. Thread* thread;
  62. //Semaphore* semaphore;
  63. bool thread_abort;
  64. void resolve_queues() {
  65. for(int i=0;i<IP::RESOLVER_MAX_QUERIES;i++) {
  66. if (queue[i].status!=IP::RESOLVER_STATUS_WAITING)
  67. continue;
  68. queue[i].response=IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type);
  69. if (queue[i].response.type==IP_Address::TYPE_NONE)
  70. queue[i].status=IP::RESOLVER_STATUS_ERROR;
  71. else
  72. queue[i].status=IP::RESOLVER_STATUS_DONE;
  73. }
  74. }
  75. static void _thread_function(void *self) {
  76. _IP_ResolverPrivate *ipr=(_IP_ResolverPrivate*)self;
  77. while(!ipr->thread_abort) {
  78. ipr->sem->wait();
  79. GLOBAL_LOCK_FUNCTION;
  80. ipr->resolve_queues();
  81. }
  82. }
  83. HashMap<String, IP_Address> cache;
  84. };
  85. IP_Address IP::resolve_hostname(const String& p_hostname, IP_Address::AddrType p_type) {
  86. GLOBAL_LOCK_FUNCTION;
  87. if (resolver->cache.has(p_hostname))
  88. return resolver->cache[p_hostname];
  89. IP_Address res = _resolve_hostname(p_hostname, p_type);
  90. resolver->cache[p_hostname]=res;
  91. return res;
  92. }
  93. IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname, IP_Address::AddrType p_type) {
  94. GLOBAL_LOCK_FUNCTION;
  95. ResolverID id = resolver->find_empty_id();
  96. if (id==RESOLVER_INVALID_ID) {
  97. WARN_PRINT("Out of resolver queries");
  98. return id;
  99. }
  100. resolver->queue[id].hostname=p_hostname;
  101. resolver->queue[id].type = p_type;
  102. if (resolver->cache.has(p_hostname)) {
  103. resolver->queue[id].response=resolver->cache[p_hostname];
  104. resolver->queue[id].status=IP::RESOLVER_STATUS_DONE;
  105. } else {
  106. resolver->queue[id].response=IP_Address();
  107. resolver->queue[id].status=IP::RESOLVER_STATUS_WAITING;
  108. if (resolver->thread)
  109. resolver->sem->post();
  110. else
  111. resolver->resolve_queues();
  112. }
  113. return id;
  114. }
  115. IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const {
  116. ERR_FAIL_INDEX_V(p_id,IP::RESOLVER_MAX_QUERIES,IP::RESOLVER_STATUS_NONE);
  117. GLOBAL_LOCK_FUNCTION;
  118. ERR_FAIL_COND_V(resolver->queue[p_id].status==IP::RESOLVER_STATUS_NONE,IP::RESOLVER_STATUS_NONE);
  119. return resolver->queue[p_id].status;
  120. }
  121. IP_Address IP::get_resolve_item_address(ResolverID p_id) const {
  122. ERR_FAIL_INDEX_V(p_id,IP::RESOLVER_MAX_QUERIES,IP_Address());
  123. GLOBAL_LOCK_FUNCTION;
  124. if (resolver->queue[p_id].status!=IP::RESOLVER_STATUS_DONE) {
  125. ERR_EXPLAIN("Resolve of '"+resolver->queue[p_id].hostname+"'' didn't complete yet.");
  126. ERR_FAIL_COND_V(resolver->queue[p_id].status!=IP::RESOLVER_STATUS_DONE,IP_Address());
  127. }
  128. return resolver->queue[p_id].response;
  129. }
  130. void IP::erase_resolve_item(ResolverID p_id) {
  131. ERR_FAIL_INDEX(p_id,IP::RESOLVER_MAX_QUERIES);
  132. GLOBAL_LOCK_FUNCTION;
  133. resolver->queue[p_id].status=IP::RESOLVER_STATUS_NONE;
  134. }
  135. Array IP::_get_local_addresses() const {
  136. Array addresses;
  137. List<IP_Address> ip_addresses;
  138. get_local_addresses(&ip_addresses);
  139. for(List<IP_Address>::Element *E=ip_addresses.front();E;E=E->next()) {
  140. addresses.push_back(E->get());
  141. }
  142. return addresses;
  143. }
  144. void IP::_bind_methods() {
  145. ObjectTypeDB::bind_method(_MD("resolve_hostname","host"),&IP::resolve_hostname);
  146. ObjectTypeDB::bind_method(_MD("resolve_hostname_queue_item","host"),&IP::resolve_hostname_queue_item);
  147. ObjectTypeDB::bind_method(_MD("get_resolve_item_status","id"),&IP::get_resolve_item_status);
  148. ObjectTypeDB::bind_method(_MD("get_resolve_item_address","id"),&IP::get_resolve_item_address);
  149. ObjectTypeDB::bind_method(_MD("erase_resolve_item","id"),&IP::erase_resolve_item);
  150. ObjectTypeDB::bind_method(_MD("get_local_addresses"),&IP::_get_local_addresses);
  151. BIND_CONSTANT( RESOLVER_STATUS_NONE );
  152. BIND_CONSTANT( RESOLVER_STATUS_WAITING );
  153. BIND_CONSTANT( RESOLVER_STATUS_DONE );
  154. BIND_CONSTANT( RESOLVER_STATUS_ERROR );
  155. BIND_CONSTANT( RESOLVER_MAX_QUERIES );
  156. BIND_CONSTANT( RESOLVER_INVALID_ID );
  157. }
  158. IP*IP::singleton=NULL;
  159. IP* IP::get_singleton() {
  160. return singleton;
  161. }
  162. IP* (*IP::_create)()=NULL;
  163. IP* IP::create() {
  164. ERR_FAIL_COND_V(singleton,NULL);
  165. ERR_FAIL_COND_V(!_create,NULL);
  166. return _create();
  167. }
  168. IP::IP() {
  169. singleton=this;
  170. resolver = memnew( _IP_ResolverPrivate );
  171. resolver->sem=NULL;
  172. #ifndef NO_THREADS
  173. //resolver->sem = Semaphore::create();
  174. resolver->sem=NULL;
  175. if (resolver->sem) {
  176. resolver->thread_abort=false;
  177. resolver->thread = Thread::create( _IP_ResolverPrivate::_thread_function,resolver );
  178. if (!resolver->thread)
  179. memdelete(resolver->sem); //wtf
  180. } else {
  181. resolver->thread=NULL;
  182. }
  183. #else
  184. resolver->sem = NULL;
  185. resolver->thread=NULL;
  186. #endif
  187. }
  188. IP::~IP() {
  189. #ifndef NO_THREADS
  190. if (resolver->thread) {
  191. resolver->thread_abort=true;
  192. resolver->sem->post();
  193. Thread::wait_to_finish(resolver->thread);
  194. memdelete( resolver->thread );
  195. memdelete( resolver->sem);
  196. }
  197. memdelete(resolver);
  198. #endif
  199. }