diff options
-rw-r--r-- | include/salticidae/conn.h | 22 | ||||
-rw-r--r-- | include/salticidae/network.h | 6 | ||||
-rw-r--r-- | src/network.cpp | 16 | ||||
-rw-r--r-- | test/test_msgnet_c.c | 7 | ||||
-rw-r--r-- | test/test_p2p.cpp | 9 | ||||
-rw-r--r-- | test/test_p2p_stress.cpp | 10 |
6 files changed, 48 insertions, 22 deletions
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index e19ae3a..665e1d5 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -59,7 +59,7 @@ class ConnPool { using conn_t = ArcObj<Conn>; /** The type of callback invoked when connection status is changed. */ using conn_callback_t = std::function<void(const conn_t &, bool)>; - using error_callback_t = std::function<void(const std::exception &, bool)>; + using error_callback_t = std::function<void(const std::exception_ptr, bool)>; /** Abstraction for a bi-directional connection. */ class Conn { friend ConnPool; @@ -271,13 +271,7 @@ class ConnPool { void _listen(NetAddr listen_addr); void recoverable_error(const std::exception_ptr err) const { user_tcall->async_call([this, err](ThreadCall::Handle &) { - if (error_cb) { - try { - std::rethrow_exception(err); - } catch (const std::exception &e) { - error_cb(e, false); - } - } + if (error_cb) error_cb(err, false); }); } @@ -356,14 +350,10 @@ class ConnPool { disp_ec = workers[0].get_ec(); disp_tcall = workers[0].get_tcall(); workers[0].set_dispatcher(); - disp_error_cb = [this](const std::exception_ptr _err) { - user_tcall->async_call([this, _err](ThreadCall::Handle &) { - try { - std::rethrow_exception(_err); - } catch (const std::exception &err) { - stop_workers(); - if (error_cb) error_cb(err, true); - } + disp_error_cb = [this](const std::exception_ptr err) { + user_tcall->async_call([this, err](ThreadCall::Handle &) { + stop_workers(); + if (error_cb) error_cb(err, true); }); disp_ec.stop(); workers[0].stop_tcall(); diff --git a/include/salticidae/network.h b/include/salticidae/network.h index de7c32e..09b85a0 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -974,9 +974,13 @@ void msgnetwork_terminate(msgnetwork_t *self, const msgnetwork_conn_t *conn); typedef void (*msgnetwork_msg_callback_t)(const msg_t *, const msgnetwork_conn_t *, void *userdata); void msgnetwork_reg_handler(msgnetwork_t *self, _opcode_t opcode, msgnetwork_msg_callback_t cb, void *userdata); -typedef void (*msgnetwork_conn_callback_t)(const msgnetwork_conn_t *, bool, void *userdata); +typedef void (*msgnetwork_conn_callback_t)(const msgnetwork_conn_t *, bool connected, void *userdata); void msgnetwork_reg_conn_handler(msgnetwork_t *self, msgnetwork_conn_callback_t cb, void *userdata); + +typedef void (*msgnetwork_error_callback_t)(const SalticidaeCError *, bool fatal, void *userdata); +void msgnetwork_reg_error_handler(msgnetwork_t *self, msgnetwork_error_callback_t cb, void *userdata); + msgnetwork_t *msgnetwork_conn_get_net(const msgnetwork_conn_t *conn); msgnetwork_conn_mode_t msgnetwork_conn_get_mode(const msgnetwork_conn_t *conn); netaddr_t *msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn); diff --git a/src/network.cpp b/src/network.cpp index 91fc770..ef16bed 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -100,6 +100,22 @@ void msgnetwork_reg_conn_handler(msgnetwork_t *self, }); } +void msgnetwork_reg_error_handler(msgnetwork_t *self, + msgnetwork_error_callback_t cb, + void *userdata) { + self->reg_error_handler([=](const std::exception_ptr _err, bool fatal) { + SalticidaeCError cerror; + try { + std::rethrow_exception(_err); + } catch (SalticidaeError &err) { + cerror = err.get_cerr(); + } catch (...) { + cerror = salticidae_cerror_unknown(); + } + cb(&cerror, fatal, userdata); + }); +} + void msgnetwork_start(msgnetwork_t *self) { self->start(); } void msgnetwork_terminate(msgnetwork_t *self, const msgnetwork_conn_t *conn) { diff --git a/test/test_msgnet_c.c b/test/test_msgnet_c.c index b2fb35a..c2fb492 100644 --- a/test/test_msgnet_c.c +++ b/test/test_msgnet_c.c @@ -139,6 +139,13 @@ void conn_handler(const msgnetwork_conn_t *conn, bool connected, void *userdata) } } +void error_handler(const SalticidaeCError *err, bool fatal, void *userdata) { + MyNet *n = (MyNet *)userdata; + printf("[%s] Captured %s error during an async call: %s\n", + n->name, fatal ? "fatal" : "recoverable", + salticidae_strerror(err->code)); +} + MyNet gen_mynet(const eventcontext_t *ec, const char *name) { MyNet res; diff --git a/test/test_p2p.cpp b/test/test_p2p.cpp index 6d311a6..14304eb 100644 --- a/test/test_p2p.cpp +++ b/test/test_p2p.cpp @@ -79,8 +79,13 @@ struct Net { net->reg_handler([this](const MsgText &msg, const PeerNetwork::conn_t &) { fprintf(stdout, "net %lu: peer %lu says %s\n", this->id, msg.id, msg.text.c_str()); }); - net->reg_error_handler([this](const std::exception &err, bool fatal) { - fprintf(stdout, "net %lu: captured %s error during an async call: %s\n", this->id, fatal ? "fatal" : "recoverable", err.what()); + net->reg_error_handler([this](const std::exception_ptr _err, bool fatal) { + try { + std::rethrow_exception(_err); + } catch (const std::exception &err) { + fprintf(stdout, "net %lu: captured %s error during an async call: %s\n", + this->id, fatal ? "fatal" : "recoverable", err.what()); + } }); net->reg_unknown_peer_handler([this](const NetAddr &addr) { fprintf(stdout, "net %lu: unknown peer attempts to connnect %s\n", this->id, std::string(addr).c_str()); diff --git a/test/test_p2p_stress.cpp b/test/test_p2p_stress.cpp index 3a71660..92e5bb4 100644 --- a/test/test_p2p_stress.cpp +++ b/test/test_p2p_stress.cpp @@ -114,9 +114,13 @@ void install_proto(AppContext &app, const size_t &seg_buff_size) { } } }); - net.reg_error_handler([ec](const std::exception &err, bool fatal) { - SALTICIDAE_LOG_WARN("main thread captured %s error: %s", - fatal ? "fatal" : "recoverable", err.what()); + net.reg_error_handler([ec](const std::exception_ptr _err, bool fatal) { + try { + std::rethrow_exception(_err); + } catch (const std::exception & err) { + SALTICIDAE_LOG_WARN("main thread captured %s error: %s", + fatal ? "fatal" : "recoverable", err.what()); + } }); net.reg_handler([&](MsgRand &&msg, const MyNet::conn_t &conn) { uint256_t hash = salticidae::get_hash(msg.bytes); |