lua error fix

This commit is contained in:
Anonymous275 2020-11-11 00:40:19 +02:00
parent 6d6d8ef7e7
commit 726af5bf1d
3 changed files with 91 additions and 35 deletions

View File

@ -17,9 +17,9 @@ extern std::string UlStatus;
extern std::string ListOfMods; extern std::string ListOfMods;
void UDPSend(std::string Data); void UDPSend(std::string Data);
void CoreNetwork(); void CoreNetwork();
void GameSend(std::string Data);
void SendLarge(std::string Data); void SendLarge(std::string Data);
void TCPSend(const std::string&Data); void TCPSend(const std::string&Data);
void GameSend(const std::string& Data);
std::string GetAddr(const std::string&IP); std::string GetAddr(const std::string&IP);
void ServerParser(const std::string& Data); void ServerParser(const std::string& Data);
void TCPClientMain(const std::string& IP,int Port); void TCPClientMain(const std::string& IP,int Port);

View File

@ -11,6 +11,8 @@
#include "Logger.h" #include "Logger.h"
#include <thread> #include <thread>
#include <set> #include <set>
#include <charconv>
extern int TraceBack; extern int TraceBack;
std::set<std::string>* ConfList = nullptr; std::set<std::string>* ConfList = nullptr;
bool TCPTerminate = false; bool TCPTerminate = false;
@ -116,16 +118,34 @@ void GameHandler(SOCKET Client){
Memory.detach(); Memory.detach();
once = true; once = true;
} }
char buf[64000]; //Read byte by byte until '>' is rcved then get the size and read based on it
int res,len = 64000; int32_t Size,Temp,Rcv;
char Header[10] = {0};
do{ do{
res = recv(Client, buf, len, 0); Rcv = 0;
if(res < 1)break; do{
std::string data(buf, res); Temp = recv(Client,&Header[Rcv],1,0);
std::thread Respond(Parse, data, Client); if(Temp < 1)break;
}while(Header[Rcv++] != '>');
if(Temp < 1)break;
if(std::from_chars(Header,&Header[Rcv],Size).ptr[0] != '>'){
debug(Sec("(Core) Invalid lua Header -> ") + std::string(Header,Rcv));
break;
}
std::string Ret(Size,0);
Rcv = 0;
do{
Temp = recv(Client,&Ret[Rcv],Size-Rcv,0);
if(Temp < 1)break;
Rcv += Temp;
}while(Rcv < Size);
if(Temp < 1)break;
std::thread Respond(Parse, Ret, Client);
Respond.detach(); Respond.detach();
}while(res > 0); }while(Temp > 0);
if (res == 0) { if (Temp == 0) {
debug(Sec("(Core) Connection closing")); debug(Sec("(Core) Connection closing"));
} else { } else {
debug(Sec("(Core) recv failed with error: ") + std::to_string(WSAGetLastError())); debug(Sec("(Core) recv failed with error: ") + std::to_string(WSAGetLastError()));

View File

@ -6,9 +6,10 @@
#include <WinSock2.h> #include <WinSock2.h>
#include <WS2tcpip.h> #include <WS2tcpip.h>
#include "Logger.h" #include "Logger.h"
#include <sstream> #include <charconv>
#include <string> #include <string>
#include <thread> #include <thread>
#include <mutex>
std::chrono::time_point<std::chrono::steady_clock> PingStart,PingEnd; std::chrono::time_point<std::chrono::steady_clock> PingStart,PingEnd;
bool GConnected = false; bool GConnected = false;
@ -16,20 +17,43 @@ bool CServer = true;
extern SOCKET UDPSock; extern SOCKET UDPSock;
extern SOCKET TCPSock; extern SOCKET TCPSock;
SOCKET CSocket; SOCKET CSocket;
void GameSend(const std::string& Data){
if(TCPTerminate || !GConnected || CSocket == -1)return; bool CheckBytes(uint32_t Bytes){
#ifdef DEBUG if(Bytes == 0){
//debug("Launcher game send -> " + std::to_string(Data.size())); debug(Sec("(Proxy) Connection closing"));
#endif return false;
int iSRes = send(CSocket, (Data + "\n").c_str(), int(Data.size()) + 1, 0); }else if(Bytes < 0){
if (iSRes == SOCKET_ERROR) { debug(Sec("(Proxy) send failed with error: ") + std::to_string(WSAGetLastError()));
debug(Sec("(Proxy) send failed with error: ") + std::to_string(WSAGetLastError())); return false;
} else if (Data.length() > 1000){
debug(Sec("(Launcher->Game) Bytes sent: ") + std::to_string(iSRes));
} }
return true;
}
void GameSend(std::string Data){
static std::mutex Lock;
Lock.lock();
if(TCPTerminate || !GConnected || CSocket == -1)return;
static thread_local int32_t Size,Temp,Sent;
Data += '\n';
Size = int32_t(Data.size());
Sent = 0;
#ifdef DEBUG
if(Size > 1000){
debug("Launcher -> game (" +std::to_string(Size)+")");
}
#endif
do{
Temp = send(CSocket, &Data[Sent], Size - Sent, 0);
if(!CheckBytes(Temp))return;
Sent += Temp;
}while(Sent < Size);
Lock.unlock();
} }
void ServerSend(std::string Data, bool Rel){ void ServerSend(std::string Data, bool Rel){
if(Terminate || Data.empty())return; if(Terminate || Data.empty())return;
if(Data.find("Zp") != std::string::npos && Data.size() > 500){
abort();
}
char C = 0; char C = 0;
bool Ack = false; bool Ack = false;
int DLen = int(Data.length()); int DLen = int(Data.length());
@ -176,23 +200,35 @@ void TCPGameServer(const std::string& IP, int Port){
t1.detach(); t1.detach();
CServer = false; CServer = false;
} }
char buf[10000]; int32_t Size,Temp,Rcv;
int Res,len = 10000; char Header[10] = {0};
ZeroMemory(buf, len);
//Read byte by byte until '>' is rcved then get the size and read based on it
do{ do{
Res = recv(CSocket,buf,len,0); Rcv = 0;
if(Res < 1)break;
std::string t; do{
std::string buff(Res,0); Temp = recv(CSocket,&Header[Rcv],1,0);
memcpy_s(&buff[0],Res,buf,Res); if(Temp < 1)break;
std::stringstream ss(buff); }while(Header[Rcv++] != '>');
int S = 0; if(Temp < 1)break;
while (std::getline(ss, t, '\n')) { if(std::from_chars(Header,&Header[Rcv],Size).ptr[0] != '>'){
ServerSend(t,false); debug(Sec("(Game) Invalid lua Header -> ") + std::string(Header,Rcv));
S++; break;
} }
}while(Res > 0); std::string Ret(Size,0);
if(Res == 0)debug(Sec("(Proxy) Connection closing")); Rcv = 0;
do{
Temp = recv(CSocket,&Ret[Rcv],Size-Rcv,0);
if(Temp < 1)break;
Rcv += Temp;
}while(Rcv < Size);
if(Temp < 1)break;
ServerSend(Ret,false);
}while(Temp > 0);
if(Temp == 0)debug(Sec("(Proxy) Connection closing"));
else debug(Sec("(Proxy) recv failed error : ") + std::to_string(WSAGetLastError())); else debug(Sec("(Proxy) recv failed error : ") + std::to_string(WSAGetLastError()));
} }
TCPTerminate = true; TCPTerminate = true;