mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-08-27 04:37:41 +00:00
Add parallel udp initialization
This commit is contained in:
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user