From 8deb1c3ad57bb0a84b5821cbe176438bced74eba Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Wed, 24 Nov 2010 23:03:52 +0100 Subject: Added error handling around connections. --- common/connectionbase.cpp | 15 ++++++++++----- common/connectionbase.h | 5 +++++ 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'common') diff --git a/common/connectionbase.cpp b/common/connectionbase.cpp index 165614f..a113e6a 100644 --- a/common/connectionbase.cpp +++ b/common/connectionbase.cpp @@ -1,6 +1,6 @@ #include "connectionbase.h" -#include +#include #include @@ -11,7 +11,7 @@ void ConnectionBase::start_recv() { void ConnectionBase::got_data(uint8_t* data, std::size_t bytes) { if(pending_size == 0) { if(bytes != 4) { - throw std::runtime_error("Deserialization header error."); + error("Deserialization header error."); } uint16_t* header = (uint16_t*)data; @@ -29,7 +29,7 @@ void ConnectionBase::got_data(uint8_t* data, std::size_t bytes) { if(bytes != pending_size) { std::cout << "Bytes: " << bytes << " Pending size: " << pending_size << std::endl; - throw std::runtime_error("Deserialization attempted with incomplete data."); + error("Deserialization attempted with incomplete data."); // TODO: Calling got_data() with incomplete data should be allowed. } @@ -75,11 +75,16 @@ void ConnectionBase::got_data(uint8_t* data, std::size_t bytes) { break; default: - throw std::runtime_error("Deserialization attempted on unknown message type."); + error("Deserialization attempted on unknown message type."); } if(bytes) { - m->deserialize(data, bytes); + try { + m->deserialize(data, bytes); + + } catch(std::exception& e) { + error(std::string("Deserialization failed: ") + e.what()); + } } got_message(m); diff --git a/common/connectionbase.h b/common/connectionbase.h index 4604896..f86a9ab 100644 --- a/common/connectionbase.h +++ b/common/connectionbase.h @@ -3,6 +3,7 @@ #include #include +#include #include "message.h" @@ -33,6 +34,10 @@ class ConnectionBase { //! \param msg Received message. virtual void got_message(const Message::p& msg) = 0; + //! Called upon error (lost connection or failed deserialization). + //! \param msg Error message. + virtual void error(const std::string& msg) = 0; + public: ConnectionBase(); virtual ~ConnectionBase(); -- cgit v1.2.3