From f7050c145e81a70be85bb0287044abdfa7ba444f Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 5 Jun 2019 15:20:23 -0400 Subject: finish PeerNetwork C APIs --- include/salticidae/network.h | 56 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 10 deletions(-) (limited to 'include') diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 60f08e6..99c34ee 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -200,9 +200,7 @@ class MsgNetwork: public ConnPool { template bool send_msg(MsgType &&msg, const conn_t &conn); -#ifdef SALTICIDAE_CBINDINGS - inline bool send_msg(const Msg &msg, const conn_t &conn); -#endif + inline bool _send_msg(const Msg &msg, const conn_t &conn); using ConnPool::listen; conn_t connect(const NetAddr &addr) { return static_pointer_cast(ConnPool::connect(addr)); @@ -430,14 +428,18 @@ class PeerNetwork: public MsgNetwork { ~PeerNetwork() { this->stop_workers(); } void add_peer(const NetAddr &paddr); + bool has_peer(const NetAddr &paddr) const; const conn_t get_peer_conn(const NetAddr &paddr) const; using MsgNet::send_msg; template void send_msg(MsgType &&msg, const NetAddr &paddr); + inline void _send_msg(Msg &&msg, const NetAddr &paddr); + template void multicast_msg(MsgType &&msg, const std::vector &paddrs); + inline void _multicast_msg(Msg &&msg, const NetAddr *paddrs, size_t npaddrs); + void listen(NetAddr listen_addr); - bool has_peer(const NetAddr &paddr) const; conn_t connect(const NetAddr &addr) = delete; }; @@ -481,11 +483,11 @@ template template bool MsgNetwork::send_msg(MsgType &&_msg, const conn_t &conn) { Msg msg(std::forward(_msg)); - return send_msg(msg, conn); + return _send_msg(msg, conn); } template -inline bool MsgNetwork::send_msg(const Msg &msg, const conn_t &conn) { +inline bool MsgNetwork::_send_msg(const Msg &msg, const conn_t &conn) { bytearray_t msg_data = msg.serialize(); SALTICIDAE_LOG_DEBUG("wrote message %s to %s", std::string(msg).c_str(), @@ -719,8 +721,13 @@ bool PeerNetwork::has_peer(const NetAddr &paddr) const { template template void PeerNetwork::send_msg(MsgType &&msg, const NetAddr &paddr) { + return _send_msg(MsgType(msg), paddr); +} + +template +void PeerNetwork::_send_msg(Msg &&msg, const NetAddr &paddr) { this->disp_tcall->async_call( - [this, msg=std::forward(msg), paddr](ThreadCall::Handle &) { + [this, msg=std::move(msg), paddr](ThreadCall::Handle &) { auto it = id2peer.find(paddr); if (it == id2peer.end()) throw PeerNetworkError("peer does not exist"); @@ -731,11 +738,16 @@ void PeerNetwork::send_msg(MsgType &&msg, const NetAddr &paddr) { template template void PeerNetwork::multicast_msg(MsgType &&msg, const std::vector &paddrs) { + return _multicast_msg(MsgType(msg), &paddrs[0], paddrs.size()); +} + +template +void PeerNetwork::_multicast_msg(Msg &&msg, const NetAddr *paddrs, size_t npaddrs) { this->disp_tcall->async_call( - [this, msg=std::forward(msg), paddrs](ThreadCall::Handle &) { - for (auto &paddr: paddrs) + [this, msg=std::move(msg), paddrs, npaddrs](ThreadCall::Handle &) { + for (size_t i = 0; i < npaddrs; i++) { - auto it = id2peer.find(paddr); + auto it = id2peer.find(paddrs[i]); if (it == id2peer.end()) throw PeerNetworkError("peer does not exist"); send_msg(std::move(msg), it->second->conn); @@ -786,6 +798,10 @@ const O PeerNetwork::MsgPong::opcode = OPCODE_PONG; using msgnetwork_t = salticidae::MsgNetwork<_opcode_t>; using msgnetwork_config_t = msgnetwork_t::Config; using msgnetwork_conn_t = msgnetwork_t::conn_t; + +using peernetwork_t = salticidae::PeerNetwork<_opcode_t>; +using peernetwork_config_t = peernetwork_t::Config; +using peernetwork_conn_t = peernetwork_t::conn_t; #endif #else @@ -794,6 +810,10 @@ using msgnetwork_conn_t = msgnetwork_t::conn_t; typedef struct msgnetwork_t msgnetwork_t; typedef struct msgnetwork_config_t msgnetwork_config_t; typedef struct msgnetwork_conn_t msgnetwork_conn_t; + +typedef struct peernetwork_t peernetwork_t; +typedef struct peernetwork_config_t peernetwork_config_t; +typedef struct peernetwork_conn_t peernetwork_conn_t; #endif #endif @@ -811,6 +831,7 @@ extern "C" { void salticidae_injected_msg_callback(const msg_t *msg, msgnetwork_conn_t *conn); +// MsgNetwork msgnetwork_config_t *msgnetwork_config_new(); void msgnetwork_config_free(const msgnetwork_config_t *self); @@ -831,6 +852,21 @@ msgnetwork_t *msgnetwork_conn_get_net(const msgnetwork_conn_t *conn); msgnetwork_conn_mode_t msgnetwork_conn_get_mode(const msgnetwork_conn_t *conn); netaddr_t *msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn); +// PeerNetwork + +peernetwork_config_t *peernetwork_config_new(); +void peernetwork_config_free(const peernetwork_config_t *self); +peernetwork_t *peernetwork_new(const eventcontext_t *ec, const peernetwork_config_t *config); +void peernetwork_free(const peernetwork_t *self); +void peernetwork_add_peer(peernetwork_t *self, const netaddr_t *paddr); +bool peernetwork_has_peer(const peernetwork_t *self, const netaddr_t *paddr); +const peernetwork_conn_t *get_peer_conn(const peernetwork_t *self, const netaddr_t *paddr); +msgnetwork_t *peernetwork_as_msgnetwork(peernetwork_t *self); +msgnetwork_conn_t *peernetwork_conn_as_msgnetwork_conn(peernetwork_conn_t *self); +void peernetwork_send_msg(peernetwork_t *self, msg_t * _moved_msg, const netaddr_t *paddr); +void peernetwork_multicast_msg(peernetwork_t *self, msg_t *_moved_msg, const netaddr_t *paddrs, size_t npaddrs); +void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr); + #ifdef __cplusplus } #endif -- cgit v1.2.3-70-g09d2