Skip to content

Commit

Permalink
inherit tcpd_client from tcp_client
Browse files Browse the repository at this point in the history
  • Loading branch information
pionere committed Nov 18, 2023
1 parent 0fd8e8f commit 0d06caf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 89 deletions.
25 changes: 17 additions & 8 deletions Source/dvlnet/tcp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -28,17 +24,32 @@ 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<unsigned char*>(&cookie_self),
sizeof(cookie_t));
randombytes_buf(reinterpret_cast<unsigned char*>(&cookie_self), sizeof(cookie_t));
// connect to the server
asio::error_code err;
tcp_server::connect_socket(sock, addrstr, port, ioc, err);
if (err) {
SStrCopy(errorText, err.message().c_str(), lengthof(errorText));
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<PT_JOIN_REQUEST>(PLR_BROADCAST, PLR_MASTER, cookie_self);
send_packet(*pkt);
delete pkt;
Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion Source/dvlnet/tcp_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
63 changes: 4 additions & 59 deletions Source/dvlnet/tcpd_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<unsigned char*>(&cookie_self), sizeof(cookie_t));
// connect to the server
asio::error_code err;
Expand All @@ -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<PT_JOIN_REQUEST>(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));
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
Expand Down Expand Up @@ -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
23 changes: 2 additions & 21 deletions Source/dvlnet/tcpd_client.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
#pragma once
#ifdef TCPIP
#include <asio/ts/internet.hpp>
#include <asio/ts/io_context.hpp>
#include <asio/ts/net.hpp>

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

0 comments on commit 0d06caf

Please sign in to comment.