From 5cea500b410f8e34e3ad4aeef64fb0e88dce4126 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 24 Jul 2018 20:09:37 -0400 Subject: update example --- test/test_network.cpp | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) (limited to 'test') diff --git a/test/test_network.cpp b/test/test_network.cpp index da72b93..f84ead5 100644 --- a/test/test_network.cpp +++ b/test/test_network.cpp @@ -24,6 +24,7 @@ #include #include +#include #include "salticidae/msg.h" #include "salticidae/event.h" @@ -35,6 +36,8 @@ using salticidae::DataStream; using salticidae::MsgNetwork; using salticidae::htole; using salticidae::letoh; +using std::placeholders::_1; +using std::placeholders::_2; using opcode_t = uint8_t; /** Hello Message. */ @@ -77,9 +80,13 @@ struct MyNet: public MsgNetworkByteOp { MyNet(const salticidae::EventContext &ec, const std::string name, const NetAddr &peer): - MsgNetwork(ec, 10, 1.0, 4096), - name(name), - peer(peer) {} + MsgNetwork(ec, 10, 1.0, 4096), + name(name), + peer(peer) { + /* message handler could be a bound method */ + reg_handler(salticidae::handler_bind( + &MyNet::on_receive_hello, this, _1, _2)); + } struct Conn: public MsgNetworkByteOp::Conn { MyNet *get_net() { return static_cast(get_pool()); } @@ -113,18 +120,17 @@ struct MyNet: public MsgNetworkByteOp { salticidae::ConnPool::Conn *create_conn() override { return new Conn(); } -}; + void on_receive_hello(MsgHello &&msg, conn_t conn) { + printf("[%s] %s says %s\n", + name.c_str(), + msg.name.c_str(), msg.text.c_str()); + /* send acknowledgement */ + send_msg(MsgAck(), conn); + } +}; -void on_receive_hello(MsgHello &&msg, MyNet::conn_t conn) { - auto net = conn->get_net(); - printf("[%s] %s says %s\n", - net->name.c_str(), - msg.name.c_str(), msg.text.c_str()); - /* send acknowledgement */ - net->send_msg(MsgAck(), conn); -} - + void on_receive_ack(MsgAck &&msg, MyNet::conn_t conn) { auto net = conn->get_net(); printf("[%s] the peer knows\n", net->name.c_str()); @@ -139,10 +145,8 @@ int main() { MyNet alice(ec, "Alice", bob_addr); MyNet bob(ec, "Bob", alice_addr); - /* register the message handler */ - alice.reg_handler(on_receive_hello); + /* message handler could be a normal function */ alice.reg_handler(on_receive_ack); - bob.reg_handler(on_receive_hello); bob.reg_handler(on_receive_ack); alice.listen(alice_addr); -- cgit v1.2.3-70-g09d2