Skip to content

Commit

Permalink
split (shared-)client_connection from tcp_server
Browse files Browse the repository at this point in the history
  • Loading branch information
pionere committed Nov 18, 2023
1 parent 0d06caf commit f862b34
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 39 deletions.
12 changes: 6 additions & 6 deletions Source/dvlnet/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
DEVILUTION_BEGIN_NAMESPACE
namespace net {

scc make_shared_cc(asio::io_context& ioc)
{
return std::make_shared<client_connection>(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)
{
Expand Down Expand Up @@ -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<client_connection>(ioc);
}

plr_t tcp_server::next_free_conn()
{
plr_t i;
Expand Down Expand Up @@ -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;
Expand Down
29 changes: 14 additions & 15 deletions Source/dvlnet/tcp_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<client_connection> scc;
scc make_shared_cc(asio::io_context& ioc);

class tcp_server {
friend class tcpd_client;

Expand All @@ -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<client_connection> scc;

asio::io_context& ioc;
asio::ip::tcp::acceptor acceptor;
asio::steady_timer connTimer;
Expand All @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions Source/dvlnet/tcpd_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;

Expand All @@ -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;

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

Expand Down
18 changes: 9 additions & 9 deletions Source/dvlnet/tcpd_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ 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);
plr_t next_free_conn();
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
Expand Down

0 comments on commit f862b34

Please sign in to comment.