From 74112bf0dc8013819eeadd94d1fd03168e0bd311 Mon Sep 17 00:00:00 2001 From: pionere Date: Sun, 19 Nov 2023 19:09:36 +0100 Subject: [PATCH] minor cleanup (dvlnet) --- Source/dvlnet/abstract_net.h | 17 ++---------- Source/dvlnet/protocol_zt.cpp | 6 ++-- Source/dvlnet/tcp_host.h | 2 +- Source/dvlnet/zt_client.h | 52 ++++++++++++++++------------------- Source/storm/storm_net.cpp | 4 +-- Source/storm/storm_net.h | 2 +- 6 files changed, 31 insertions(+), 52 deletions(-) diff --git a/Source/dvlnet/abstract_net.h b/Source/dvlnet/abstract_net.h index 58f1e59cce2..4d6dc347529 100644 --- a/Source/dvlnet/abstract_net.h +++ b/Source/dvlnet/abstract_net.h @@ -17,11 +17,6 @@ typedef std::vector 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) @@ -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) @@ -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 get_gamelist() - { - return std::vector(); - } + virtual void get_gamelist(std::vector& games) { }; #endif static std::unique_ptr make_net(unsigned provider); }; diff --git a/Source/dvlnet/protocol_zt.cpp b/Source/dvlnet/protocol_zt.cpp index 6da58aaad81..6708da7c729 100644 --- a/Source/dvlnet/protocol_zt.cpp +++ b/Source/dvlnet/protocol_zt.cpp @@ -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; @@ -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 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'; diff --git a/Source/dvlnet/tcp_host.h b/Source/dvlnet/tcp_host.h index 13e5280a9c5..8914a50ddd6 100644 --- a/Source/dvlnet/tcp_host.h +++ b/Source/dvlnet/tcp_host.h @@ -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 diff --git a/Source/dvlnet/zt_client.h b/Source/dvlnet/zt_client.h index 665391dcf02..7e3d19911d0 100644 --- a/Source/dvlnet/zt_client.h +++ b/Source/dvlnet/zt_client.h @@ -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 get_gamelist(); + virtual void get_gamelist(std::vector& games); virtual ~zt_client() = default; @@ -70,7 +70,7 @@ template bool zt_client

::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); @@ -88,7 +88,7 @@ template bool zt_client

::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]; @@ -119,7 +119,7 @@ bool zt_client

::wait_join() packet* pkt = pktfty.make_out_packet(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 @@ -164,7 +164,7 @@ void zt_client

::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 { @@ -180,28 +180,23 @@ void zt_client

::send_packet(packet& pkt) template void zt_client

::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; } } @@ -293,14 +288,13 @@ void zt_client

::recv_decrypted(packet& pkt, endpoint sender) } template -std::vector zt_client

::get_gamelist() +void zt_client

::get_gamelist(std::vector& games) { + send_info_request(); poll(); - std::vector ret; for (auto& s : game_list) { - ret.push_back(s.first); + games.push_back(s.first); } - return ret; } template diff --git a/Source/storm/storm_net.cpp b/Source/storm/storm_net.cpp index a3a30f26ebe..cbb452cd18c 100644 --- a/Source/storm/storm_net.cpp +++ b/Source/storm/storm_net.cpp @@ -137,9 +137,9 @@ unsigned SNetGetTurnsInTransit() } #ifdef ZEROTIER -std::vector SNetGetGamelist() +void SNetGetGamelist(std::vector& games) { - return dvlnet_inst->get_gamelist(); + return dvlnet_inst->get_gamelist(games); } #endif diff --git a/Source/storm/storm_net.h b/Source/storm/storm_net.h index a5795a28e94..dd48a2ccb1f 100644 --- a/Source/storm/storm_net.h +++ b/Source/storm/storm_net.h @@ -90,7 +90,7 @@ turn_t SNetLastTurn(unsigned (&status)[MAX_PLRS]); */ unsigned SNetGetTurnsInTransit(); #ifdef ZEROTIER -std::vector SNetGetGamelist(); +void SNetGetGamelist(std::vector& games); #endif /*