diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/salticidae/conn.h | 8 | ||||
-rw-r--r-- | include/salticidae/network.h | 38 | ||||
-rw-r--r-- | include/salticidae/util.h | 1 |
3 files changed, 21 insertions, 26 deletions
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 69402d3..c357875 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -63,7 +63,7 @@ class ConnPool { /** The handle to a bi-directional connection. */ using conn_t = ArcObj<Conn>; /** The type of callback invoked when connection status is changed. */ - using conn_callback_t = std::function<void(Conn &, bool)>; + using conn_callback_t = std::function<void(const conn_t &, bool)>; /** Abstraction for a bi-directional connection. */ class Conn { friend ConnPool; @@ -168,7 +168,7 @@ class ConnPool { void update_conn(const conn_t &conn, bool connected) { user_tcall->async_call([this, conn, connected](ThreadCall::Handle &) { - if (conn_cb) conn_cb(*conn, connected); + if (conn_cb) conn_cb(conn, connected); }); } @@ -391,9 +391,7 @@ class ConnPool { template<typename Func> void reg_conn_handler(Func cb) { conn_cb = cb; } - void terminate(Conn &_conn) { - auto conn = _conn.self(); - if (!conn) return; + void terminate(const conn_t &conn) { disp_tcall->async_call([this, conn](ThreadCall::Handle &) { conn->disp_terminate(); }); diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 4e182fc..43638cf 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -46,7 +46,7 @@ class MsgNetwork: public ConnPool { template<typename ReturnType, typename MsgType, typename ConnType> struct callback_traits<ReturnType(MsgType, ConnType)> { using ret_type = ReturnType; - using conn_type = ConnType; + using conn_type = typename std::remove_reference<ConnType>::type::type; using msg_type = typename std::remove_reference<MsgType>::type; }; @@ -121,7 +121,7 @@ class MsgNetwork: public ConnPool { private: std::unordered_map< typename Msg::opcode_t, - std::function<void(const Msg &msg, Conn &)>> handler_map; + std::function<void(const Msg &msg, const conn_t &)>> handler_map; using queue_t = MPSCQueueEventDriven<std::pair<Msg, conn_t>>; queue_t incoming_msgs; @@ -168,7 +168,7 @@ class MsgNetwork: public ConnPool { SALTICIDAE_LOG_DEBUG("got message %s from %s", std::string(msg).c_str(), std::string(*conn).c_str()); - it->second(msg, *conn); + it->second(msg, conn); #ifdef SALTICIDAE_MSG_STAT conn->nrecv++; recv_by_opcode.add(msg); @@ -185,14 +185,14 @@ class MsgNetwork: public ConnPool { typename callback_traits<Func>::msg_type, DataStream &&>::value>::type reg_handler(Func handler) { using callback_t = callback_traits<Func>; - handler_map[callback_t::msg_type::opcode] = [handler](const Msg &msg, Conn &conn) { + handler_map[callback_t::msg_type::opcode] = [handler](const Msg &msg, const conn_t &conn) { handler(typename callback_t::msg_type(msg.get_payload()), - static_cast<typename callback_t::conn_type>(conn)); + static_pointer_cast<typename callback_t::conn_type>(conn)); }; } template<typename MsgType> - void send_msg(const MsgType &msg, Conn &conn); + void send_msg(const MsgType &msg, const conn_t &conn); using ConnPool::listen; #ifdef SALTICIDAE_MSG_STAT msg_stat_by_opcode_t &get_sent_by_opcode() const { @@ -357,8 +357,8 @@ class PeerNetwork: public MsgNetwork<OpcodeType> { } }; - void msg_ping(MsgPing &&msg, Conn &conn); - void msg_pong(MsgPong &&msg, Conn &conn); + void msg_ping(MsgPing &&msg, const conn_t &conn); + void msg_pong(MsgPong &&msg, const conn_t &conn); void _ping_msg_cb(const conn_t &conn, uint16_t port); void _pong_msg_cb(const conn_t &conn, uint16_t port); bool check_new_conn(const conn_t &conn, uint16_t port); @@ -472,15 +472,15 @@ void MsgNetwork<OpcodeType>::Conn::on_read() { template<typename OpcodeType> template<typename MsgType> -void MsgNetwork<OpcodeType>::send_msg(const MsgType &_msg, Conn &conn) { +void MsgNetwork<OpcodeType>::send_msg(const MsgType &_msg, const conn_t &conn) { Msg msg(_msg); bytearray_t msg_data = msg.serialize(); SALTICIDAE_LOG_DEBUG("wrote message %s to %s", std::string(msg).c_str(), - std::string(conn).c_str()); - conn.write(std::move(msg_data)); + std::string(*conn).c_str()); + conn->write(std::move(msg_data)); #ifdef SALTICIDAE_MSG_STAT - conn.nsent++; + conn->nsent++; sent_by_opcode.add(msg); #endif } @@ -510,7 +510,7 @@ void PeerNetwork<O, _, __>::Conn::on_setup() { }); /* the initial ping-pong to set up the connection */ tcall_reset_timeout(worker, conn, pn->conn_timeout); - pn->send_msg(MsgPing(pn->listen_port), *conn); + pn->send_msg(MsgPing(pn->listen_port), conn); } template<typename O, O _, O __> @@ -564,7 +564,7 @@ void PeerNetwork<O, _, __>::Peer::send_ping() { ping_timer_ok = false; pong_msg_ok = false; tcall_reset_timeout(conn->worker, conn, pn->conn_timeout); - pn->send_msg(MsgPing(pn->listen_port), *conn); + pn->send_msg(MsgPing(pn->listen_port), conn); } template<typename O, O _, O __> @@ -621,9 +621,7 @@ void PeerNetwork<O, _, __>::start_active_conn(const NetAddr &addr) { /* begin: functions invoked by the user loop */ template<typename O, O _, O __> -void PeerNetwork<O, _, __>::msg_ping(MsgPing &&msg, Conn &_conn) { - auto conn = static_pointer_cast<Conn>(_conn.self()); - if (!conn) return; +void PeerNetwork<O, _, __>::msg_ping(MsgPing &&msg, const conn_t &conn) { uint16_t port = msg.port; this->disp_tcall->async_call([this, conn, port](ThreadCall::Handle &msg) { if (conn->get_mode() == ConnPool::Conn::DEAD) return; @@ -631,14 +629,12 @@ void PeerNetwork<O, _, __>::msg_ping(MsgPing &&msg, Conn &_conn) { std::string(*conn).c_str(), ntohs(port)); if (check_new_conn(conn, port)) return; auto p = id2peer.find(conn->peer_id)->second.get(); - send_msg(MsgPong(this->listen_port), *conn); + send_msg(MsgPong(this->listen_port), conn); }); } template<typename O, O _, O __> -void PeerNetwork<O, _, __>::msg_pong(MsgPong &&msg, Conn &_conn) { - auto conn = static_pointer_cast<Conn>(_conn.self()); - if (!conn) return; +void PeerNetwork<O, _, __>::msg_pong(MsgPong &&msg, const conn_t &conn) { uint16_t port = msg.port; this->disp_tcall->async_call([this, conn, port](ThreadCall::Handle &msg) { if (conn->get_mode() == ConnPool::Conn::DEAD) return; diff --git a/include/salticidae/util.h b/include/salticidae/util.h index 4e6825a..7552f8e 100644 --- a/include/salticidae/util.h +++ b/include/salticidae/util.h @@ -302,6 +302,7 @@ class Config { void update(Opt &opt, const char *optval); public: + Config() {} Config(const std::string &conf_fname): conf_fname(conf_fname), opt_val_conf(OptValConf::create(this)) { |