Skip to content

Commit

Permalink
move the constants of tcp_server to defs.h
Browse files Browse the repository at this point in the history
  • Loading branch information
pionere committed Nov 18, 2023
1 parent f862b34 commit e3c1f9d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
12 changes: 6 additions & 6 deletions Source/dvlnet/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}
}
Expand All @@ -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);
}
Expand All @@ -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));
}

Expand Down Expand Up @@ -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<PT_DISCONNECT>(PLR_MASTER, PLR_BROADCAST, pnum);
send_packet(*pkt);
Expand Down
8 changes: 0 additions & 8 deletions Source/dvlnet/tcp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ typedef std::shared_ptr<client_connection> 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]);
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions Source/dvlnet/tcpd_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -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';

Expand All @@ -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<PT_JOIN_REQUEST>(plr_self, PLR_BROADCAST, cookie_self);
Expand All @@ -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));
}
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e3c1f9d

Please sign in to comment.