Merge branch 'performance-improvements'

This commit is contained in:
Lion Kortlepel 2024-06-22 23:01:15 +02:00
commit e46d4b2f0e
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
5 changed files with 20 additions and 14 deletions

View File

@ -41,12 +41,12 @@ int KillSocket(uint64_t Dead);
void UUl(const std::string& R); void UUl(const std::string& R);
void UDPSend(std::string Data); void UDPSend(std::string Data);
bool CheckBytes(int32_t Bytes); bool CheckBytes(int32_t Bytes);
void GameSend(std::string Data); void GameSend(std::string_view Data);
void SendLarge(std::string Data); void SendLarge(std::string Data);
std::string TCPRcv(uint64_t Sock); std::string TCPRcv(uint64_t Sock);
void SyncResources(uint64_t TCPSock); void SyncResources(uint64_t TCPSock);
std::string GetAddr(const std::string& IP); std::string GetAddr(const std::string& IP);
void ServerParser(const std::string& Data); void ServerParser(std::string_view Data);
std::string Login(const std::string& fields); std::string Login(const std::string& fields);
void TCPSend(const std::string& Data, uint64_t Sock); void TCPSend(const std::string& Data, uint64_t Sock);
void TCPClientMain(const std::string& IP, int Port); void TCPClientMain(const std::string& IP, int Port);

View File

@ -230,8 +230,7 @@ void GameHandler(SOCKET Client) {
if (Temp < 1) if (Temp < 1)
break; break;
std::thread Respond(Parse, Ret, Client); Parse(Ret, Client);
Respond.detach();
} while (Temp > 0); } while (Temp > 0);
if (Temp == 0) { if (Temp == 0) {
debug("(Core) Connection closing"); debug("(Core) Connection closing");

View File

@ -56,13 +56,12 @@ bool CheckBytes(uint32_t Bytes) {
return true; return true;
} }
void GameSend(std::string Data) { void GameSend(std::string_view Data) {
static std::mutex Lock; static std::mutex Lock;
std::scoped_lock Guard(Lock); std::scoped_lock Guard(Lock);
if (TCPTerminate || !GConnected || CSocket == -1) if (TCPTerminate || !GConnected || CSocket == -1)
return; return;
int32_t Size, Temp, Sent; int32_t Size, Temp, Sent;
Data += '\n';
Size = int32_t(Data.size()); Size = int32_t(Data.size());
Sent = 0; Sent = 0;
#ifdef DEBUG #ifdef DEBUG
@ -78,6 +77,11 @@ void GameSend(std::string Data) {
return; return;
Sent += Temp; Sent += Temp;
} while (Sent < Size); } while (Sent < Size);
// send separately to avoid an allocation for += "\n"
Temp = send(CSocket, "\n", 1, 0);
if (!CheckBytes(Temp)) {
return;
}
} }
void ServerSend(std::string Data, bool Rel) { void ServerSend(std::string Data, bool Rel) {
if (Terminate || Data.empty()) if (Terminate || Data.empty())
@ -194,7 +198,7 @@ void AutoPing() {
} }
} }
int ClientID = -1; int ClientID = -1;
void ParserAsync(const std::string& Data) { void ParserAsync(std::string_view Data) {
if (Data.empty()) if (Data.empty())
return; return;
char Code = Data.at(0), SubCode = 0; char Code = Data.at(0), SubCode = 0;
@ -217,7 +221,7 @@ void ParserAsync(const std::string& Data) {
} }
GameSend(Data); GameSend(Data);
} }
void ServerParser(const std::string& Data) { void ServerParser(std::string_view Data) {
ParserAsync(Data); ParserAsync(Data);
} }
void NetMain(const std::string& IP, int Port) { void NetMain(const std::string& IP, int Port) {

View File

@ -296,13 +296,14 @@ void SyncResources(SOCKET Sock) {
if (!fs::exists(GetGamePath() + "mods/multiplayer")) { if (!fs::exists(GetGamePath() + "mods/multiplayer")) {
fs::create_directories(GetGamePath() + "mods/multiplayer"); fs::create_directories(GetGamePath() + "mods/multiplayer");
} }
auto name = GetGamePath() + "mods/multiplayer" + a.substr(a.find_last_of('/')); auto modname = a.substr(a.find_last_of('/'));
#if defined(__linux__) #if defined(__linux__)
// Linux version of the game doesnt support uppercase letters in mod names // Linux version of the game doesnt support uppercase letters in mod names
for (char& c : name) { for (char& c : modname) {
c = ::tolower(c); c = ::tolower(c);
} }
#endif #endif
auto name = GetGamePath() + "mods/multiplayer" + modname;
auto tmp_name = name + ".tmp"; auto tmp_name = name + ".tmp";
fs::copy_file(a, tmp_name, fs::copy_options::overwrite_existing); fs::copy_file(a, tmp_name, fs::copy_options::overwrite_existing);
fs::rename(tmp_name, name); fs::rename(tmp_name, name);

View File

@ -21,6 +21,7 @@
#endif #endif
#include "Logger.h" #include "Logger.h"
#include <array>
#include <set> #include <set>
#include <string> #include <string>
@ -48,7 +49,7 @@ void SendLarge(std::string Data) {
TCPSend(Data, TCPSock); TCPSend(Data, TCPSock);
} }
void UDPParser(std::string 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);
auto res = DeComp(std::span<char>(substr.data(), substr.size())); auto res = DeComp(std::span<char>(substr.data(), substr.size()));
@ -64,13 +65,14 @@ void UDPRcv() {
socklen_t clientLength = sizeof(FromServer); socklen_t clientLength = sizeof(FromServer);
#endif #endif
ZeroMemory(&FromServer, clientLength); ZeroMemory(&FromServer, clientLength);
std::string Ret(10240, 0); static thread_local std::array<char, 10240> Ret {};
if (UDPSock == -1) if (UDPSock == -1)
return; return;
int32_t Rcv = recvfrom(UDPSock, &Ret[0], 10240, 0, (sockaddr*)&FromServer, &clientLength); int32_t Rcv = recvfrom(UDPSock, Ret.data(), Ret.size() - 1, 0, (sockaddr*)&FromServer, &clientLength);
if (Rcv == SOCKET_ERROR) if (Rcv == SOCKET_ERROR)
return; return;
UDPParser(Ret.substr(0, Rcv)); Ret[Rcv] = 0;
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