aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2018-11-19 10:45:03 -0500
committerDeterminant <[email protected]>2018-11-19 10:45:03 -0500
commit86a583982c0ea8bd62be42c1c137fac1c82b706f (patch)
treec3885bfe163832118dbda2ee858c2b471d8e79e3 /include
parentd088f1826ab704c9b932765458591c052e939bbc (diff)
use async_call() for PeerNetwork::send_msg
Diffstat (limited to 'include')
-rw-r--r--include/salticidae/network.h25
1 files changed, 15 insertions, 10 deletions
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<OpcodeType> {
MsgNet(ec, config) {}
template<typename MsgType>
- 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<OpcodeType> {
const conn_t get_peer_conn(const NetAddr &paddr) const;
using MsgNet::send_msg;
template<typename MsgType>
- 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<O, _, __>::has_peer(const NetAddr &paddr) const {
template<typename O, O _, O __>
template<typename MsgType>
-void PeerNetwork<O, _, __>::send_msg(const MsgType &msg, const NetAddr &addr) {
- send_msg(msg, *get_peer_conn(addr));
+void PeerNetwork<O, _, __>::send_msg(MsgType msg, const NetAddr &paddr) {
+ this->disp_tcall->async_call(
+ [this, msg=std::forward<MsgType>(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<OpcodeType>::Conn::on_teardown() {
template<typename OpcodeType>
template<typename MsgType>
-void ClientNetwork<OpcodeType>::send_msg(const MsgType &msg, const NetAddr &addr) {
- auto ret = *(static_cast<conn_t *>(this->disp_tcall->call(
- [this, addr](ThreadCall::Handle &h) {
+void ClientNetwork<OpcodeType>::send_msg(MsgType msg, const NetAddr &addr) {
+ this->disp_tcall->async_call(
+ [this, addr, msg=std::forward<MsgType>(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<typename O, O OPCODE_PING, O _>