mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-08-16 16:27:15 +00:00
add more debug statements, wait for threads before shutting down
This commit is contained in:
parent
0b589a74c9
commit
cd17df5cc2
@ -6,6 +6,7 @@
|
|||||||
/// Created by Anonymous275 on 7/25/2020
|
/// Created by Anonymous275 on 7/25/2020
|
||||||
///
|
///
|
||||||
#include "Network/network.hpp"
|
#include "Network/network.hpp"
|
||||||
|
#include <memory>
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
@ -234,6 +235,8 @@ void NetMain(const std::string& IP, int Port) {
|
|||||||
}
|
}
|
||||||
void TCPGameServer(const std::string& IP, int Port) {
|
void TCPGameServer(const std::string& IP, int Port) {
|
||||||
GSocket = SetupListener();
|
GSocket = SetupListener();
|
||||||
|
std::unique_ptr<std::thread> ClientThread {};
|
||||||
|
std::unique_ptr<std::thread> NetMainThread {};
|
||||||
while (!TCPTerminate && GSocket != -1) {
|
while (!TCPTerminate && GSocket != -1) {
|
||||||
debug("MAIN LOOP OF GAME SERVER");
|
debug("MAIN LOOP OF GAME SERVER");
|
||||||
GConnected = false;
|
GConnected = false;
|
||||||
@ -245,8 +248,7 @@ void TCPGameServer(const std::string& IP, int Port) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (CServer) {
|
if (CServer) {
|
||||||
std::thread Client(TCPClientMain, IP, Port);
|
ClientThread = std::make_unique<std::thread>(TCPClientMain, IP, Port);
|
||||||
Client.detach();
|
|
||||||
}
|
}
|
||||||
CSocket = accept(GSocket, nullptr, nullptr);
|
CSocket = accept(GSocket, nullptr, nullptr);
|
||||||
if (CSocket == -1) {
|
if (CSocket == -1) {
|
||||||
@ -256,8 +258,7 @@ void TCPGameServer(const std::string& IP, int Port) {
|
|||||||
debug("(Proxy) Game Connected!");
|
debug("(Proxy) Game Connected!");
|
||||||
GConnected = true;
|
GConnected = true;
|
||||||
if (CServer) {
|
if (CServer) {
|
||||||
std::thread t1(NetMain, IP, Port);
|
NetMainThread = std::make_unique<std::thread>(NetMain, IP, Port);
|
||||||
t1.detach();
|
|
||||||
CServer = false;
|
CServer = false;
|
||||||
}
|
}
|
||||||
int32_t Size, Temp, Rcv;
|
int32_t Size, Temp, Rcv;
|
||||||
@ -300,6 +301,16 @@ void TCPGameServer(const std::string& IP, int Port) {
|
|||||||
TCPTerminate = true;
|
TCPTerminate = true;
|
||||||
GConnected = false;
|
GConnected = false;
|
||||||
Terminate = true;
|
Terminate = true;
|
||||||
|
if (ClientThread) {
|
||||||
|
debug("Waiting for client thread");
|
||||||
|
ClientThread->join();
|
||||||
|
debug("Client thread done");
|
||||||
|
}
|
||||||
|
if (NetMainThread) {
|
||||||
|
debug("Waiting for net main thread");
|
||||||
|
NetMainThread->join();
|
||||||
|
debug("Net main thread done");
|
||||||
|
}
|
||||||
if (CSocket != SOCKET_ERROR)
|
if (CSocket != SOCKET_ERROR)
|
||||||
KillSocket(CSocket);
|
KillSocket(CSocket);
|
||||||
debug("END OF GAME SERVER");
|
debug("END OF GAME SERVER");
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
///
|
///
|
||||||
#include "Network/network.hpp"
|
#include "Network/network.hpp"
|
||||||
#include "Zlib/Compressor.h"
|
#include "Zlib/Compressor.h"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
@ -50,9 +51,13 @@ void SendLarge(std::string Data) {
|
|||||||
void UDPParser(std::string_view Packet) {
|
void UDPParser(std::string_view Packet) {
|
||||||
if (Packet.substr(0, 4) == "ABG:") {
|
if (Packet.substr(0, 4) == "ABG:") {
|
||||||
auto substr = Packet.substr(4);
|
auto substr = Packet.substr(4);
|
||||||
|
try {
|
||||||
auto res = DeComp(std::span<const char>(substr.data(), substr.size()));
|
auto res = DeComp(std::span<const char>(substr.data(), substr.size()));
|
||||||
std::string DeCompPacket = std::string(res.data(), res.size());
|
std::string DeCompPacket = std::string(res.data(), res.size());
|
||||||
ServerParser(DeCompPacket);
|
ServerParser(DeCompPacket);
|
||||||
|
} catch (const std::runtime_error& err) {
|
||||||
|
error("Error in decompression of UDP, ignoring");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ServerParser(Packet);
|
ServerParser(Packet);
|
||||||
}
|
}
|
||||||
@ -92,8 +97,11 @@ void UDPClientMain(const std::string& IP, int Port) {
|
|||||||
GameSend("P" + std::to_string(ClientID));
|
GameSend("P" + std::to_string(ClientID));
|
||||||
TCPSend("H", TCPSock);
|
TCPSend("H", TCPSock);
|
||||||
UDPSend("p");
|
UDPSend("p");
|
||||||
while (!Terminate)
|
debug("Starting UDP receive loop");
|
||||||
|
while (!Terminate) {
|
||||||
UDPRcv();
|
UDPRcv();
|
||||||
|
}
|
||||||
|
debug("UDP receive loop done");
|
||||||
KillSocket(UDPSock);
|
KillSocket(UDPSock);
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
}
|
}
|
||||||
|
@ -107,8 +107,14 @@ std::string TCPRcv(SOCKET Sock) {
|
|||||||
|
|
||||||
if (Ret.substr(0, 4) == "ABG:") {
|
if (Ret.substr(0, 4) == "ABG:") {
|
||||||
auto substr = Ret.substr(4);
|
auto substr = Ret.substr(4);
|
||||||
|
try {
|
||||||
auto res = DeComp(std::span<char>(substr.data(), substr.size()));
|
auto res = DeComp(std::span<char>(substr.data(), substr.size()));
|
||||||
Ret = std::string(res.data(), res.size());
|
Ret = std::string(res.data(), res.size());
|
||||||
|
} catch (const std::runtime_error& err) {
|
||||||
|
// this happens e.g. when we're out of memory, or when we get incomplete data
|
||||||
|
error("Decompression failed");
|
||||||
|
return "";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
|
Loading…
x
Reference in New Issue
Block a user