Refactor network implementations to be more reusable and less buggy (#2107)

encapsulate network interfaces
This commit is contained in:
Jesse Talavera
2024-08-01 16:02:45 -04:00
committed by GitHub
parent c6bf5d5181
commit 327ce45124
20 changed files with 715 additions and 439 deletions

View File

@ -79,6 +79,9 @@
#include "CLI.h"
#include "Net_PCap.h"
#include "Net_Slirp.h"
using namespace melonDS;
QString* systemThemeName;
@ -92,6 +95,38 @@ EmuInstance* emuInstances[kMaxEmuInstances];
CameraManager* camManager[2];
bool camStarted[2];
LocalMP localMp;
std::optional<LibPCap> pcap;
Net net;
void NetInit()
{
Config::Table cfg = Config::GetGlobalTable();
if (cfg.GetBool("LAN.DirectMode"))
{
if (!pcap)
pcap = LibPCap::New();
if (pcap)
{
std::string devicename = cfg.GetString("LAN.Device");
std::unique_ptr<Net_PCap> netPcap = pcap->Open(devicename, [](const u8* data, int len) {
net.RXEnqueue(data, len);
});
if (netPcap)
{
net.SetDriver(std::move(netPcap));
}
}
}
else
{
net.SetDriver(std::make_unique<Net_Slirp>([](const u8* data, int len) {
net.RXEnqueue(data, len);
}));
}
}
bool createEmuInstance()
@ -273,13 +308,8 @@ int main(int argc, char** argv)
}
}
LocalMP::Init();
{
Config::Table cfg = Config::GetGlobalTable();
bool direct = cfg.GetBool("LAN.DirectMode");
std::string devicename = cfg.GetString("LAN.Device");
Net::Init(direct, devicename.c_str());
}
// localMp is initialized at this point
NetInit();
createEmuInstance();
@ -316,9 +346,6 @@ int main(int argc, char** argv)
// but with this we make extra sure they are all deleted
deleteAllEmuInstances();
LocalMP::DeInit();
Net::DeInit();
delete camManager[0];
delete camManager[1];