high accuracy ping + cleanup

This commit is contained in:
Anonymous275 2020-04-16 22:55:12 +03:00
parent 4d42289b1d
commit e5b0c6591b
4 changed files with 43 additions and 23 deletions

View File

@ -9,6 +9,6 @@ project(BeamMP-Launcher)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
add_executable(BeamMP-Launcher main.cpp proxy.cpp Security.cpp http.cpp Discord.cpp UpdateCheck.cpp CoreNetwork.cpp Resources.cpp) add_executable(BeamMP-Launcher main.cpp proxy.cpp Security.cpp http.cpp Discord.cpp UpdateCheck.cpp CoreNetwork.cpp Resources.cpp ProxyParser.cpp)
target_link_libraries(BeamMP-Launcher discord-rpc libcurl_a) target_link_libraries(BeamMP-Launcher discord-rpc libcurl_a)

4
ProxyParser.cpp Normal file
View File

@ -0,0 +1,4 @@
///
/// Created by Anonymous275 on 4/16/2020
///

View File

@ -35,7 +35,7 @@ void Exit(const std::string& Msg){
exit(-1); exit(-1);
} }
std::string CheckDir(char*dir, std::string ver){ std::string CheckDir(char*dir, const std::string& ver){
system(("title BeamMP Launcher v" + ver).c_str()); system(("title BeamMP Launcher v" + ver).c_str());
char*temp;size_t len; char*temp;size_t len;
_dupenv_s(&temp, &len,"APPDATA"); _dupenv_s(&temp, &len,"APPDATA");
@ -79,7 +79,7 @@ std::string CheckVer(const std::string &path){
void SyncResources(const std::string&IP,int Port); void SyncResources(const std::string&IP,int Port);
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
std::string ver = "0.36", Path = CheckDir(argv[0],ver),HTTP_Result; std::string ver = "0.38", Path = CheckDir(argv[0],ver),HTTP_Result;
CheckForUpdates(ver); //Update Check CheckForUpdates(ver); //Update Check
//Security //Security

View File

@ -23,10 +23,29 @@ typedef struct {
std::queue<std::string> RUDPData; std::queue<std::string> RUDPData;
std::queue<std::string> RUDPToSend; std::queue<std::string> RUDPToSend;
bool Terminate = false; bool Terminate = false;
int ping = 0; int ping = 0;
std::chrono::time_point<std::chrono::steady_clock> PingStart;
void CoreNetworkThread(); void CoreNetworkThread();
void AutoPing(ENetPeer*peer){
while(!Terminate){
enet_peer_send(peer, 0, enet_packet_create("p", 1, ENET_PACKET_FLAG_RELIABLE));
PingStart = std::chrono::high_resolution_clock::now();
Sleep(1000);
}
}
void RUDPParser(const std::string& Data){
if(Data == "p"){
auto PingEnd = std::chrono::high_resolution_clock::now();
ping = std::chrono::duration_cast<std::chrono::milliseconds>(PingEnd-PingStart).count();
return;
}
std::cout << "Received: " << Data << std::endl;
RUDPData.push(Data);
}
#pragma clang diagnostic pop #pragma clang diagnostic pop
#pragma clang diagnostic pop #pragma clang diagnostic pop
void HandleEvent(ENetEvent event,Client client){ void HandleEvent(ENetEvent event,Client client){
@ -37,19 +56,18 @@ void HandleEvent(ENetEvent event,Client client){
event.peer->data = (void *)"Connected Server"; event.peer->data = (void *)"Connected Server";
break; break;
case ENET_EVENT_TYPE_RECEIVE: case ENET_EVENT_TYPE_RECEIVE:
printf("Received: %s\n",event.packet->data); RUDPParser((char*)event.packet->data);
RUDPData.push(reinterpret_cast<const char *const>(event.packet->data));
enet_packet_destroy (event.packet); enet_packet_destroy (event.packet);
break; break;
case ENET_EVENT_TYPE_DISCONNECT: case ENET_EVENT_TYPE_DISCONNECT:
printf ("%s disconnected.\n", (char *)event.peer->data); printf ("%s disconnected.\n", (char *)event.peer->data);
// Reset the peer's client information. // Reset the peer's client information.
event.peer->data = NULL; event.peer->data = nullptr;
break; break;
case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT: case ENET_EVENT_TYPE_DISCONNECT_TIMEOUT:
printf ("%s timeout.\n", (char *)event.peer->data); printf ("%s timeout.\n", (char *)event.peer->data);
event.peer->data = NULL; event.peer->data = nullptr;
break; break;
case ENET_EVENT_TYPE_NONE: break; case ENET_EVENT_TYPE_NONE: break;
@ -57,9 +75,12 @@ void HandleEvent(ENetEvent event,Client client){
} }
void RUDPClientThread(const std::string& IP, int Port){ void RUDPClientThread(const std::string& IP, int Port){
if (enet_initialize() != 0) { if (enet_initialize() != 0) {
std::cout << "An error occurred while initializing ENet.\n"; std::cout << "An error occurred while initializing RUDP.\n";
} }
auto start = std::chrono::high_resolution_clock::now();
int Interval = 0;
Client client; Client client;
ENetAddress address = {0}; ENetAddress address = {0};
@ -75,7 +96,8 @@ void RUDPClientThread(const std::string& IP, int Port){
if (client.peer == nullptr) { if (client.peer == nullptr) {
std::cout << "could not connect\n"; std::cout << "could not connect\n";
} }
std::thread Ping(AutoPing,client.peer);
Ping.detach();
do { do {
ENetEvent event; ENetEvent event;
enet_host_service(client.host, &event, 0); enet_host_service(client.host, &event, 0);
@ -83,20 +105,14 @@ void RUDPClientThread(const std::string& IP, int Port){
while (!RUDPToSend.empty()){ while (!RUDPToSend.empty()){
ENetPacket* packet = enet_packet_create (RUDPToSend.front().c_str(), ENetPacket* packet = enet_packet_create (RUDPToSend.front().c_str(),
RUDPToSend.front().size()+1, RUDPToSend.front().size()+1,
ENET_PACKET_FLAG_RELIABLE); //Create A reliable packet using the data ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(client.peer, 0, packet); enet_peer_send(client.peer, 0, packet);
ping = client.peer->ping;
std::cout << "sending : " << RUDPToSend.front() << std::endl; std::cout << "sending : " << RUDPToSend.front() << std::endl;
RUDPToSend.pop(); RUDPToSend.pop();
} }
while(RUDPToSend.empty() && Interval < 1000){
auto start = std::chrono::high_resolution_clock::now();
int Interval = 0;
while(Interval < 200 && RUDPToSend.empty()){
auto done = std::chrono::high_resolution_clock::now(); auto done = std::chrono::high_resolution_clock::now();
Interval = std::chrono::duration_cast<std::chrono::milliseconds>(done-start).count(); Interval = std::chrono::duration_cast<std::chrono::milliseconds>(done-start).count();
ping = client.peer->ping;
} }
} while (!Terminate); } while (!Terminate);
} }
@ -111,8 +127,8 @@ void TCPServerThread(const std::string& IP, int Port){
SOCKET ListenSocket = INVALID_SOCKET; SOCKET ListenSocket = INVALID_SOCKET;
SOCKET ClientSocket = INVALID_SOCKET; SOCKET ClientSocket = INVALID_SOCKET;
struct addrinfo *result = NULL; struct addrinfo *result = nullptr;
struct addrinfo hints; struct addrinfo hints{};
int iSendResult; int iSendResult;
char recvbuf[DEFAULT_BUFLEN]; char recvbuf[DEFAULT_BUFLEN];
@ -131,7 +147,7 @@ void TCPServerThread(const std::string& IP, int Port){
hints.ai_flags = AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
// Resolve the server address and port // Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result); iResult = getaddrinfo(nullptr, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 ) { if ( iResult != 0 ) {
std::cout << "getaddrinfo failed with error: " << iResult << std::endl; std::cout << "getaddrinfo failed with error: " << iResult << std::endl;
WSACleanup(); WSACleanup();
@ -227,8 +243,8 @@ void ProxyStart(){
t1.join(); t1.join();
} }
void ProxyThread(const std::string& IP, int port){ void ProxyThread(const std::string& IP, int Port){
Terminate = false; Terminate = false;
std::thread t1(TCPServerThread,IP,port); std::thread t1(TCPServerThread,IP,Port);
t1.detach(); t1.detach();
} }