2016-01-08 21:21:59 -05:00
|
|
|
namespace Components
|
|
|
|
{
|
|
|
|
class Download : public Component
|
|
|
|
{
|
|
|
|
public:
|
2016-01-09 09:30:13 -05:00
|
|
|
Download();
|
2016-08-15 10:40:30 -04:00
|
|
|
~Download();
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2016-01-09 09:30:13 -05:00
|
|
|
const char* GetName() { return "Download"; };
|
2016-08-15 10:40:30 -04:00
|
|
|
#endif
|
2016-01-09 09:30:13 -05:00
|
|
|
|
2016-08-31 11:54:08 -04:00
|
|
|
static void InitiateClientDownload(std::string mod);
|
|
|
|
|
2016-01-09 09:30:13 -05:00
|
|
|
private:
|
2016-08-31 11:54:08 -04:00
|
|
|
class ClientDownload
|
|
|
|
{
|
|
|
|
public:
|
2016-08-31 12:12:31 -04:00
|
|
|
ClientDownload() : Valid(false), Running(false), TerminateThread(false) {}
|
2016-08-31 11:54:08 -04:00
|
|
|
~ClientDownload() { this->Clear(); }
|
|
|
|
|
|
|
|
bool Running;
|
|
|
|
bool Valid;
|
2016-08-31 12:12:31 -04:00
|
|
|
bool TerminateThread;
|
2016-08-31 11:54:08 -04:00
|
|
|
mg_mgr Mgr;
|
|
|
|
Network::Address Target;
|
|
|
|
std::string Mod;
|
|
|
|
std::mutex Mutex;
|
|
|
|
std::thread Thread;
|
|
|
|
std::string Progress;
|
|
|
|
|
2016-08-31 12:12:31 -04:00
|
|
|
|
2016-08-31 11:54:08 -04:00
|
|
|
class File
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
std::string Name;
|
|
|
|
std::string Hash;
|
|
|
|
size_t Size;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<File> Files;
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
2016-08-31 12:12:31 -04:00
|
|
|
this->TerminateThread = true;
|
|
|
|
|
|
|
|
if (this->Thread.joinable())
|
|
|
|
{
|
|
|
|
this->Thread.join();
|
|
|
|
}
|
|
|
|
|
2016-08-31 11:54:08 -04:00
|
|
|
this->Running = false;
|
|
|
|
this->Mod.clear();
|
|
|
|
this->Files.clear();
|
|
|
|
|
|
|
|
if (this->Valid)
|
|
|
|
{
|
|
|
|
this->Valid = false;
|
|
|
|
mg_mgr_free(&(this->Mgr));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->Mutex.lock();
|
|
|
|
this->Progress.clear();
|
|
|
|
this->Mutex.unlock();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileDownload
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ClientDownload* download;
|
|
|
|
ClientDownload::File file;
|
|
|
|
|
|
|
|
bool downloading;
|
|
|
|
unsigned int index;
|
|
|
|
std::string buffer;
|
|
|
|
size_t receivedBytes;
|
|
|
|
};
|
|
|
|
|
2016-06-04 11:06:49 -04:00
|
|
|
static mg_mgr Mgr;
|
2016-08-31 11:54:08 -04:00
|
|
|
static ClientDownload CLDownload;
|
2016-01-08 21:21:59 -05:00
|
|
|
|
2016-06-04 11:06:49 -04:00
|
|
|
static void EventHandler(mg_connection *nc, int ev, void *ev_data);
|
2016-06-04 15:24:03 -04:00
|
|
|
static void ListHandler(mg_connection *nc, int ev, void *ev_data);
|
|
|
|
static void FileHandler(mg_connection *nc, int ev, void *ev_data);
|
2016-06-05 11:34:55 -04:00
|
|
|
static void InfoHandler(mg_connection *nc, int ev, void *ev_data);
|
2016-08-31 11:54:08 -04:00
|
|
|
static void DownloadHandler(mg_connection *nc, int ev, void *ev_data);
|
2016-06-04 15:24:03 -04:00
|
|
|
|
|
|
|
static bool IsClient(mg_connection *nc);
|
|
|
|
static Game::client_t* GetClient(mg_connection *nc);
|
|
|
|
static void Forbid(mg_connection *nc);
|
2016-08-31 11:54:08 -04:00
|
|
|
|
|
|
|
static void ModDownloader(ClientDownload* download);
|
|
|
|
static bool ParseModList(ClientDownload* download, std::string list);
|
|
|
|
static bool DownloadFile(ClientDownload* download, unsigned int index);
|
2016-01-08 21:21:59 -05:00
|
|
|
};
|
|
|
|
}
|