ip.cpp 7.2 KB

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