lot of work

This commit is contained in:
Anonymous275 2020-11-08 21:48:44 +02:00
parent c6fbd3dc49
commit 8e9bf46778
5 changed files with 21 additions and 19 deletions

View File

@ -17,6 +17,7 @@ typedef unsigned long DWORD, *PDWORD, *LPDWORD;
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <thread> #include <thread>
#include <array>
std::vector<std::string> QConsoleOut; std::vector<std::string> QConsoleOut;
std::string CInputBuff; std::string CInputBuff;

View File

@ -87,7 +87,7 @@ int Dec(int value,int d,int n){
#ifdef WIN32 #ifdef WIN32
int Handle(EXCEPTION_POINTERS *ep,char* Origin){ int Handle(EXCEPTION_POINTERS *ep,char* Origin){
Assert(false); //Assert(false);
std::stringstream R; std::stringstream R;
R << Sec("Code : ") << std::hex R << Sec("Code : ") << std::hex
<< ep->ExceptionRecord->ExceptionCode << ep->ExceptionRecord->ExceptionCode

View File

@ -255,7 +255,7 @@ void TCPServerMain(){
std::thread ID(Identify,client); std::thread ID(Identify,client);
ID.detach(); ID.detach();
} catch (const std::exception& e) { } catch (const std::exception& e) {
error(Sec("fatal: ") + std::string(e)); error(Sec("fatal: ") + std::string(e.what()));
} }
}while(client); }while(client);

View File

