2017-01-20 08:36:52 -05:00
|
|
|
#pragma once
|
2022-08-01 13:15:03 -04:00
|
|
|
#include <mongoose.h>
|
2017-01-20 08:36:52 -05:00
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
class Download : public Component
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Download();
|
|
|
|
~Download();
|
|
|
|
|
2017-01-23 16:06:50 -05:00
|
|
|
void preDestroy() override;
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
static void InitiateClientDownload(const std::string& mod, bool needPassword, bool map = false);
|
|
|
|
static void InitiateMapDownload(const std::string& map, bool needPassword);
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
class ClientDownload
|
|
|
|
{
|
|
|
|
public:
|
2017-04-06 16:22:47 -04:00
|
|
|
ClientDownload(bool _isMap = false) : running(false), valid(false), terminateThread(false), isMap(_isMap), totalBytes(0), downBytes(0), lastTimeStamp(0), timeStampBytes(0) {}
|
2017-01-19 16:23:59 -05:00
|
|
|
~ClientDownload() { this->clear(); }
|
|
|
|
|
|
|
|
bool running;
|
|
|
|
bool valid;
|
|
|
|
bool terminateThread;
|
2017-04-06 16:22:47 -04:00
|
|
|
bool isMap;
|
2017-06-19 15:39:48 -04:00
|
|
|
bool isPrivate;
|
2019-12-25 17:03:43 -05:00
|
|
|
//mg_mgr mgr;
|
2017-01-19 16:23:59 -05:00
|
|
|
Network::Address target;
|
2017-07-02 08:16:06 -04:00
|
|
|
std::string hashedPassword;
|
2017-01-19 16:23:59 -05:00
|
|
|
std::string mod;
|
|
|
|
std::thread thread;
|
|
|
|
|
|
|
|
size_t totalBytes;
|
|
|
|
size_t downBytes;
|
|
|
|
|
|
|
|
int lastTimeStamp;
|
|
|
|
size_t timeStampBytes;
|
|
|
|
|
|
|
|
class File
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string name;
|
|
|
|
std::string hash;
|
|
|
|
size_t size;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<File> files;
|
|
|
|
|
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
this->terminateThread = true;
|
|
|
|
|
|
|
|
if (this->thread.joinable())
|
|
|
|
{
|
|
|
|
this->thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
this->running = false;
|
|
|
|
this->mod.clear();
|
|
|
|
this->files.clear();
|
|
|
|
|
|
|
|
if (this->valid)
|
|
|
|
{
|
|
|
|
this->valid = false;
|
2019-12-25 17:03:43 -05:00
|
|
|
//mg_mgr_free(&(this->mgr));
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileDownload
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ClientDownload* download;
|
|
|
|
ClientDownload::File file;
|
|
|
|
|
|
|
|
int timestamp;
|
|
|
|
bool downloading;
|
|
|
|
unsigned int index;
|
|
|
|
std::string buffer;
|
|
|
|
size_t receivedBytes;
|
|
|
|
};
|
|
|
|
|
|
|
|
static mg_mgr Mgr;
|
|
|
|
static ClientDownload CLDownload;
|
2017-05-25 14:18:20 -04:00
|
|
|
static std::thread ServerThread;
|
|
|
|
static bool Terminate;
|
2022-06-28 03:26:43 -04:00
|
|
|
static bool ServerRunning;
|
2017-01-19 16:23:59 -05:00
|
|
|
|
2019-12-25 17:03:43 -05:00
|
|
|
static void DownloadProgress(FileDownload* fDownload, size_t bytes);
|
|
|
|
|
2017-06-19 15:39:48 -04:00
|
|
|
static bool VerifyPassword(mg_connection *nc, http_message* message);
|
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
static void EventHandler(mg_connection *nc, int ev, void *ev_data);
|
|
|
|
static void ListHandler(mg_connection *nc, int ev, void *ev_data);
|
2017-04-06 16:22:47 -04:00
|
|
|
static void MapHandler(mg_connection *nc, int ev, void *ev_data);
|
2018-12-02 12:17:45 -05:00
|
|
|
static void ServerlistHandler(mg_connection *nc, int ev, void *ev_data);
|
2017-01-19 16:23:59 -05:00
|
|
|
static void FileHandler(mg_connection *nc, int ev, void *ev_data);
|
|
|
|
static void InfoHandler(mg_connection *nc, int ev, void *ev_data);
|
|
|
|
static void DownloadHandler(mg_connection *nc, int ev, void *ev_data);
|
|
|
|
|
|
|
|
static bool IsClient(mg_connection *nc);
|
|
|
|
static Game::client_t* GetClient(mg_connection *nc);
|
|
|
|
static void Forbid(mg_connection *nc);
|
|
|
|
|
|
|
|
static void ModDownloader(ClientDownload* download);
|
2018-12-17 08:29:18 -05:00
|
|
|
static bool ParseModList(ClientDownload* download, const std::string& list);
|
2017-01-19 16:23:59 -05:00
|
|
|
static bool DownloadFile(ClientDownload* download, unsigned int index);
|
|
|
|
};
|
|
|
|
}
|