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 UDPSend(std::string Data);
|
||||
bool CheckBytes(int32_t Bytes);
|
||||
void GameSend(std::string Data);
|
||||
void GameSend(std::string_view Data);
|
||||
void SendLarge(std::string Data);
|
||||
std::string TCPRcv(uint64_t Sock);
|
||||
void SyncResources(uint64_t TCPSock);
|
||||
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);
|
||||
void TCPSend(const std::string& Data, uint64_t Sock);
|
||||
void TCPClientMain(const std::string& IP, int Port);
|
||||
|
@ -8,4 +8,4 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
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;
|
||||
return Ret;
|
||||
}
|
||||
std::string DeComp(std::string Compressed) {
|
||||
std::string DeComp(std::string_view Compressed) {
|
||||
char* C = new char[Biggest];
|
||||
memset(C, 0, Biggest);
|
||||
z_stream infstream;
|
||||
|
@ -55,13 +55,12 @@ bool CheckBytes(uint32_t Bytes) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void GameSend(std::string Data) {
|
||||
void GameSend(std::string_view Data) {
|
||||
static std::mutex Lock;
|
||||
std::scoped_lock Guard(Lock);
|
||||
if (TCPTerminate || !GConnected || CSocket == -1)
|
||||
return;
|
||||
int32_t Size, Temp, Sent;
|
||||
Data += '\n';
|
||||
Size = int32_t(Data.size());
|
||||
Sent = 0;
|
||||
#ifdef DEBUG
|
||||
@ -77,6 +76,11 @@ void GameSend(std::string Data) {
|
||||
return;
|
||||
Sent += Temp;
|
||||
} 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) {
|
||||
if (Terminate || Data.empty())
|
||||
@ -191,7 +195,7 @@ void AutoPing() {
|
||||
}
|
||||
}
|
||||
int ClientID = -1;
|
||||
void ParserAsync(const std::string& Data) {
|
||||
void ParserAsync(std::string_view Data) {
|
||||
if (Data.empty())
|
||||
return;
|
||||
char Code = Data.at(0), SubCode = 0;
|
||||
@ -214,7 +218,7 @@ void ParserAsync(const std::string& Data) {
|
||||
}
|
||||
GameSend(Data);
|
||||
}
|
||||
void ServerParser(const std::string& Data) {
|
||||
void ServerParser(std::string_view Data) {
|
||||
ParserAsync(Data);
|
||||
}
|
||||
void NetMain(const std::string& IP, int Port) {
|
||||
|
@ -21,6 +21,7 @@
|
||||
#endif
|
||||
|
||||
#include "Logger.h"
|
||||
#include <array>
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
@ -48,7 +49,7 @@ void SendLarge(std::string Data) {
|
||||
TCPSend(Data, TCPSock);
|
||||
}
|
||||
|
||||
void UDPParser(std::string Packet) {
|
||||
void UDPParser(std::string_view Packet) {
|
||||
if (Packet.substr(0, 4) == "ABG:") {
|
||||
Packet = DeComp(Packet.substr(4));
|
||||
}
|
||||
@ -62,13 +63,14 @@ void UDPRcv() {
|
||||
socklen_t clientLength = sizeof(FromServer);
|
||||
#endif
|
||||
ZeroMemory(&FromServer, clientLength);
|
||||
std::string Ret(10240, 0);
|
||||
std::array<char, 10240> Ret {};
|
||||
Ret.fill(0);
|
||||
if (UDPSock == -1)
|
||||
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)
|
||||
return;
|
||||
UDPParser(Ret.substr(0, Rcv));
|
||||
UDPParser(std::string_view(Ret.data(), Rcv));
|
||||
}
|
||||
void UDPClientMain(const std::string& IP, int Port) {
|
||||
#ifdef _WIN32
|
||||
|
Loading…
x
Reference in New Issue
Block a user