summaryrefslogtreecommitdiff
path: root/core/io/ip.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'core/io/ip.cpp')
-rw-r--r--core/io/ip.cpp47
1 files changed, 15 insertions, 32 deletions
diff --git a/core/io/ip.cpp b/core/io/ip.cpp
index 5de7fb7186..6fb812e78d 100644
--- a/core/io/ip.cpp
+++ b/core/io/ip.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -30,18 +30,16 @@
#include "ip.h"
-#include "core/hash_map.h"
#include "core/os/semaphore.h"
#include "core/os/thread.h"
+#include "core/templates/hash_map.h"
VARIANT_ENUM_CAST(IP::ResolverStatus);
/************* RESOLVER ******************/
struct _IP_ResolverPrivate {
-
struct QueueItem {
-
volatile IP::ResolverStatus status;
IP_Address response;
String hostname;
@@ -56,16 +54,16 @@ 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;
}
@@ -78,26 +76,24 @@ struct _IP_ResolverPrivate {
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();
MutexLock lock(ipr->mutex);
@@ -113,7 +109,6 @@ struct _IP_ResolverPrivate {
};
IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
-
MutexLock lock(resolver->mutex);
String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
@@ -128,7 +123,6 @@ IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
}
IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) {
-
MutexLock lock(resolver->mutex);
ResolverID id = resolver->find_empty_id();
@@ -147,17 +141,17 @@ 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)
+ if (resolver->thread) {
resolver->sem.post();
- else
+ } else {
resolver->resolve_queues();
+ }
}
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);
MutexLock lock(resolver->mutex);
@@ -171,7 +165,6 @@ IP::ResolverStatus IP::get_resolve_item_status(ResolverID p_id) const {
}
IP_Address IP::get_resolve_item_address(ResolverID p_id) const {
-
ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address());
MutexLock lock(resolver->mutex);
@@ -186,7 +179,6 @@ IP_Address IP::get_resolve_item_address(ResolverID p_id) const {
}
void IP::erase_resolve_item(ResolverID p_id) {
-
ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES);
MutexLock lock(resolver->mutex);
@@ -195,10 +187,9 @@ void IP::erase_resolve_item(ResolverID p_id) {
}
void IP::clear_cache(const String &p_hostname) {
-
MutexLock lock(resolver->mutex);
- if (p_hostname.empty()) {
+ if (p_hostname.is_empty()) {
resolver->cache.clear();
} else {
resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE));
@@ -209,7 +200,6 @@ void IP::clear_cache(const String &p_hostname) {
}
Array IP::_get_local_addresses() const {
-
Array addresses;
List<IP_Address> ip_addresses;
get_local_addresses(&ip_addresses);
@@ -221,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);
@@ -245,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()) {
@@ -256,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);
@@ -283,21 +270,18 @@ void IP::_bind_methods() {
IP *IP::singleton = nullptr;
IP *IP::get_singleton() {
-
return singleton;
}
IP *(*IP::_create)() = nullptr;
IP *IP::create() {
-
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);
@@ -312,7 +296,6 @@ IP::IP() {
}
IP::~IP() {
-
#ifndef NO_THREADS
if (resolver->thread) {
resolver->thread_abort = true;