iw4x-client/src/Components/Modules/Download.cpp

76 lines
1.4 KiB
C++
Raw Normal View History

2016-01-08 21:21:59 -05:00
#include "STDInclude.hpp"
namespace Components
{
2016-06-04 11:06:49 -04:00
mg_mgr Download::Mgr;
2016-01-08 21:21:59 -05:00
2016-06-04 11:06:49 -04:00
void Download::EventHandler(mg_connection *nc, int ev, void *ev_data)
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
// Only handle http requests
if (ev != MG_EV_HTTP_REQUEST) return;
2016-01-08 21:21:59 -05:00
2016-06-04 11:06:49 -04:00
http_message* message = reinterpret_cast<http_message*>(ev_data);
2016-01-08 21:21:59 -05:00
2016-06-04 11:06:49 -04:00
if (std::string(message->uri.p, message->uri.len) == "/")
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
mg_printf(nc, "%s",
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n"
"\r\n"
"Hi fella!");
2016-01-08 21:21:59 -05:00
}
2016-06-04 11:06:49 -04:00
else
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
mg_printf(nc, "%s",
"HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/html\r\n"
"Connection: close\r\n"
"\r\n"
"<h1>404 - Not Found</h1>");
2016-01-08 21:21:59 -05:00
}
2016-06-04 11:06:49 -04:00
nc->flags |= MG_F_SEND_AND_CLOSE;
2016-01-08 21:21:59 -05:00
}
2016-06-04 11:06:49 -04:00
Download::Download()
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
if (Dedicated::IsDedicated())
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
mg_mgr_init(&Download::Mgr, NULL);
2016-01-08 21:21:59 -05:00
2016-06-04 11:06:49 -04:00
Network::OnStart([] ()
2016-01-09 09:30:13 -05:00
{
2016-06-04 11:06:49 -04:00
mg_connection* nc = mg_bind(&Download::Mgr, Utils::VA("%hu", (Dvar::Var("net_port").Get<int>() & 0xFFFF)), Download::EventHandler);
mg_set_protocol_http_websocket(nc);
});
2016-01-08 21:21:59 -05:00
2016-06-04 11:06:49 -04:00
QuickPatch::OnFrame([]
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
mg_mgr_poll(&Download::Mgr, 0);
});
2016-01-08 21:21:59 -05:00
}
2016-06-04 11:06:49 -04:00
else
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
Utils::Hook(0x5AC6E9, [] ()
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
// TODO: Perform moddownload here
2016-01-08 21:21:59 -05:00
2016-06-04 11:06:49 -04:00
Game::CL_DownloadsComplete(0);
}, HOOK_CALL).Install()->Quick();
2016-01-08 21:21:59 -05:00
}
}
2016-06-04 11:06:49 -04:00
Download::~Download()
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
if (Dedicated::IsDedicated())
2016-01-08 21:21:59 -05:00
{
2016-06-04 11:06:49 -04:00
mg_mgr_free(&Download::Mgr);
2016-01-08 21:21:59 -05:00
}
2016-06-04 11:06:49 -04:00
else
2016-01-08 21:21:59 -05:00
{
}
}
}