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.cpp154
1 files changed, 68 insertions, 86 deletions
diff --git a/core/io/ip.cpp b/core/io/ip.cpp
index d820273a14..6713963495 100644
--- a/core/io/ip.cpp
+++ b/core/io/ip.cpp
@@ -27,15 +27,14 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "ip.h"
-#include "os/thread.h"
-#include "os/semaphore.h"
#include "hash_map.h"
+#include "os/semaphore.h"
+#include "os/thread.h"
VARIANT_ENUM_CAST(IP::ResolverStatus);
/************* RESOLVER ******************/
-
struct _IP_ResolverPrivate {
struct QueueItem {
@@ -49,7 +48,7 @@ struct _IP_ResolverPrivate {
status = IP::RESOLVER_STATUS_NONE;
response = IP_Address();
type = IP::TYPE_NONE;
- hostname="";
+ hostname = "";
};
QueueItem() {
@@ -61,8 +60,8 @@ struct _IP_ResolverPrivate {
IP::ResolverID find_empty_id() const {
- for(int i=0;i<IP::RESOLVER_MAX_QUERIES;i++) {
- if (queue[i].status==IP::RESOLVER_STATUS_NONE)
+ for (int i = 0; i < IP::RESOLVER_MAX_QUERIES; i++) {
+ if (queue[i].status == IP::RESOLVER_STATUS_NONE)
return i;
}
return IP::RESOLVER_INVALID_ID;
@@ -70,39 +69,35 @@ struct _IP_ResolverPrivate {
Semaphore *sem;
- Thread* thread;
+ Thread *thread;
//Semaphore* semaphore;
bool thread_abort;
void resolve_queues() {
- for(int i=0;i<IP::RESOLVER_MAX_QUERIES;i++) {
+ 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);
+ queue[i].response = IP::get_singleton()->resolve_hostname(queue[i].hostname, queue[i].type);
if (!queue[i].response.is_valid())
- queue[i].status=IP::RESOLVER_STATUS_ERROR;
+ queue[i].status = IP::RESOLVER_STATUS_ERROR;
else
- queue[i].status=IP::RESOLVER_STATUS_DONE;
-
+ queue[i].status = IP::RESOLVER_STATUS_DONE;
}
}
-
static void _thread_function(void *self) {
- _IP_ResolverPrivate *ipr=(_IP_ResolverPrivate*)self;
+ _IP_ResolverPrivate *ipr = (_IP_ResolverPrivate *)self;
- while(!ipr->thread_abort) {
+ while (!ipr->thread_abort) {
ipr->sem->wait();
GLOBAL_LOCK_FUNCTION;
ipr->resolve_queues();
-
}
-
}
HashMap<String, IP_Address> cache;
@@ -110,12 +105,9 @@ struct _IP_ResolverPrivate {
static String get_cache_key(String p_hostname, IP::Type p_type) {
return itos(p_type) + p_hostname;
}
-
};
-
-
-IP_Address IP::resolve_hostname(const String& p_hostname, IP::Type p_type) {
+IP_Address IP::resolve_hostname(const String &p_hostname, IP::Type p_type) {
GLOBAL_LOCK_FUNCTION;
@@ -124,30 +116,29 @@ IP_Address IP::resolve_hostname(const String& p_hostname, IP::Type p_type) {
return resolver->cache[key];
IP_Address res = _resolve_hostname(p_hostname, p_type);
- resolver->cache[key]=res;
+ resolver->cache[key] = res;
return res;
-
}
-IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname, IP::Type p_type) {
+IP::ResolverID IP::resolve_hostname_queue_item(const String &p_hostname, IP::Type p_type) {
GLOBAL_LOCK_FUNCTION;
ResolverID id = resolver->find_empty_id();
- if (id==RESOLVER_INVALID_ID) {
+ if (id == RESOLVER_INVALID_ID) {
WARN_PRINT("Out of resolver queries");
return id;
}
String key = _IP_ResolverPrivate::get_cache_key(p_hostname, p_type);
- resolver->queue[id].hostname=p_hostname;
+ resolver->queue[id].hostname = p_hostname;
resolver->queue[id].type = p_type;
if (resolver->cache.has(key)) {
- resolver->queue[id].response=resolver->cache[key];
- resolver->queue[id].status=IP::RESOLVER_STATUS_DONE;
+ resolver->queue[id].response = resolver->cache[key];
+ resolver->queue[id].status = IP::RESOLVER_STATUS_DONE;
} else {
- resolver->queue[id].response=IP_Address();
- resolver->queue[id].status=IP::RESOLVER_STATUS_WAITING;
+ resolver->queue[id].response = IP_Address();
+ resolver->queue[id].status = IP::RESOLVER_STATUS_WAITING;
if (resolver->thread)
resolver->sem->post();
else
@@ -159,37 +150,33 @@ IP::ResolverID IP::resolve_hostname_queue_item(const String& p_hostname, IP::Typ
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);
+ ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP::RESOLVER_STATUS_NONE);
GLOBAL_LOCK_FUNCTION;
- ERR_FAIL_COND_V(resolver->queue[p_id].status==IP::RESOLVER_STATUS_NONE,IP::RESOLVER_STATUS_NONE);
+ ERR_FAIL_COND_V(resolver->queue[p_id].status == IP::RESOLVER_STATUS_NONE, IP::RESOLVER_STATUS_NONE);
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());
+ ERR_FAIL_INDEX_V(p_id, IP::RESOLVER_MAX_QUERIES, IP_Address());
GLOBAL_LOCK_FUNCTION;
- if (resolver->queue[p_id].status!=IP::RESOLVER_STATUS_DONE) {
- ERR_EXPLAIN("Resolve of '"+resolver->queue[p_id].hostname+"'' didn't complete yet.");
- ERR_FAIL_COND_V(resolver->queue[p_id].status!=IP::RESOLVER_STATUS_DONE,IP_Address());
+ if (resolver->queue[p_id].status != IP::RESOLVER_STATUS_DONE) {
+ ERR_EXPLAIN("Resolve of '" + resolver->queue[p_id].hostname + "'' didn't complete yet.");
+ ERR_FAIL_COND_V(resolver->queue[p_id].status != IP::RESOLVER_STATUS_DONE, IP_Address());
}
-
return resolver->queue[p_id].response;
-
}
void IP::erase_resolve_item(ResolverID p_id) {
- ERR_FAIL_INDEX(p_id,IP::RESOLVER_MAX_QUERIES);
+ ERR_FAIL_INDEX(p_id, IP::RESOLVER_MAX_QUERIES);
GLOBAL_LOCK_FUNCTION;
- resolver->queue[p_id].status=IP::RESOLVER_STATUS_NONE;
-
+ resolver->queue[p_id].status = IP::RESOLVER_STATUS_NONE;
}
void IP::clear_cache(const String &p_hostname) {
@@ -209,7 +196,7 @@ Array IP::_get_local_addresses() const {
Array addresses;
List<IP_Address> ip_addresses;
get_local_addresses(&ip_addresses);
- for(List<IP_Address>::Element *E=ip_addresses.front();E;E=E->next()) {
+ for (List<IP_Address>::Element *E = ip_addresses.front(); E; E = E->next()) {
addresses.push_back(E->get());
}
@@ -218,87 +205,82 @@ Array IP::_get_local_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);
- ClassDB::bind_method(D_METHOD("get_resolve_item_address","id"),&IP::get_resolve_item_address);
- ClassDB::bind_method(D_METHOD("erase_resolve_item","id"),&IP::erase_resolve_item);
- ClassDB::bind_method(D_METHOD("get_local_addresses"),&IP::_get_local_addresses);
- ClassDB::bind_method(D_METHOD("clear_cache"),&IP::clear_cache, DEFVAL(""));
-
- BIND_CONSTANT( RESOLVER_STATUS_NONE );
- BIND_CONSTANT( RESOLVER_STATUS_WAITING );
- BIND_CONSTANT( RESOLVER_STATUS_DONE );
- BIND_CONSTANT( RESOLVER_STATUS_ERROR );
-
- BIND_CONSTANT( RESOLVER_MAX_QUERIES );
- BIND_CONSTANT( RESOLVER_INVALID_ID );
-
- BIND_CONSTANT( TYPE_NONE );
- BIND_CONSTANT( TYPE_IPV4 );
- BIND_CONSTANT( TYPE_IPV6 );
- BIND_CONSTANT( TYPE_ANY );
+ 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);
+ ClassDB::bind_method(D_METHOD("get_resolve_item_address", "id"), &IP::get_resolve_item_address);
+ ClassDB::bind_method(D_METHOD("erase_resolve_item", "id"), &IP::erase_resolve_item);
+ ClassDB::bind_method(D_METHOD("get_local_addresses"), &IP::_get_local_addresses);
+ ClassDB::bind_method(D_METHOD("clear_cache"), &IP::clear_cache, DEFVAL(""));
+
+ BIND_CONSTANT(RESOLVER_STATUS_NONE);
+ BIND_CONSTANT(RESOLVER_STATUS_WAITING);
+ BIND_CONSTANT(RESOLVER_STATUS_DONE);
+ BIND_CONSTANT(RESOLVER_STATUS_ERROR);
+
+ BIND_CONSTANT(RESOLVER_MAX_QUERIES);
+ BIND_CONSTANT(RESOLVER_INVALID_ID);
+
+ BIND_CONSTANT(TYPE_NONE);
+ BIND_CONSTANT(TYPE_IPV4);
+ BIND_CONSTANT(TYPE_IPV6);
+ BIND_CONSTANT(TYPE_ANY);
}
+IP *IP::singleton = NULL;
-IP*IP::singleton=NULL;
-
-IP* IP::get_singleton() {
+IP *IP::get_singleton() {
return singleton;
}
+IP *(*IP::_create)() = NULL;
-IP* (*IP::_create)()=NULL;
+IP *IP::create() {
-IP* IP::create() {
-
- ERR_FAIL_COND_V(singleton,NULL);
- ERR_FAIL_COND_V(!_create,NULL);
+ ERR_FAIL_COND_V(singleton, NULL);
+ ERR_FAIL_COND_V(!_create, NULL);
return _create();
}
IP::IP() {
- singleton=this;
- resolver = memnew( _IP_ResolverPrivate );
- resolver->sem=NULL;
+ singleton = this;
+ resolver = memnew(_IP_ResolverPrivate);
+ resolver->sem = NULL;
#ifndef NO_THREADS
//resolver->sem = Semaphore::create();
- resolver->sem=NULL;
+ resolver->sem = NULL;
if (resolver->sem) {
- resolver->thread_abort=false;
+ resolver->thread_abort = false;
- resolver->thread = Thread::create( _IP_ResolverPrivate::_thread_function,resolver );
+ resolver->thread = Thread::create(_IP_ResolverPrivate::_thread_function, resolver);
if (!resolver->thread)
memdelete(resolver->sem); //wtf
} else {
- resolver->thread=NULL;
+ resolver->thread = NULL;
}
#else
resolver->sem = NULL;
- resolver->thread=NULL;
+ resolver->thread = NULL;
#endif
-
-
}
IP::~IP() {
#ifndef NO_THREADS
if (resolver->thread) {
- resolver->thread_abort=true;
+ resolver->thread_abort = true;
resolver->sem->post();
Thread::wait_to_finish(resolver->thread);
- memdelete( resolver->thread );
- memdelete( resolver->sem);
+ memdelete(resolver->thread);
+ memdelete(resolver->sem);
}
memdelete(resolver);
#endif
-
}