mirror of
https://git.citron-emu.org/citron/emulator
synced 2026-01-25 12:13:27 +00:00
fix(multiplayer): Race Condition during Packet Discovery
This commit is contained in:
@@ -77,6 +77,8 @@ void LANDiscovery::SetState(State new_state) {
|
||||
}
|
||||
|
||||
Result LANDiscovery::GetNetworkInfo(NetworkInfo& out_network) const {
|
||||
// Lock must be held while copying to prevent the Network thread from corrupting the copy
|
||||
std::scoped_lock lock{packet_mutex};
|
||||
if (state == State::AccessPointCreated || state == State::StationConnected) {
|
||||
std::memcpy(&out_network, &network_info, sizeof(network_info));
|
||||
return ResultSuccess;
|
||||
@@ -117,7 +119,7 @@ Result LANDiscovery::Scan(std::span<NetworkInfo> out_networks, s16& out_count,
|
||||
}
|
||||
|
||||
LOG_INFO(Service_LDN, "Waiting for scan replies");
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
std::scoped_lock lock{packet_mutex};
|
||||
for (const auto& [key, info] : scan_results) {
|
||||
@@ -567,10 +569,19 @@ void LANDiscovery::ReceivePacket(const Network::LDNPacket& packet) {
|
||||
}
|
||||
case Network::LDNPacketType::SyncNetwork: {
|
||||
if (state == State::StationOpened || state == State::StationConnected) {
|
||||
LOG_INFO(Frontend, "SyncNetwork packet received!");
|
||||
if (packet.data.size() < sizeof(NetworkInfo)) {
|
||||
LOG_ERROR(Service_LDN, "SyncNetwork packet too small!");
|
||||
break;
|
||||
}
|
||||
|
||||
NetworkInfo info{};
|
||||
std::memcpy(&info, packet.data.data(), sizeof(NetworkInfo));
|
||||
|
||||
if (info.ldn.node_count == 0) {
|
||||
LOG_ERROR(Service_LDN, "SyncNetwork reported 0 nodes! Ignoring to prevent SDK crash.");
|
||||
break;
|
||||
}
|
||||
|
||||
OnSyncNetwork(info);
|
||||
} else {
|
||||
LOG_INFO(Frontend, "SyncNetwork packet received but in wrong State!");
|
||||
|
||||
@@ -112,7 +112,7 @@ protected:
|
||||
static constexpr Ssid fake_ssid{"CitronFakeSsidForLdn"};
|
||||
|
||||
bool inited{};
|
||||
std::mutex packet_mutex;
|
||||
mutable std::mutex packet_mutex;
|
||||
std::array<LanStation, StationCountMax> stations;
|
||||
std::array<NodeLatestUpdate, NodeCountMax> node_changes{};
|
||||
std::array<u8, NodeCountMax> node_last_states{};
|
||||
|
||||
Reference in New Issue
Block a user