lot of work

This commit is contained in:
Anonymous275
2020-11-08 21:48:52 +02:00
parent 2ab1a9dce6
commit cd05b0a59a
5 changed files with 38 additions and 25 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ void UDPSend(std::string Data);
void CoreNetwork(); void CoreNetwork();
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); void GameSend(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);
+11 -2
View File
@@ -64,8 +64,17 @@ void Parse(std::string Data,SOCKET CSocket){
break; break;
case 'U': case 'U':
if(SubCode == 'l')Data = UlStatus; if(SubCode == 'l')Data = UlStatus;
if(SubCode == 'p')Data = "Up" + std::to_string(ping); if(SubCode == 'p'){
if(!SubCode)Data = std::string(UlStatus) + "\n" + "Up" + std::to_string(ping); if(ping == -1 && Terminate){
Data = "Up-2";
}else Data = "Up" + std::to_string(ping);
}
if(!SubCode){
std::string Ping;
if(ping == -1 && Terminate)Ping = "-2";
else Ping = std::to_string(ping);
Data = std::string(UlStatus) + "\n" + "Up" + Ping;
}
break; break;
case 'M': case 'M':
Data = MStatus; Data = MStatus;
+4 -1
View File
@@ -15,8 +15,11 @@ 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){ void GameSend(std::string Data){
if(TCPTerminate || !GConnected || CSocket == -1)return; if(TCPTerminate || !GConnected || CSocket == -1)return;
#ifdef DEBUG
//debug("Launcher game send -> " + std::to_string(Data.size()));
#endif
int iSRes = send(CSocket, (Data + "\n").c_str(), int(Data.size()) + 1, 0); int iSRes = send(CSocket, (Data + "\n").c_str(), int(Data.size()) + 1, 0);
if (iSRes == SOCKET_ERROR) { 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()));
+2 -6
View File
@@ -55,7 +55,6 @@ void ClearAll(){
} }
void UDPSend(std::string Data){ void UDPSend(std::string Data){
if(ClientID == -1 || UDPSock == -1)return; if(ClientID == -1 || UDPSock == -1)return;
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;
@@ -105,7 +104,6 @@ int SplitID(){
return SID; return SID;
} }
void SendLarge(std::string Data){ void SendLarge(std::string Data){
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;
@@ -175,7 +173,6 @@ void UDPParser(std::string Packet){
if(Packet.substr(0,4) == "ABG:"){ if(Packet.substr(0,4) == "ABG:"){
Packet = DeComp(Packet.substr(4)); Packet = DeComp(Packet.substr(4));
} }
Packet = Packet.substr(0,Packet.find(char(0)));
if(Packet.substr(0,4) == "TRG:"){ if(Packet.substr(0,4) == "TRG:"){
AckID(stoi(Packet.substr(4))); AckID(stoi(Packet.substr(4)));
return; return;
@@ -201,10 +198,9 @@ void UDPRcv(){
ZeroMemory(&FromServer, clientLength); ZeroMemory(&FromServer, clientLength);
std::string Ret(10240,0); std::string Ret(10240,0);
if(UDPSock == -1)return; if(UDPSock == -1)return;
int Rcv = recvfrom(UDPSock, &Ret[0], 10240, 0, (sockaddr*)&FromServer, &clientLength); int32_t Rcv = recvfrom(UDPSock, &Ret[0], 10240, 0, (sockaddr*)&FromServer, &clientLength);
if (Rcv == SOCKET_ERROR)return; if (Rcv == SOCKET_ERROR)return;
Ret.resize(Rcv); UDPParser(Ret.substr(0,Rcv));
UDPParser(Ret);
} }
void UDPClientMain(const std::string& IP,int Port){ void UDPClientMain(const std::string& IP,int Port){
WSADATA data; WSADATA data;
+20 -15
View File
@@ -3,6 +3,7 @@
/// ///
#include <chrono> #include <chrono>
#include <vector>
#include "Logger.h" #include "Logger.h"
#include <iostream> #include <iostream>
#include <WS2tcpip.h> #include <WS2tcpip.h>
@@ -13,7 +14,8 @@
SOCKET TCPSock; SOCKET TCPSock;
bool CheckBytes(int32_t Bytes){ bool CheckBytes(int32_t Bytes){
if (Bytes == 0){ if (Bytes == 0){
debug(Sec("(TCP) Connection closing...")); debug(Sec("(TCP) Connection closing... CheckBytes(16)"));
Terminate = true; Terminate = true;
return false; return false;
}else if (Bytes < 0) { }else if (Bytes < 0) {
@@ -29,20 +31,23 @@ void TCPSend(const std::string&Data){
Terminate = true; Terminate = true;
return; return;
} }
// Size is BIG-ENDIAN! // Size is BIG-ENDIAN!
auto Size = htonl(int32_t(Data.size())); //auto Size = htonl(int32_t(Data.size()));
///TODO
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;
// Do not use Size before this point for anything but the header // Do not use Size before this point for anything but the header
Size = int32_t(Send.size()); Sent = 0;
int32_t Sent = 0,Temp; Size += 4;
do{ do{
Temp = send(TCPSock, &Send[Sent], Size - Sent, 0); Temp = send(TCPSock, &Send[Sent], Size - Sent, 0);
if(!CheckBytes(Temp))return; if(!CheckBytes(Temp))return;
Sent += Temp; Sent += Temp;
}while(Sent < Size); }while(Sent < Size);
} }
void TCPRcv(){ void TCPRcv(){
@@ -50,26 +55,26 @@ void TCPRcv(){
Terminate = true; Terminate = true;
return; return;
} }
static thread_local int32_t Header,BytesRcv,Temp; int32_t Header,BytesRcv,Temp;
BytesRcv = recv(TCPSock, reinterpret_cast<char*>(&Header), sizeof(Header),0); BytesRcv = recv(TCPSock, reinterpret_cast<char*>(&Header), sizeof(Header),0);
// convert back to LITTLE ENDIAN // convert back to LITTLE ENDIAN
Header = ntohl(Header); //Header = ntohl(Header);
if(!CheckBytes(BytesRcv))return; if(!CheckBytes(BytesRcv))return;
char* Data = new char[Header]; std::vector<char> Data(Header);
BytesRcv = 0; BytesRcv = 0;
do{ do{
Temp = recv(TCPSock,Data+BytesRcv,Header-BytesRcv,0); Temp = recv(TCPSock,&Data[BytesRcv],Header-BytesRcv,0);
if(!CheckBytes(Temp)){ if(!CheckBytes(Temp))return;
delete[] Data;
return;
}
BytesRcv += Temp; BytesRcv += Temp;
}while(BytesRcv < Header); }while(BytesRcv < Header);
std::string Ret = std::string(Data,Header);
delete[] Data; std::string Ret(Data.data(),Header);
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 server -> " + std::to_string(Ret.size()));
#endif
ServerParser(Ret); ServerParser(Ret);
} }