diff options
Diffstat (limited to 'core/io/ip.cpp')
-rw-r--r-- | core/io/ip.cpp | 116 |
1 files changed, 36 insertions, 80 deletions
diff --git a/core/io/ip.cpp b/core/io/ip.cpp index ea791fb327..24b8ec7cc1 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -39,9 +39,7 @@ VARIANT_ENUM_CAST(IP::ResolverStatus); /************* RESOLVER ******************/ struct _IP_ResolverPrivate { - struct QueueItem { - volatile IP::ResolverStatus status; IP_Address response; String hostname; @@ -56,53 +54,50 @@ struct _IP_ResolverPrivate { QueueItem() { clear(); - }; + } }; QueueItem queue[IP::RESOLVER_MAX_QUERIES]; IP::ResolverID find_empty_id() const { - for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) { - if (queue[i].status == IP::RESOLVER_STATUS_NONE) + if (queue[i].status == IP::RESOLVER_STATUS_NONE) { return i; + } } return IP::RESOLVER_INVALID_ID; } - Mutex *mutex; - Semaphore *sem; + Mutex mutex; + Semaphore sem; Thread *thread; //Semaphore* semaphore; bool thread_abort; void resolve_queues() { - for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) { - - if (queue[i].status != IP::RESOLVER_STATUS_WAITING) + if (queue[i].status != IP::RESOLVER_STATUS_WAITING) { continue; + } queue[i].response = IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type); - if (!queue[i].response.is_valid()) + if (!queue[i].response.is_valid()) { queue[i].status = IP::RESOLVER_STATUS_ERROR; - else + } else { queue[i].status = IP::RESOLVER_STATUS_DONE; + } } } static void _thread_function(void *self) { - _IP_ResolverPrivate *ipr = (_IP_ResolverPrivate *)self; while (!ipr->thread_abort) { + ipr->sem.wait(); - ipr->sem->wait(); - - ipr->mutex->lock(); + MutexLock lock(ipr->mutex); ipr->resolve_queues(); - ipr->mutex->unlock(); } } @@ -114,31 +109,26 @@ struct _IP_ResolverPrivate { }; IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) { - - resolver->mutex->lock(); + MutexLock lock(resolver->mutex); String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type); if (resolver->cache.has(key) && resolver->cache[key].is_valid()) { IP_Address res = resolver->cache[key]; - resolver->mutex->unlock(); return res; } IP_Address res = _resolve_hostname(p_hostname, p_type); resolver->cache[key] = res; - resolver->mutex->unlock(); return res; } IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) { - - resolver->mutex->lock(); + MutexLock lock(resolver->mutex); ResolverID id = resolver->find_empty_id(); if (id == RESOLVER_INVALID_ID) { WARN_PRINT("Out of resolver queries"); - resolver->mutex->unlock(); return id; } @@ -151,64 +141,53 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Typ } else { resolver->queue[id].response = IP_Address(); resolver->queue[id].status = IP::RESOLVER_STATUS_WAITING; - if (resolver->thread) - resolver->sem->post(); - else + if (resolver->thread) { + resolver->sem.post(); + } else { resolver->resolve_queues(); + } } - resolver->mutex->unlock(); return id; } IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const { - ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE); - resolver->mutex->lock(); + MutexLock lock(resolver->mutex); + if (resolver->queue[p_id].status == IP::RESOLVER_STATUS_NONE) { ERR_PRINT("Condition status == IP::RESOLVER_STATUS_NONE"); - resolver->mutex->unlock(); + resolver->mutex.unlock(); return IP::RESOLVER_STATUS_NONE; } - IP::ResolverStatus res = resolver->queue[p_id].status; - - resolver->mutex->unlock(); - return res; + return resolver->queue[p_id].status; } IP_Address IP::get_resolve_item_address(ResolverID p_id) const { - ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address()); - resolver->mutex->lock(); + MutexLock lock(resolver->mutex); if (resolver->queue[p_id].status != IP::RESOLVER_STATUS_DONE) { - ERR_PRINTS("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet."); - resolver->mutex->unlock(); + ERR_PRINT("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet."); + resolver->mutex.unlock(); return IP_Address(); } - IP_Address res = resolver->queue[p_id].response; - - resolver->mutex->unlock(); - return res; + return resolver->queue[p_id].response; } void IP::erase_resolve_item(ResolverID p_id) { - ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES); - resolver->mutex->lock(); + MutexLock lock(resolver->mutex); resolver->queue[p_id].status = IP::RESOLVER_STATUS_NONE; - - resolver->mutex->unlock(); } void IP::clear_cache(const String &p_hostname) { - - resolver->mutex->lock(); + MutexLock lock(resolver->mutex); if (p_hostname.empty()) { resolver->cache.clear(); @@ -218,12 +197,9 @@ void IP::clear_cache(const String &p_hostname) { resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_IPV6)); resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_ANY)); } - - resolver->mutex->unlock(); } Array IP::_get_local_addresses() const { - Array addresses; List<IP_Address> ip_addresses; get_local_addresses(&ip_addresses); @@ -235,7 +211,6 @@ Array IP::_get_local_addresses() const { } Array IP::_get_local_interfaces() const { - Array results; Map<String, Interface_Info> interfaces; get_local_interfaces(&interfaces); @@ -259,7 +234,6 @@ Array IP::_get_local_interfaces() const { } void IP::get_local_addresses(List<IP_Address> *r_addresses) const { - Map<String, Interface_Info> interfaces; get_local_interfaces(&interfaces); for (Map<String, Interface_Info>::Element *E = interfaces.front(); E; E = E->next()) { @@ -270,7 +244,6 @@ void IP::get_local_addresses(List<IP_Address> *r_addresses) const { } void IP::_bind_methods() { - ClassDB::bind_method(D_METHOD("resolve_hostname", "host", "ip_type"), &IP::resolve_hostname, DEFVAL(IP::TYPE_ANY)); ClassDB::bind_method(D_METHOD("resolve_hostname_queue_item", "host", "ip_type"), &IP::resolve_hostname_queue_item, DEFVAL(IP::TYPE_ANY)); ClassDB::bind_method(D_METHOD("get_resolve_item_status", "id"), &IP::get_resolve_item_status); @@ -294,61 +267,44 @@ void IP::_bind_methods() { BIND_ENUM_CONSTANT(TYPE_ANY); } -IP *IP::singleton = NULL; +IP *IP::singleton = nullptr; IP *IP::get_singleton() { - return singleton; } -IP *(*IP::_create)() = NULL; +IP *(*IP::_create)() = nullptr; IP *IP::create() { - - ERR_FAIL_COND_V_MSG(singleton, NULL, "IP singleton already exist."); - ERR_FAIL_COND_V(!_create, NULL); + ERR_FAIL_COND_V_MSG(singleton, nullptr, "IP singleton already exist."); + ERR_FAIL_COND_V(!_create, nullptr); return _create(); } IP::IP() { - singleton = this; resolver = memnew(_IP_ResolverPrivate); - resolver->sem = NULL; - resolver->mutex = Mutex::create(); #ifndef NO_THREADS - resolver->sem = Semaphore::create(); - if (resolver->sem) { - resolver->thread_abort = false; + resolver->thread_abort = false; - resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver); - - if (!resolver->thread) - memdelete(resolver->sem); //wtf - } else { - resolver->thread = NULL; - } + resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver); #else - resolver->sem = NULL; - resolver->thread = NULL; + resolver->thread = nullptr; #endif } IP::~IP() { - #ifndef NO_THREADS if (resolver->thread) { resolver->thread_abort = true; - resolver->sem->post(); + resolver->sem.post(); Thread::wait_to_finish(resolver->thread); memdelete(resolver->thread); - memdelete(resolver->sem); } #endif - memdelete(resolver->mutex); memdelete(resolver); } |