From de8a768b7912e12a5718f633a60d1c143cf688de Mon Sep 17 00:00:00 2001 From: Arisotura Date: Thu, 23 Mar 2023 14:29:16 +0100 Subject: [PATCH] get enet going, I guess still very experimental --- src/frontend/qt_sdl/Netplay.cpp | 96 +++++++++++++++++++++++++++++++++ src/frontend/qt_sdl/Netplay.h | 7 +++ src/frontend/qt_sdl/main.cpp | 20 +++++++ src/frontend/qt_sdl/main.h | 4 ++ 4 files changed, 127 insertions(+) diff --git a/src/frontend/qt_sdl/Netplay.cpp b/src/frontend/qt_sdl/Netplay.cpp index e524e857..0096e505 100644 --- a/src/frontend/qt_sdl/Netplay.cpp +++ b/src/frontend/qt_sdl/Netplay.cpp @@ -28,8 +28,19 @@ namespace Netplay { +bool Active; +bool IsHost; + +ENetHost* Host; +ENetPeer* Peer; + + bool Init() { + Active = false; + IsHost = false; + Host = nullptr; + if (enet_initialize() != 0) { printf("enet shat itself :(\n"); @@ -45,4 +56,89 @@ void DeInit() enet_deinitialize(); } + +void StartHost() +{ + ENetAddress addr; + addr.host = ENET_HOST_ANY; + addr.port = 8064; + + // TODO determine proper number of clients/channels + Host = enet_host_create(&addr, 16, 16, 0, 0); + if (!Host) + { + printf("host shat itself :(\n"); + return; + } + + Active = true; + IsHost = true; +} + +void StartClient() +{ + // TODO determine proper number of clients/channels + Host = enet_host_create(nullptr, 16, 16, 0, 0); + if (!Host) + { + printf("client shat itself :(\n"); + return; + } + + printf("client created, connecting\n"); + + ENetAddress addr; + enet_address_set_host(&addr, "127.0.0.1"); // TODO!!!! + addr.port = 8064; + Peer = enet_host_connect(Host, &addr, 2, 0); + if (!Peer) + { + printf("connect shat itself :(\n"); + return; + } + + ENetEvent event; + bool conn = false; + if (enet_host_service(Host, &event, 5000) > 0) + { + if (event.type == ENET_EVENT_TYPE_CONNECT) + { + printf("connected!\n"); + conn = true; + } + } + + if (!conn) + { + printf("connection failed\n"); + enet_peer_reset(Peer); + } + + Active = true; + IsHost = false; +} + + +void ProcessFrame() +{ + ENetEvent event; + while (enet_host_service(Host, &event, 0) > 0) + { + switch (event.type) + { + case ENET_EVENT_TYPE_CONNECT: + printf("client connected %08X %d\n", event.peer->address.host, event.peer->address.port); + break; + + case ENET_EVENT_TYPE_DISCONNECT: + printf("client disconnected %08X %d\n", event.peer->address.host, event.peer->address.port); + break; + + case ENET_EVENT_TYPE_RECEIVE: + printf("received shit\n"); + break; + } + } +} + } diff --git a/src/frontend/qt_sdl/Netplay.h b/src/frontend/qt_sdl/Netplay.h index c7ce6ef3..7cef744a 100644 --- a/src/frontend/qt_sdl/Netplay.h +++ b/src/frontend/qt_sdl/Netplay.h @@ -24,9 +24,16 @@ namespace Netplay { +extern bool Active; + bool Init(); void DeInit(); +void StartHost(); +void StartClient(); + +void ProcessFrame(); + } #endif // NETPLAY_H diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp index f5635ff2..42db6788 100644 --- a/src/frontend/qt_sdl/main.cpp +++ b/src/frontend/qt_sdl/main.cpp @@ -363,6 +363,8 @@ void EmuThread::run() while (EmuRunning != emuStatus_Exit) { + if (Netplay::Active) Netplay::ProcessFrame(); + IPC::ProcessCommands(); Input::Process(); @@ -1566,6 +1568,14 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) actMPNewInstance = submenu->addAction("Launch new instance"); connect(actMPNewInstance, &QAction::triggered, this, &MainWindow::onMPNewInstance); + + submenu->addSeparator(); + + actMPStartHost = submenu->addAction("NETPLAY HOST"); + connect(actMPStartHost, &QAction::triggered, this, &MainWindow::onMPStartHost); + + actMPStartClient = submenu->addAction("NETPLAY CLIENT"); + connect(actMPStartClient, &QAction::triggered, this, &MainWindow::onMPStartClient); } } { @@ -2824,6 +2834,16 @@ void MainWindow::onMPNewInstance() newinst.startDetached(); } +void MainWindow::onMPStartHost() +{ + Netplay::StartHost(); +} + +void MainWindow::onMPStartClient() +{ + Netplay::StartClient(); +} + void MainWindow::onOpenEmuSettings() { emuThread->emuPause(); diff --git a/src/frontend/qt_sdl/main.h b/src/frontend/qt_sdl/main.h index 107b5714..338af664 100644 --- a/src/frontend/qt_sdl/main.h +++ b/src/frontend/qt_sdl/main.h @@ -311,6 +311,8 @@ private slots: void onRAMInfo(); void onOpenTitleManager(); void onMPNewInstance(); + void onMPStartHost(); + void onMPStartClient(); void onOpenEmuSettings(); void onEmuSettingsDialogFinished(int res); @@ -408,6 +410,8 @@ public: QAction* actRAMInfo; QAction* actTitleManager; QAction* actMPNewInstance; + QAction* actMPStartHost; + QAction* actMPStartClient; QAction* actEmuSettings; #ifdef __APPLE__