diff options
author | Determinant <[email protected]> | 2018-07-31 20:48:37 -0400 |
---|---|---|
committer | Determinant <[email protected]> | 2018-07-31 20:48:37 -0400 |
commit | 8c965718031f0de6401aa30579da2e10afbcaf96 (patch) | |
tree | 6dbebc5f252ad93f2b28c24768bd547ffcdde31d | |
parent | c9faab354fb314bacd9849503d8b4566a8c9ea62 (diff) |
optimize by replacing RcObj with pointers
-rw-r--r-- | README.rst | 46 | ||||
-rw-r--r-- | include/salticidae/conn.h | 8 | ||||
-rw-r--r-- | include/salticidae/network.h | 22 | ||||
-rw-r--r-- | test/bench_network.cpp | 8 | ||||
-rw-r--r-- | test/test_network.cpp | 10 |
5 files changed, 46 insertions, 48 deletions
@@ -36,7 +36,7 @@ Functionalities handler functions are registered by ``reg_handler()`` and invoked upon receiving a new message. ``OpcodeType`` is the type used for identifying message types. A valid message class must have: - + - a static member ``opcode`` typed ``OpcodeType`` as the message type identifier - a member ``serialized`` typed ``DataStream`` which contains the serialized data of the message. @@ -71,12 +71,12 @@ Example (MsgNetwork layer) #include <cstdio> #include <string> #include <functional> - + #include "salticidae/msg.h" #include "salticidae/event.h" #include "salticidae/network.h" #include "salticidae/stream.h" - + using salticidae::NetAddr; using salticidae::DataStream; using salticidae::MsgNetwork; @@ -84,7 +84,7 @@ Example (MsgNetwork layer) using salticidae::letoh; using std::placeholders::_1; using std::placeholders::_2; - + /** Hello Message. */ struct MsgHello { static const uint8_t opcode = 0x0; @@ -107,7 +107,7 @@ Example (MsgNetwork layer) text = std::string((const char *)s.get_data_inplace(len), len); } }; - + /** Acknowledgement Message. */ struct MsgAck { static const uint8_t opcode = 0x1; @@ -115,12 +115,12 @@ Example (MsgNetwork layer) MsgAck() {} MsgAck(DataStream &&s) {} }; - + const uint8_t MsgHello::opcode; const uint8_t MsgAck::opcode; - + using MsgNetworkByteOp = MsgNetwork<uint8_t>; - + struct MyNet: public MsgNetworkByteOp { const std::string name; const NetAddr peer; @@ -134,8 +134,8 @@ Example (MsgNetwork layer) /* message handler could be a bound method */ reg_handler(salticidae::handler_bind( &MyNet::on_receive_hello, this, _1, _2)); - - reg_conn_handler([this](const ConnPool::conn_t &conn) { + + reg_conn_handler([this](ConnPool::Conn *conn) { if (conn->get_fd() != -1) { if (conn->get_mode() == ConnPool::Conn::ACTIVE) @@ -144,7 +144,7 @@ Example (MsgNetwork layer) this->name.c_str()); /* send the first message through this connection */ send_msg(MsgHello(this->name, "Hello there!"), - salticidae::static_pointer_cast<Conn>(conn)); + static_cast<Conn *>(conn)); } else printf("[%s] Accepted, waiting for greetings.\n", @@ -158,12 +158,12 @@ Example (MsgNetwork layer) } }); } - + salticidae::ConnPool::Conn *create_conn() override { return new Conn(); } - - void on_receive_hello(MsgHello &&msg, MyNet::conn_t 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()); @@ -171,33 +171,33 @@ Example (MsgNetwork layer) send_msg(MsgAck(), conn); } }; - - - void on_receive_ack(MsgAck &&msg, MyNet::conn_t conn) { + + + void on_receive_ack(MsgAck &&msg, MyNet::Conn *conn) { auto net = static_cast<MyNet *>(conn->get_net()); printf("[%s] the peer knows\n", net->name.c_str()); } - + salticidae::EventContext ec; NetAddr alice_addr("127.0.0.1:1234"); NetAddr bob_addr("127.0.0.1:1235"); - + int main() { /* test two nodes */ MyNet alice(ec, "Alice", bob_addr); MyNet bob(ec, "Bob", alice_addr); - + /* message handler could be a normal function */ alice.reg_handler(on_receive_ack); bob.reg_handler(on_receive_ack); - + alice.listen(alice_addr); bob.listen(bob_addr); - + /* first attempt */ alice.connect(bob_addr); bob.connect(alice_addr); - + ec.dispatch(); return 0; } diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index f8156ee..4f17665 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<Conn>; /** The type of callback invoked when connection status is changed. */ - using conn_callback_t = std::function<void(const conn_t &)>; + using conn_callback_t = std::function<void(Conn *)>; /** 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(self()); + 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(self()); + 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(self()); + if (cpool->conn_cb) cpool->conn_cb(this); } }; diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 8ede187..8128119 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<void(const Msg &msg, conn_t conn)>> handler_map; + std::function<void(const Msg &msg, Conn *)>> handler_map; protected: #ifdef SALTICIDAE_MSG_STAT @@ -149,14 +149,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_t conn) { + handler_map[callback_t::msg_type::opcode] = [handler](const Msg &msg, Conn *conn) { handler(typename callback_t::msg_type(msg.get_payload()), - static_pointer_cast<typename callback_t::conn_type::type>(conn)); + static_cast<typename callback_t::conn_type>(conn)); }; } template<typename MsgType> - void send_msg(const MsgType &msg, conn_t 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,8 +323,8 @@ class PeerNetwork: public MsgNetwork<OpcodeType> { } }; - void msg_ping(MsgPing &&msg, conn_t conn); - void msg_pong(MsgPong &&msg, conn_t conn); + 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 start_active_conn(const NetAddr &paddr); @@ -405,7 +405,7 @@ void MsgNetwork<OpcodeType>::Conn::on_read() { SALTICIDAE_LOG_DEBUG("got message %s from %s", std::string(msg).c_str(), std::string(*this).c_str()); - it->second(msg, static_pointer_cast<Conn>(self())); + it->second(msg, this); #ifdef SALTICIDAE_MSG_STAT nrecv++; mn->recv_by_opcode.add(msg); @@ -498,8 +498,7 @@ bool PeerNetwork<O, _, __>::check_new_conn(conn_t conn, uint16_t port) { } template<typename O, O _, O __> -void PeerNetwork<O, _, __>::msg_ping(MsgPing &&msg, conn_t conn_) { - auto conn = static_pointer_cast<Conn>(conn_); +void PeerNetwork<O, _, __>::msg_ping(MsgPing &&msg, const Conn *conn) { uint16_t port = msg.port; SALTICIDAE_LOG_INFO("ping from %s, port %u", std::string(*conn).c_str(), ntohs(port)); if (check_new_conn(conn, port)) return; @@ -508,8 +507,7 @@ void PeerNetwork<O, _, __>::msg_ping(MsgPing &&msg, conn_t conn_) { } template<typename O, O _, O __> -void PeerNetwork<O, _, __>::msg_pong(MsgPong &&msg, conn_t conn_) { - auto conn = static_pointer_cast<Conn>(conn_); +void PeerNetwork<O, _, __>::msg_pong(MsgPong &&msg, const Conn *conn) { auto it = id2peer.find(conn->peer_id); if (it == id2peer.end()) { @@ -573,7 +571,7 @@ bool PeerNetwork<O, _, __>::has_peer(const NetAddr &paddr) const { template<typename OpcodeType> template<typename MsgType> -void MsgNetwork<OpcodeType>::send_msg(const MsgType &_msg, conn_t conn) { +void MsgNetwork<OpcodeType>::send_msg(const MsgType &_msg, Conn *conn) { Msg msg(_msg); bytearray_t msg_data = msg.serialize(); SALTICIDAE_LOG_DEBUG("wrote message %s to %s", diff --git a/test/bench_network.cpp b/test/bench_network.cpp index 0276629..c2406d5 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, conn = self()](int, short) { - net->send_msg(MsgBytes(256), conn); + [net, this](int, short) { + net->send_msg(MsgBytes(256), this); net->ev_period_send.add_with_timeout(0); }); net->ev_period_send.add_with_timeout(0); @@ -122,18 +122,18 @@ struct MyNet: public MsgNetworkByteOp { } void on_teardown() override { auto net = get_net(); + net->ev_period_send = Event(); printf("[%s] Disconnected, retrying.\n", net->name.c_str()); /* try to reconnect to the same address */ net->connect(get_addr()); } }; - using conn_t = salticidae::RcObj<Conn>; salticidae::ConnPool::Conn *create_conn() override { return new Conn(); } - void on_receive_bytes(MsgBytes &&msg, conn_t conn) { + void on_receive_bytes(MsgBytes &&msg, const Conn *conn) { nrecv++; } }; diff --git a/test/test_network.cpp b/test/test_network.cpp index 8c1d0db..49a13c2 100644 --- a/test/test_network.cpp +++ b/test/test_network.cpp @@ -89,7 +89,7 @@ struct MyNet: public MsgNetworkByteOp { reg_handler(salticidae::handler_bind( &MyNet::on_receive_hello, this, _1, _2)); - reg_conn_handler([this](const ConnPool::conn_t &conn) { + reg_conn_handler([this](ConnPool::Conn *conn) { if (conn->get_fd() != -1) { if (conn->get_mode() == ConnPool::Conn::ACTIVE) @@ -98,7 +98,7 @@ struct MyNet: public MsgNetworkByteOp { this->name.c_str()); /* send the first message through this connection */ send_msg(MsgHello(this->name, "Hello there!"), - salticidae::static_pointer_cast<Conn>(conn)); + static_cast<Conn *>(conn)); } else printf("[%s] Accepted, waiting for greetings.\n", @@ -117,7 +117,7 @@ struct MyNet: public MsgNetworkByteOp { return new Conn(); } - void on_receive_hello(MsgHello &&msg, MyNet::conn_t 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()); @@ -126,8 +126,8 @@ struct MyNet: public MsgNetworkByteOp { } }; - -void on_receive_ack(MsgAck &&msg, MyNet::conn_t conn) { + +void on_receive_ack(MsgAck &&msg, MyNet::Conn *conn) { auto net = static_cast<MyNet *>(conn->get_net()); printf("[%s] the peer knows\n", net->name.c_str()); } |