From 0d06cafe5ad0609fa443b10861a6a357aa84e54d Mon Sep 17 00:00:00 2001 From: pionere Date: Sat, 18 Nov 2023 16:30:22 +0100 Subject: [PATCH] inherit tcpd_client from tcp_client --- Source/dvlnet/tcp_client.cpp | 25 +++++++++----- Source/dvlnet/tcp_client.h | 3 +- Source/dvlnet/tcpd_client.cpp | 63 +++-------------------------------- Source/dvlnet/tcpd_client.h | 23 ++----------- 4 files changed, 25 insertions(+), 89 deletions(-) diff --git a/Source/dvlnet/tcp_client.cpp b/Source/dvlnet/tcp_client.cpp index 2d7a1fbd29e..55d6e909b0f 100644 --- a/Source/dvlnet/tcp_client.cpp +++ b/Source/dvlnet/tcp_client.cpp @@ -11,10 +11,6 @@ namespace net { bool tcp_client::setup_game(_uigamedata* gameData, const char* addrstr, unsigned port, const char* passwd, char (&errorText)[256]) { - int i; - constexpr int MS_SLEEP = 10; - constexpr int NUM_SLEEP = 250; - setup_password(passwd); if (gameData != NULL) { @@ -28,8 +24,8 @@ bool tcp_client::setup_game(_uigamedata* gameData, const char* addrstr, unsigned plr_self = PLR_BROADCAST; memset(connected_table, 0, sizeof(connected_table)); - randombytes_buf(reinterpret_cast(&cookie_self), - sizeof(cookie_t)); + randombytes_buf(reinterpret_cast(&cookie_self), sizeof(cookie_t)); + // connect to the server asio::error_code err; tcp_server::connect_socket(sock, addrstr, port, ioc, err); if (err) { @@ -37,8 +33,23 @@ bool tcp_client::setup_game(_uigamedata* gameData, const char* addrstr, unsigned close(); return false; } + start_recv(); + if (join_game()) { + return true; + } + copy_cstr(errorText, "Unable to connect"); + close(); + return false; +} + +bool tcp_client::join_game() +{ + int i; + constexpr int MS_SLEEP = 10; + constexpr int NUM_SLEEP = 250; + packet* pkt = pktfty.make_out_packet(PLR_BROADCAST, PLR_MASTER, cookie_self); send_packet(*pkt); delete pkt; @@ -48,8 +59,6 @@ bool tcp_client::setup_game(_uigamedata* gameData, const char* addrstr, unsigned return true; // join successful SDL_Delay(MS_SLEEP); } - copy_cstr(errorText, "Unable to connect"); - close(); return false; } diff --git a/Source/dvlnet/tcp_client.h b/Source/dvlnet/tcp_client.h index ba5e424d735..7486d32bd08 100644 --- a/Source/dvlnet/tcp_client.h +++ b/Source/dvlnet/tcp_client.h @@ -20,11 +20,12 @@ class tcp_client : public base_client { void make_default_gamename(char (&gamename)[NET_MAX_GAMENAME_LEN + 1]) override; protected: + bool join_game(); void poll() override; void send_packet(packet& pkt) override; void close() override; -private: +protected: frame_queue recv_queue; buffer_t recv_buffer = buffer_t(frame_queue::MAX_FRAME_SIZE); diff --git a/Source/dvlnet/tcpd_client.cpp b/Source/dvlnet/tcpd_client.cpp index a4d8fe1c0e9..cc34f060bd7 100644 --- a/Source/dvlnet/tcpd_client.cpp +++ b/Source/dvlnet/tcpd_client.cpp @@ -11,10 +11,6 @@ namespace net { bool tcpd_client::setup_game(_uigamedata* gameData, const char* addrstr, unsigned port, const char* passwd, char (&errorText)[256]) { - int i; - constexpr int MS_SLEEP = 10; - constexpr int NUM_SLEEP = 250; - setup_password(passwd); if (gameData != NULL) { @@ -26,8 +22,8 @@ bool tcpd_client::setup_game(_uigamedata* gameData, const char* addrstr, unsigne } } - memset(connected_table, 0, sizeof(connected_table)); plr_self = PLR_BROADCAST; + memset(connected_table, 0, sizeof(connected_table)); randombytes_buf(reinterpret_cast(&cookie_self), sizeof(cookie_t)); // connect to the server asio::error_code err; @@ -43,32 +39,18 @@ bool tcpd_client::setup_game(_uigamedata* gameData, const char* addrstr, unsigne close(); return false; } - // start the communication start_recv(); start_accept_conn(); start_timeout(); - packet* pkt = pktfty.make_out_packet(PLR_BROADCAST, PLR_MASTER, cookie_self); - send_packet(*pkt); - delete pkt; - for (i = 0; i < NUM_SLEEP; i++) { - poll(); - if (plr_self != PLR_BROADCAST) - return true; // join successful - SDL_Delay(MS_SLEEP); + if (join_game()) { + return true; } copy_cstr(errorText, "Unable to connect"); close(); return false; } -void tcpd_client::poll() -{ - asio::error_code err; - ioc.poll(err); - assert(!err); -} - void tcpd_client::start_timeout() { connTimer.expires_after(std::chrono::seconds(tcp_server::TIMEOUT_BASE)); @@ -317,33 +299,6 @@ void tcpd_client::drop_connection(const tcp_server::scc& con) con->socket.close(err); } -void tcpd_client::handle_recv(const asio::error_code& ec, size_t bytesRead) -{ - if (ec || bytesRead == 0) { - // error in recv from server - // returning and doing nothing should be the same - // as if all connections to other clients were lost - return; - } - recv_buffer.resize(bytesRead); - recv_queue.write(std::move(recv_buffer)); - recv_buffer.resize(frame_queue::MAX_FRAME_SIZE); - while (recv_queue.packet_ready()) { - packet* pkt = pktfty.make_in_packet(recv_queue.read_packet()); - if (pkt != NULL) - recv_local(*pkt); - delete pkt; - } - start_recv(); -} - -void tcpd_client::start_recv() -{ - sock.async_receive(asio::buffer(recv_buffer), - std::bind(&tcpd_client::handle_recv, this, - std::placeholders::_1, std::placeholders::_2)); -} - void tcpd_client::send_packet(packet& pkt) { if (pkt.pktType() == PT_TURN && pkt.pktTurn() != 0) { @@ -362,12 +317,7 @@ void tcpd_client::send_packet(packet& pkt) }*/ } - const auto* frame = frame_queue::make_frame(pkt.encrypted_data()); - auto buf = asio::buffer(*frame); - asio::async_write(sock, buf, - [frame](const asio::error_code& ec, size_t bytesSent) { - delete frame; - }); + tcp_client::send_packet(pkt); } void tcpd_client::close() @@ -420,11 +370,6 @@ void tcpd_client::close() ioc.restart(); } -void tcpd_client::make_default_gamename(char (&gamename)[NET_MAX_GAMENAME_LEN + 1]) -{ - tcp_server::make_default_gamename(gamename); -} - } // namespace net DEVILUTION_END_NAMESPACE #endif // TCPIP diff --git a/Source/dvlnet/tcpd_client.h b/Source/dvlnet/tcpd_client.h index 9a841705d6f..6e347c4faeb 100644 --- a/Source/dvlnet/tcpd_client.h +++ b/Source/dvlnet/tcpd_client.h @@ -1,39 +1,23 @@ #pragma once #ifdef TCPIP -#include -#include -#include - -#include "dvlnet/frame_queue.h" -#include "dvlnet/base_client.h" -#include "dvlnet/tcp_server.h" +#include "tcp_client.h" DEVILUTION_BEGIN_NAMESPACE namespace net { -class tcpd_client : public base_client { +class tcpd_client : public tcp_client { public: bool setup_game(_uigamedata* gameData, const char* addrstr, unsigned port, const char* passwd, char (&errorText)[256]) override; ~tcpd_client() override = default; - void make_default_gamename(char (&gamename)[NET_MAX_GAMENAME_LEN + 1]) override; - protected: - void poll() override; void send_packet(packet& pkt) override; void recv_connect(packet& pkt) override; void disconnect_net(plr_t pnum) override; void close() override; private: - frame_queue recv_queue; - buffer_t recv_buffer = buffer_t(frame_queue::MAX_FRAME_SIZE); - - asio::io_context ioc; - asio::ip::tcp::socket sock = asio::ip::tcp::socket(ioc); - tcp_server* local_server = NULL; - asio::ip::tcp::acceptor acceptor = asio::ip::tcp::acceptor(ioc); asio::steady_timer connTimer = asio::steady_timer(ioc); tcp_server::scc nextcon; @@ -52,9 +36,6 @@ class tcpd_client : public base_client { 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 handle_recv(const asio::error_code& ec, size_t bytesRead); - void start_recv(); }; } // namespace net