From f8af7542c6c7dee7a214504351ce86d37e8ac8c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 22 Oct 2023 12:30:23 +0200 Subject: [PATCH 1/2] Move `CHostLookup` to separate compilation unit The host lookup job and the engine interface are independent so they are moved to separate files. The include of `engine.h` in `client.h` is therefore unnecessary and other includes also had to be adjusted because of this. The variable `m_VersionServeraddr` is unused and therefore removed. The host lookup job is currently not used on the client-side. --- CMakeLists.txt | 2 ++ src/engine/client/client.h | 5 +++-- src/engine/engine.h | 15 --------------- src/engine/server/server.cpp | 1 + src/engine/shared/engine.cpp | 13 ------------- src/engine/shared/host_lookup.cpp | 19 +++++++++++++++++++ src/engine/shared/host_lookup.h | 25 +++++++++++++++++++++++++ src/game/client/components/console.cpp | 1 + src/test/jobs.cpp | 3 ++- 9 files changed, 53 insertions(+), 31 deletions(-) create mode 100644 src/engine/shared/host_lookup.cpp create mode 100644 src/engine/shared/host_lookup.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 043b5d51f3f..2aef79299bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1912,6 +1912,8 @@ set_src(ENGINE_SHARED GLOB_RECURSE src/engine/shared filecollection.cpp filecollection.h global_uuid_manager.cpp + host_lookup.cpp + host_lookup.h http.cpp http.h huffman.cpp diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 7f5cbdd84c5..894f5fc8e9b 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -7,6 +7,7 @@ #include #include + #include #include #include @@ -14,7 +15,6 @@ #include #include #include -#include #include #include #include @@ -32,10 +32,12 @@ class CMsgPacker; class CUnpacker; class IConfigManager; class IDiscord; +class IEngine; class IEngineInput; class IEngineMap; class IEngineSound; class IFriends; +class ILogger; class ISteam; class IStorage; class IUpdater; @@ -221,7 +223,6 @@ class CClient : public IClient, public CDemoPlayer::IListener }; int m_State; - class CHostLookup m_VersionServeraddr; } m_VersionInfo; std::vector m_vWarnings; diff --git a/src/engine/engine.h b/src/engine/engine.h index 1f1c959571f..328cde7d055 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -9,21 +9,6 @@ class CFutureLogger; class ILogger; -class CHostLookup : public IJob -{ -private: - void Run() override; - -public: - CHostLookup(); - CHostLookup(const char *pHostname, int Nettype); - - int m_Result; - char m_aHostname[128]; - int m_Nettype; - NETADDR m_Addr; -}; - class IEngine : public IInterface { MACRO_INTERFACE("engine", 0) diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index f0d43b38a90..ed9c776fa29 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include diff --git a/src/engine/shared/engine.cpp b/src/engine/shared/engine.cpp index 395cf78531e..259c28523fc 100644 --- a/src/engine/shared/engine.cpp +++ b/src/engine/shared/engine.cpp @@ -10,19 +10,6 @@ #include #include -CHostLookup::CHostLookup() = default; - -CHostLookup::CHostLookup(const char *pHostname, int Nettype) -{ - str_copy(m_aHostname, pHostname); - m_Nettype = Nettype; -} - -void CHostLookup::Run() -{ - m_Result = net_host_lookup(m_aHostname, &m_Addr, m_Nettype); -} - class CEngine : public IEngine { public: diff --git a/src/engine/shared/host_lookup.cpp b/src/engine/shared/host_lookup.cpp new file mode 100644 index 00000000000..ea79b97f0b7 --- /dev/null +++ b/src/engine/shared/host_lookup.cpp @@ -0,0 +1,19 @@ +/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ +/* If you are missing that file, acquire a complete release at teeworlds.com. */ + +#include "host_lookup.h" + +#include + +CHostLookup::CHostLookup() = default; + +CHostLookup::CHostLookup(const char *pHostname, int Nettype) +{ + str_copy(m_aHostname, pHostname); + m_Nettype = Nettype; +} + +void CHostLookup::Run() +{ + m_Result = net_host_lookup(m_aHostname, &m_Addr, m_Nettype); +} diff --git a/src/engine/shared/host_lookup.h b/src/engine/shared/host_lookup.h new file mode 100644 index 00000000000..1314183931a --- /dev/null +++ b/src/engine/shared/host_lookup.h @@ -0,0 +1,25 @@ +/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ +/* If you are missing that file, acquire a complete release at teeworlds.com. */ +#ifndef ENGINE_SHARED_HOST_LOOKUP_H +#define ENGINE_SHARED_HOST_LOOKUP_H + +#include + +#include + +class CHostLookup : public IJob +{ +private: + void Run() override; + +public: + CHostLookup(); + CHostLookup(const char *pHostname, int Nettype); + + int m_Result; + char m_aHostname[128]; + int m_Nettype; + NETADDR m_Addr; +}; + +#endif diff --git a/src/game/client/components/console.cpp b/src/game/client/components/console.cpp index fd097e05499..c6a6b90c0bb 100644 --- a/src/game/client/components/console.cpp +++ b/src/game/client/components/console.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include diff --git a/src/test/jobs.cpp b/src/test/jobs.cpp index 9d7b4c32248..2f74686bd0e 100644 --- a/src/test/jobs.cpp +++ b/src/test/jobs.cpp @@ -2,7 +2,8 @@ #include #include -#include + +#include #include #include From bea7aea43199342e931b8768fd7297a84a832bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 22 Oct 2023 13:29:25 +0200 Subject: [PATCH 2/2] Encapsulate `CHostLookup` member variables The host lookup job is currently only used for the DNSBL lookup on the server-side and in tests. --- src/engine/server/server.cpp | 2 +- src/engine/shared/host_lookup.h | 13 +++++++++---- src/test/jobs.cpp | 24 ++++++++++++------------ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index ed9c776fa29..d6008d6d072 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -2821,7 +2821,7 @@ int CServer::Run() else if(m_aClients[ClientID].m_DnsblState == CClient::DNSBL_STATE_PENDING && m_aClients[ClientID].m_pDnsblLookup->Status() == IJob::STATE_DONE) { - if(m_aClients[ClientID].m_pDnsblLookup->m_Result != 0) + if(m_aClients[ClientID].m_pDnsblLookup->Result() != 0) { // entry not found -> whitelisted m_aClients[ClientID].m_DnsblState = CClient::DNSBL_STATE_WHITELISTED; diff --git a/src/engine/shared/host_lookup.h b/src/engine/shared/host_lookup.h index 1314183931a..940be405b8d 100644 --- a/src/engine/shared/host_lookup.h +++ b/src/engine/shared/host_lookup.h @@ -10,16 +10,21 @@ class CHostLookup : public IJob { private: + int m_Result; + char m_aHostname[128]; + int m_Nettype; + NETADDR m_Addr; + void Run() override; public: CHostLookup(); CHostLookup(const char *pHostname, int Nettype); - int m_Result; - char m_aHostname[128]; - int m_Nettype; - NETADDR m_Addr; + int Result() const { return m_Result; } + const char *Hostname() const { return m_aHostname; } + int Nettype() const { return m_Nettype; } + NETADDR Addr() const { return m_Addr; } }; #endif diff --git a/src/test/jobs.cpp b/src/test/jobs.cpp index 2f74686bd0e..9c8b49fd0bd 100644 --- a/src/test/jobs.cpp +++ b/src/test/jobs.cpp @@ -73,8 +73,8 @@ TEST_F(Jobs, LookupHost) static const int NETTYPE = NETTYPE_ALL; auto pJob = std::make_shared(HOST, NETTYPE); - EXPECT_STREQ(pJob->m_aHostname, HOST); - EXPECT_EQ(pJob->m_Nettype, NETTYPE); + EXPECT_STREQ(pJob->Hostname(), HOST); + EXPECT_EQ(pJob->Nettype(), NETTYPE); Add(pJob); while(pJob->Status() != IJob::STATE_DONE) @@ -83,11 +83,11 @@ TEST_F(Jobs, LookupHost) thread_yield(); } - EXPECT_STREQ(pJob->m_aHostname, HOST); - EXPECT_EQ(pJob->m_Nettype, NETTYPE); - if(pJob->m_Result == 0) + EXPECT_STREQ(pJob->Hostname(), HOST); + EXPECT_EQ(pJob->Nettype(), NETTYPE); + if(pJob->Result() == 0) { - EXPECT_EQ(pJob->m_Addr.type & NETTYPE, pJob->m_Addr.type); + EXPECT_EQ(pJob->Addr().type & NETTYPE, pJob->Addr().type); } } @@ -97,8 +97,8 @@ TEST_F(Jobs, LookupHostWebsocket) static const int NETTYPE = NETTYPE_ALL; auto pJob = std::make_shared(HOST, NETTYPE); - EXPECT_STREQ(pJob->m_aHostname, HOST); - EXPECT_EQ(pJob->m_Nettype, NETTYPE); + EXPECT_STREQ(pJob->Hostname(), HOST); + EXPECT_EQ(pJob->Nettype(), NETTYPE); Add(pJob); while(pJob->Status() != IJob::STATE_DONE) @@ -107,11 +107,11 @@ TEST_F(Jobs, LookupHostWebsocket) thread_yield(); } - EXPECT_STREQ(pJob->m_aHostname, HOST); - EXPECT_EQ(pJob->m_Nettype, NETTYPE); - if(pJob->m_Result == 0) + EXPECT_STREQ(pJob->Hostname(), HOST); + EXPECT_EQ(pJob->Nettype(), NETTYPE); + if(pJob->Result() == 0) { - EXPECT_EQ(pJob->m_Addr.type & NETTYPE_WEBSOCKET_IPV4, pJob->m_Addr.type); + EXPECT_EQ(pJob->Addr().type & NETTYPE_WEBSOCKET_IPV4, pJob->Addr().type); } }