From 10b23b5b90197566c187c89a64ebfefa31115783 Mon Sep 17 00:00:00 2001 From: Determinant Date: Sat, 21 Jul 2018 17:23:35 -0400 Subject: ... --- include/salticidae/conn.h | 1 + include/salticidae/msg.h | 9 ++++++ include/salticidae/network.h | 68 +++++++++++++++++++++++--------------------- 3 files changed, 45 insertions(+), 33 deletions(-) (limited to 'include') diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index a254505..3742975 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -191,6 +191,7 @@ class ConnPool { int get_fd() const { return fd; } const NetAddr &get_addr() const { return addr; } ConnMode get_mode() const { return mode; } + ConnPool *get_pool() const { return cpool; } SegBuffer &read() { return recv_buffer; } /** Set the buffer size used for send/receive data. */ void set_seg_buff_size(size_t size) { seg_buff_size = size; } diff --git a/include/salticidae/msg.h b/include/salticidae/msg.h index 8a63a50..3a1eebf 100644 --- a/include/salticidae/msg.h +++ b/include/salticidae/msg.h @@ -56,6 +56,15 @@ class MsgBase { public: MsgBase(): magic(0x0), no_payload(true) {} + template::value && + !std::is_same::value>::type> + MsgBase(const MsgType &msg): magic(0x0) { + set_opcode(MsgType::opcode); + set_payload(std::move(msg.serialized)); + } + MsgBase(const MsgBase &other): magic(other.magic), opcode(other.opcode), diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 1e0f560..0d754d6 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -71,6 +71,7 @@ class MsgNetwork: public ConnPool { public callback_traits {}; class Conn: public ConnPool::Conn { + friend MsgNetwork; enum MsgState { HEADER, PAYLOAD @@ -78,7 +79,9 @@ class MsgNetwork: public ConnPool { Msg msg; MsgState msg_state; - MsgNetwork *mn; + MsgNetwork *get_net() { + return static_cast(get_pool()); + } protected: #ifdef SALTICIDAE_MSG_STAT @@ -87,9 +90,7 @@ class MsgNetwork: public ConnPool { #endif public: - friend MsgNetwork; - Conn(MsgNetwork *mn): - msg_state(HEADER), mn(mn) + Conn(): msg_state(HEADER) #ifdef SALTICIDAE_MSG_STAT , nsent(0), nrecv(0) #endif @@ -109,7 +110,6 @@ class MsgNetwork: public ConnPool { }; using conn_t = RcObj; - using msg_callback_t = std::function; #ifdef SALTICIDAE_MSG_STAT class msg_stat_by_opcode_t: public std::unordered_map handler_map; + std::unordered_map< + typename Msg::opcode_t, + std::function> handler_map; protected: #ifdef SALTICIDAE_MSG_STAT @@ -133,10 +134,9 @@ class MsgNetwork: public ConnPool { mutable msg_stat_by_opcode_t recv_by_opcode; #endif - ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); } + ConnPool::conn_t create_conn() override { return (new Conn())->self(); } public: - MsgNetwork(const EventContext &eb, int max_listen_backlog, double conn_server_timeout, @@ -181,12 +181,13 @@ class ClientNetwork: public MsgNetwork { public: class Conn: public MsgNet::Conn { - ClientNetwork *cn; + friend ClientNetwork; + ClientNetwork *get_net() { + return static_cast(ConnPool::Conn::get_pool()); + } public: - Conn(ClientNetwork *cn): - MsgNet::Conn(static_cast(cn)), - cn(cn) {} + Conn() = default; protected: void on_setup() override; @@ -196,7 +197,7 @@ class ClientNetwork: public MsgNetwork { using conn_t = RcObj; protected: - ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); } + ConnPool::conn_t create_conn() override { return (new Conn())->self(); } public: ClientNetwork(const EventContext &eb, @@ -231,16 +232,16 @@ class PeerNetwork: public MsgNetwork { }; class Conn: public MsgNet::Conn { + friend PeerNetwork; NetAddr peer_id; Event ev_timeout; - PeerNetwork *pn; + PeerNetwork *get_net() { + return static_cast(ConnPool::Conn::get_pool()); + } public: - friend PeerNetwork; + Conn() = default; const NetAddr &get_peer() { return peer_id; } - Conn(PeerNetwork *pn): - MsgNet::Conn(static_cast(pn)), - pn(pn) {} protected: void on_close() override { @@ -260,7 +261,6 @@ class PeerNetwork: public MsgNetwork { NetAddr addr; /** the underlying connection, may be invalid when connected = false */ conn_t conn; - PeerNetwork *pn; Event ev_ping_timer; Event ev_retry_timer; bool ping_timer_ok; @@ -268,8 +268,8 @@ class PeerNetwork: public MsgNetwork { bool connected; Peer() = delete; - Peer(NetAddr addr, conn_t conn, PeerNetwork *pn, const EventContext &eb): - addr(addr), conn(conn), pn(pn), + Peer(NetAddr addr, conn_t conn, const EventContext &eb): + addr(addr), conn(conn), ev_ping_timer( Event(eb, -1, 0, std::bind(&Peer::ping_timer, this, _1, _2))), connected(false) {} @@ -329,7 +329,7 @@ class PeerNetwork: public MsgNetwork { void start_active_conn(const NetAddr &paddr); protected: - ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); } + ConnPool::conn_t create_conn() override { return (new Conn())->self(); } virtual double gen_conn_timeout() { return gen_rand_timeout(retry_conn_delay); } @@ -368,7 +368,7 @@ class PeerNetwork: public MsgNetwork { template void MsgNetwork::Conn::on_read() { auto &recv_buffer = read(); - auto conn = static_pointer_cast(self()); + auto mn = get_net(); while (get_fd() != -1) { if (msg_state == Conn::HEADER) @@ -401,7 +401,7 @@ void MsgNetwork::Conn::on_read() { SALTICIDAE_LOG_DEBUG("got message %s from %s", std::string(msg).c_str(), std::string(*this).c_str()); - it->second(msg, conn); + it->second(msg, static_pointer_cast(self())); #ifdef SALTICIDAE_MSG_STAT nrecv++; mn->recv_by_opcode.add(msg); @@ -430,6 +430,7 @@ void PeerNetwork::Peer::reset_conn(conn_t new_conn) { template void PeerNetwork::Conn::on_setup() { + auto pn = get_net(); assert(!ev_timeout); ev_timeout = Event(pn->eb, -1, 0, [this](evutil_socket_t, short) { SALTICIDAE_LOG_INFO("peer ping-pong timeout"); @@ -443,6 +444,7 @@ void PeerNetwork::Conn::on_setup() { template void PeerNetwork::Conn::on_teardown() { + auto pn = get_net(); auto it = pn->id2peer.find(peer_id); if (it == pn->id2peer.end()) return; auto p = it->second.get(); @@ -454,8 +456,7 @@ void PeerNetwork::Conn::on_teardown() { std::string(*this).c_str(), std::string(peer_id).c_str()); p->ev_retry_timer = Event(pn->eb, -1, 0, - [pn = this->pn, - peer_id = this->peer_id](evutil_socket_t, short) { + [pn, peer_id = this->peer_id](evutil_socket_t, short) { pn->start_active_conn(peer_id); }); p->ev_retry_timer.add_with_timeout(pn->gen_conn_timeout()); @@ -545,7 +546,7 @@ void PeerNetwork::add_peer(const NetAddr &addr) { auto it = id2peer.find(addr); if (it != id2peer.end()) throw PeerNetworkError("peer already exists"); - id2peer.insert(std::make_pair(addr, new Peer(addr, nullptr, this, this->eb))); + id2peer.insert(std::make_pair(addr, new Peer(addr, nullptr, this->eb))); peer_list.push_back(addr); start_active_conn(addr); } @@ -567,9 +568,7 @@ bool PeerNetwork::has_peer(const NetAddr &paddr) const { template template void MsgNetwork::send_msg(const MsgType &_msg, conn_t conn) { - Msg msg; - msg.set_opcode(MsgType::opcode); - msg.set_payload(std::move(_msg.serialized)); + Msg msg(_msg); bytearray_t msg_data = msg.serialize(); SALTICIDAE_LOG_DEBUG("wrote message %s to %s", std::string(msg).c_str(), @@ -607,7 +606,8 @@ template void PeerNetwork::Peer::reset_ping_timer() { assert(ev_ping_timer); ev_ping_timer.del(); - ev_ping_timer.add_with_timeout(gen_rand_timeout(pn->ping_period)); + ev_ping_timer.add_with_timeout( + gen_rand_timeout(conn->get_net()->ping_period)); } template @@ -620,6 +620,7 @@ void PeerNetwork::reset_conn_timeout(conn_t conn) { template void PeerNetwork::Peer::send_ping() { + auto pn = conn->get_net(); ping_timer_ok = false; pong_msg_ok = false; pn->reset_conn_timeout(conn); @@ -645,6 +646,7 @@ template void ClientNetwork::Conn::on_setup() { assert(this->get_mode() == Conn::PASSIVE); const auto &addr = this->get_addr(); + auto cn = get_net(); cn->addr2conn.erase(addr); cn->addr2conn.insert( std::make_pair(addr, @@ -654,7 +656,7 @@ void ClientNetwork::Conn::on_setup() { template void ClientNetwork::Conn::on_teardown() { assert(this->get_mode() == Conn::PASSIVE); - cn->addr2conn.erase(this->get_addr()); + get_net()->addr2conn.erase(this->get_addr()); } template -- cgit v1.2.3-70-g09d2