diff --git a/source/src/cube.h b/source/src/cube.h index b541d4ec..141d052b 100644 --- a/source/src/cube.h +++ b/source/src/cube.h @@ -62,6 +62,8 @@ extern int verbose; #define AC_VERSION 1201 #define AC_MASTER_URI "ms.cubers.net" #define AC_MASTER_PORT 28760 +#define AC_MASTER_HTTP 1 // default +#define AC_MASTER_RAW 0 #define MAXCL 16 #include "protos.h" // external function decls diff --git a/source/src/protos.h b/source/src/protos.h index 34e4a6f0..7d46b481 100644 --- a/source/src/protos.h +++ b/source/src/protos.h @@ -875,8 +875,7 @@ extern void cutcolorstring(char *text, int len); extern void startintermission(); extern void restoreserverstate(vector &ents); extern string mastername; -extern int masterport; -extern int mastertype; +extern int masterport, mastertype; extern ENetSocket connectmaster(); extern uchar *retrieveservers(uchar *buf, int buflen); extern void serverms(int mode, int numplayers, int minremain, char *smapname, int millis, const ENetAddress &localaddr, int *mnum, int *msend, int *mrec, int *cnum, int *csend, int *crec, int protocol_version); diff --git a/source/src/serverbrowser.cpp b/source/src/serverbrowser.cpp index 5c5523c2..625bab04 100644 --- a/source/src/serverbrowser.cpp +++ b/source/src/serverbrowser.cpp @@ -1266,7 +1266,7 @@ void clearservers() servers.deletecontents(); } -#define RETRIEVELIMIT 20000 +#define RETRIEVELIMIT 5000 extern char *global_name; bool cllock = false, clfail = false; @@ -1294,11 +1294,61 @@ static size_t write_callback(void *ptr, size_t size, size_t nmemb, FILE *stream) return fwrite(ptr, size, nmemb, stream); } -int mastertype = 1; //0: UDP, 1: TCP void retrieveservers(vector &data) { - if (mastertype==0) { - conoutf("Contacting masterserver %s using UDP.", mastername); + if(mastertype == AC_MASTER_HTTP) + { + string request; + sprintf(request, "http://%s/retrieve.do?action=list&name=%s&version=%d&build=%d", mastername, global_name, AC_VERSION, getbuildtype()|(1<<16)); + + const char *tmpname = findfile(path("config/servers.cfg", true), "wb"); + FILE *outfile = fopen(tmpname, "w+"); + if(!outfile) + { + conoutf("\f3cannot write server list"); + return; + } + + resolver_data *rd = new resolver_data(); + formatstring(rd->text)("retrieving servers from %s:%d... (esc to abort)", mastername, masterport); + show_out_of_renderloop_progress(0, rd->text); + + rd->starttime = SDL_GetTicks(); + rd->timeout = 0; + + CURL *curl = curl_easy_init(); + int result = 0, httpresult = 0; + + curl_easy_setopt(curl, CURLOPT_URL, request); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); + curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); + curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback); + curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, rd); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, RETRIEVELIMIT/1000); + result = curl_easy_perform(curl); + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpresult); + curl_easy_cleanup(curl); + curl = NULL; + if(outfile) fclose(outfile); + + if(result == CURLE_OPERATION_TIMEDOUT || result == CURLE_COULDNT_RESOLVE_HOST) + { + clfail = true; + } + + if(!result && httpresult == 200) + { + int size = 0; + char *content = loadfile(path("config/servers.cfg", true), &size); + data.shrink(0); + data.insert(0, content, size); + if(data.length()) data.add('\0'); + } + DELETEP(rd); + } + else + { ENetSocket sock = connectmaster(); if(sock == ENET_SOCKET_NULL) { @@ -1307,10 +1357,8 @@ void retrieveservers(vector &data) return; } clfail = false; - defformatstring(text)("retrieving servers from %s:%d... (esc to abort)", mastername, masterport); show_out_of_renderloop_progress(0, text); - int starttime = SDL_GetTicks(), timeout = 0; string request; sprintf(request, "list %s %d %d\n",global_name,AC_VERSION,getbuildtype()); @@ -1335,7 +1383,6 @@ void retrieveservers(vector &data) if(interceptkey(SDLK_ESCAPE)) timeout = RETRIEVELIMIT + 1; if(timeout > RETRIEVELIMIT) break; } - if(reqlen <= 0) for(;;) { enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE; @@ -1353,59 +1400,8 @@ void retrieveservers(vector &data) if(interceptkey(SDLK_ESCAPE)) timeout = RETRIEVELIMIT + 1; if(timeout > RETRIEVELIMIT) break; } - if(data.length()) data.add('\0'); - enet_socket_destroy(sock); - } - else if(mastertype==1) { - conoutf("Contacting masterserver %s using HTTP.", mastername); - string request; - sprintf(request, "http://%s/retrieve.do?action=list&name=%s&version=%d&build=%d",mastername, global_name, AC_VERSION, getbuildtype()); - - const char *tmpname = findfile(path("config/servers_tmp.cfg", true), "wb"); - FILE *outfile = fopen(tmpname, "w+"); - if(!outfile) - { - conoutf("\f3cannot write server list"); - return; - } - - resolver_data *rd = new resolver_data(); - formatstring(rd->text)("retrieving servers from %s:%d... (esc to abort)", mastername, masterport); - show_out_of_renderloop_progress(0, rd->text); - - rd->starttime = SDL_GetTicks(); - rd->timeout = 0; - - CURL *curl = curl_easy_init(); - int result = 0, httpresult = 0; - - curl_easy_setopt(curl, CURLOPT_URL, request); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); - curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback); - curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, rd); - curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10); - result = curl_easy_perform(curl); - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpresult); - curl_easy_cleanup(curl); - curl = NULL; - if(outfile) fclose(outfile); - - if(result == CURLE_OPERATION_TIMEDOUT || result == CURLE_COULDNT_RESOLVE_HOST) - { - clfail = true; - } - - if(!result && httpresult == 200) - { - int size = 0; - char *content = loadfile(path("config/servers_tmp.cfg", true), &size); - data.shrink(0); - data.insert(0, content, size); - if(data.length()) data.add('\0'); - } - DELETEP(rd); + enet_socket_destroy(sock); } }