From: Mikko Rasa Date: Mon, 6 Apr 2015 22:19:21 +0000 (+0300) Subject: Provide a signal to handle errors from Communicator X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=a0b7f9bcf0654a7a3f24bac95746ac587b71986b;p=libs%2Fnet.git Provide a signal to handle errors from Communicator Since Communicator is typically used with an EventDispatcher, it can be inconvenient to just throw exceptions out. Handling them would require wrapping the EventDispatcher's tick call in a try block, and the exact source of the exception would be lost. --- diff --git a/source/net/communicator.cpp b/source/net/communicator.cpp index c9b277d..618e8b4 100644 --- a/source/net/communicator.cpp +++ b/source/net/communicator.cpp @@ -1,6 +1,8 @@ #include #include "communicator.h" +using namespace std; + namespace { using namespace Msp::Net; @@ -85,9 +87,10 @@ void Communicator::data_available() if(!good) return; - in_end += socket.read(in_end, in_buf+buf_size-in_end); try { + in_end += socket.read(in_end, in_buf+buf_size-in_end); + bool more = true; while(more) { @@ -114,10 +117,12 @@ void Communicator::data_available() } } } - catch(...) + catch(const exception &e) { good = false; - throw; + if(signal_error.empty()) + throw; + signal_error.emit(e); } } diff --git a/source/net/communicator.h b/source/net/communicator.h index 8530db1..cd465a5 100644 --- a/source/net/communicator.h +++ b/source/net/communicator.h @@ -19,6 +19,7 @@ class Communicator { public: sigc::signal signal_handshake_done; + sigc::signal signal_error; private: StreamSocket &socket; @@ -47,7 +48,17 @@ public: if(handshake_status!=2) throw sequence_error("handshaking not done"); unsigned size = protocol.assemble(pkt, out_buf, buf_size); - socket.write(out_buf, size); + try + { + socket.write(out_buf, size); + } + catch(const std::exception &e) + { + good = false; + if(signal_error.empty()) + throw; + signal_error.emit(e); + } } private: