Mingw Compatible

This commit is contained in:
Anonymous-275
2021-03-29 11:43:24 +03:00
parent d96f968dde
commit fbc5e28d25
65 changed files with 883 additions and 822 deletions

8
src/Network/Core.cpp Normal file → Executable file
View File

@@ -9,8 +9,8 @@
#include "Security/Init.h"
#include "Curl/http.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include "Startup.h"
#include "Logger.h"
#include <charconv>
@@ -242,9 +242,13 @@ int Handle(EXCEPTION_POINTERS *ep){
void CoreNetwork(){
while(TraceBack >= 4){
#ifndef __MINGW32__
__try{
#endif
CoreMain();
#ifndef __MINGW32__
}__except(Handle(GetExceptionInformation())){}
#endif
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}

0
src/Network/DNS.cpp Normal file → Executable file
View File

6
src/Network/GlobalHandler.cpp Normal file → Executable file
View File

@@ -6,15 +6,15 @@
/// Created by Anonymous275 on 7/25/2020
///
#include "Network/network.h"
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include "Logger.h"
#include <charconv>
#include <string>
#include <thread>
#include <mutex>
std::chrono::time_point<std::chrono::steady_clock> PingStart,PingEnd;
std::chrono::time_point<std::chrono::high_resolution_clock> PingStart,PingEnd;
bool GConnected = false;
bool CServer = true;
SOCKET CSocket = -1;

282
src/Network/Http.cpp Normal file → Executable file
View File

@@ -1,140 +1,144 @@
// Copyright (c) 2019-present Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
///
/// Created by Anonymous275 on 7/18/2020
///
#include <curl/curl.h>
#include <iostream>
#include <Logger.h>
#include <mutex>
class CurlManager{
public:
CurlManager(){
curl = curl_easy_init();
}
~CurlManager(){
curl_easy_cleanup(curl);
}
inline CURL* Get(){
return curl;
}
private:
CURL *curl;
};
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string HTTP_REQUEST(const std::string& IP,int port){
static thread_local CurlManager M;
static std::mutex Lock;
std::scoped_lock Guard(Lock);
CURL *curl = M.Get();
CURLcode res;
std::string readBuffer;
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_PORT, port);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
res = curl_easy_perform(curl);
if(res != CURLE_OK)return "-1";
}
return readBuffer;
}
int nb_bar;
double last_progress, progress_bar_adv;
bool Downloaded;
int progress_bar (void *bar, double t, double d){
if(t > 0 && last_progress != round(d/t*100)){
nb_bar = 25;
progress_bar_adv = round(d/t*nb_bar);
std::cout<<"\r";
std::cout<< "Progress : [ ";
std::cout<<round(d/t*100);
std::cout << "% ] [";
int i;
for(i = 0; i <= progress_bar_adv; i++)std::cout<<"#";
for(i = 0; i < nb_bar - progress_bar_adv; i++)std::cout<<".";
std::cout<<"]";
last_progress = round(d/t*100);
if(round(d/t*100) == 100){
Downloaded = true;
}
}
return 0;
}
struct File {
const char *filename;
FILE *stream;
};
static size_t my_fwrite(void *buffer,size_t size,size_t nmemb,void *stream){
auto *out = (struct File*)stream;
if(!out->stream) {
fopen_s(&out->stream,out->filename,"wb");
if(!out->stream)return -1;
}
return fwrite(buffer, size, nmemb, out->stream);
}
int Download(const std::string& URL,const std::string& Path,bool close){
static thread_local CurlManager M;
CURL *curl = M.Get();
CURLcode res;
struct File file = {Path.c_str(),nullptr};
Downloaded = false;
if(curl){
curl_easy_setopt(curl, CURLOPT_URL,URL.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &file);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_bar);
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
res = curl_easy_perform(curl);
if(res != CURLE_OK){
return res;
}
}
if(file.stream)fclose(file.stream);
if(!Downloaded){
remove(Path.c_str());
fatal("Failed to download please try again later!");
}
std::cout << std::endl;
return -1;
}
std::string PostHTTP(const std::string& IP, const std::string& Fields) {
static auto *header = new curl_slist{(char*)"Content-Type: application/json"};
static thread_local CurlManager M;
static std::mutex Lock;
std::scoped_lock Guard(Lock);
CURL* curl = M.Get();
CURLcode res;
std::string readBuffer;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
return "-1";
}
return readBuffer;
// Copyright (c) 2019-present Anonymous275.
// BeamMP Launcher code is not in the public domain and is not free software.
// One must be granted explicit permission by the copyright holder in order to modify or distribute any part of the source or binaries.
// Anything else is prohibited. Modified works may not be published and have be upstreamed to the official repository.
///
/// Created by Anonymous275 on 7/18/2020
///
#include <curl/curl.h>
#include <iostream>
#include <Logger.h>
#include <mutex>
#include <cmath>
class CurlManager{
public:
CurlManager(){
curl = curl_easy_init();
}
~CurlManager(){
curl_easy_cleanup(curl);
}
inline CURL* Get(){
return curl;
}
private:
CURL *curl;
};
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string HTTP_REQUEST(const std::string& IP,int port){
static thread_local CurlManager M;
static std::mutex Lock;
std::scoped_lock Guard(Lock);
CURL *curl = M.Get();
CURLcode res;
std::string readBuffer;
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_PORT, port);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
res = curl_easy_perform(curl);
if(res != CURLE_OK)return "-1";
}
return readBuffer;
}
int nb_bar;
double last_progress, progress_bar_adv;
bool Downloaded;
int progress_bar (void *bar, double t, double d){
if(t > 0 && last_progress != round(d/t*100)){
nb_bar = 25;
progress_bar_adv = round(d/t*nb_bar);
std::cout<<"\r";
std::cout<< "Progress : [ ";
std::cout<<round(d/t*100);
std::cout << "% ] [";
int i;
for(i = 0; i <= progress_bar_adv; i++)std::cout<<"#";
for(i = 0; i < nb_bar - progress_bar_adv; i++)std::cout<<".";
std::cout<<"]";
last_progress = round(d/t*100);
if(round(d/t*100) == 100){
Downloaded = true;
}
}
return 0;
}
struct File {
const char *filename;
FILE *stream;
};
static size_t my_fwrite(void *buffer,size_t size,size_t nmemb,void *stream){
auto *out = (struct File*)stream;
if(!out->stream) {
fopen_s(&out->stream,out->filename,"wb");
if(!out->stream)return -1;
}
return fwrite(buffer, size, nmemb, out->stream);
}
int Download(const std::string& URL,const std::string& Path,bool close){
static thread_local CurlManager M;
CURL *curl = M.Get();
CURLcode res;
struct File file = {Path.c_str(),nullptr};
Downloaded = false;
if(curl){
curl_easy_setopt(curl, CURLOPT_URL,URL.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &file);
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_bar);
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
res = curl_easy_perform(curl);
if(res != CURLE_OK){
return res;
}
}
if(file.stream)fclose(file.stream);
if(!Downloaded){
remove(Path.c_str());
fatal("Failed to download please try again later!");
}
std::cout << std::endl;
return -1;
}
std::string PostHTTP(const std::string& IP, const std::string& Fields) {
static auto *header = new curl_slist{(char*)"Content-Type: application/json"};
static thread_local CurlManager M;
static std::mutex Lock;
std::scoped_lock Guard(Lock);
CURL* curl = M.Get();
CURLcode res;
std::string readBuffer;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, IP.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, Fields.size());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, Fields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
//curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
res = curl_easy_perform(curl);
if (res != CURLE_OK)
return "-1";
}
return readBuffer;
}

