Use httpfetch_async in serverlist announce code
This commit is contained in:
parent
0a903e69fb
commit
b03135548b
|
@ -206,6 +206,10 @@ struct HTTPFetchOngoing
|
||||||
request.timeout);
|
request.timeout);
|
||||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS,
|
||||||
request.connect_timeout);
|
request.connect_timeout);
|
||||||
|
|
||||||
|
if (request.useragent != "")
|
||||||
|
curl_easy_setopt(curl, CURLOPT_USERAGENT, request.useragent.c_str());
|
||||||
|
|
||||||
// Set up a write callback that writes to the
|
// Set up a write callback that writes to the
|
||||||
// ostringstream ongoing->oss, unless the data
|
// ostringstream ongoing->oss, unless the data
|
||||||
// is to be discarded
|
// is to be discarded
|
||||||
|
|
|
@ -54,6 +54,9 @@ struct HTTPFetchRequest
|
||||||
// If not empty, should contain entries such as "Accept: text/html"
|
// If not empty, should contain entries such as "Accept: text/html"
|
||||||
std::vector<std::string> extra_headers;
|
std::vector<std::string> extra_headers;
|
||||||
|
|
||||||
|
//useragent to use
|
||||||
|
std::string useragent;
|
||||||
|
|
||||||
HTTPFetchRequest()
|
HTTPFetchRequest()
|
||||||
{
|
{
|
||||||
url = "";
|
url = "";
|
||||||
|
|
|
@ -30,9 +30,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "json/json.h"
|
#include "json/json.h"
|
||||||
#include "convert_json.h"
|
#include "convert_json.h"
|
||||||
#if USE_CURL
|
#include "httpfetch.h"
|
||||||
#include <curl/curl.h>
|
#include "util/string.h"
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace ServerList
|
namespace ServerList
|
||||||
{
|
{
|
||||||
|
@ -189,11 +188,6 @@ std::string serializeJson(std::vector<ServerListSpec> serverlist)
|
||||||
|
|
||||||
|
|
||||||
#if USE_CURL
|
#if USE_CURL
|
||||||
static size_t ServerAnnounceCallback(void *contents, size_t size, size_t nmemb, void *userp)
|
|
||||||
{
|
|
||||||
//((std::string*)userp)->append((char*)contents, size * nmemb);
|
|
||||||
return size * nmemb;
|
|
||||||
}
|
|
||||||
void sendAnnounce(std::string action, const std::vector<std::string> & clients_names, double uptime, u32 game_time, std::string gameid, std::vector<ModSpec> mods) {
|
void sendAnnounce(std::string action, const std::vector<std::string> & clients_names, double uptime, u32 game_time, std::string gameid, std::vector<ModSpec> mods) {
|
||||||
Json::Value server;
|
Json::Value server;
|
||||||
if (action.size())
|
if (action.size())
|
||||||
|
@ -235,24 +229,14 @@ void sendAnnounce(std::string action, const std::vector<std::string> & clients_n
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::StyledWriter writer;
|
Json::StyledWriter writer;
|
||||||
CURL *curl;
|
HTTPFetchRequest fetchrequest;
|
||||||
curl = curl_easy_init();
|
fetchrequest.url = g_settings->get("serverlist_url")
|
||||||
if (curl)
|
+ std::string("/announce?json=")
|
||||||
{
|
+ urlencode(writer.write(server));
|
||||||
CURLcode res;
|
fetchrequest.useragent = std::string("Minetest ")+minetest_version_hash;
|
||||||
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
|
fetchrequest.caller = HTTPFETCH_DISCARD;
|
||||||
curl_easy_setopt(curl, CURLOPT_URL, (g_settings->get("serverlist_url")+std::string("/announce?json=")+curl_easy_escape(curl, writer.write( server ).c_str(), 0)).c_str());
|
fetchrequest.timeout = g_settings->getS32("curl_timeout");
|
||||||
curl_easy_setopt(curl, CURLOPT_USERAGENT, (std::string("Minetest ")+minetest_version_hash).c_str());
|
httpfetch_async(fetchrequest);
|
||||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ServerList::ServerAnnounceCallback);
|
|
||||||
//curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
|
|
||||||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1);
|
|
||||||
res = curl_easy_perform(curl);
|
|
||||||
if (res != CURLE_OK)
|
|
||||||
errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" error ("<<curl_easy_strerror(res)<<")"<<std::endl;
|
|
||||||
curl_easy_cleanup(curl);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue