From fd419ce4f2bcadae313f4f368361c733c1249ae9 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Sun, 22 Mar 2009 17:50:10 +0000 Subject: added error callback to resolver for better handling of resolvCallback exceptions --- src/resolver.cpp | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'src/resolver.cpp') diff --git a/src/resolver.cpp b/src/resolver.cpp index 5e2d862..06db1b4 100644 --- a/src/resolver.cpp +++ b/src/resolver.cpp @@ -34,34 +34,33 @@ #include "resolver.h" #include "log.h" -#include "signalController.h" using ::boost::asio::ip::udp; using ::boost::asio::ip::tcp; template -void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ResolvAddrType r) +void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) { cLog.msg(Log::PRIO_ERROR) << "the resolver only supports udp and tcp"; } template<> -void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ResolvAddrType r) +void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) { boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000)); - gResolver.resolveUdp(addr, port, onResolve, r); + gResolver.resolveUdp(addr, port, onResolve, onError, r); } template<> -void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ResolvAddrType r) +void waitAndEnqueue(u_int32_t s, const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) { boost::this_thread::sleep(boost::posix_time::milliseconds(s * 1000)); - gResolver.resolveTcp(addr, port, onResolve, r); + gResolver.resolveTcp(addr, port, onResolve, onError, r); } template -ResolveHandler::ResolveHandler(const std::string& addr, const std::string& port, boost::function)> const& onResolve, ResolvAddrType r) : addr_(addr), port_(port), callback_(onResolve), resolv_addr_type_(r) +ResolveHandler::ResolveHandler(const std::string& addr, const std::string& port, boost::function)> const& onResolve, ErrorCallback const& onError, ResolvAddrType r) : addr_(addr), port_(port), onResolve_(onResolve), onError_(onError), resolv_addr_type_(r) { } @@ -70,15 +69,15 @@ void ResolveHandler::operator()(const boost::system::error_code& e, const { if(boost::system::posix_error::success == e) { try { - callback_(*endpointIt); + onResolve_(*endpointIt); } catch(const std::runtime_error& e) { - gSignalController.inject(SIGERROR, e.what()); + onError_(e); } } else { - cLog.msg(Log::PRIO_ERROR) << "Error while resolving '" << addr_ << "' '" << port_ << "', retrying in 10 sec."; - boost::thread(boost::bind(waitAndEnqueue, 10, addr_, port_, callback_, resolv_addr_type_)); + cLog.msg(Log::PRIO_ERROR) << "Error while resolving '" << addr_ << "' '" << port_ << "', retrying in 10 sec."; + boost::thread(boost::bind(waitAndEnqueue, 10, addr_, port_, onResolve_, onError_, resolv_addr_type_)); } } @@ -134,7 +133,7 @@ void Resolver::run() } -void Resolver::resolveUdp(const std::string& addr, const std::string& port, boost::function const& onResolve, ResolvAddrType r) +void Resolver::resolveUdp(const std::string& addr, const std::string& port, UdpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r) { cLog.msg(Log::PRIO_DEBUG) << "trying to resolv UDP: '" << addr << "' '" << port << "'"; @@ -153,11 +152,11 @@ void Resolver::resolveUdp(const std::string& addr, const std::string& port, boos default: query = std::auto_ptr(new udp::resolver::query(port)); break; } } - UdpResolveHandler handler(addr, port, onResolve, r); + UdpResolveHandler handler(addr, port, onResolve, onError, r); udp_resolver_.async_resolve(*query, handler); } -void Resolver::resolveTcp(const std::string& addr, const std::string& port, boost::function const& onResolve, ResolvAddrType r) +void Resolver::resolveTcp(const std::string& addr, const std::string& port, TcpResolveCallback const& onResolve, ErrorCallback const& onError, ResolvAddrType r) { cLog.msg(Log::PRIO_DEBUG) << "trying to resolv TCP: '" << addr << "' '" << port << "'"; @@ -176,6 +175,6 @@ void Resolver::resolveTcp(const std::string& addr, const std::string& port, boos default: query = std::auto_ptr(new tcp::resolver::query(port)); break; } } - TcpResolveHandler handler(addr, port, onResolve, r); + TcpResolveHandler handler(addr, port, onResolve, onError, r); tcp_resolver_.async_resolve(*query, handler); } -- cgit v1.2.3