From 8c965718031f0de6401aa30579da2e10afbcaf96 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 31 Jul 2018 20:48:37 -0400 Subject: optimize by replacing RcObj with pointers --- README.rst | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'README.rst') diff --git a/README.rst b/README.rst index 1645671..4f8a643 100644 --- a/README.rst +++ b/README.rst @@ -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 #include #include - + #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; - + 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)); + static_cast(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(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; } -- cgit v1.2.3