From e3c1f9d7eddfa62d5647ab423e590d07c6f9a4d6 Mon Sep 17 00:00:00 2001 From: pionere Date: Sat, 18 Nov 2023 17:19:37 +0100 Subject: [PATCH] move the constants of tcp_server to defs.h --- Source/dvlnet/tcp_server.cpp | 12 ++++++------ Source/dvlnet/tcp_server.h | 8 -------- Source/dvlnet/tcpd_client.cpp | 12 ++++++------ defs.h | 16 ++++++++++++++++ 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Source/dvlnet/tcp_server.cpp b/Source/dvlnet/tcp_server.cpp index d0f3e94e4bd..a26625a3855 100644 --- a/Source/dvlnet/tcp_server.cpp +++ b/Source/dvlnet/tcp_server.cpp @@ -71,7 +71,7 @@ void tcp_server::endpoint_to_string(const scc& con, std::string& addr) asio::error_code err; const auto& ep = con->socket.remote_endpoint(err); assert(!err); - char buf[PORT_LENGTH + 2]; + char buf[NET_TCP_PORT_LENGTH + 2]; snprintf(buf, sizeof(buf), ":%05d", ep.port()); addr = ep.address().to_string(); addr.append(buf); @@ -119,7 +119,7 @@ void tcp_server::handle_recv(const scc& con, const asio::error_code& ec, size_t drop_connection(con); return; } - con->timeout = TIMEOUT_ACTIVE; + con->timeout = NET_TIMEOUT_ACTIVE; con->recv_buffer.resize(bytesRead); con->recv_queue.write(std::move(con->recv_buffer)); con->recv_buffer.resize(frame_queue::MAX_FRAME_SIZE); @@ -241,7 +241,7 @@ void tcp_server::start_accept() acceptor.async_accept(nextcon->socket, std::bind(&tcp_server::handle_accept, this, true, std::placeholders::_1)); } else { nextcon = NULL; - connTimer.expires_after(std::chrono::seconds(WAIT_PENDING)); + connTimer.expires_after(std::chrono::seconds(NET_WAIT_PENDING)); connTimer.async_wait(std::bind(&tcp_server::handle_accept, this, false, std::placeholders::_1)); } } @@ -255,7 +255,7 @@ void tcp_server::handle_accept(bool valid, const asio::error_code& ec) asio::ip::tcp::no_delay option(true); nextcon->socket.set_option(option, err); assert(!err); - nextcon->timeout = TIMEOUT_CONNECT; + nextcon->timeout = NET_TIMEOUT_CONNECT; pending_connections[next_free_queue()] = nextcon; start_recv(nextcon); } @@ -264,7 +264,7 @@ void tcp_server::handle_accept(bool valid, const asio::error_code& ec) void tcp_server::start_timeout() { - connTimer.expires_after(std::chrono::seconds(TIMEOUT_BASE)); + connTimer.expires_after(std::chrono::seconds(NET_TIMEOUT_BASE)); connTimer.async_wait(std::bind(&tcp_server::handle_timeout, this, std::placeholders::_1)); } @@ -316,7 +316,7 @@ void tcp_server::drop_connection(const scc& con) // live connection if (active_connections[pnum] == con) { active_connections[pnum] = NULL; - ghost_connections[pnum] = TIMEOUT_GHOST; + ghost_connections[pnum] = NET_TIMEOUT_GHOST; // notify the other clients packet* pkt = pktfty.make_out_packet(PLR_MASTER, PLR_BROADCAST, pnum); send_packet(*pkt); diff --git a/Source/dvlnet/tcp_server.h b/Source/dvlnet/tcp_server.h index b9190a1a6f5..1ffe25e809f 100644 --- a/Source/dvlnet/tcp_server.h +++ b/Source/dvlnet/tcp_server.h @@ -40,8 +40,6 @@ typedef std::shared_ptr scc; scc make_shared_cc(asio::io_context& ioc); class tcp_server { - friend class tcpd_client; - public: tcp_server(asio::io_context& ioc, packet_factory& pktfty, buffer_t& gameinfo, unsigned serverType); bool setup_server(const char* bindAddr, unsigned short port, char (&errorText)[256]); @@ -53,12 +51,6 @@ class tcp_server { static void connect_socket(asio::ip::tcp::socket& sock, const char* addrstr, unsigned port, asio::io_context& ioc, asio::error_code& ec); private: - static constexpr int TIMEOUT_BASE = 1; // seconds between the timeout-checks - static constexpr int TIMEOUT_CONNECT = 30; // number of iterations before a pending connection timeouts - static constexpr int TIMEOUT_ACTIVE = 60; // number of iterations before an active connection timeouts - 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; asio::io_context& ioc; asio::ip::tcp::acceptor acceptor; asio::steady_timer connTimer; diff --git a/Source/dvlnet/tcpd_client.cpp b/Source/dvlnet/tcpd_client.cpp index 2db51ee6c50..6e2af3cd568 100644 --- a/Source/dvlnet/tcpd_client.cpp +++ b/Source/dvlnet/tcpd_client.cpp @@ -53,7 +53,7 @@ bool tcpd_client::setup_game(_uigamedata* gameData, const char* addrstr, unsigne void tcpd_client::start_timeout() { - connTimer.expires_after(std::chrono::seconds(tcp_server::TIMEOUT_BASE)); + connTimer.expires_after(std::chrono::seconds(NET_TIMEOUT_BASE)); connTimer.async_wait(std::bind(&tcpd_client::handle_timeout, this, std::placeholders::_1)); } @@ -122,7 +122,7 @@ void tcpd_client::recv_connect(packet& pkt) return; std::string addrstr = std::string(pkt.pktConnectAddrBegin(), pkt.pktConnectAddrEnd()); - int offset = addrstr.length() - tcp_server::PORT_LENGTH; + int offset = addrstr.length() - NET_TCP_PORT_LENGTH; int port = SDL_atoi(addrstr.data() + offset); addrstr[offset - 1] = '\0'; @@ -134,7 +134,7 @@ void tcpd_client::recv_connect(packet& pkt) return; } cliCon->pnum = pnum; - cliCon->timeout = tcp_server::TIMEOUT_ACTIVE; + cliCon->timeout = NET_TIMEOUT_ACTIVE; active_connections[pnum] = cliCon; start_recv_conn(cliCon); packet* joinPkt = pktfty.make_out_packet(plr_self, PLR_BROADCAST, cookie_self); @@ -149,7 +149,7 @@ void tcpd_client::start_accept_conn() acceptor.async_accept(nextcon->socket, std::bind(&tcpd_client::handle_accept_conn, this, true, std::placeholders::_1)); } else { nextcon = NULL; - connTimer.expires_after(std::chrono::seconds(tcp_server::WAIT_PENDING)); + connTimer.expires_after(std::chrono::seconds(NET_WAIT_PENDING)); connTimer.async_wait(std::bind(&tcpd_client::handle_accept_conn, this, false, std::placeholders::_1)); } } @@ -164,7 +164,7 @@ void tcpd_client::handle_accept_conn(bool valid, const asio::error_code& ec) asio::ip::tcp::no_delay option(true); nextcon->socket.set_option(option, err); assert(!err); - nextcon->timeout = tcp_server::TIMEOUT_CONNECT; + nextcon->timeout = NET_TIMEOUT_CONNECT; pending_connections[next_free_queue()] = nextcon; start_recv_conn(nextcon); } @@ -184,7 +184,7 @@ void tcpd_client::handle_recv_conn(const scc& con, const asio::error_code& ec, s drop_connection(con); return; } - con->timeout = tcp_server::TIMEOUT_ACTIVE; + con->timeout = NET_TIMEOUT_ACTIVE; con->recv_buffer.resize(bytesRead); con->recv_queue.write(std::move(con->recv_buffer)); con->recv_buffer.resize(frame_queue::MAX_FRAME_SIZE); diff --git a/defs.h b/defs.h index 7d73d40d038..7d969b99714 100644 --- a/defs.h +++ b/defs.h @@ -68,12 +68,28 @@ static_assert(DMAXY % 2 == 0, "DRLG_L4 constructs the dungeon by mirroring a qua #define NET_LARGE_MSG_SIZE 0x8000 // the minimum size of a large message which needs to be compressed #define NET_COMP_MSG_SIZE 256 +// the default port if there is no corresponding entry in the .ini (tcp) #define NET_DEFAULT_PORT 6112 +// the maximum length of the name of an 'instance' #define NET_MAX_GAMENAME_LEN 31 +// the maximum length of the password of an 'instance' #define NET_MAX_PASSWD_LEN 15 +// the maximum length of a text-message to other players #define MAX_SEND_STR_LEN 80 +// the length of the port-string (tcp) +#define NET_TCP_PORT_LENGTH 5 // the number of turns to wait for level-deltas #define NET_JOIN_TIMEOUT 30 +// seconds between the timeout-checks (tcp) +#define NET_TIMEOUT_BASE 1 +// number of iterations before a pending connection timeouts (tcp) +#define NET_TIMEOUT_CONNECT 30 +// number of iterations before an active connection timeouts (tcp) +#define NET_TIMEOUT_ACTIVE 60 +// number of iterations before a ghost connection timeouts (tcp) +#define NET_TIMEOUT_GHOST 30 +// seconds to wait if there is no free connection (tcp) +#define NET_WAIT_PENDING 10 #define DEAD_MULTI 0xFF #define MAXITEMS 127