implement binary header

This commit is contained in:
Lion Kortlepel 2024-06-23 20:46:09 +02:00
parent 137d9dd1e2
commit 4a5728d421
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
2 changed files with 13 additions and 7 deletions

View File

@ -190,9 +190,11 @@ void Parse(std::string Data, SOCKET CSocket) {
break; break;
} }
if (!Data.empty() && CSocket != -1) { if (!Data.empty() && CSocket != -1) {
Data = std::to_string(Data.size()) + ">" + Data; uint32_t DataSize = Data.size();
debug("Sending '" + Data + "'"); std::vector<char> ToSend(sizeof(DataSize) + Data.size());
int res = send(CSocket, Data.c_str(), int(Data.size()), 0); std::copy_n(reinterpret_cast<char*>(&DataSize), sizeof(DataSize), ToSend.begin());
std::copy_n(Data.data(), Data.size(), ToSend.begin() + sizeof(DataSize));
int res = send(CSocket, ToSend.data(), int(ToSend.size()), 0);
if (res < 0) { if (res < 0) {
debug("(Core) send failed with error: " + std::to_string(WSAGetLastError())); debug("(Core) send failed with error: " + std::to_string(WSAGetLastError()));
} }

View File

@ -6,6 +6,8 @@
/// Created by Anonymous275 on 7/25/2020 /// Created by Anonymous275 on 7/25/2020
/// ///
#include "Network/network.hpp" #include "Network/network.hpp"
#include <algorithm>
#include <vector>
#include <zlib.h> #include <zlib.h>
#if defined(_WIN32) #if defined(_WIN32)
#include <winsock2.h> #include <winsock2.h>
@ -56,15 +58,17 @@ bool CheckBytes(uint32_t Bytes) {
return true; return true;
} }
void GameSend(std::string_view Data) { void GameSend(std::string_view RawData) {
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;
Size = int32_t(Data.size()); uint32_t DataSize = RawData.size();
auto SizeStr = std::to_string(Size) + ">"; std::vector<char> Data(sizeof(DataSize) + RawData.size());
send(CSocket, SizeStr.c_str(), SizeStr.size(), 0); std::copy_n(reinterpret_cast<char*>(&DataSize), sizeof(DataSize), Data.begin());
std::copy_n(RawData.data(), RawData.size(), Data.begin() + sizeof(DataSize));
Size = Data.size();
Sent = 0; Sent = 0;
#ifdef DEBUG #ifdef DEBUG
if (Size > 1000) { if (Size > 1000) {