]> git.tdb.fi Git - libs/net.git/commitdiff
Provide a signal to handle errors from Communicator
authorMikko Rasa <tdb@tdb.fi>
Mon, 6 Apr 2015 22:19:21 +0000 (01:19 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 6 Apr 2015 22:19:21 +0000 (01:19 +0300)
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.

source/net/communicator.cpp
source/net/communicator.h

index c9b277d3748de137381b0df792d5230b0e8e0c62..618e8b45e9e295f01fc4948ef19daecd56f51242 100644 (file)
@@ -1,6 +1,8 @@
 #include <cstring>
 #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);
        }
 }
 
index 8530db128d26c5d168d99fb0af34e7c874484ffc..cd465a52d525c25cf837db2cca614a5a6e8cfc98 100644 (file)
@@ -19,6 +19,7 @@ class Communicator
 {
 public:
        sigc::signal<void> signal_handshake_done;
+       sigc::signal<void, const std::exception &> 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: