From bc078abd9d5fcc420dfbcda06869a95aba39b1ea Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 1 Aug 2018 13:30:43 -0400 Subject: use reference in place of pointers --- include/salticidae/conn.h | 8 +++--- include/salticidae/network.h | 66 ++++++++++++++++++++++---------------------- test/bench_network.cpp | 6 ++-- test/test_network.cpp | 16 +++++------ 4 files changed, 48 insertions(+), 48 deletions(-) diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 4f17665..4cc8b11 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -147,7 +147,7 @@ class ConnPool { /** The handle to a bi-directional connection. */ using conn_t = RcObj; /** The type of callback invoked when connection status is changed. */ - using conn_callback_t = std::function; + using conn_callback_t = std::function; /** Abstraction for a bi-directional connection. */ class Conn { @@ -228,15 +228,15 @@ class ConnPool { /** Called when new data is available. */ virtual void on_read() { - if (cpool->read_cb) cpool->read_cb(this); + if (cpool->read_cb) cpool->read_cb(*this); } /** Called when the underlying connection is established. */ virtual void on_setup() { - if (cpool->conn_cb) cpool->conn_cb(this); + if (cpool->conn_cb) cpool->conn_cb(*this); } /** Called when the underlying connection breaks. */ virtual void on_teardown() { - if (cpool->conn_cb) cpool->conn_cb(this); + if (cpool->conn_cb) cpool->conn_cb(*this); } }; diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 8128119..f6623b6 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -125,7 +125,7 @@ class MsgNetwork: public ConnPool { private: std::unordered_map< typename Msg::opcode_t, - std::function> handler_map; + std::function> handler_map; protected: #ifdef SALTICIDAE_MSG_STAT @@ -149,14 +149,14 @@ class MsgNetwork: public ConnPool { typename callback_traits::msg_type, DataStream &&>::value>::type reg_handler(Func handler) { using callback_t = callback_traits; - handler_map[callback_t::msg_type::opcode] = [handler](const Msg &msg, Conn *conn) { + handler_map[callback_t::msg_type::opcode] = [handler](const Msg &msg, Conn &conn) { handler(typename callback_t::msg_type(msg.get_payload()), static_cast(conn)); }; } template - void send_msg(const MsgType &msg, Conn *conn); + void send_msg(const MsgType &msg, Conn &conn); using ConnPool::listen; #ifdef SALTICIDAE_MSG_STAT msg_stat_by_opcode_t &get_sent_by_opcode() const { @@ -323,10 +323,10 @@ class PeerNetwork: public MsgNetwork { } }; - void msg_ping(MsgPing &&msg, const Conn *conn); - void msg_pong(MsgPong &&msg, const Conn *conn); - void reset_conn_timeout(conn_t conn); - bool check_new_conn(conn_t conn, uint16_t port); + void msg_ping(MsgPing &&msg, Conn &conn); + void msg_pong(MsgPong &&msg, Conn &conn); + void reset_conn_timeout(Conn &conn); + bool check_new_conn(Conn &conn, uint16_t port); void start_active_conn(const NetAddr &paddr); protected: @@ -405,7 +405,7 @@ void MsgNetwork::Conn::on_read() { SALTICIDAE_LOG_DEBUG("got message %s from %s", std::string(msg).c_str(), std::string(*this).c_str()); - it->second(msg, this); + it->second(msg, *this); #ifdef SALTICIDAE_MSG_STAT nrecv++; mn->recv_by_opcode.add(msg); @@ -442,7 +442,7 @@ void PeerNetwork::Conn::on_setup() { this->terminate(); }); /* the initial ping-pong to set up the connection */ - auto conn = static_pointer_cast(this->self()); + auto &conn = static_cast(*this); pn->reset_conn_timeout(conn); pn->MsgNet::send_msg(MsgPing(pn->listen_port), conn); } @@ -469,46 +469,46 @@ void PeerNetwork::Conn::on_teardown() { } template -bool PeerNetwork::check_new_conn(conn_t conn, uint16_t port) { - if (conn->peer_id.is_null()) +bool PeerNetwork::check_new_conn(Conn &conn, uint16_t port) { + if (conn.peer_id.is_null()) { /* passive connections can eventually have ids after getting the port number in IP_BASED_PORT mode */ assert(id_mode == IP_PORT_BASED); - conn->peer_id.ip = conn->get_addr().ip; - conn->peer_id.port = port; + conn.peer_id.ip = conn.get_addr().ip; + conn.peer_id.port = port; } - auto p = id2peer.find(conn->peer_id)->second.get(); + auto p = id2peer.find(conn.peer_id)->second.get(); if (p->connected) { - if (conn != p->conn) + if (conn.self() != p->conn) { - conn->terminate(); + conn.terminate(); return true; } return false; } - p->reset_conn(conn); + p->reset_conn(static_pointer_cast(conn.self())); p->connected = true; p->reset_ping_timer(); p->send_ping(); if (p->connected) SALTICIDAE_LOG_INFO("PeerNetwork: established connection with %s via %s", - std::string(conn->peer_id).c_str(), std::string(*conn).c_str()); + std::string(conn.peer_id).c_str(), std::string(conn).c_str()); return false; } template -void PeerNetwork::msg_ping(MsgPing &&msg, const Conn *conn) { +void PeerNetwork::msg_ping(MsgPing &&msg, Conn &conn) { uint16_t port = msg.port; - SALTICIDAE_LOG_INFO("ping from %s, port %u", std::string(*conn).c_str(), ntohs(port)); + SALTICIDAE_LOG_INFO("ping from %s, port %u", std::string(conn).c_str(), ntohs(port)); if (check_new_conn(conn, port)) return; - auto p = id2peer.find(conn->peer_id)->second.get(); + auto p = id2peer.find(conn.peer_id)->second.get(); send_msg(MsgPong(this->listen_port), p); } template -void PeerNetwork::msg_pong(MsgPong &&msg, const Conn *conn) { - auto it = id2peer.find(conn->peer_id); +void PeerNetwork::msg_pong(MsgPong &&msg, Conn &conn) { + auto it = id2peer.find(conn.peer_id); if (it == id2peer.end()) { SALTICIDAE_LOG_WARN("pong message discarded"); @@ -571,15 +571,15 @@ bool PeerNetwork::has_peer(const NetAddr &paddr) const { template template -void MsgNetwork::send_msg(const MsgType &_msg, Conn *conn) { +void MsgNetwork::send_msg(const MsgType &_msg, Conn &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)); + conn.write(std::move(msg_data)); #ifdef SALTICIDAE_MSG_STAT - conn->nsent++; + conn.nsent++; sent_by_opcode.add(msg); #endif } @@ -588,7 +588,7 @@ template template void PeerNetwork::send_msg(const MsgType &msg, const Peer *peer) { if (peer->connected) - MsgNet::send_msg(msg, peer->conn); + MsgNet::send_msg(msg, *(peer->conn)); else SALTICIDAE_LOG_DEBUG("dropped"); } @@ -615,10 +615,10 @@ void PeerNetwork::Peer::reset_ping_timer() { } template -void PeerNetwork::reset_conn_timeout(conn_t conn) { - assert(conn->ev_timeout); - conn->ev_timeout.del(); - conn->ev_timeout.add_with_timeout(conn_timeout); +void PeerNetwork::reset_conn_timeout(Conn &conn) { + assert(conn.ev_timeout); + conn.ev_timeout.del(); + conn.ev_timeout.add_with_timeout(conn_timeout); SALTICIDAE_LOG_INFO("reset timeout %.2f", conn_timeout); } @@ -627,7 +627,7 @@ void PeerNetwork::Peer::send_ping() { auto pn = conn->get_net(); ping_timer_ok = false; pong_msg_ok = false; - pn->reset_conn_timeout(conn); + pn->reset_conn_timeout(*conn); pn->send_msg(MsgPing(pn->listen_port), this); } @@ -670,7 +670,7 @@ template void ClientNetwork::send_msg(const MsgType &msg, const NetAddr &addr) { auto it = addr2conn.find(addr); if (it == addr2conn.end()) return; - MsgNet::send_msg(msg, it->second); + MsgNet::send_msg(msg, *(it->second)); } template diff --git a/test/bench_network.cpp b/test/bench_network.cpp index c2406d5..19f0dd9 100644 --- a/test/bench_network.cpp +++ b/test/bench_network.cpp @@ -109,8 +109,8 @@ struct MyNet: public MsgNetworkByteOp { net->name.c_str()); /* send the first message through this connection */ net->ev_period_send = Event(net->ec, -1, 0, - [net, this](int, short) { - net->send_msg(MsgBytes(256), this); + [net, conn = self()](int, short) { + net->send_msg(MsgBytes(256), *conn); net->ev_period_send.add_with_timeout(0); }); net->ev_period_send.add_with_timeout(0); @@ -133,7 +133,7 @@ struct MyNet: public MsgNetworkByteOp { return new Conn(); } - void on_receive_bytes(MsgBytes &&msg, const Conn *conn) { + void on_receive_bytes(MsgBytes &&msg, Conn &conn) { nrecv++; } }; diff --git a/test/test_network.cpp b/test/test_network.cpp index 49a13c2..fa08596 100644 --- a/test/test_network.cpp +++ b/test/test_network.cpp @@ -89,16 +89,16 @@ struct MyNet: public MsgNetworkByteOp { reg_handler(salticidae::handler_bind( &MyNet::on_receive_hello, this, _1, _2)); - reg_conn_handler([this](ConnPool::Conn *conn) { - if (conn->get_fd() != -1) + reg_conn_handler([this](ConnPool::Conn &conn) { + if (conn.get_fd() != -1) { - if (conn->get_mode() == ConnPool::Conn::ACTIVE) + if (conn.get_mode() == ConnPool::Conn::ACTIVE) { printf("[%s] Connected, sending hello.\n", this->name.c_str()); /* send the first message through this connection */ send_msg(MsgHello(this->name, "Hello there!"), - static_cast(conn)); + static_cast(conn)); } else printf("[%s] Accepted, waiting for greetings.\n", @@ -108,7 +108,7 @@ struct MyNet: public MsgNetworkByteOp { { printf("[%s] Disconnected, retrying.\n", this->name.c_str()); /* try to reconnect to the same address */ - connect(conn->get_addr()); + connect(conn.get_addr()); } }); } @@ -117,7 +117,7 @@ struct MyNet: public MsgNetworkByteOp { return new Conn(); } - void on_receive_hello(MsgHello &&msg, MyNet::Conn *conn) { + void on_receive_hello(MsgHello &&msg, MyNet::Conn &conn) { printf("[%s] %s says %s\n", name.c_str(), msg.name.c_str(), msg.text.c_str()); @@ -127,8 +127,8 @@ struct MyNet: public MsgNetworkByteOp { }; -void on_receive_ack(MsgAck &&msg, MyNet::Conn *conn) { - auto net = static_cast(conn->get_net()); +void on_receive_ack(MsgAck &&msg, MyNet::Conn &conn) { + auto net = static_cast(conn.get_net()); printf("[%s] the peer knows\n", net->name.c_str()); } -- cgit v1.2.3