Skip to content

Commit

Permalink
Remove hacky hardcoded local ip address for hub/spoke mapping & ip hi…
Browse files Browse the repository at this point in the history
…dding
  • Loading branch information
Kolfering committed Oct 28, 2024
1 parent 9ea3cf8 commit 8a6072d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Source_Files/Network/Pinger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ std::unordered_map<uint16_t, uint16_t> Pinger::GetResponseTime(uint16_t timeout_
return results;
}

void Pinger::StoreResponse(uint16_t identifier)
void Pinger::StoreResponse(uint16_t identifier, const IPaddress& address)
{
auto ping_request = _registered_ipv4s.find(identifier);

if (ping_request != _registered_ipv4s.end() && !ping_request->second.pong_received_tick)
if (ping_request != _registered_ipv4s.end() && ping_request->second.ipv4 == address && !ping_request->second.pong_received_tick)
{
ping_request->second.pong_received_tick = machine_tick_count();
}
Expand Down
2 changes: 1 addition & 1 deletion Source_Files/Network/Pinger.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Pinger
uint16_t Register(const IPaddress& ipv4);
void Ping(uint8_t number_of_tries = 1, bool unpinged_addresses_only = false);
std::unordered_map<uint16_t, uint16_t> GetResponseTime(uint16_t timeout_ms = 0);
void StoreResponse(uint16_t identifier);
void StoreResponse(uint16_t identifier, const IPaddress& address);
private:

struct PingAddress
Expand Down
19 changes: 4 additions & 15 deletions Source_Files/Network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,6 @@ void NetInitializeSessionIdentifier(void);
// reason to try to keep that... and I suspect Jason abandoned this separation long ago anyway.
// For now, the only effect I see is a reduction in type-safety. :)
static void NetInitializeTopology(void *game_data, short game_data_size, void *player_data, short player_data_size);
static void NetLocalAddrBlock(IPaddress *address, short socketNumber);

static void NetUpdateTopology(void);
static void NetDistributeTopology(short tag);
Expand Down Expand Up @@ -1833,8 +1832,8 @@ static void NetInitializeTopology(
localPlayerIndex = localPlayerIdentifier = NONE;
topology->player_count = 0;
topology->nextIdentifier = 0;
NetLocalAddrBlock(&topology->server.dspAddress, GAME_PORT);
NetLocalAddrBlock(&topology->server.ddpAddress, GAME_PORT);
topology->server.dspAddress.set_port(GAME_PORT);
topology->server.ddpAddress.set_port(GAME_PORT);
#else
topology->player_count = 1;
localPlayerIndex = localPlayerIdentifier = 0;
Expand All @@ -1847,8 +1846,8 @@ static void NetInitializeTopology(
local_player->net_dead = false;
local_player->stream_id = 0;

NetLocalAddrBlock(&local_player->dspAddress, GAME_PORT);
NetLocalAddrBlock(&local_player->ddpAddress, GAME_PORT);
local_player->dspAddress.set_port(GAME_PORT);
local_player->ddpAddress.set_port(GAME_PORT);

topology->server.dspAddress = local_player->dspAddress;
topology->server.ddpAddress = local_player->ddpAddress;
Expand All @@ -1865,16 +1864,6 @@ static void NetInitializeTopology(
gameSessionIdentifier.clear();
}

static void NetLocalAddrBlock(
IPaddress *address,
short socketNumber)
{
address->set_port(socketNumber);
address->set_address("127.0.0.1");
}



static void NetUpdateTopology(
void)
{
Expand Down
2 changes: 1 addition & 1 deletion Source_Files/Network/network_star.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class InfoTree;

extern void hub_initialize(int32 inStartingTick, int inNumPlayers, const IPaddress* const* inPlayerAddresses, int inLocalPlayerIndex);
extern void hub_cleanup(bool inGraceful, int32 inSmallestPostGameTick);
extern void hub_received_network_packet(UDPpacket& inPacket);
extern void hub_received_network_packet(UDPpacket& inPacket, bool from_local_spoke = false);
extern bool hub_is_active();
extern void DefaultHubPreferences();
extern InfoTree HubPreferencesTree();
Expand Down
34 changes: 14 additions & 20 deletions Source_Files/Network/network_star_hub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,7 @@ static void
send_frame_to_local_spoke(UDPpacket& frame)
{
#ifndef A1_NETWORK_STANDALONE_HUB
static IPaddress spokeLocalAddress = IPaddress("127.0.0.1", 0);
sLocalOutgoingBuffer = frame;
sLocalOutgoingBuffer.address = spokeLocalAddress;
sNeedToSendLocalOutgoingBuffer = true;
#else
// Standalone hub should never call this routine
Expand All @@ -381,6 +379,11 @@ check_send_packet_to_spoke()
#endif // A1_NETWORK_STANDALONE_HUB
}

static int get_player_index_from_address(const IPaddress& address)
{
auto it = sAddressToPlayerIndex.find(address);
return it != sAddressToPlayerIndex.end() ? it->second : NONE;
}


#ifndef INT32_MAX
Expand Down Expand Up @@ -463,14 +466,9 @@ hub_initialize(int32 inStartingTick, int inNumPlayers, const IPaddress* const* i
#endif
thePlayer.mConnected = true;
sConnectedPlayersBitmask |= (((uint32)1) << i);
thePlayer.mAddressKnown = false;
// jkvw: The "real" addresses for spokes won't be known unti we get some UDP traffic
// from them - we'll update as they become known.
if(i == sLocalPlayerIndex) { // jkvw: I don't need this, do I?
thePlayer.mAddress = IPaddress("127.0.0.1", 0);
sAddressToPlayerIndex[thePlayer.mAddress] = i;
thePlayer.mAddressKnown = true;
}
thePlayer.mAddressKnown = i == sLocalPlayerIndex;
// jkvw: The "real" addresses for spokes won't be known unti we get some UDP traffic
// from them - we'll update as they become known.
}
else
{
Expand Down Expand Up @@ -613,7 +611,7 @@ hub_check_for_completion()


void
hub_received_network_packet(UDPpacket& inPacket)
hub_received_network_packet(UDPpacket& inPacket, bool from_local_spoke)
{
logContextNMT("hub processing a received packet");

Expand All @@ -640,10 +638,10 @@ hub_received_network_packet(UDPpacket& inPacket)
{
if (thePacketMagic == kSpokeToHubGameDataPacketV1Magic)
{
AddressToPlayerIndexType::iterator theEntry = sAddressToPlayerIndex.find(inPacket.address);
if (theEntry != sAddressToPlayerIndex.end())
int theSenderIndex = from_local_spoke ? sLocalPlayerIndex : get_player_index_from_address(inPacket.address);

if (theSenderIndex != NONE)
{
int theSenderIndex = theEntry->second;
getNetworkPlayer(theSenderIndex).mStats.errors++;
}
}
Expand All @@ -654,12 +652,8 @@ hub_received_network_packet(UDPpacket& inPacket)
{
case kSpokeToHubGameDataPacketV1Magic:
{
// Find sender
AddressToPlayerIndexType::iterator theEntry = sAddressToPlayerIndex.find(inPacket.address);
if(theEntry == sAddressToPlayerIndex.end())
return;

int theSenderIndex = theEntry->second;
int theSenderIndex = from_local_spoke ? sLocalPlayerIndex : get_player_index_from_address(inPacket.address);
if (theSenderIndex == NONE) return;

if (getNetworkPlayer(theSenderIndex).mConnected)
{
Expand Down
13 changes: 5 additions & 8 deletions Source_Files/Network/network_star_spoke.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,21 @@ getNetworkPlayer(size_t inIndex)
static void
send_frame_to_local_hub(const UDPpacket& frame)
{
static IPaddress hubLocalAddress = IPaddress("127.0.0.1", 0);
sLocalOutgoingBuffer = frame;
sLocalOutgoingBuffer.address = hubLocalAddress;
sNeedToSendLocalOutgoingBuffer = true;
sNeedToSendLocalOutgoingBuffer = true;
}



static inline void
check_send_packet_to_hub()
{
if(sNeedToSendLocalOutgoingBuffer)
if(sNeedToSendLocalOutgoingBuffer)
{
logContextNMT("delivering stored packet to local hub");
hub_received_network_packet(sLocalOutgoingBuffer);
hub_received_network_packet(sLocalOutgoingBuffer, true);
}

sNeedToSendLocalOutgoingBuffer = false;
sNeedToSendLocalOutgoingBuffer = false;
}


Expand Down Expand Up @@ -663,7 +660,7 @@ spoke_received_ping_response(AIStream& ps, const IPaddress& address)
ps >> pingIdentifier;

if (auto pinger = NetGetPinger().lock())
pinger->StoreResponse(pingIdentifier);
pinger->StoreResponse(pingIdentifier, address);
else
logWarningNMT("Received unexpected ping response packet");
} // spoke_received_ping_response()
Expand Down

0 comments on commit 8a6072d

Please sign in to comment.