From 26fb816da7078d687f7540e2e0a2d525801592f7 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 22 Nov 2009 21:40:35 +0000 Subject: [PATCH] Prevent crashing if the Resolver jams --- source/netvis.cpp | 1 + source/resolver.cpp | 30 ++++++++++++++++++++++++------ source/resolver.h | 2 ++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/source/netvis.cpp b/source/netvis.cpp index 3aff9cd..dce0837 100644 --- a/source/netvis.cpp +++ b/source/netvis.cpp @@ -141,6 +141,7 @@ void NetVis::tick() } else if(i->second->get_activity()cancel(i->second); delete i->second; disabled_hosts.erase(i++); } diff --git a/source/resolver.cpp b/source/resolver.cpp index d8dc41d..702ce96 100644 --- a/source/resolver.cpp +++ b/source/resolver.cpp @@ -34,6 +34,24 @@ void Resolver::push(Host *h) in_queue.push_back(h); } +void Resolver::cancel(Host *h) +{ + MutexLock l_(mutex); + + if(h==current) + { + current=0; + return; + } + + for(list::iterator i=in_queue.begin(); i!=in_queue.end(); ++i) + if(*i==h) + { + in_queue.erase(i); + break; + } +} + void Resolver::tick() { MutexLock l_(mutex); @@ -49,24 +67,24 @@ void Resolver::main() { while(!done) { - Host *host; + sockaddr_in addr; + addr.sin_family = AF_INET; { MutexLock l_(mutex); if(in_queue.empty()) break; - host = in_queue.front(); + current = in_queue.front(); in_queue.erase(in_queue.begin()); + addr.sin_addr.s_addr = htonl(current->get_address()); } - sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = htonl(host->get_address()); char buf[128]; int err = getnameinfo(reinterpret_cast(&addr), sizeof(addr), buf, sizeof(buf), 0, 0, NI_NOFQDN|NI_NAMEREQD); if(err==0) { MutexLock l_(mutex); - out_queue.push_back(Result(host, buf)); + if(current) + out_queue.push_back(Result(current, buf)); } } diff --git a/source/resolver.h b/source/resolver.h index fb9dd10..4b6e498 100644 --- a/source/resolver.h +++ b/source/resolver.h @@ -27,6 +27,7 @@ private: std::list in_queue; std::list out_queue; + Host *current; Msp::Mutex mutex; bool done; @@ -35,6 +36,7 @@ public: ~Resolver(); void push(Host *); + void cancel(Host *); void tick(); private: void main(); -- 2.45.2