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 ++++++++++++++++++++++++++++++++++-------- src/network.cpp | 58 +++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 101 insertions(+), 13 deletions(-) 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 diff --git a/src/network.cpp b/src/network.cpp index 9fc07fe..869dd40 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -6,6 +6,8 @@ using namespace salticidae; extern "C" { +// MsgNetwork + msgnetwork_config_t *msgnetwork_config_new() { return new msgnetwork_config_t(); } @@ -20,11 +22,11 @@ void msgnetwork_free(const msgnetwork_t *self) { delete self; } bool msgnetwork_send_msg(msgnetwork_t *self, const msg_t *msg, const msgnetwork_conn_t *conn) { - return self->send_msg(*msg, *conn); + return self->_send_msg(*msg, *conn); } msgnetwork_conn_t *msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr) { - return new msgnetwork_t::conn_t(self->connect(*addr)); + return new msgnetwork_conn_t(self->connect(*addr)); } void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr) { @@ -35,7 +37,7 @@ void msgnetwork_reg_handler(msgnetwork_t *self, _opcode_t opcode, msgnetwork_msg_callback_t cb) { self->set_handler(opcode, - [cb](const msgnetwork_t::Msg &msg, const msgnetwork_t::conn_t &conn) { + [cb](const msgnetwork_t::Msg &msg, const msgnetwork_conn_t &conn) { cb(&msg, &conn); }); } @@ -61,6 +63,56 @@ netaddr_t *msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn) { return new netaddr_t((*conn)->get_addr()); } +// PeerNetwork + +peernetwork_config_t *peernetwork_config_new() { + return new peernetwork_config_t(); +} + +void peernetwork_config_free(const peernetwork_config_t *self) { delete self; } + +peernetwork_t *peernetwork_new(const eventcontext_t *ec, const peernetwork_config_t *config) { + return new peernetwork_t(*ec, *config); +} + +void peernetwork_free(const peernetwork_t *self) { delete self; } + +void peernetwork_add_peer(peernetwork_t *self, const netaddr_t *paddr) { + self->add_peer(*paddr); +} + +bool peernetwork_has_peer(const peernetwork_t *self, const netaddr_t *paddr) { + return self->has_peer(*paddr); +} + +const peernetwork_conn_t *get_peer_conn(const peernetwork_t *self, + const netaddr_t *paddr) { + return new peernetwork_conn_t(self->get_peer_conn(*paddr)); +} + +msgnetwork_t *peernetwork_as_msgnetwork(peernetwork_t *self) { return self; } + +msgnetwork_conn_t *msgnetwork_conn_new_from_peernetwork_conn(const peernetwork_conn_t *conn) { + return new msgnetwork_conn_t(*conn); +} + +void peernetwork_send_msg(peernetwork_t *self, + msg_t * _moved_msg, const netaddr_t *paddr) { + self->_send_msg(std::move(*_moved_msg), *paddr); + delete _moved_msg; +} + +void peernetwork_multicast_msg(peernetwork_t *self, + msg_t *_moved_msg, + const netaddr_t *paddrs, size_t npaddrs) { + self->_multicast_msg(std::move(*_moved_msg), paddrs, npaddrs); + delete _moved_msg; +} + +void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr) { + self->listen(*listen_addr); +} + } #endif -- cgit v1.2.3-70-g09d2