Add parallel udp initialization (#269)

By creating this pull request, I understand that code that is AI
generated or otherwise automatically generated may be rejected without
further discussion.
I declare that I fully understand all code I pushed into this PR, and
wrote all this code myself and own the rights to this code.
This commit is contained in:
Tixx
2026-08-22 22:05:16 +02:00
committed by GitHub
+16 -1
View File
@@ -22,12 +22,17 @@
#include "Logger.h" #include "Logger.h"
#include <array> #include <array>
#include <future>
#include <mutex>
#include <string> #include <string>
SOCKET UDPSock = -1; SOCKET UDPSock = -1;
sockaddr_in* ToServer = nullptr; sockaddr_in* ToServer = nullptr;
std::mutex UDPSendMutex;
void UDPSend(std::string Data) { void UDPSend(std::string Data) {
std::scoped_lock lock(UDPSendMutex);
if (ClientID == -1 || UDPSock == -1) if (ClientID == -1 || UDPSock == -1)
return; return;
if (Data.length() > 400) { if (Data.length() > 400) {
@@ -79,6 +84,7 @@ void UDPRcv() {
Ret[Rcv] = 0; Ret[Rcv] = 0;
UDPParser(std::string_view(Ret.data(), Rcv)); UDPParser(std::string_view(Ret.data(), Rcv));
} }
void UDPClientMain(const std::string& IP, int Port) { void UDPClientMain(const std::string& IP, int Port) {
#ifdef _WIN32 #ifdef _WIN32
WSADATA data; WSADATA data;
@@ -94,11 +100,15 @@ void UDPClientMain(const std::string& IP, int Port) {
ToServer->sin_port = htons(Port); ToServer->sin_port = htons(Port);
inet_pton(AF_INET, IP.c_str(), &ToServer->sin_addr); inet_pton(AF_INET, IP.c_str(), &ToServer->sin_addr);
UDPSock = socket(AF_INET, SOCK_DGRAM, 0); UDPSock = socket(AF_INET, SOCK_DGRAM, 0);
std::future<void> magicSend;
if (!magic.empty()) { if (!magic.empty()) {
magicSend = std::async(std::launch::async, ([](){
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
UDPSend(magic); UDPSend(magic);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
} }
}));
} }
GameSend("P" + std::to_string(ClientID)); GameSend("P" + std::to_string(ClientID));
TCPSend("H", TCPSock); TCPSend("H", TCPSock);
@@ -108,6 +118,11 @@ void UDPClientMain(const std::string& IP, int Port) {
UDPRcv(); UDPRcv();
} }
debug("UDP receive loop done"); debug("UDP receive loop done");
if (magicSend.valid()) {
magicSend.get();
}
KillSocket(UDPSock); KillSocket(UDPSock);
WSACleanup(); WSACleanup();
} }