From f862b3449c843c252985829a3cab74d9180ee280 Mon Sep 17 00:00:00 2001 From: pionere Date: Sat, 18 Nov 2023 17:13:08 +0100 Subject: [PATCH] split (shared-)client_connection from tcp_server --- Source/dvlnet/tcp_server.cpp | 12 ++++++------ Source/dvlnet/tcp_server.h | 29 ++++++++++++++--------------- Source/dvlnet/tcpd_client.cpp | 18 +++++++++--------- Source/dvlnet/tcpd_client.h | 18 +++++++++--------- 4 files changed, 38 insertions(+), 39 deletions(-) diff --git a/Source/dvlnet/tcp_server.cpp b/Source/dvlnet/tcp_server.cpp index eebddef1ec2..d0f3e94e4bd 100644 --- a/Source/dvlnet/tcp_server.cpp +++ b/Source/dvlnet/tcp_server.cpp @@ -8,6 +8,11 @@ DEVILUTION_BEGIN_NAMESPACE namespace net { +scc make_shared_cc(asio::io_context& ioc) +{ + return std::make_shared(ioc); +} + tcp_server::tcp_server(asio::io_context& ioc, packet_factory& pf, buffer_t& gameinfo, unsigned srvType) : ioc(ioc), acceptor(ioc), connTimer(ioc), pktfty(pf), game_init_info(gameinfo), serverType(srvType) { @@ -80,11 +85,6 @@ void tcp_server::make_default_gamename(char (&gamename)[NET_MAX_GAMENAME_LEN + 1 } } -tcp_server::scc tcp_server::make_connection(asio::io_context& ioc) -{ - return std::make_shared(ioc); -} - plr_t tcp_server::next_free_conn() { plr_t i; @@ -237,7 +237,7 @@ void tcp_server::start_send(const scc& con, packet& pkt) void tcp_server::start_accept() { if (next_free_queue() != MAX_PLRS) { - nextcon = make_connection(ioc); + nextcon = make_shared_cc(ioc); acceptor.async_accept(nextcon->socket, std::bind(&tcp_server::handle_accept, this, true, std::placeholders::_1)); } else { nextcon = NULL; diff --git a/Source/dvlnet/tcp_server.h b/Source/dvlnet/tcp_server.h index e1c490cca8e..b9190a1a6f5 100644 --- a/Source/dvlnet/tcp_server.h +++ b/Source/dvlnet/tcp_server.h @@ -25,6 +25,20 @@ void throw_exception(Exception const &e) DEVILUTION_BEGIN_NAMESPACE namespace net { +typedef struct client_connection { + frame_queue recv_queue; + buffer_t recv_buffer = buffer_t(frame_queue::MAX_FRAME_SIZE); + plr_t pnum = PLR_BROADCAST; + asio::ip::tcp::socket socket; + int timeout; + client_connection(asio::io_context& ioc) + : socket(ioc) + { + } +} client_connection; +typedef std::shared_ptr scc; +scc make_shared_cc(asio::io_context& ioc); + class tcp_server { friend class tcpd_client; @@ -45,20 +59,6 @@ class tcp_server { static constexpr int TIMEOUT_GHOST = 30; // number of iterations before a ghost connection timeouts static constexpr int WAIT_PENDING = 10; // seconds to wait if there is no free connection static constexpr int PORT_LENGTH = 5; - struct client_connection { - frame_queue recv_queue; - buffer_t recv_buffer = buffer_t(frame_queue::MAX_FRAME_SIZE); - plr_t pnum = PLR_BROADCAST; - asio::ip::tcp::socket socket; - int timeout; - client_connection(asio::io_context& ioc) - : socket(ioc) - { - } - }; - - typedef std::shared_ptr scc; - asio::io_context& ioc; asio::ip::tcp::acceptor acceptor; asio::steady_timer connTimer; @@ -70,7 +70,6 @@ class tcp_server { buffer_t& game_init_info; unsigned serverType; - static scc make_connection(asio::io_context& ioc); static void endpoint_to_string(const scc& con, std::string& addr); plr_t next_free_conn(); diff --git a/Source/dvlnet/tcpd_client.cpp b/Source/dvlnet/tcpd_client.cpp index cc34f060bd7..2db51ee6c50 100644 --- a/Source/dvlnet/tcpd_client.cpp +++ b/Source/dvlnet/tcpd_client.cpp @@ -66,7 +66,7 @@ void tcpd_client::handle_timeout(const asio::error_code& ec) // TODO: add timeout to the server connection? - tcp_server::scc expired_connections[2 * MAX_PLRS] = { }; + scc expired_connections[2 * MAX_PLRS] = { }; n = 0; for (i = 0; i < MAX_PLRS; i++) { if (active_connections[i] != NULL) { @@ -126,7 +126,7 @@ void tcpd_client::recv_connect(packet& pkt) int port = SDL_atoi(addrstr.data() + offset); addrstr[offset - 1] = '\0'; - auto cliCon = tcp_server::make_connection(ioc); + auto cliCon = make_shared_cc(ioc); asio::error_code err; tcp_server::connect_socket(cliCon->socket, addrstr.c_str(), port, ioc, err); if (err) { @@ -145,7 +145,7 @@ void tcpd_client::recv_connect(packet& pkt) void tcpd_client::start_accept_conn() { if (next_free_queue() != MAX_PLRS) { - nextcon = tcp_server::make_connection(ioc); + nextcon = make_shared_cc(ioc); acceptor.async_accept(nextcon->socket, std::bind(&tcpd_client::handle_accept_conn, this, true, std::placeholders::_1)); } else { nextcon = NULL; @@ -171,14 +171,14 @@ void tcpd_client::handle_accept_conn(bool valid, const asio::error_code& ec) start_accept_conn(); } -void tcpd_client::start_recv_conn(const tcp_server::scc& con) +void tcpd_client::start_recv_conn(const scc& con) { con->socket.async_receive(asio::buffer(con->recv_buffer), std::bind(&tcpd_client::handle_recv_conn, this, con, std::placeholders::_1, std::placeholders::_2)); } -void tcpd_client::handle_recv_conn(const tcp_server::scc& con, const asio::error_code& ec, size_t bytesRead) +void tcpd_client::handle_recv_conn(const scc& con, const asio::error_code& ec, size_t bytesRead) { if (ec || bytesRead == 0) { drop_connection(con); @@ -200,7 +200,7 @@ void tcpd_client::handle_recv_conn(const tcp_server::scc& con, const asio::error start_recv_conn(con); } -void tcpd_client::start_send(const tcp_server::scc& con, packet& pkt) +void tcpd_client::start_send(const scc& con, packet& pkt) { const buffer_t* frame = frame_queue::make_frame(pkt.encrypted_data()); auto buf = asio::buffer(*frame); @@ -210,7 +210,7 @@ void tcpd_client::start_send(const tcp_server::scc& con, packet& pkt) }); } -bool tcpd_client::handle_recv_newplr(const tcp_server::scc& con, packet& pkt) +bool tcpd_client::handle_recv_newplr(const scc& con, packet& pkt) { plr_t i, pnum; @@ -237,7 +237,7 @@ bool tcpd_client::handle_recv_newplr(const tcp_server::scc& con, packet& pkt) return true; } -bool tcpd_client::handle_recv_packet(const tcp_server::scc& con, packet& pkt) +bool tcpd_client::handle_recv_packet(const scc& con, packet& pkt) { plr_t pkt_plr = con->pnum; @@ -274,7 +274,7 @@ void tcpd_client::disconnect_net(plr_t pnum) }*/ } -void tcpd_client::drop_connection(const tcp_server::scc& con) +void tcpd_client::drop_connection(const scc& con) { plr_t i, pnum = con->pnum; diff --git a/Source/dvlnet/tcpd_client.h b/Source/dvlnet/tcpd_client.h index 6e347c4faeb..b3024c6e505 100644 --- a/Source/dvlnet/tcpd_client.h +++ b/Source/dvlnet/tcpd_client.h @@ -20,9 +20,9 @@ class tcpd_client : public tcp_client { private: asio::ip::tcp::acceptor acceptor = asio::ip::tcp::acceptor(ioc); asio::steady_timer connTimer = asio::steady_timer(ioc); - tcp_server::scc nextcon; - tcp_server::scc pending_connections[MAX_PLRS] = { }; - tcp_server::scc active_connections[MAX_PLRS] = { }; + scc nextcon; + scc pending_connections[MAX_PLRS] = { }; + scc active_connections[MAX_PLRS] = { }; void start_timeout(); void handle_timeout(const asio::error_code& ec); @@ -30,12 +30,12 @@ class tcpd_client : public tcp_client { plr_t next_free_queue(); void start_accept_conn(); void handle_accept_conn(bool valid, const asio::error_code& ec); - void start_recv_conn(const tcp_server::scc& con); - void handle_recv_conn(const tcp_server::scc& con, const asio::error_code& ec, size_t bytesRead); - void start_send(const tcp_server::scc& con, packet& pkt); - bool handle_recv_newplr(const tcp_server::scc& con, packet& pkt); - bool handle_recv_packet(const tcp_server::scc& con, packet& pkt); - void drop_connection(const tcp_server::scc& con); + void start_recv_conn(const scc& con); + void handle_recv_conn(const scc& con, const asio::error_code& ec, size_t bytesRead); + void start_send(const scc& con, packet& pkt); + bool handle_recv_newplr(const scc& con, packet& pkt); + bool handle_recv_packet(const scc& con, packet& pkt); + void drop_connection(const scc& con); }; } // namespace net