diff options
Diffstat (limited to 'README.rst')
-rw-r--r-- | README.rst | 46 |
1 files changed, 23 insertions, 23 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; } |