mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-08-16 00:06:41 +00:00
avoid a substr() which costs us ~20% of runtime performance
This commit is contained in:
parent
fc454cd11e
commit
17e887442c
@ -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);
|
||||||
|
@ -8,4 +8,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
std::string Comp(std::string Data);
|
std::string Comp(std::string Data);
|
||||||
std::string DeComp(std::string Compressed);
|
std::string DeComp(std::string_view Compressed);
|
||||||
|
@ -34,7 +34,7 @@ std::string Comp(std::string Data) {
|
|||||||
delete[] C;
|
delete[] C;
|
||||||
return Ret;
|
return Ret;
|
||||||
}
|
}
|
||||||
std::string DeComp(std::string Compressed) {
|
std::string DeComp(std::string_view Compressed) {
|
||||||
char* C = new char[Biggest];
|
char* C = new char[Biggest];
|
||||||
memset(C, 0, Biggest);
|
memset(C, 0, Biggest);
|
||||||
z_stream infstream;
|
z_stream infstream;
|
||||||
|
@ -55,13 +55,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
|
||||||
@ -77,6 +76,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())
|
||||||
@ -191,7 +195,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;
|
||||||
@ -214,7 +218,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) {
|
||||||
|
@ -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:") {
|
||||||
Packet = DeComp(Packet.substr(4));
|
Packet = DeComp(Packet.substr(4));
|
||||||
}
|
}
|
||||||
@ -62,13 +63,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);
|
std::array<char, 10240> Ret {};
|
||||||
|
Ret.fill(0);
|
||||||
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(), 0, (sockaddr*)&FromServer, &clientLength);
|
||||||
if (Rcv == SOCKET_ERROR)
|
if (Rcv == SOCKET_ERROR)
|
||||||
return;
|
return;
|
||||||
UDPParser(Ret.substr(0, 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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user