Skip to content

Commit

Permalink
Merge pull request ddnet#7360 from Robyt3/Engine-Host-Lookup-Split
Browse files Browse the repository at this point in the history
Move `CHostLookup` to separate compilation unit and encapsulate member variables
  • Loading branch information
def- authored Oct 22, 2023
2 parents 09978f3 + bea7aea commit 7a2004e
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/engine/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
#include <memory>

#include <base/hash.h>

#include <engine/client.h>
#include <engine/client/checksum.h>
#include <engine/client/friends.h>
#include <engine/client/ghost.h>
#include <engine/client/serverbrowser.h>
#include <engine/client/updater.h>
#include <engine/editor.h>
#include <engine/engine.h>
#include <engine/graphics.h>
#include <engine/shared/config.h>
#include <engine/shared/demo.h>
Expand All @@ -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;
Expand Down Expand Up @@ -221,7 +223,6 @@ class CClient : public IClient, public CDemoPlayer::IListener
};

int m_State;
class CHostLookup m_VersionServeraddr;
} m_VersionInfo;

std::vector<SWarning> m_vWarnings;
Expand Down
15 changes: 0 additions & 15 deletions src/engine/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/engine/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <engine/shared/econ.h>
#include <engine/shared/fifo.h>
#include <engine/shared/filecollection.h>
#include <engine/shared/host_lookup.h>
#include <engine/shared/http.h>
#include <engine/shared/json.h>
#include <engine/shared/masterserver.h>
Expand Down Expand Up @@ -2820,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;
Expand Down
13 changes: 0 additions & 13 deletions src/engine/shared/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@
#include <engine/shared/network.h>
#include <engine/storage.h>

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:
Expand Down
19 changes: 19 additions & 0 deletions src/engine/shared/host_lookup.cpp
Original file line number Diff line number Diff line change
@@ -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 <base/system.h>

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);
}
30 changes: 30 additions & 0 deletions src/engine/shared/host_lookup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* (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 <base/system.h>

#include <engine/shared/jobs.h>

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 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
1 change: 1 addition & 0 deletions src/game/client/components/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <game/generated/client_data.h>

#include <engine/console.h>
#include <engine/engine.h>
#include <engine/graphics.h>
#include <engine/keys.h>
#include <engine/shared/config.h>
Expand Down
27 changes: 14 additions & 13 deletions src/test/jobs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include <gtest/gtest.h>

#include <base/system.h>
#include <engine/engine.h>

#include <engine/shared/host_lookup.h>
#include <engine/shared/jobs.h>

#include <functional>
Expand Down Expand Up @@ -72,8 +73,8 @@ TEST_F(Jobs, LookupHost)
static const int NETTYPE = NETTYPE_ALL;
auto pJob = std::make_shared<CHostLookup>(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)
Expand All @@ -82,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);
}
}

Expand All @@ -96,8 +97,8 @@ TEST_F(Jobs, LookupHostWebsocket)
static const int NETTYPE = NETTYPE_ALL;
auto pJob = std::make_shared<CHostLookup>(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)
Expand All @@ -106,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);
}
}

Expand Down

0 comments on commit 7a2004e

Please sign in to comment.