mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-07-01 23:46:59 +00:00
lot of work
This commit is contained in:
parent
2ab1a9dce6
commit
cd05b0a59a
@ -19,7 +19,7 @@ void UDPSend(std::string Data);
|
||||
void CoreNetwork();
|
||||
void SendLarge(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);
|
||||
void ServerParser(const std::string& Data);
|
||||
void TCPClientMain(const std::string& IP,int Port);
|
||||
|
@ -64,8 +64,17 @@ void Parse(std::string Data,SOCKET CSocket){
|
||||
break;
|
||||
case 'U':
|
||||
if(SubCode == 'l')Data = UlStatus;
|
||||
if(SubCode == 'p')Data = "Up" + std::to_string(ping);
|
||||
if(!SubCode)Data = std::string(UlStatus) + "\n" + "Up" + std::to_string(ping);
|
||||
if(SubCode == 'p'){
|
||||
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;
|
||||
case 'M':
|
||||
Data = MStatus;
|
||||
|
@ -15,8 +15,11 @@ bool CServer = true;
|
||||
extern SOCKET UDPSock;
|
||||
extern SOCKET TCPSock;
|
||||
SOCKET CSocket;
|
||||
void GameSend(const std::string&Data){
|
||||
void GameSend(std::string Data){
|
||||
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);
|
||||
if (iSRes == SOCKET_ERROR) {
|
||||
debug(Sec("(Proxy) send failed with error: ") + std::to_string(WSAGetLastError()));
|
||||
|
@ -55,7 +55,6 @@ void ClearAll(){
|
||||
}
|
||||
void UDPSend(std::string Data){
|
||||
if(ClientID == -1 || UDPSock == -1)return;
|
||||
Data = Data.substr(0,Data.find(char(0)));
|
||||
if(Data.length() > 400){
|
||||
std::string CMP(Comp(Data));
|
||||
Data = "ABG:" + CMP;
|
||||
@ -105,7 +104,6 @@ int SplitID(){
|
||||
return SID;
|
||||
}
|
||||
void SendLarge(std::string Data){
|
||||
Data = Data.substr(0,Data.find(char(0)));
|
||||
if(Data.length() > 400){
|
||||
std::string CMP(Comp(Data));
|
||||
Data = "ABG:" + CMP;
|
||||
@ -175,7 +173,6 @@ void UDPParser(std::string Packet){
|
||||
if(Packet.substr(0,4) == "ABG:"){
|
||||
Packet = DeComp(Packet.substr(4));
|
||||
}
|
||||
Packet = Packet.substr(0,Packet.find(char(0)));
|
||||
if(Packet.substr(0,4) == "TRG:"){
|
||||
AckID(stoi(Packet.substr(4)));
|
||||
return;
|
||||
@ -201,10 +198,9 @@ void UDPRcv(){
|
||||
ZeroMemory(&FromServer, clientLength);
|
||||
std::string Ret(10240,0);
|
||||
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;
|
||||
Ret.resize(Rcv);
|
||||
UDPParser(Ret);
|
||||
UDPParser(Ret.substr(0,Rcv));
|
||||
}
|
||||
void UDPClientMain(const std::string& IP,int Port){
|
||||
WSADATA data;
|
||||
|
@ -3,6 +3,7 @@
|
||||
///
|
||||
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include "Logger.h"
|
||||
#include <iostream>
|
||||
#include <WS2tcpip.h>
|
||||
@ -13,7 +14,8 @@
|
||||
SOCKET TCPSock;
|
||||
bool CheckBytes(int32_t Bytes){
|
||||
if (Bytes == 0){
|
||||
debug(Sec("(TCP) Connection closing..."));
|
||||
debug(Sec("(TCP) Connection closing... CheckBytes(16)"));
|
||||
|
||||
Terminate = true;
|
||||
return false;
|
||||
}else if (Bytes < 0) {
|
||||
@ -29,20 +31,23 @@ void TCPSend(const std::string&Data){
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// 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);
|
||||
Size = int32_t(Data.size());
|
||||
memcpy(&Send[0],&Size,sizeof(Size));
|
||||
Send += Data;
|
||||
// Do not use Size before this point for anything but the header
|
||||
Size = int32_t(Send.size());
|
||||
int32_t Sent = 0,Temp;
|
||||
Sent = 0;
|
||||
Size += 4;
|
||||
do{
|
||||
Temp = send(TCPSock, &Send[Sent], Size - Sent, 0);
|
||||
if(!CheckBytes(Temp))return;
|
||||
Sent += Temp;
|
||||
}while(Sent < Size);
|
||||
|
||||
}
|
||||
|
||||
void TCPRcv(){
|
||||
@ -50,26 +55,26 @@ void TCPRcv(){
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
static thread_local int32_t Header,BytesRcv,Temp;
|
||||
int32_t Header,BytesRcv,Temp;
|
||||
BytesRcv = recv(TCPSock, reinterpret_cast<char*>(&Header), sizeof(Header),0);
|
||||
// convert back to LITTLE ENDIAN
|
||||
Header = ntohl(Header);
|
||||
//Header = ntohl(Header);
|
||||
if(!CheckBytes(BytesRcv))return;
|
||||
char* Data = new char[Header];
|
||||
std::vector<char> Data(Header);
|
||||
BytesRcv = 0;
|
||||
do{
|
||||
Temp = recv(TCPSock,Data+BytesRcv,Header-BytesRcv,0);
|
||||
if(!CheckBytes(Temp)){
|
||||
delete[] Data;
|
||||
return;
|
||||
}
|
||||
Temp = recv(TCPSock,&Data[BytesRcv],Header-BytesRcv,0);
|
||||
if(!CheckBytes(Temp))return;
|
||||
BytesRcv += Temp;
|
||||
}while(BytesRcv < Header);
|
||||
std::string Ret = std::string(Data,Header);
|
||||
delete[] Data;
|
||||
|
||||
std::string Ret(Data.data(),Header);
|
||||
if (Ret.substr(0, 4) == "ABG:") {
|
||||
Ret = DeComp(Ret.substr(4));
|
||||
}
|
||||
#ifdef DEBUG
|
||||
//debug("Parsing from server -> " + std::to_string(Ret.size()));
|
||||
#endif
|
||||
ServerParser(Ret);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user