diff options
-rw-r--r-- | TODO.rst | 1 | ||||
-rw-r--r-- | include/salticidae/conn.h | 50 | ||||
-rw-r--r-- | include/salticidae/network.h | 92 | ||||
-rw-r--r-- | test/bench_network.cpp | 2 | ||||
-rw-r--r-- | test/test_network.cpp | 2 | ||||
-rw-r--r-- | test/test_p2p.cpp | 2 |
6 files changed, 105 insertions, 44 deletions
@@ -1,2 +1 @@ -- use config factory class to configure ConnPool, MsgNetwork, PeerNetwork, etc. - stress test for PeerNetwork diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 5f5fef4..53465fc 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -261,17 +261,49 @@ class ConnPool { } public: - ConnPool(const EventContext &ec, - int max_listen_backlog = 10, - double conn_server_timeout = 2, - size_t seg_buff_size = 4096, - size_t nworker = 4): + + class Config { + friend ConnPool; + int _max_listen_backlog; + double _conn_server_timeout; + size_t _seg_buff_size; + size_t _nworker; + + public: + Config(): + _max_listen_backlog(10), + _conn_server_timeout(2), + _seg_buff_size(4096), + _nworker(1) {} + + Config &max_listen_backlog(int x) { + _max_listen_backlog = x; + return *this; + } + + Config &conn_server_timeout(double x) { + _conn_server_timeout = x; + return *this; + } + + Config &seg_buff_size(size_t x) { + _seg_buff_size = x; + return *this; + } + + Config &nworker(size_t x) { + _nworker = std::max((size_t)1, x); + return *this; + } + }; + + ConnPool(const EventContext &ec, const Config &config): ec(ec), - max_listen_backlog(max_listen_backlog), - conn_server_timeout(conn_server_timeout), - seg_buff_size(seg_buff_size), + max_listen_backlog(config._max_listen_backlog), + conn_server_timeout(config._conn_server_timeout), + seg_buff_size(config._seg_buff_size), listen_fd(-1), - nworker(std::max((size_t)1, nworker)) { + nworker(config._nworker) { workers = new Worker[nworker]; user_tcall = new ThreadCall(ec); disp_ec = workers[0].get_ec(); diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 4f934cd..e5f4406 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -134,15 +134,23 @@ class MsgNetwork: public ConnPool { ConnPool::Conn *create_conn() override { return new Conn(); } public: - MsgNetwork(const EventContext &ec, - int max_listen_backlog, - double conn_server_timeout, - size_t seg_buff_size, - size_t burst_size = 1000): - ConnPool(ec, max_listen_backlog, - conn_server_timeout, - seg_buff_size) { - incoming_msgs.reg_handler(ec, [this, burst_size](queue_t &q) { + + class Config: public ConnPool::Config { + friend MsgNetwork; + size_t _burst_size; + + public: + Config(): _burst_size(1000) {} + + Config &burst_size(size_t x) { + _burst_size = x; + return *this; + } + }; + + MsgNetwork(const EventContext &ec, const Config &config): + ConnPool(ec, config) { + incoming_msgs.reg_handler(ec, [this, burst_size=config._burst_size](queue_t &q) { std::pair<Msg, conn_t> item; size_t cnt = 0; while (q.try_dequeue(item)) @@ -229,13 +237,8 @@ class ClientNetwork: public MsgNetwork<OpcodeType> { ConnPool::Conn *create_conn() override { return new Conn(); } public: - ClientNetwork(const EventContext &ec, - int max_listen_backlog = 10, - double conn_server_timeout = 0, - size_t seg_buff_size = 4096): - MsgNet(ec, max_listen_backlog, - conn_server_timeout, - seg_buff_size) {} + ClientNetwork(const EventContext &ec, const Config &config): + MsgNet(ec, config) {} template<typename MsgType> void send_msg(const MsgType &msg, const NetAddr &addr); @@ -368,21 +371,48 @@ class PeerNetwork: public MsgNetwork<OpcodeType> { } public: - PeerNetwork(const EventContext &ec, - int max_listen_backlog = 10, - double retry_conn_delay = 2, - double conn_server_timeout = 2, - size_t seg_buff_size = 4096, - double ping_period = 30, - double conn_timeout = 180, - IdentityMode id_mode = IP_PORT_BASED): - MsgNet(ec, max_listen_backlog, - conn_server_timeout, - seg_buff_size), - id_mode(id_mode), - retry_conn_delay(retry_conn_delay), - ping_period(ping_period), - conn_timeout(conn_timeout) { + + class Config: public MsgNet::Config { + friend PeerNetwork; + double _retry_conn_delay; + double _ping_period; + double _conn_timeout; + IdentityMode _id_mode; + + public: + Config(): + _retry_conn_delay(2), + _ping_period(30), + _conn_timeout(180), + _id_mode(IP_PORT_BASED) {} + + Config &retry_conn_delay(double x) { + _retry_conn_delay = x; + return *this; + } + + Config &ping_period(double x) { + _ping_period = x; + return *this; + } + + Config &conn_timeout(double x) { + _conn_timeout = x; + return *this; + } + + Config &id_mode(IdentityMode x) { + _id_mode = x; + return *this; + } + }; + + PeerNetwork(const EventContext &ec, const Config &config): + MsgNet(ec, config), + id_mode(config._id_mode), + retry_conn_delay(config._retry_conn_delay), + ping_period(config._ping_period), + conn_timeout(config._conn_timeout) { this->reg_handler(generic_bind(&PeerNetwork::msg_ping, this, _1, _2)); this->reg_handler(generic_bind(&PeerNetwork::msg_pong, this, _1, _2)); } diff --git a/test/bench_network.cpp b/test/bench_network.cpp index 40ba17a..d63d377 100644 --- a/test/bench_network.cpp +++ b/test/bench_network.cpp @@ -79,7 +79,7 @@ struct MyNet: public MsgNetworkByteOp { const std::string name, const NetAddr &peer, double stat_timeout = -1): - MsgNetwork<opcode_t>(ec, 10, 1.0, 4096), + MsgNetworkByteOp(ec, MsgNetworkByteOp::Config()), name(name), peer(peer), ev_period_stat(ec, -1, [this, stat_timeout](int, short) { diff --git a/test/test_network.cpp b/test/test_network.cpp index d52b6c2..e439bca 100644 --- a/test/test_network.cpp +++ b/test/test_network.cpp @@ -82,7 +82,7 @@ struct MyNet: public MsgNetworkByteOp { MyNet(const salticidae::EventContext &ec, const std::string name, const NetAddr &peer): - MsgNetwork<uint8_t>(ec, 10, 1.0, 4096), + MsgNetwork<uint8_t>(ec, MsgNetwork::Config()), name(name), peer(peer) { /* message handler could be a bound method */ diff --git a/test/test_p2p.cpp b/test/test_p2p.cpp index 99c28a0..1c37aa6 100644 --- a/test/test_p2p.cpp +++ b/test/test_p2p.cpp @@ -92,7 +92,7 @@ int main(int argc, char **argv) { salticidae::EventContext ec; /* test two nodes */ - MyNet net(ec, 10, 2, 2, 4096, 3, 5); + MyNet net(ec, MyNet::Config().conn_timeout(5).ping_period(2)); try { int i; |