From a75778995a4e0742f244670e9cc02a56611ccfe0 Mon Sep 17 00:00:00 2001 From: Determinant Date: Wed, 18 Jul 2018 20:13:50 -0400 Subject: improve network interface --- include/salticidae/conn.h | 2 +- include/salticidae/network.h | 56 +++++++++++++++++++++++++++++++++----------- src/conn.cpp | 2 +- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index 4db5f40..0cb721e 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -263,7 +263,7 @@ class ConnPool { /** create an active mode connection to addr */ conn_t create_conn(const NetAddr &addr); /** setup and start listening */ - void init(NetAddr listen_addr); + void listen(NetAddr listen_addr); }; } diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 04679dd..93b5766 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -83,21 +83,32 @@ class MsgNetwork: public ConnPool { protected: mutable msg_stat_by_opcode_t sent_by_opcode; mutable msg_stat_by_opcode_t recv_by_opcode; - uint16_t listen_port; ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); } public: - MsgNetwork(const EventContext &eb): ConnPool(eb) {} + MsgNetwork(const EventContext &eb, + int max_listen_backlog, + double try_conn_delay, + double conn_server_timeout, + size_t seg_buff_size): + ConnPool(eb, max_listen_backlog, + try_conn_delay, + conn_server_timeout, + seg_buff_size) {} + void reg_handler(typename MsgType::opcode_t opcode, msg_callback_t handler); void send_msg(const MsgType &msg, conn_t conn); - void init(NetAddr listen_addr); + using ConnPool::listen; msg_stat_by_opcode_t &get_sent_by_opcode() const { return sent_by_opcode; } msg_stat_by_opcode_t &get_recv_by_opcode() const { return recv_by_opcode; } + conn_t create_conn(const NetAddr &addr) { + return static_pointer_cast(ConnPool::create_conn(addr)); + } }; /** Simple network that handles client-server requests. */ @@ -120,12 +131,24 @@ class ClientNetwork: public MsgNetwork { void on_teardown() override; }; + using conn_t = RcObj; + protected: ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); } public: - ClientNetwork(const EventContext &eb): MsgNet(eb) {} + ClientNetwork(const EventContext &eb, + int max_listen_backlog = 10, + double try_conn_delay = 0, + double conn_server_timeout = 0, + size_t seg_buff_size = 4096): + MsgNet(eb, max_listen_backlog, + try_conn_delay, + conn_server_timeout, + seg_buff_size) {} + void send_msg(const MsgType &msg, const NetAddr &addr); + conn_t create_conn(const NetAddr &addr) = delete; }; class PeerNetworkError: public SalticidaeError { @@ -205,6 +228,7 @@ class PeerNetwork: public MsgNetwork { IdentityMode id_mode; double ping_period; double conn_timeout; + uint16_t listen_port; void msg_ping(const MsgType &msg, ConnPool::conn_t conn); void msg_pong(const MsgType &msg, ConnPool::conn_t conn); @@ -217,10 +241,17 @@ class PeerNetwork: public MsgNetwork { public: PeerNetwork(const EventContext &eb, + int max_listen_backlog = 10, + double try_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(eb), + MsgNet(eb, max_listen_backlog, + try_conn_delay, + conn_server_timeout, + seg_buff_size), id_mode(id_mode), ping_period(ping_period), conn_timeout(conn_timeout) {} @@ -229,10 +260,12 @@ class PeerNetwork: public MsgNetwork { const conn_t get_peer_conn(const NetAddr &paddr) const; void send_msg(const MsgType &msg, const Peer *peer); void send_msg(const MsgType &msg, const NetAddr &paddr); - void init(NetAddr listen_addr); + void listen(NetAddr listen_addr); bool has_peer(const NetAddr &paddr) const; const std::vector &all_peers() const; - using ConnPool::create_conn; + conn_t create_conn(const NetAddr &addr) { + return static_pointer_cast(ConnPool::create_conn(addr)); + } }; template @@ -390,14 +423,9 @@ void PeerNetwork::msg_pong(const MsgType &msg, ConnPool::conn_t conn_) } template -void MsgNetwork::init(NetAddr listen_addr) { +void PeerNetwork::listen(NetAddr listen_addr) { + MsgNet::listen(listen_addr); listen_port = listen_addr.port; - ConnPool::init(listen_addr); -} - -template -void PeerNetwork::init(NetAddr listen_addr) { - MsgNet::init(listen_addr); this->reg_handler(MsgType::OPCODE_PING, std::bind(&PeerNetwork::msg_ping, this, _1, _2)); this->reg_handler(MsgType::OPCODE_PONG, diff --git a/src/conn.cpp b/src/conn.cpp index 62cba8a..052f2ad 100644 --- a/src/conn.cpp +++ b/src/conn.cpp @@ -180,7 +180,7 @@ void ConnPool::Conn::conn_server(evutil_socket_t fd, short events) { } } -void ConnPool::init(NetAddr listen_addr) { +void ConnPool::listen(NetAddr listen_addr) { int one = 1; if ((listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) throw ConnPoolError(std::string("cannot create socket for listening")); -- cgit v1.2.3-70-g09d2