From 86a583982c0ea8bd62be42c1c137fac1c82b706f Mon Sep 17 00:00:00 2001 From: Determinant Date: Mon, 19 Nov 2018 10:45:03 -0500 Subject: use async_call() for PeerNetwork::send_msg --- include/salticidae/network.h | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'include') diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 8bee52d..c74ba7d 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -243,7 +243,7 @@ class ClientNetwork: public MsgNetwork { MsgNet(ec, config) {} template - void send_msg(const MsgType &msg, const NetAddr &addr); + void send_msg(MsgType msg, const NetAddr &addr); }; class PeerNetworkError: public ConnPoolError { @@ -429,7 +429,7 @@ class PeerNetwork: public MsgNetwork { const conn_t get_peer_conn(const NetAddr &paddr) const; using MsgNet::send_msg; template - void send_msg(const MsgType &msg, const NetAddr &paddr); + void send_msg(MsgType msg, const NetAddr &paddr); void listen(NetAddr listen_addr); bool has_peer(const NetAddr &paddr) const; conn_t connect(const NetAddr &addr) = delete; @@ -708,8 +708,14 @@ bool PeerNetwork::has_peer(const NetAddr &paddr) const { template template -void PeerNetwork::send_msg(const MsgType &msg, const NetAddr &addr) { - send_msg(msg, *get_peer_conn(addr)); +void PeerNetwork::send_msg(MsgType msg, const NetAddr &paddr) { + this->disp_tcall->async_call( + [this, msg=std::forward(msg), paddr](ThreadCall::Handle &h) { + auto it = id2peer.find(paddr); + if (it == id2peer.end()) + throw PeerNetworkError("peer does not exist"); + send_msg(msg, it->second->conn); + }); } /* end: functions invoked by the user loop */ @@ -734,15 +740,14 @@ void ClientNetwork::Conn::on_teardown() { template template -void ClientNetwork::send_msg(const MsgType &msg, const NetAddr &addr) { - auto ret = *(static_cast(this->disp_tcall->call( - [this, addr](ThreadCall::Handle &h) { +void ClientNetwork::send_msg(MsgType msg, const NetAddr &addr) { + this->disp_tcall->async_call( + [this, addr, msg=std::forward(msg)](ThreadCall::Handle &h) { auto it = addr2conn.find(addr); if (it == addr2conn.end()) throw PeerNetworkError("client does not exist"); - h.set_result(it->second->conn); - }).get())); - send_msg(msg, *ret); + send_msg(msg, it->second->conn); + }); } template -- cgit v1.2.3-70-g09d2