Skip to content

Commit

Permalink
minor cleanup (dvlnet)
Browse files Browse the repository at this point in the history
  • Loading branch information
pionere committed Nov 19, 2023
1 parent dde3e52 commit 74112bf
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 52 deletions.
17 changes: 2 additions & 15 deletions Source/dvlnet/abstract_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ typedef std::vector<BYTE> buffer_t;
struct SNetTurn {
turn_t turn_id;
buffer_t payload;
SNetTurn()
: turn_id(-1)
, payload({})
{
}
SNetTurn(turn_t t, buffer_t p)
: turn_id(t)
, payload(p)
Expand All @@ -30,13 +25,8 @@ struct SNetTurn {
};

struct SNetMessage {
int sender; // change int to something else in devilution code later
int sender;
buffer_t payload;
SNetMessage()
: sender(-1)
, payload({})
{
}
SNetMessage(int s, buffer_t p)
: sender(s)
, payload(p)
Expand Down Expand Up @@ -66,10 +56,7 @@ class abstract_net {

virtual void make_default_gamename(char (&gamename)[NET_MAX_GAMENAME_LEN + 1]) = 0;
#ifdef ZEROTIER
virtual std::vector<std::string> get_gamelist()
{
return std::vector<std::string>();
}
virtual void get_gamelist(std::vector<std::string>& games) { };
#endif
static std::unique_ptr<abstract_net> make_net(unsigned provider);
};
Expand Down
6 changes: 2 additions & 4 deletions Source/dvlnet/protocol_zt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ bool protocol_zt::send_queued_peer(const endpoint& peer)
delete frame;
peer_list[peer].send_queue.pop_front();
} else {
throw protocol_exception();
return false;
}
}
return true;
Expand Down Expand Up @@ -315,12 +315,10 @@ void protocol_zt::endpoint::to_addr(unsigned char* dest_addr) const

void protocol_zt::make_default_gamename(char (&gamename)[NET_MAX_GAMENAME_LEN + 1])
{
int i;

std::string allowedChars = "abcdefghkopqrstuvwxyz";
std::random_device rd;
std::uniform_int_distribution<int> dist(0, allowedChars.size() - 1);
for (i = 0; i < 5; i++) {
for (int i = 0; i < 5; i++) {
gamename[i] = allowedChars.at(dist(rd));
}
gamename[i] = '\0';
Expand Down
2 changes: 1 addition & 1 deletion Source/dvlnet/tcp_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class tcp_host_client : public base_client {
asio::io_context ioc;
tcp_host_server* local_server = NULL;
turn_t hostTurn;
int serverType;
int serverType; // server_type
};

} //namespace net
Expand Down
52 changes: 23 additions & 29 deletions Source/dvlnet/zt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class zt_client : public base_client {
virtual bool setup_game(_uigamedata* gameData, const char* addrstr, unsigned port, const char* passwd, char (&errorText)[256]);

virtual void make_default_gamename(char (&gamename)[NET_MAX_GAMENAME_LEN + 1]);
virtual std::vector<std::string> get_gamelist();
virtual void get_gamelist(std::vector<std::string>& games);

virtual ~zt_client() = default;

Expand Down Expand Up @@ -70,7 +70,7 @@ template <class P>
bool zt_client<P>::wait_network()
{
// wait for ZeroTier for 5 seconds
for (auto i = 0; i < 500; ++i) {
for (int i = 0; i < 500; ++i) {
if (proto.network_online())
return true;
SDL_Delay(10);
Expand All @@ -88,7 +88,7 @@ template <class P>
bool zt_client<P>::wait_firstpeer(endpoint& peer)
{
// wait for peer for 5 seconds
for (auto i = 0; i < 500; i++) {
for (int i = 0; i < 500; i++) {
poll();
if (game_list.count(gamename)) {
peer = game_list[gamename];
Expand Down Expand Up @@ -119,7 +119,7 @@ bool zt_client<P>::wait_join()
packet* pkt = pktfty.make_out_packet<PT_JOIN_REQUEST>(PLR_BROADCAST, PLR_MASTER, cookie_self);
proto.send(peer, pkt->encrypted_data());
delete pkt;
for (auto i = 0; i < 500; ++i) {
for (int i = 0; i < 500; ++i) {
poll();
if (plr_self != PLR_BROADCAST)
return true; // join successful
Expand Down Expand Up @@ -164,7 +164,7 @@ void zt_client<P>::send_packet(packet& pkt)
plr_t src = pkt.pktSrc();

if (dest == PLR_BROADCAST) {
for (int i = 0; i < MAX_PLRS; i++)
for (plr_t i = 0; i < MAX_PLRS; i++)
if (i != src && peers[i] != NULL)
proto.send(peers[i], pkt.encrypted_data());
} else {
Expand All @@ -180,28 +180,23 @@ void zt_client<P>::send_packet(packet& pkt)
template <class P>
void zt_client<P>::poll()
{
try {
buffer_t pkt_buf;
endpoint sender;
while (proto.recv(sender, pkt_buf)) { // read until kernel buffer is empty?
packet* pkt = pktfty.make_in_packet(pkt_buf);
if (pkt != NULL)
recv_decrypted(*pkt, sender);
else
disconnect_peer(sender);
delete pkt;
}
while (proto.get_disconnected(sender)) {
for (plr_t i = 0; i < MAX_PLRS; i++) {
if (peers[i] == sender) {
disconnect_net(i);
break;
}
buffer_t pkt_buf;
endpoint sender;
while (proto.recv(sender, pkt_buf)) { // read until kernel buffer is empty?
packet* pkt = pktfty.make_in_packet(pkt_buf);
if (pkt != NULL)
recv_decrypted(*pkt, sender);
else
disconnect_peer(sender);
delete pkt;
}
while (proto.get_disconnected(sender)) {
for (plr_t i = 0; i < MAX_PLRS; i++) {
if (peers[i] == sender) {
disconnect_net(i);
break;
}
}
} catch (std::exception& e) {
DoLog(e.what());
return;
}
}

Expand Down Expand Up @@ -293,14 +288,13 @@ void zt_client<P>::recv_decrypted(packet& pkt, endpoint sender)
}

template <class P>
std::vector<std::string> zt_client<P>::get_gamelist()
void zt_client<P>::get_gamelist(std::vector<std::string>& games)
{
send_info_request();
poll();
std::vector<std::string> ret;
for (auto& s : game_list) {
ret.push_back(s.first);
games.push_back(s.first);
}
return ret;
}

template <class P>
Expand Down
4 changes: 2 additions & 2 deletions Source/storm/storm_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ unsigned SNetGetTurnsInTransit()
}

#ifdef ZEROTIER
std::vector<std::string> SNetGetGamelist()
void SNetGetGamelist(std::vector<std::string>& games)
{
return dvlnet_inst->get_gamelist();
return dvlnet_inst->get_gamelist(games);
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Source/storm/storm_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ turn_t SNetLastTurn(unsigned (&status)[MAX_PLRS]);
*/
unsigned SNetGetTurnsInTransit();
#ifdef ZEROTIER
std::vector<std::string> SNetGetGamelist();
void SNetGetGamelist(std::vector<std::string>& games);
#endif

/*
Expand Down

0 comments on commit 74112bf

Please sign in to comment.