7
src/Network/Resources.cpp Normal file → Executable file
View File

@@ -7,8 +7,7 @@
///
#include "Network/network.h"
#include <WS2tcpip.h>
#include <ws2tcpip.h>
#include <filesystem>
#include "Startup.h"
#include "Logger.h"
@@ -20,6 +19,7 @@
#include <atomic>
#include <vector>
#include <future>
#include <cmath>
namespace fs = std::filesystem;
std::string ListOfMods;
@@ -37,8 +37,7 @@ std::vector<std::string> Split(const std::string& String,const std::string& deli
}
void CheckForDir(){
struct stat info{};
if(stat( "Resources", &info) != 0){
if(!fs::exists("Resources")){
_wmkdir(L"Resources");
}
}

2
src/Network/VehicleData.cpp Normal file → Executable file
View File

@@ -8,7 +8,7 @@
#include "Zlib/Compressor.h"
#include "Network/network.h"
#include <WS2tcpip.h>
#include <ws2tcpip.h>
#include "Logger.h"
#include <string>
#include <set>

4
src/Network/VehicleEvent.cpp Normal file → Executable file
View File

@@ -10,7 +10,7 @@
#include <vector>
#include "Logger.h"
#include <iostream>
#include <WS2tcpip.h>
#include <ws2tcpip.h>
#include <Zlib/Compressor.h>
#include "Network/network.h"
@@ -133,7 +133,7 @@ void TCPClientMain(const std::string& IP,int Port){
RetCode = connect(TCPSock, (SOCKADDR *) &ServerAddr, sizeof(ServerAddr));
if(RetCode != 0){
UlStatus = "UlConnection Failed!";
std::cout << "Client: connect failed! Error code: " << WSAGetLastError() << std::endl;
error("Client: connect failed! Error code: " + std::to_string(WSAGetLastError()));
KillSocket(TCPSock);
WSACleanup();
Terminate = true;