diff --git a/Source_Files/Network/Pinger.cpp b/Source_Files/Network/Pinger.cpp index e1fdb3f33..d09a0a8cb 100644 --- a/Source_Files/Network/Pinger.cpp +++ b/Source_Files/Network/Pinger.cpp @@ -120,11 +120,11 @@ std::unordered_map 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(); } diff --git a/Source_Files/Network/Pinger.h b/Source_Files/Network/Pinger.h index fcf5025d3..6263090d4 100644 --- a/Source_Files/Network/Pinger.h +++ b/Source_Files/Network/Pinger.h @@ -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 GetResponseTime(uint16_t timeout_ms = 0); - void StoreResponse(uint16_t identifier); + void StoreResponse(uint16_t identifier, const IPaddress& address); private: struct PingAddress diff --git a/Source_Files/Network/network.cpp b/Source_Files/Network/network.cpp index 5deecb0ac..da654505a 100644 --- a/Source_Files/Network/network.cpp +++ b/Source_Files/Network/network.cpp @@ -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); @@ -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; @@ -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; @@ -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) { diff --git a/Source_Files/Network/network_star.h b/Source_Files/Network/network_star.h index 144d77b24..462bea94c 100644 --- a/Source_Files/Network/network_star.h +++ b/Source_Files/Network/network_star.h @@ -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(); diff --git a/Source_Files/Network/network_star_hub.cpp b/Source_Files/Network/network_star_hub.cpp index ae0b97168..8a8061583 100644 --- a/Source_Files/Network/network_star_hub.cpp +++ b/Source_Files/Network/network_star_hub.cpp @@ -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 @@ -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 @@ -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 { @@ -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"); @@ -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++; } } @@ -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) { diff --git a/Source_Files/Network/network_star_spoke.cpp b/Source_Files/Network/network_star_spoke.cpp index 96fcf0aa6..b6897c493 100644 --- a/Source_Files/Network/network_star_spoke.cpp +++ b/Source_Files/Network/network_star_spoke.cpp @@ -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; } @@ -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()