mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-04 14:56:24 +00:00
V1.6
Rewrite
This commit is contained in:
92
src/Network/VehicleEvent.cpp
Normal file
92
src/Network/VehicleEvent.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
///
|
||||
/// Created by Anonymous275 on 5/8/2020
|
||||
///
|
||||
|
||||
#include <chrono>
|
||||
#include "Logger.h"
|
||||
#include <iostream>
|
||||
#include <WS2tcpip.h>
|
||||
#include "Security/Enc.h"
|
||||
#include "Network/network.h"
|
||||
|
||||
SOCKET TCPSock;
|
||||
void TCPSend(const std::string&Data){
|
||||
if(TCPSock == -1){
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
int BytesSent = send(TCPSock, Data.c_str(), int(Data.length())+1, 0);
|
||||
if (BytesSent == 0){
|
||||
debug(Sec("(TCP) Connection closing..."));
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
else if (BytesSent < 0) {
|
||||
debug(Sec("(TCP) send failed with error: ") + std::to_string(WSAGetLastError()));
|
||||
closesocket(TCPSock);
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ServerParser(const std::string& Data);
|
||||
void TCPRcv(){
|
||||
char buf[4096];
|
||||
int len = 4096;
|
||||
ZeroMemory(buf, len);
|
||||
if(TCPSock == -1){
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
int BytesRcv = recv(TCPSock, buf, len,0);
|
||||
if (BytesRcv == 0){
|
||||
debug(Sec("(TCP) Connection closing..."));
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
else if (BytesRcv < 0) {
|
||||
debug(Sec("(TCP) recv failed with error: ") + std::to_string(WSAGetLastError()));
|
||||
closesocket(TCPSock);
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
ServerParser(std::string(buf));
|
||||
}
|
||||
|
||||
void SyncResources(SOCKET TCPSock);
|
||||
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(Sec("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){
|
||||
UlStatus = Sec("UlConnection Failed!");
|
||||
std::cout << Sec("Client: connect failed! Error code: ") << WSAGetLastError() << std::endl;
|
||||
closesocket(TCPSock);
|
||||
WSACleanup();
|
||||
Terminate = true;
|
||||
return;
|
||||
}
|
||||
getsockname(TCPSock, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));
|
||||
|
||||
SyncResources(TCPSock);
|
||||
while(!Terminate)TCPRcv();
|
||||
GameSend(Sec("T"));
|
||||
////Game Send Terminate
|
||||
if(closesocket(TCPSock) != 0)
|
||||
debug(Sec("(TCP) Cannot close socket. Error code: ") + std::to_string(WSAGetLastError()));
|
||||
|
||||
if(WSACleanup() != 0)
|
||||
debug(Sec("(TCP) Client: WSACleanup() failed!..."));
|
||||
}
|
||||
Reference in New Issue
Block a user