Skip to content

Commit ae24f3d

Browse files
BelonitZivDero
andcommitted
fix: port-match logic (closes #65)
Co-Authored-By: Kirill Andriiashin <5639388+ZivDero@users.noreply.github.com>
1 parent b2ad0ca commit ae24f3d

4 files changed

Lines changed: 14 additions & 11 deletions

File tree

Private

src/Spawner/NetHack.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
ListAddress ListAddress::Array[8] = {};
2626

27-
bool NetHack::PortHack = true;
27+
bool NetHack::RequirePortMatch = false;
2828

2929
u_short Tunnel::Id = 0;
3030
u_long Tunnel::Ip = 0;
@@ -80,7 +80,7 @@ int WINAPI NetHack::RecvFrom(
8080
continue;
8181

8282
// compare port
83-
if (!NetHack::PortHack && src_addr->sin_port != player.Port)
83+
if (NetHack::RequirePortMatch && src_addr->sin_port != player.Port)
8484
continue;
8585

8686
// found it, set this index to source addr

src/Spawner/NetHack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct ListAddress
2727
class NetHack
2828
{
2929
public:
30-
static bool PortHack;
30+
static bool RequirePortMatch;
3131

3232
static int WINAPI SendTo(
3333
int sockfd,

src/Spawner/Spawner.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ bool Spawner::StartScenario(const char* pScenarioName)
245245
}
246246

247247
{ // Added Human Players
248-
NetHack::PortHack = true;
248+
NetHack::RequirePortMatch = false;
249249
const char maxPlayers = Spawner::Config->IsCampaign ? 1 : (char)std::size(Spawner::Config->Players);
250250
for (char playerIndex = 0; playerIndex < maxPlayers; playerIndex++)
251251
{
@@ -274,14 +274,17 @@ bool Spawner::StartScenario(const char* pScenarioName)
274274

275275
if (playerIndex > 0)
276276
{
277+
const auto playerPort = static_cast<u_short>(pPlayer->Port);
278+
const auto listenPort = static_cast<u_short>(Spawner::Config->ListenPort);
279+
277280
pNode->Address.sin_addr.s_addr = playerIndex;
281+
ListAddress::Array[playerIndex - 1].Ip = inet_addr(pPlayer->Ip);
282+
ListAddress::Array[playerIndex - 1].Port = htons(playerPort);
278283

279-
const auto Ip = inet_addr(pPlayer->Ip);
280-
const auto Port = htons((u_short)pPlayer->Port);
281-
ListAddress::Array[playerIndex - 1].Ip = Ip;
282-
ListAddress::Array[playerIndex - 1].Port = Port;
283-
if (Port != (u_short)Spawner::Config->ListenPort)
284-
NetHack::PortHack = false;
284+
// Accumulate this flag across all players in the loop.
285+
// Do not overwrite it on each iteration.
286+
if (playerPort != listenPort)
287+
NetHack::RequirePortMatch = true;
285288
}
286289
}
287290

0 commit comments

Comments
 (0)