mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2025-07-01 23:46:59 +00:00
Major rewrite of the network
This commit is contained in:
parent
a132fd7b9f
commit
042671b146
@ -1,14 +1,14 @@
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /O2")
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/curl)
|
||||
|
||||
project(BeamMP-Launcher)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
|
||||
add_executable(BeamMP-Launcher main.cpp VehicleNetwork.cpp proxy.cpp Security.cpp http.cpp Discord.cpp UpdateCheck.cpp CoreNetwork.cpp Resources.cpp Compressor.cpp VehicleNetwork.cpp GameStart.cpp)
|
||||
file(GLOB source_files
|
||||
"src/*.cpp" "src/include/*.h"
|
||||
"src/include/*.hpp" "src/curl/*.h"
|
||||
"src/Network 2.0/*.cpp" "src/Network 2.0/*.h")
|
||||
|
||||
add_executable(BeamMP-Launcher ${source_files})
|
||||
|
||||
target_link_libraries(BeamMP-Launcher discord-rpc libcurl_a zlibstatic)
|
162
Resources.cpp
162
Resources.cpp
@ -1,162 +0,0 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 4/11/2020
|
||||
///
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::experimental::filesystem;
|
||||
void Exit(const std::string& Msg);
|
||||
extern bool MPDEV;
|
||||
std::string UlStatus = "Ulstart";
|
||||
std::string MStatus = " ";
|
||||
|
||||
std::vector<std::string> Split(const std::string& String,const std::string& delimiter){
|
||||
std::vector<std::string> Val;
|
||||
size_t pos = 0;
|
||||
std::string token,s = String;
|
||||
while ((pos = s.find(delimiter)) != std::string::npos) {
|
||||
token = s.substr(0, pos);
|
||||
Val.push_back(token);
|
||||
s.erase(0, pos + delimiter.length());
|
||||
}
|
||||
Val.push_back(s);
|
||||
return Val;
|
||||
}
|
||||
void ProxyThread(const std::string& IP, int port);
|
||||
void SyncResources(const std::string&IP,int Port){
|
||||
if(MPDEV)std::cout << "Called" << std::endl;
|
||||
std::string FileList;
|
||||
struct stat info{};
|
||||
if(stat( "Resources", &info) != 0){
|
||||
_wmkdir(L"Resources");
|
||||
}
|
||||
|
||||
WSADATA wsaData;
|
||||
SOCKET SendingSocket;
|
||||
SOCKADDR_IN ServerAddr, ThisSenderInfo;
|
||||
int RetCode;
|
||||
int BytesSent, nlen;
|
||||
|
||||
WSAStartup(MAKEWORD(2,2), &wsaData);
|
||||
|
||||
|
||||
SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
|
||||
if(SendingSocket == INVALID_SOCKET)
|
||||
{
|
||||
if(MPDEV)printf("Client: socket() failed! Error code: %d\n", WSAGetLastError());
|
||||
WSACleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
ServerAddr.sin_family = AF_INET;
|
||||
|
||||
ServerAddr.sin_port = htons(Port);
|
||||
|
||||
ServerAddr.sin_addr.s_addr = inet_addr(IP.c_str());
|
||||
|
||||
RetCode = connect(SendingSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
|
||||
|
||||
if(RetCode != 0)
|
||||
{
|
||||
if(MPDEV)printf("Client: connect() failed! Error code: %ld\n", WSAGetLastError());
|
||||
closesocket(SendingSocket);
|
||||
WSACleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
getsockname(SendingSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
|
||||
BytesSent = send(SendingSocket, "a", 1, 0);
|
||||
|
||||
int iResult;
|
||||
char recvbuf[65535];
|
||||
int recvbuflen = 65535;
|
||||
|
||||
do {
|
||||
iResult = recv(SendingSocket, recvbuf, recvbuflen, 0);
|
||||
if (iResult > 0) {
|
||||
std::string File, Data = recvbuf,Response,toSend;
|
||||
Data.resize(iResult);
|
||||
std::vector<std::string> list = Split(Data,";");
|
||||
std::vector<std::string> FileNames(list.begin(),list.begin() + (list.size()/2));
|
||||
std::vector<std::string> FileSizes(list.begin() + (list.size()/2), list.end());
|
||||
list.clear();
|
||||
Data.clear();
|
||||
Data.resize(recvbuflen);
|
||||
int index = 0;
|
||||
for(const std::string& a : FileNames){
|
||||
if(a.empty() || a.length() < 2)continue;
|
||||
if(stat(a.c_str(),&info)==0){
|
||||
if(fs::file_size(a)==std::stoi(FileSizes.at(index))){
|
||||
index++;
|
||||
continue;
|
||||
}else remove(a.c_str());
|
||||
}
|
||||
|
||||
std::ofstream LFS;
|
||||
LFS.open(a.c_str());
|
||||
LFS.close();
|
||||
toSend = "b"+a;
|
||||
//std::cout << a << std::endl;
|
||||
send(SendingSocket, toSend.c_str(), toSend.length(), 0);
|
||||
LFS.open (a.c_str(), std::ios_base::app | std::ios::binary);
|
||||
do{
|
||||
iResult = recv(SendingSocket, recvbuf, recvbuflen, 0);
|
||||
if(iResult < 0){File.clear(); break;}
|
||||
Data.resize(iResult);
|
||||
memcpy(&Data[0],recvbuf,iResult);
|
||||
if(Data.find("Cannot Open") != std::string::npos){File.clear();break;}
|
||||
LFS << Data;
|
||||
float per = LFS.tellp()/std::stof(FileSizes.at(index))*100;
|
||||
std::string Percent = std::to_string(truncf(per*10)/10);
|
||||
UlStatus="UlDownloading Resource: "+a.substr(a.find_last_of('/'))+" ("+Percent.substr(0,Percent.find('.')+2)+"%)";
|
||||
//std::cout << UlStatus << std::endl;
|
||||
}while(LFS.tellp() != std::stoi(FileSizes.at(index)));
|
||||
LFS.close();
|
||||
File.clear();
|
||||
}
|
||||
|
||||
toSend = "M";
|
||||
send(SendingSocket, toSend.c_str(), toSend.length(), 0);
|
||||
iResult = recv(SendingSocket, recvbuf, recvbuflen, 0);
|
||||
if(iResult < 0)break;
|
||||
Data.resize(iResult);
|
||||
memcpy(&Data[0],recvbuf,iResult);
|
||||
MStatus = "M" + Data;
|
||||
break;
|
||||
}
|
||||
else if (iResult == 0)
|
||||
if(MPDEV)printf("Connection closing...\n");
|
||||
else {
|
||||
if(MPDEV)printf("(Resources) recv failed with error: %d\n", WSAGetLastError());
|
||||
closesocket(SendingSocket);
|
||||
break;
|
||||
}
|
||||
}while (iResult > 0);
|
||||
if(BytesSent == SOCKET_ERROR)
|
||||
if(MPDEV)printf("Client: send() error %d.\n", WSAGetLastError());
|
||||
|
||||
|
||||
|
||||
if( shutdown(SendingSocket, SD_SEND) != 0)
|
||||
if(MPDEV)printf("Client: Well, there is something wrong with the shutdown() The error code: %d\n", WSAGetLastError());
|
||||
|
||||
|
||||
if(closesocket(SendingSocket) != 0)
|
||||
if(MPDEV)printf("Client: Cannot close \"SendingSocket\" socket. Error code: %d\n", WSAGetLastError());
|
||||
|
||||
|
||||
if(WSACleanup() != 0)
|
||||
if(MPDEV)printf("Client: WSACleanup() failed!...\n");
|
||||
|
||||
UlStatus = "Uldone";
|
||||
std::cout << "Done!" << std::endl;
|
||||
ProxyThread(IP,Port);
|
||||
}
|
5909
include/enet.hpp
5909
include/enet.hpp
File diff suppressed because it is too large
Load Diff
@ -10,9 +10,6 @@
|
||||
|
||||
extern int DEFAULT_PORT;
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wmissing-noreturn"
|
||||
|
||||
std::string HTTP_REQUEST(const std::string&url,int port);
|
||||
void SyncResources(const std::string& IP, int port);
|
||||
void Exit(const std::string& Msg);
|
||||
@ -22,9 +19,10 @@ extern int ping;
|
||||
extern bool Terminate;
|
||||
extern bool TCPTerminate;
|
||||
extern bool MPDEV;
|
||||
|
||||
void StartSync(const std::string &Data){
|
||||
std::thread t1(SyncResources,Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1)));
|
||||
//std::thread t1(SyncResources,"127.0.0.1",30814);
|
||||
//std::thread t1(SyncResources,Data.substr(1,Data.find(':')-1),std::stoi(Data.substr(Data.find(':')+1)));
|
||||
std::thread t1(SyncResources,"127.0.0.1",30814);
|
||||
t1.detach();
|
||||
}
|
||||
|
@ -13,7 +13,6 @@
|
||||
#include <vector>
|
||||
extern bool MPDEV;
|
||||
static const char* APPLICATION_ID = "629743237988352010";
|
||||
static int FrustrationLevel = 0;
|
||||
static int64_t StartTime;
|
||||
static int SendPresence = 1;
|
||||
static std::vector<std::string> LocalInfo;
|
||||
@ -126,7 +125,7 @@ static void discordInit()
|
||||
Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL);
|
||||
}
|
||||
|
||||
static void Loop()
|
||||
[[noreturn]] static void Loop()
|
||||
{
|
||||
char line[512];
|
||||
char* space;
|
@ -1,10 +1,9 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 5/2/2020
|
||||
///
|
||||
#include <windows.h>
|
||||
#include <Windows.h>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <string>
|
||||
|
||||
std::string QueryKey(HKEY hKey,int ID);
|
||||
void SystemExec(const std::string&cmd);
|
||||
@ -37,5 +36,7 @@ void StartGame(const std::string&ExeDir,const std::string&Current){
|
||||
std::thread RB(RollBack,Write(Current));
|
||||
RB.detach();
|
||||
SystemExec(ExeDir + " -nocrashreport");
|
||||
Exit("Game Closed!");
|
||||
std::cout << "\nGame Closed! launcher closing in 5 secs\n";
|
||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||
exit(2);
|
||||
}
|
@ -1,37 +1,29 @@
|
||||
////
|
||||
//// Created by Anonymous275 on 3/3/2020.
|
||||
////
|
||||
|
||||
#define ENET_IMPLEMENTATION
|
||||
|
||||
#include <condition_variable>
|
||||
#include "include/enet.hpp"
|
||||
#include <WinSock2.h>
|
||||
#include <WS2tcpip.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
|
||||
int ClientID = -1;
|
||||
|
||||
extern int DEFAULT_PORT;
|
||||
typedef struct {
|
||||
ENetHost *host;
|
||||
ENetPeer *peer;
|
||||
} Client;
|
||||
|
||||
std::vector<std::string> Split(const std::string& String,const std::string& delimiter);
|
||||
std::chrono::time_point<std::chrono::steady_clock> PingStart,PingEnd;
|
||||
extern std::vector<std::string> GlobalInfo;
|
||||
bool TCPTerminate = false;
|
||||
bool Terminate = false;
|
||||
bool CServer = true;
|
||||
ENetPeer*ServerPeer;
|
||||
SOCKET*ClientSocket;
|
||||
extern bool MPDEV;
|
||||
int ping = 0;
|
||||
|
||||
[[noreturn]] void CoreNetworkThread();
|
||||
|
||||
void TCPSEND(const std::string&Data){
|
||||
void GameSend(const std::string&Data){
|
||||
if(!TCPTerminate) {
|
||||
int iSendResult = send(*ClientSocket, (Data + "\n").c_str(), int(Data.length()) + 1, 0);
|
||||
if (iSendResult == SOCKET_ERROR) {
|
||||
@ -45,121 +37,97 @@ void TCPSEND(const std::string&Data){
|
||||
}
|
||||
}
|
||||
}
|
||||
void RUDPSEND(const std::string&Data,bool Rel){
|
||||
if(!Terminate && ServerPeer != nullptr){
|
||||
void TCPSend(const std::string&Data);
|
||||
void UDPSend(const std::string&Data);
|
||||
void ServerSend(const std::string&Data, bool Rel){
|
||||
if(!Terminate){
|
||||
char C = 0;
|
||||
if(Data.length() > 3)C = Data.at(0);
|
||||
if (C == 'O' || C == 'T')Rel = true;
|
||||
enet_peer_send(ServerPeer, 0, enet_packet_create(Data.c_str(), Data.length()+1, Rel?1:8));
|
||||
|
||||
if(Rel)TCPSend(Data);
|
||||
else UDPSend(Data);
|
||||
|
||||
if (MPDEV && Data.length() > 1000) {
|
||||
std::cout << "(Launcher->Server) Bytes sent: " << Data.length()
|
||||
<< " : "
|
||||
<< Data.substr(0, 10)
|
||||
<< Data.substr(Data.length() - 10) << std::endl;
|
||||
}else if(MPDEV){
|
||||
std::cout << "(Game->Launcher) : " << Data << std::endl;
|
||||
}else if(MPDEV && C == 'Z'){
|
||||
//std::cout << "(Game->Launcher) : " << Data << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
void NameRespond(){
|
||||
std::string Packet = "NR" + GlobalInfo.at(0)+":"+GlobalInfo.at(2);
|
||||
RUDPSEND(Packet,true);
|
||||
ServerSend(Packet,true);
|
||||
}
|
||||
|
||||
void AutoPing(ENetPeer*peer){
|
||||
while(!Terminate && peer != nullptr){
|
||||
RUDPSEND("p",true);
|
||||
void AutoPing(){
|
||||
while(!Terminate){
|
||||
ServerSend("p",false);
|
||||
PingStart = std::chrono::high_resolution_clock::now();
|
||||
std::this_thread::sleep_for(std::chrono::seconds (1));
|
||||
}
|
||||
}
|
||||
|
||||
void RUDPParser(const std::string& Data){
|
||||
std::string UlStatus = "Ulstart";
|
||||
std::string MStatus = " ";
|
||||
void ServerParser(const std::string& Data){
|
||||
char Code = Data.at(0),SubCode = 0;
|
||||
if(Data.length() > 1)SubCode = Data.at(1);
|
||||
switch (Code) {
|
||||
case 'P':
|
||||
ClientID = std::stoi(Data.substr(1));
|
||||
break;
|
||||
case 'p':
|
||||
PingEnd = std::chrono::high_resolution_clock::now();
|
||||
ping = int(std::chrono::duration_cast<std::chrono::microseconds>(PingEnd-PingStart).count())/1000;
|
||||
ping = std::chrono::duration_cast<std::chrono::milliseconds>(PingEnd-PingStart).count();
|
||||
return;
|
||||
case 'N':
|
||||
if(SubCode == 'R')NameRespond();
|
||||
return;
|
||||
case 'M':
|
||||
MStatus = Data;
|
||||
UlStatus = "Uldone";
|
||||
return;
|
||||
}
|
||||
///std::cout << "Received: " << Data << std::endl;
|
||||
TCPSEND(Data);
|
||||
GameSend(Data);
|
||||
}
|
||||
|
||||
void HandleEvent(ENetEvent event,Client client){
|
||||
switch (event.type){
|
||||
case ENET_EVENT_TYPE_CONNECT:
|
||||
std::cout << "Connected to server!" << std::endl;
|
||||
//printf("Client Connected port : %u.\n",event.peer->address.port);
|
||||
event.peer->data = (void*)"Connected Server";
|
||||
break;
|
||||
case ENET_EVENT_TYPE_RECEIVE:
|
||||
RUDPParser((char*)event.packet->data);
|
||||
enet_packet_destroy(event.packet);
|
||||
break;
|
||||
case ENET_EVENT_TYPE_DISCONNECT:
|
||||
printf ("%s disconnected.\n", (char *)event.peer->data);
|
||||
CServer = true;
|
||||
Terminate = true;
|
||||
event.peer->data = nullptr;
|
||||
break;
|
||||
|
||||
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
|
||||
printf ("%s timeout.\n", (char *)event.peer->data);
|
||||
CServer = true;
|
||||
Terminate = true;
|
||||
TCPSEND("TTimeout");
|
||||
event.peer->data = nullptr;
|
||||
break;
|
||||
case ENET_EVENT_TYPE_NONE: break;
|
||||
}
|
||||
}
|
||||
void RUDPClientThread(const std::string& IP, int Port){
|
||||
std::condition_variable lock;
|
||||
if (enet_initialize() != 0) {
|
||||
std::cout << "An error occurred while initializing!\n";
|
||||
}
|
||||
Client client;
|
||||
ENetAddress address = {0};
|
||||
|
||||
address.host = ENET_HOST_ANY;
|
||||
address.port = Port;
|
||||
if(MPDEV)std::cout << "(Launcher->Server) Connecting...\n";
|
||||
|
||||
enet_address_set_host(&address, IP.c_str());
|
||||
client.host = enet_host_create(nullptr, 1, 2, 0, 0);
|
||||
client.peer = enet_host_connect(client.host, &address, 2, 0);
|
||||
if (client.peer == nullptr) {
|
||||
if(MPDEV)std::cout << "could not connect\n";
|
||||
TCPSEND("TTimeout");
|
||||
TCPTerminate = true;
|
||||
Terminate = true;
|
||||
}
|
||||
std::thread Ping(AutoPing,client.peer);
|
||||
void TCPClientMain(const std::string& IP,int Port);
|
||||
void UDPClientMain(const std::string& IP,int Port);
|
||||
void NetMain(const std::string& IP, int Port){
|
||||
std::thread Ping(AutoPing);
|
||||
Ping.detach();
|
||||
ENetEvent event;
|
||||
while (!Terminate) {
|
||||
ServerPeer = client.peer;
|
||||
enet_host_service(client.host, &event, 1);
|
||||
HandleEvent(event,client);
|
||||
}
|
||||
enet_peer_disconnect(client.peer,0);
|
||||
enet_host_service(client.host, &event, 1);
|
||||
HandleEvent(event,client);
|
||||
UDPClientMain(IP,Port);
|
||||
CServer = true;
|
||||
Terminate = true;
|
||||
std::cout << "Connection Terminated!" << std::endl;
|
||||
}
|
||||
extern SOCKET UDPSock;
|
||||
extern SOCKET TCPSock;
|
||||
void Reset() {
|
||||
ClientSocket = nullptr;
|
||||
TCPTerminate = false;
|
||||
Terminate = false;
|
||||
UlStatus = "Ulstart";
|
||||
MStatus = " ";
|
||||
UDPSock = -1;
|
||||
TCPSock = -1;
|
||||
}
|
||||
|
||||
std::string Compress(const std::string&Data);
|
||||
std::string Decompress(const std::string&Data);
|
||||
void TCPServerThread(const std::string& IP, int Port){
|
||||
if(MPDEV)std::cout << "Proxy Started! " << IP << ":" << Port << std::endl;
|
||||
void TCPGameServer(const std::string& IP, int Port){
|
||||
if(MPDEV)std::cout << "Game server Started! " << IP << ":" << Port << std::endl;
|
||||
do {
|
||||
if(MPDEV)std::cout << "Proxy on Start" << std::endl;
|
||||
Reset();
|
||||
if(CServer) {
|
||||
std::thread Client(TCPClientMain, IP, Port);
|
||||
Client.detach();
|
||||
}
|
||||
if(MPDEV)std::cout << "Game server on Start" << std::endl;
|
||||
WSADATA wsaData;
|
||||
int iResult;
|
||||
SOCKET ListenSocket = INVALID_SOCKET;
|
||||
@ -228,10 +196,11 @@ void TCPServerThread(const std::string& IP, int Port){
|
||||
closesocket(ListenSocket);
|
||||
if(MPDEV)std::cout << "(Proxy) Game Connected!" << std::endl;
|
||||
if(CServer){
|
||||
std::thread t1(RUDPClientThread, IP, Port);
|
||||
std::thread t1(NetMain, IP, Port);
|
||||
t1.detach();
|
||||
CServer = false;
|
||||
}
|
||||
|
||||
ClientSocket = &Socket;
|
||||
do {
|
||||
//std::cout << "(Proxy) Waiting for Game Data..." << std::endl;
|
||||
@ -241,23 +210,14 @@ void TCPServerThread(const std::string& IP, int Port){
|
||||
buff.resize(iResult*2);
|
||||
memcpy(&buff[0],recvbuf,iResult);
|
||||
buff.resize(iResult);
|
||||
/*if(MPDEV && buff.length() > 1000) {
|
||||
std::string cmp = Compress(buff), dcm = Decompress(cmp);
|
||||
std::cout << "Compressed Size : " << cmp.length() << std::endl;
|
||||
std::cout << "Decompressed Size : " << dcm.length() << std::endl;
|
||||
if (cmp == dcm) {
|
||||
std::cout << "Success!" << std::endl;
|
||||
} else {
|
||||
std::cout << "Fail!" << std::endl;
|
||||
}
|
||||
}*/
|
||||
RUDPSEND(buff,false);
|
||||
//std::cout << "(Game->Launcher) Data : " << buff.length() << std::endl;
|
||||
|
||||
ServerSend(buff,false);
|
||||
|
||||
} else if (iResult == 0) {
|
||||
if(MPDEV)std::cout << "(Proxy) Connection closing...\n";
|
||||
closesocket(Socket);
|
||||
WSACleanup();
|
||||
|
||||
Terminate = true;
|
||||
continue;
|
||||
} else {
|
||||
if(MPDEV)std::cout << "(Proxy) recv failed with error: " << WSAGetLastError() << std::endl;
|
||||
@ -282,22 +242,15 @@ void TCPServerThread(const std::string& IP, int Port){
|
||||
}
|
||||
|
||||
void VehicleNetworkStart();
|
||||
|
||||
void CoreNetworkThread();
|
||||
void ProxyStart(){
|
||||
std::thread t1(CoreNetworkThread);
|
||||
if(MPDEV)std::cout << "Core Network Started!\n";
|
||||
t1.join();
|
||||
}
|
||||
void Reset() {
|
||||
ClientSocket = nullptr;
|
||||
ServerPeer = nullptr;
|
||||
TCPTerminate = false;
|
||||
Terminate = false;
|
||||
}
|
||||
|
||||
void ProxyThread(const std::string& IP, int Port){
|
||||
Reset();
|
||||
auto*t1 = new std::thread(TCPServerThread,IP,Port);
|
||||
auto*t1 = new std::thread(TCPGameServer,IP,Port);
|
||||
t1->detach();
|
||||
/*std::thread t2(VehicleNetworkStart);
|
||||
t2.detach();*/
|
62
src/Network 2.0/VehicleData.cpp
Normal file
62
src/Network 2.0/VehicleData.cpp
Normal file
@ -0,0 +1,62 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 5/8/2020
|
||||
///
|
||||
|
||||
|
||||
#include <WS2tcpip.h>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
extern bool Terminate;
|
||||
extern int ClientID;
|
||||
SOCKET UDPSock;
|
||||
sockaddr_in ToServer{};
|
||||
|
||||
void UDPSend(const std::string&Data){
|
||||
if(ClientID == -1 || UDPSock == INVALID_SOCKET)return;
|
||||
std::string Packet = char(ClientID+1) + std::string(":") + Data;
|
||||
int sendOk = sendto(UDPSock, Packet.c_str(), int(Packet.length()) + 1, 0, (sockaddr*)&ToServer, sizeof(ToServer));
|
||||
if (sendOk == SOCKET_ERROR)std::cout << "Error Code : " << WSAGetLastError() << std::endl;
|
||||
}
|
||||
|
||||
void ServerParser(const std::string& Data);
|
||||
void UDPRcv(){
|
||||
char buf[4096];
|
||||
int len = 4096;
|
||||
sockaddr_in FromServer{};
|
||||
int clientLength = sizeof(FromServer);
|
||||
ZeroMemory(&FromServer, clientLength);
|
||||
ZeroMemory(buf, len);
|
||||
if(UDPSock == INVALID_SOCKET)return;
|
||||
int bytesIn = recvfrom(UDPSock, buf, len, 0, (sockaddr*)&FromServer, &clientLength);
|
||||
if (bytesIn == SOCKET_ERROR)
|
||||
{
|
||||
//std::cout << "Error receiving from Server " << WSAGetLastError() << std::endl;
|
||||
return;
|
||||
}
|
||||
ServerParser(std::string(buf));
|
||||
}
|
||||
void TCPSend(const std::string&Data);
|
||||
void UDPClientMain(const std::string& IP,int Port){
|
||||
WSADATA data;
|
||||
if (WSAStartup(514, &data)) //2.2
|
||||
{
|
||||
std::cout << "Can't start Winsock! " << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
ToServer.sin_family = AF_INET;
|
||||
ToServer.sin_port = htons(Port);
|
||||
inet_pton(AF_INET, IP.c_str(), &ToServer.sin_addr);
|
||||
UDPSock = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
|
||||
|
||||
TCPSend("P");
|
||||
UDPSend("p");
|
||||
while (!Terminate){
|
||||
UDPRcv();
|
||||
}
|
||||
|
||||
closesocket(UDPSock);
|
||||
WSACleanup();
|
||||
}
|
96
src/Network 2.0/VehicleEvent.cpp
Normal file
96
src/Network 2.0/VehicleEvent.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 5/8/2020
|
||||
///
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <WS2tcpip.h>
|
||||
#pragma comment (lib, "ws2_32.lib")
|
||||
extern bool Terminate;
|
||||
extern bool MPDEV;
|
||||
SOCKET TCPSock;
|
||||
void TCPSend(const std::string&Data){
|
||||
if(TCPSock == INVALID_SOCKET){
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
int BytesSent = send(TCPSock, Data.c_str(), int(Data.length())+1, 0);
|
||||
if (BytesSent == 0){
|
||||
if(MPDEV)std::cout << "(TCP) Connection closing..." << std::endl;
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
else if (BytesSent < 0) {
|
||||
if(MPDEV)std::cout << "(TCP) send failed with error: " << WSAGetLastError() << std::endl;
|
||||
closesocket(TCPSock);
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerParser(const std::string& Data);
|
||||
void TCPRcv(){
|
||||
char buf[10240];
|
||||
int len = 10240;
|
||||
ZeroMemory(buf, len);
|
||||
if(TCPSock == INVALID_SOCKET){
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
int BytesRcv = recv(TCPSock, buf, len,0);
|
||||
if (BytesRcv == 0){
|
||||
if(MPDEV)std::cout << "(TCP) Connection closing..." << std::endl;
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
else if (BytesRcv < 0) {
|
||||
if(MPDEV)std::cout << "(TCP) recv failed with error: " << WSAGetLastError() << std::endl;
|
||||
closesocket(TCPSock);
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
ServerParser(std::string(buf));
|
||||
}
|
||||
|
||||
|
||||
void TCPClientMain(const std::string& IP,int Port){
|
||||
WSADATA wsaData;
|
||||
SOCKADDR_IN ServerAddr;
|
||||
int RetCode;
|
||||
|
||||
WSAStartup(514, &wsaData); //2.2
|
||||
TCPSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if(TCPSock == -1)
|
||||
{
|
||||
printf("Client: socket failed! Error code: %d\n", WSAGetLastError());
|
||||
WSACleanup();
|
||||
return;
|
||||
}
|
||||
ServerAddr.sin_family = AF_INET;
|
||||
ServerAddr.sin_port = htons(Port);
|
||||
inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr);
|
||||
RetCode = connect(TCPSock, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
|
||||
if(RetCode != 0)
|
||||
{
|
||||
std::cout << "Client: connect failed! Error code: " << WSAGetLastError() << std::endl;
|
||||
closesocket(TCPSock);
|
||||
WSACleanup();
|
||||
return;
|
||||
}
|
||||
getsockname(TCPSock, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
|
||||
|
||||
while(!Terminate){
|
||||
TCPRcv();
|
||||
}
|
||||
|
||||
|
||||
if( shutdown(TCPSock, SD_SEND) != 0 && MPDEV)
|
||||
std::cout << "(TCP) shutdown error code: " << WSAGetLastError() << std::endl;
|
||||
|
||||
if(closesocket(TCPSock) != 0 && MPDEV)
|
||||
std::cout << "(TCP) Cannot close socket. Error code: " << WSAGetLastError() << std::endl;
|
||||
|
||||
if(WSACleanup() != 0 && MPDEV)
|
||||
std::cout << "(TCP) Client: WSACleanup() failed!..." << std::endl;
|
||||
}
|
161
src/Resources.cpp
Normal file
161
src/Resources.cpp
Normal file
@ -0,0 +1,161 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 4/11/2020
|
||||
///
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
#include <WS2tcpip.h>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
||||
namespace fs = std::experimental::filesystem;
|
||||
void Exit(const std::string& Msg);
|
||||
extern bool MPDEV;
|
||||
|
||||
std::vector<std::string> Split(const std::string& String,const std::string& delimiter){
|
||||
std::vector<std::string> Val;
|
||||
size_t pos = 0;
|
||||
std::string token,s = String;
|
||||
while ((pos = s.find(delimiter)) != std::string::npos) {
|
||||
token = s.substr(0, pos);
|
||||
Val.push_back(token);
|
||||
s.erase(0, pos + delimiter.length());
|
||||
}
|
||||
Val.push_back(s);
|
||||
return Val;
|
||||
}
|
||||
std::string STCPRecv(SOCKET socket){
|
||||
char buf[65535];
|
||||
int len = 65535;
|
||||
ZeroMemory(buf, len);
|
||||
int BytesRcv = recv(socket, buf, len,0);
|
||||
if (BytesRcv == 0){
|
||||
std::cout << "(TCP) Connection closing..." << std::endl;
|
||||
return "";
|
||||
}
|
||||
else if (BytesRcv < 0) {
|
||||
std::cout << "(TCP) recv failed with error: " << WSAGetLastError() << std::endl;
|
||||
closesocket(socket);
|
||||
return "";
|
||||
}
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
void ProxyThread(const std::string& IP, int port);
|
||||
void SyncResources(const std::string&IP,int Port){
|
||||
if(MPDEV)std::cout << "Called" << std::endl;
|
||||
std::string FileList;
|
||||
struct stat info{};
|
||||
if(stat( "Resources", &info) != 0){
|
||||
_wmkdir(L"Resources");
|
||||
}
|
||||
|
||||
/*WSADATA wsaData;
|
||||
SOCKET SendingSocket;
|
||||
SOCKADDR_IN ServerAddr;
|
||||
int RetCode;
|
||||
int BytesSent, nlen;
|
||||
|
||||
WSAStartup(514, &wsaData); //2.2
|
||||
|
||||
|
||||
SendingSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
|
||||
if(SendingSocket == -1)
|
||||
{
|
||||
if(MPDEV)printf("Client: socket() failed! Error code: %d\n", WSAGetLastError());
|
||||
WSACleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
ServerAddr.sin_family = AF_INET;
|
||||
ServerAddr.sin_port = htons(Port);
|
||||
inet_pton(AF_INET, IP.c_str(), &ServerAddr.sin_addr);
|
||||
|
||||
RetCode = connect(SendingSocket, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
|
||||
|
||||
if(RetCode != 0)
|
||||
{
|
||||
if(MPDEV)std::cout <<"Client: connect() failed! Error code: " << WSAGetLastError() << std::endl;
|
||||
closesocket(SendingSocket);
|
||||
WSACleanup();
|
||||
return;
|
||||
}
|
||||
|
||||
getsockname(SendingSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
|
||||
BytesSent = send(SendingSocket, "a", 1, 0);
|
||||
|
||||
std::string File, Response, toSend, Data = STCPRecv(SendingSocket);
|
||||
std::cout << Data << std::endl;
|
||||
if (!Data.empty()) {
|
||||
std::vector<std::string> list = Split(Data, ";");
|
||||
std::vector<std::string> FileNames(list.begin(), list.begin() + (list.size() / 2));
|
||||
std::vector<std::string> FileSizes(list.begin() + (list.size() / 2), list.end());
|
||||
list.clear();
|
||||
int index = 0;
|
||||
for (const std::string &a : FileNames) {
|
||||
if (a.empty() || a.length() < 2)continue;
|
||||
if (stat(a.c_str(), &info) == 0) {
|
||||
if (fs::file_size(a) == std::stoi(FileSizes.at(index))) {
|
||||
index++;
|
||||
continue;
|
||||
} else remove(a.c_str());
|
||||
}
|
||||
|
||||
std::ofstream LFS;
|
||||
LFS.open(a.c_str());
|
||||
LFS.close();
|
||||
toSend = "b" + a;
|
||||
send(SendingSocket, toSend.c_str(), toSend.length(), 0);
|
||||
LFS.open(a.c_str(), std::ios_base::app | std::ios::binary);
|
||||
do {
|
||||
Data = STCPRecv(SendingSocket);
|
||||
if (Data.empty()) {
|
||||
File.clear();
|
||||
break;
|
||||
}
|
||||
|
||||
if (Data.find("Cannot Open") != std::string::npos) {
|
||||
File.clear();
|
||||
break;
|
||||
}
|
||||
LFS << Data;
|
||||
float per = LFS.tellp() / std::stof(FileSizes.at(index)) * 100;
|
||||
std::string Percent = std::to_string(truncf(per * 10) / 10);
|
||||
UlStatus = "UlDownloading Resource: " + a.substr(a.find_last_of('/')) + " (" +
|
||||
Percent.substr(0, Percent.find('.') + 2) + "%)";
|
||||
} while (LFS.tellp() != std::stoi(FileSizes.at(index)));
|
||||
LFS.close();
|
||||
File.clear();
|
||||
}
|
||||
|
||||
toSend = "M";
|
||||
send(SendingSocket, toSend.c_str(), toSend.length(), 0);
|
||||
Data = STCPRecv(SendingSocket);
|
||||
MStatus = "M" + Data;
|
||||
}
|
||||
UlStatus = "Uldone";
|
||||
std::cout << "Done!" << std::endl;
|
||||
|
||||
if(BytesSent == SOCKET_ERROR)
|
||||
if(MPDEV)printf("Client: send() error %d.\n", WSAGetLastError());
|
||||
|
||||
|
||||
|
||||
if( shutdown(SendingSocket, SD_SEND) != 0)
|
||||
if(MPDEV)printf("Client: Well, there is something wrong with the shutdown() The error code: %d\n", WSAGetLastError());
|
||||
|
||||
|
||||
if(closesocket(SendingSocket) != 0)
|
||||
if(MPDEV)printf("Client: Cannot close \"SendingSocket\" socket. Error code: %d\n", WSAGetLastError());
|
||||
|
||||
|
||||
if(WSACleanup() != 0)
|
||||
if(MPDEV)printf("Client: WSACleanup() failed!...\n");
|
||||
*/
|
||||
|
||||
ProxyThread(IP,Port);
|
||||
}
|
@ -13,7 +13,7 @@ extern bool TCPTerminate;
|
||||
extern bool MPDEV;
|
||||
void Print(const std::string&MSG);
|
||||
std::queue<std::string> VNTCPQueue;
|
||||
void RUDPSEND(const std::string&Data,bool Rel);
|
||||
//void RUDPSEND(const std::string&Data,bool Rel);
|
||||
#define DEFAULT_PORT "4446"
|
||||
|
||||
void Responder(const SOCKET *CS){
|
||||
@ -135,7 +135,7 @@ void VehicleNetworkStart(){
|
||||
std::cout << "Fail!" << std::endl;
|
||||
}
|
||||
}
|
||||
RUDPSEND(buff,false);
|
||||
//RUDPSEND(buff,false);
|
||||
//std::cout << "(Game->Launcher VN) Data : " << buff.length() << std::endl;
|
||||
} else if (iResult == 0) {
|
||||
if(MPDEV)std::cout << "(VN) Connection closing...\n";
|
@ -60,8 +60,8 @@
|
||||
defined(__LWIP_OPT_H__) || defined(LWIP_HDR_OPT_H))
|
||||
/* The check above prevents the winsock2 inclusion if winsock.h already was
|
||||
included, since they can't co-exist without problems */
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <WinSock2.h>
|
||||
#include <WS2tcpip.h>
|
||||
#endif
|
||||
#endif
|
||||
|
5906
src/include/enet.h
Normal file
5906
src/include/enet.h
Normal file
File diff suppressed because it is too large
Load Diff
@ -87,7 +87,7 @@ int main(int argc, char* argv[]){
|
||||
const unsigned long long NPos = std::string::npos;
|
||||
struct stat info{};
|
||||
|
||||
std::string ver = "0.921", link, Path = CheckDir(argv[0],ver),HTTP_Result;
|
||||
std::string ver = "1.0", link, Path = CheckDir(argv[0],ver),HTTP_Result;
|
||||
std::thread CFU(CheckForUpdates,ver);
|
||||
CFU.join();
|
||||
if(argc > 1){
|
||||
@ -115,7 +115,7 @@ int main(int argc, char* argv[]){
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
}else MPDEV = true;
|
||||
}else MPDEV = false;
|
||||
//Security
|
||||
auto*Sec = new std::thread(Check);
|
||||
Sec->join();
|
||||
@ -133,14 +133,12 @@ int main(int argc, char* argv[]){
|
||||
Download(link,Settings);
|
||||
std::cout << "Download Complete!" << std::endl;
|
||||
}
|
||||
std::cout << "Downloading mod..." << std::endl;
|
||||
link = "https://beamng-mp.com/builds/client?did="+GlobalInfo.at(2);
|
||||
Download(link,Path + R"(\mods\BeamMP.zip)");
|
||||
std::cout << "Download Complete!" << std::endl;
|
||||
link.clear();
|
||||
std::thread Game(StartGame,ExeDir,(Path + "\\"));
|
||||
Game.detach();
|
||||
if(!MPDEV){
|
||||
std::cout << "Downloading mod..." << std::endl;
|
||||
link = "https://beamng-mp.com/builds/client?did="+GlobalInfo.at(2);
|
||||
Download(link,Path + R"(\mods\BeamMP.zip)");
|
||||
std::cout << "Download Complete!" << std::endl;
|
||||
link.clear();
|
||||
std::thread Game(StartGame,ExeDir,(Path + "\\"));
|
||||
Game.detach();
|
||||
}else{
|
Loading…
x
Reference in New Issue
Block a user