@ -13,19 +13,21 @@ void TCPSend(Client*c,const std::string&Data){
Assert(c); Assert(c);
if(c == nullptr)return; if(c == nullptr)return;
// Size is BIG ENDIAN now, use only for header! // Size is BIG ENDIAN now, use only for header!
auto Size = htonl(int32_t(Data.size())); //auto Size = htonl(int32_t(Data.size()));
///TODO : BIG ENDIAN for other OS
int32_t Size, Sent, Temp;
std::string Send(4,0); std::string Send(4,0);
Size = int32_t(Data.size());
memcpy(&Send[0],&Size,sizeof(Size)); memcpy(&Send[0],&Size,sizeof(Size));
Send += Data; Send += Data;
Size = int32_t(Send.size()); Sent = 0;
int32_t Sent = 0,Temp; Size += 4;
do { do {
Temp = send(c->GetTCPSock(), &Send[Sent], Size - Sent, 0); Temp = send(c->GetTCPSock(), &Send[Sent], Size - Sent, 0);
if (Temp == 0) { if (Temp == 0) {
if (c->GetStatus() > -1)c->SetStatus(-1); if (c->GetStatus() > -1)c->SetStatus(-1);
return; return;
} else if (Sent < 0) { } else if (Temp < 0) {
if (c->GetStatus() > -1)c->SetStatus(-1); if (c->GetStatus() > -1)c->SetStatus(-1);
closesocket(c->GetTCPSock()); closesocket(c->GetTCPSock());
return; return;
@ -55,15 +57,14 @@ bool CheckBytes(Client*c,int32_t BytesRcv){
void TCPRcv(Client*c){ void TCPRcv(Client*c){
Assert(c); Assert(c);
static thread_local int32_t Header,BytesRcv,Temp; int32_t Header,BytesRcv,Temp;
if(c == nullptr || c->GetStatus() < 0)return; if(c == nullptr || c->GetStatus() < 0)return;
#ifdef WIN32 #ifdef WIN32
BytesRcv = recv(c->GetTCPSock(), reinterpret_cast<char*>(&Header), sizeof(Header),0); BytesRcv = recv(c->GetTCPSock(), reinterpret_cast<char*>(&Header), sizeof(Header),0);
#else #else
BytesRcv = recv(c->GetTCPSock(), reinterpret_cast<void*>(&Header), sizeof(Header), 0); BytesRcv = recv(c->GetTCPSock(), reinterpret_cast<void*>(&Header), sizeof(Header), 0);
#endif #endif
// convert back to host endianness
Header = ntohl(Header);
#ifdef DEBUG #ifdef DEBUG
//debug(std::string(__func__) + Sec(": expecting ") + std::to_string(Header) + Sec(" bytes.")); //debug(std::string(__func__) + Sec(": expecting ") + std::to_string(Header) + Sec(" bytes."));
#endif // DEBUG #endif // DEBUG
@ -74,16 +75,15 @@ void TCPRcv(Client*c){
return; return;
} }
// TODO: std::vector // TODO: std::vector
char* Data = new char[Header]; std::vector<char> Data(Header);
Assert(Data);
BytesRcv = 0; BytesRcv = 0;
do{ do{
Temp = recv(c->GetTCPSock(), Data+BytesRcv, Header-BytesRcv,0); Temp = recv(c->GetTCPSock(), &Data[BytesRcv], Header-BytesRcv,0);
if(!CheckBytes(c,Temp)){ if(!CheckBytes(c,Temp)){
#ifdef DEBUG #ifdef DEBUG
error(std::string(__func__) + Sec(": failed on CheckBytes in while(BytesRcv < Header)")); error(std::string(__func__) + Sec(": failed on CheckBytes in while(BytesRcv < Header)"));
#endif // DEBUG #endif // DEBUG
delete[] Data;
return; return;
} }
#ifdef DEBUG #ifdef DEBUG
@ -94,11 +94,14 @@ void TCPRcv(Client*c){
#ifdef DEBUG #ifdef DEBUG
//debug(std::string(__func__) + Sec(": finished recv with Temp: ") + std::to_string(Temp) + Sec(", BytesRcv: ") + std::to_string(BytesRcv)); //debug(std::string(__func__) + Sec(": finished recv with Temp: ") + std::to_string(Temp) + Sec(", BytesRcv: ") + std::to_string(BytesRcv));
#endif // DEBUG #endif // DEBUG
std::string Ret = std::string(Data,Header); std::string Ret(Data.data(),Header);
delete[] Data;
if (Ret.substr(0, 4) == "ABG:") { if (Ret.substr(0, 4) == "ABG:") {
Ret = DeComp(Ret.substr(4)); Ret = DeComp(Ret.substr(4));
} }
#ifdef DEBUG
//debug("Parsing from " + c->GetName() + " -> " +std::to_string(Ret.size()));
#endif
GParser(c,Ret); GParser(c,Ret);
} }

View File

@ -37,7 +37,6 @@ void UDPSend(Client* c, std::string Data) {
return; return;
sockaddr_in Addr = c->GetUDPAddr(); sockaddr_in Addr = c->GetUDPAddr();
socklen_t AddrSize = sizeof(c->GetUDPAddr()); socklen_t AddrSize = sizeof(c->GetUDPAddr());
Data = Data.substr(0, Data.find(char(0)));
if (Data.length() > 400) { if (Data.length() > 400) {
std::string CMP(Comp(Data)); std::string CMP(Comp(Data));
Data = "ABG:" + CMP; Data = "ABG:" + CMP;
@ -100,7 +99,6 @@ int SplitID() {
} }
void SendLarge(Client* c, std::string Data) { void SendLarge(Client* c, std::string Data) {
Assert(c); Assert(c);
Data = Data.substr(0, Data.find(char(0)));
if (Data.length() > 400) { if (Data.length() > 400) {
std::string CMP(Comp(Data)); std::string CMP(Comp(Data));
Data = "ABG:" + CMP; Data = "ABG:" + CMP;
@ -174,7 +172,7 @@ std::string UDPRcvFromClient(sockaddr_in& client) {
#endif // WIN32 #endif // WIN32
return ""; return "";
} }
return Ret; return Ret.substr(0,Rcv);
} }
SplitData* GetSplit(int SplitID) { SplitData* GetSplit(int SplitID) {