From d0cb90e3fdd1bcb943ecadff542edd4260bbae1c Mon Sep 17 00:00:00 2001 From: Determinant Date: Mon, 24 Jun 2019 16:55:52 -0400 Subject: clean up code --- src/conn.cpp | 53 ++++++++++++++++++++++------------------- src/network.cpp | 73 +++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 82 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/conn.cpp b/src/conn.cpp index 1f5e324..7f6408e 100644 --- a/src/conn.cpp +++ b/src/conn.cpp @@ -47,13 +47,13 @@ ConnPool::Conn::operator std::string() const { case Conn::PASSIVE: s << "passive"; break; case Conn::DEAD: s << "dead"; break; } - s << ">"; + s << " term=" << (is_terminated() ? "yes" : "no") << ">"; return std::move(s); } /* the following functions are executed by exactly one worker per Conn object */ -void ConnPool::Conn::_send_data(const ConnPool::conn_t &conn, int fd, int events) { +void ConnPool::Conn::_send_data(const conn_t &conn, int fd, int events) { if (events & FdEvent::ERROR) { conn->cpool->worker_terminate(conn); @@ -97,7 +97,7 @@ void ConnPool::Conn::_send_data(const ConnPool::conn_t &conn, int fd, int events conn->ready_send = true; } -void ConnPool::Conn::_recv_data(const ConnPool::conn_t &conn, int fd, int events) { +void ConnPool::Conn::_recv_data(const conn_t &conn, int fd, int events) { if (events & FdEvent::ERROR) { conn->cpool->worker_terminate(conn); @@ -133,7 +133,7 @@ void ConnPool::Conn::_recv_data(const ConnPool::conn_t &conn, int fd, int events } -void ConnPool::Conn::_send_data_tls(const ConnPool::conn_t &conn, int fd, int events) { +void ConnPool::Conn::_send_data_tls(const conn_t &conn, int fd, int events) { if (events & FdEvent::ERROR) { conn->cpool->worker_terminate(conn); @@ -177,7 +177,7 @@ void ConnPool::Conn::_send_data_tls(const ConnPool::conn_t &conn, int fd, int ev conn->ready_send = true; } -void ConnPool::Conn::_recv_data_tls(const ConnPool::conn_t &conn, int fd, int events) { +void ConnPool::Conn::_recv_data_tls(const conn_t &conn, int fd, int events) { if (events & FdEvent::ERROR) { conn->cpool->worker_terminate(conn); @@ -211,12 +211,12 @@ void ConnPool::Conn::_recv_data_tls(const ConnPool::conn_t &conn, int fd, int ev conn->cpool->on_read(conn); } -void ConnPool::Conn::_send_data_tls_handshake(const ConnPool::conn_t &conn, int fd, int events) { +void ConnPool::Conn::_send_data_tls_handshake(const conn_t &conn, int fd, int events) { conn->ready_send = true; _recv_data_tls_handshake(conn, fd, events); } -void ConnPool::Conn::_recv_data_tls_handshake(const ConnPool::conn_t &conn, int, int) { +void ConnPool::Conn::_recv_data_tls_handshake(const conn_t &conn, int, int) { int ret; if (conn->tls->do_handshake(ret)) { @@ -224,6 +224,7 @@ void ConnPool::Conn::_recv_data_tls_handshake(const ConnPool::conn_t &conn, int, conn->send_data_func = _send_data_tls; conn->recv_data_func = _recv_data_dummy; conn->peer_cert = new X509(conn->tls->get_peer_cert()); + conn->worker->enable_send_buffer(conn, conn->fd); conn->cpool->update_conn(conn, true); } else @@ -234,8 +235,7 @@ void ConnPool::Conn::_recv_data_tls_handshake(const ConnPool::conn_t &conn, int, } } -void ConnPool::Conn::_recv_data_dummy(const ConnPool::conn_t &, int, int) { -} +void ConnPool::Conn::_recv_data_dummy(const conn_t &, int, int) {} void ConnPool::Conn::stop() { if (mode != ConnMode::DEAD) @@ -308,21 +308,25 @@ void ConnPool::accept_client(int fd, int) { } void ConnPool::conn_server(const conn_t &conn, int fd, int events) { - if (send(fd, "", 0, MSG_NOSIGNAL) == 0) - { - conn->ev_connect.del(); - SALTICIDAE_LOG_INFO("connected to remote %s", std::string(*conn).c_str()); - auto &worker = select_worker(); - conn->worker = &worker; - on_setup(conn); - worker.feed(conn, fd); - } - else - { - if (events & TimedFdEvent::TIMEOUT) - SALTICIDAE_LOG_INFO("%s connect timeout", std::string(*conn).c_str()); + try { + if (send(fd, "", 0, MSG_NOSIGNAL) == 0) + { + conn->ev_connect.del(); + SALTICIDAE_LOG_INFO("connected to remote %s", std::string(*conn).c_str()); + auto &worker = select_worker(); + conn->worker = &worker; + on_setup(conn); + worker.feed(conn, fd); + } + else + { + if (events & TimedFdEvent::TIMEOUT) + SALTICIDAE_LOG_INFO("%s connect timeout", std::string(*conn).c_str()); + throw SalticidaeError(SALTI_ERROR_CONNECT); + } + } catch (...) { disp_terminate(conn); - return; + recoverable_error(std::current_exception()); } } @@ -402,6 +406,7 @@ ConnPool::conn_t ConnPool::_connect(const NetAddr &addr) { void ConnPool::del_conn(const conn_t &conn) { auto it = pool.find(conn->fd); + SALTICIDAE_LOG_INFO("%s %d\n", std::string(*conn).c_str()); assert(it != pool.end()); pool.erase(it); update_conn(conn, false); @@ -417,7 +422,7 @@ void ConnPool::release_conn(const conn_t &conn) { } ConnPool::conn_t ConnPool::add_conn(const conn_t &conn) { - //assert(pool.find(conn->fd) == pool.end()); + assert(pool.find(conn->fd) == pool.end()); return pool.insert(std::make_pair(conn->fd, conn)).first->second; } diff --git a/src/network.cpp b/src/network.cpp index 2f84f25..74f4df9 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -72,7 +72,7 @@ void msgnetwork_send_msg(msgnetwork_t *self, const msg_t *msg, const msgnetwork_ } void msgnetwork_send_msg_deferred_by_move(msgnetwork_t *self, - msg_t *_moved_msg, const msgnetwork_conn_t *conn) { + msg_t *_moved_msg, const msgnetwork_conn_t *conn) { self->_send_msg_deferred(std::move(*_moved_msg), *conn); } @@ -96,6 +96,14 @@ void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr, Saltici SALTICIDAE_CERROR_CATCH(cerror) } +void msgnetwork_start(msgnetwork_t *self) { self->start(); } + +void msgnetwork_stop(msgnetwork_t *self) { self->stop(); } + +void msgnetwork_terminate(msgnetwork_t *self, const msgnetwork_conn_t *conn) { + self->terminate(*conn); +} + void msgnetwork_reg_handler(msgnetwork_t *self, _opcode_t opcode, msgnetwork_msg_callback_t cb, @@ -131,14 +139,6 @@ void msgnetwork_reg_error_handler(msgnetwork_t *self, }); } -void msgnetwork_start(msgnetwork_t *self) { self->start(); } - -void msgnetwork_stop(msgnetwork_t *self) { self->stop(); } - -void msgnetwork_terminate(msgnetwork_t *self, const msgnetwork_conn_t *conn) { - self->terminate(*conn); -} - msgnetwork_t *msgnetwork_conn_get_net(const msgnetwork_conn_t *conn) { return (*conn)->get_net(); } @@ -181,10 +181,6 @@ void peernetwork_config_id_mode(peernetwork_config_t *self, peernetwork_id_mode_ msgnetwork_config_t *peernetwork_config_as_msgnetwork_config(peernetwork_config_t *self) { return self; } -peernetwork_t *msgnetwork_as_peernetwork_unsafe(msgnetwork_t *self) { - return static_cast(self); -} - peernetwork_t *peernetwork_new(const eventcontext_t *ec, const peernetwork_config_t *config, SalticidaeCError *cerror) { SALTICIDAE_CERROR_TRY(cerror) return new peernetwork_t(*ec, *config); @@ -213,6 +209,10 @@ const peernetwork_conn_t *peernetwork_get_peer_conn(const peernetwork_t *self, msgnetwork_t *peernetwork_as_msgnetwork(peernetwork_t *self) { return self; } +peernetwork_t *msgnetwork_as_peernetwork_unsafe(msgnetwork_t *self) { + return static_cast(self); +} + msgnetwork_conn_t *msgnetwork_conn_new_from_peernetwork_conn(const peernetwork_conn_t *conn) { return new msgnetwork_conn_t(*conn); } @@ -227,19 +227,17 @@ peernetwork_conn_t *peernetwork_conn_copy(const peernetwork_conn_t *self) { void peernetwork_conn_free(const peernetwork_conn_t *self) { delete self; } -void peernetwork_send_msg(peernetwork_t *self, - const msg_t * msg, const netaddr_t *paddr) { +void peernetwork_send_msg(peernetwork_t *self, const msg_t * msg, const netaddr_t *paddr) { self->_send_msg(*msg, *paddr); } void peernetwork_send_msg_deferred_by_move(peernetwork_t *self, - msg_t * _moved_msg, const netaddr_t *paddr) { + msg_t *_moved_msg, const netaddr_t *paddr) { self->_send_msg_deferred(std::move(*_moved_msg), *paddr); } void peernetwork_multicast_msg_by_move(peernetwork_t *self, - msg_t *_moved_msg, - const netaddr_array_t *paddrs) { + msg_t *_moved_msg, const netaddr_array_t *paddrs) { self->_multicast_msg(std::move(*_moved_msg), *paddrs); } @@ -249,8 +247,6 @@ void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr, Salti SALTICIDAE_CERROR_CATCH(cerror) } -void peernetwork_stop(peernetwork_t *self) { self->stop(); } - void peernetwork_reg_unknown_peer_handler(peernetwork_t *self, msgnetwork_unknown_peer_callback_t cb, void *userdata) { @@ -259,6 +255,43 @@ void peernetwork_reg_unknown_peer_handler(peernetwork_t *self, }); } +clientnetwork_t *clientnetwork_new(const eventcontext_t *ec, const msgnetwork_config_t *config, SalticidaeCError *cerror) { + SALTICIDAE_CERROR_TRY(cerror) + return new clientnetwork_t(*ec, *config); + SALTICIDAE_CERROR_CATCH(cerror) + return nullptr; +} + +void clientnetwork_free(const clientnetwork_t *self) { delete self; } + +msgnetwork_t *clientnetwork_as_msgnetwork(clientnetwork_t *self) { return self; } + +clientnetwork_t *msgnetwork_as_clientnetwork_unsafe(msgnetwork_t *self) { + return static_cast(self); +} + +msgnetwork_conn_t *msgnetwork_conn_new_from_clientnetwork_conn(const clientnetwork_conn_t *conn) { + return new msgnetwork_conn_t(*conn); +} + +clientnetwork_conn_t *clientnetwork_conn_new_from_msgnetwork_conn_unsafe(const msgnetwork_conn_t *conn) { + return new clientnetwork_conn_t(salticidae::static_pointer_cast(*conn)); +} + +clientnetwork_conn_t *clientnetwork_conn_copy(const clientnetwork_conn_t *self) { + return new clientnetwork_conn_t(*self); +} + +void clientnetwork_conn_free(const clientnetwork_conn_t *self) { delete self; } + +void clientnetwork_send_msg(clientnetwork_t *self, const msg_t * msg, const netaddr_t *addr) { + self->_send_msg(*msg, *addr); +} + +void clientnetwork_send_msg_deferred_by_move(clientnetwork_t *self, msg_t *_moved_msg, const netaddr_t *addr) { + self->_send_msg_deferred(std::move(*_moved_msg), *addr); +} + } #endif -- cgit v1.2.3-70-g09d2