Skip to content

Commit

Permalink
Don't reinitialize the network driver if it didn't change
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseTG committed Aug 15, 2024
1 parent 173db98 commit 4212e09
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
70 changes: 64 additions & 6 deletions src/libretro/net/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,30 @@ vector<melonDS::AdapterData> MelonDsDs::NetState::GetAdapters() const noexcept
return {};
}

bool operator==(const melonDS::AdapterData& lhs, const melonDS::AdapterData& rhs)
{
return
lhs.Flags == rhs.Flags &&
memcmp(lhs.MAC, rhs.MAC, sizeof(lhs.MAC)) == 0 &&
memcmp(lhs.IP_v4, rhs.IP_v4, sizeof(lhs.IP_v4)) == 0 &&
strncmp(lhs.Description, rhs.Description, sizeof(lhs.Description)) == 0 &&
strncmp(lhs.FriendlyName, rhs.FriendlyName, sizeof(lhs.FriendlyName)) == 0 &&
strncmp(lhs.DeviceName, rhs.DeviceName, sizeof(lhs.DeviceName)) == 0
;
}

void MelonDsDs::NetState::Apply(const CoreConfig& config) noexcept
{
ZoneScopedN(TracyFunction);

NetworkMode lastMode = GetNetworkMode();

switch (config.NetworkMode())
{
#ifdef HAVE_NETWORKING_DIRECT_MODE
case NetworkMode::Direct:
if (!_pcap)
{
// If a previous attempt to load libpcap failed...
{ // If a previous attempt to load libpcap failed...
_pcap = melonDS::LibPCap::New(); // ...then try again.
// (This can happen if the player installed it with RetroArch running in the background)
}
Expand All @@ -160,6 +173,17 @@ void MelonDsDs::NetState::Apply(const CoreConfig& config) noexcept

if (const AdapterData* adapter = SelectNetworkInterface(config.NetworkInterface(), adapters); adapter)
{
if (lastMode == NetworkMode::Direct && _adapter && *adapter == *_adapter)
{ // If we were already using direct-mode, and with the same selected adapter...
retro::debug(
"Already using direct-mode Wi-fi support with adapter {} ({:02x}); no need to reset\n",
adapter->FriendlyName,
fmt::join(adapter->MAC, ":")
);

return;
}

auto driver = _pcap->Open(*adapter, [this](const u8* data, int len)
{
_net.RXEnqueue(data, len);
Expand All @@ -173,6 +197,7 @@ void MelonDsDs::NetState::Apply(const CoreConfig& config) noexcept
fmt::join(adapter->MAC, ":")
);
_net.SetDriver(std::move(driver));
_adapter = *adapter;
return;
}
else
Expand All @@ -194,15 +219,48 @@ void MelonDsDs::NetState::Apply(const CoreConfig& config) noexcept
#endif

case NetworkMode::Indirect:
_net.SetDriver(std::make_unique<Net_Slirp>([this](const u8* data, int len)
if (lastMode != NetworkMode::Indirect)
{
_net.RXEnqueue(data, len);
}));
// If we're not already using indirect mode...
_net.SetDriver(std::make_unique<Net_Slirp>([this](const u8* data, int len)
{
_net.RXEnqueue(data, len);
}));

#ifdef HAVE_NETWORKING_DIRECT_MODE
_adapter = std::nullopt;
#endif

retro::debug("Initialized indirect-mode Wi-fi support\n");
}
else
{
retro::debug("Already using indirect mode, no need to reset network driver\n");
}

retro::debug("Initialized indirect-mode Wi-fi support\n");
break;

case NetworkMode::None:
_net.SetDriver(nullptr);
#ifdef HAVE_NETWORKING_DIRECT_MODE
_adapter = std::nullopt;
#endif
}
}

[[nodiscard]] MelonDsDs::NetworkMode MelonDsDs::NetState::GetNetworkMode() const noexcept
{
#ifdef HAVE_NETWORKING_DIRECT_MODE
if (dynamic_cast<const Net_PCap*>(_net.GetDriver().get()))
{
return NetworkMode::Direct;
}
#endif

if (dynamic_cast<const Net_Slirp*>(_net.GetDriver().get()))
{
return NetworkMode::Indirect;
}

return NetworkMode::None;
}
3 changes: 3 additions & 0 deletions src/libretro/net/net.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#endif

#include "Net.h"
#include "config/types.hpp"
#include "std/span.hpp"

namespace melonDS
Expand All @@ -49,10 +50,12 @@ namespace MelonDsDs
int RecvPacket(melonDS::u8* data) noexcept;
[[nodiscard]] std::vector<melonDS::AdapterData> GetAdapters() const noexcept;
void Apply(const CoreConfig& config) noexcept;
[[nodiscard]] NetworkMode GetNetworkMode() const noexcept;
private:
melonDS::Net _net;
#ifdef HAVE_NETWORKING_DIRECT_MODE
std::optional<melonDS::LibPCap> _pcap;
std::optional<melonDS::AdapterData> _adapter;
#endif
};
}

0 comments on commit 4212e09

Please sign in to comment.