From 25b63b10e132bab364b3917edf02b0d37969e068 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 11 May 2010 05:39:00 +0000 Subject: [PATCH] - Added libcurl to mega-glest for mu;ti-purpose / multi-platform http and ftp requests (for linux requires the following new dependency: sudo apt-get install libcurl4-dev) --- mk/linux/Jamfile | 12 ++-- mk/linux/configure.ac | 8 +++ source/glest_game/main/main.cpp | 1 + .../menu/menu_state_masterserver.cpp | 39 ++++++++++- source/shared_lib/include/util/util.h | 10 +++ source/shared_lib/sources/util/util.cpp | 70 ++++++++++++++++++- 6 files changed, 130 insertions(+), 10 deletions(-) diff --git a/mk/linux/Jamfile b/mk/linux/Jamfile index ab7fbb6df..1a74191f4 100644 --- a/mk/linux/Jamfile +++ b/mk/linux/Jamfile @@ -20,7 +20,7 @@ for i in $(LIB_STREFLOP_DIRS) { } Library strefloplib : $(LIB_STREFLOP_SOURCES) ; -ExternalLibs strefloplib : SDL GL GLU XERCES VORBIS VORBISFILE OGG OPENAL LUA JPEG PNG ; +ExternalLibs strefloplib : SDL GL GLU XERCES VORBIS VORBISFILE OGG OPENAL LUA JPEG PNG CURL ; IncludeDir strefloplib : $(LIB_STREFLOP_INCLUDE_DIRS) ; #### Library #### @@ -50,7 +50,7 @@ for i in $(LIB_DIRS) { Library glestlib : $(LIB_SOURCES) ; LinkWith glestlib : strefloplib ; -ExternalLibs glestlib : SDL GL GLU XERCES VORBIS VORBISFILE OGG OPENAL LUA JPEG PNG ; +ExternalLibs glestlib : SDL GL GLU XERCES VORBIS VORBISFILE OGG OPENAL LUA JPEG PNG CURL ; IncludeDir glestlib : $(LIB_INCLUDE_DIRS) ; #### Game #### @@ -79,7 +79,7 @@ for i in $(GLEST_DIRS) { Application glest.bin : $(GLEST_SOURCES) ; LinkWith glest.bin : glestlib strefloplib ; -ExternalLibs glest.bin : SDL GL GLU XERCES VORBIS VORBISFILE OGG OPENAL LUA JPEG PNG ; +ExternalLibs glest.bin : SDL GL GLU XERCES VORBIS VORBISFILE OGG OPENAL LUA JPEG PNG CURL ; IncludeDir glest.bin : ../shared_lib/include/$(LIB_INCLUDE_DIRS) $(GLEST_DIRS) ; #### Editor #### @@ -93,7 +93,7 @@ if $(WX_AVAILABLE) = "yes" { Application glest_editor : $(GLEST_MAP_SOURCES) ; LinkWith glest_editor : glestlib strefloplib ; - ExternalLibs glest_editor : SDL GL GLU XERCES VORBIS VORBISFILE OGG OPENAL WX ; + ExternalLibs glest_editor : SDL GL GLU XERCES VORBIS VORBISFILE OGG OPENAL WX CURL ; IncludeDir glest_editor : ../shared_lib/include/$(LIB_INCLUDE_DIRS) $(GLEST_MAP_DIRS) ; } @@ -108,7 +108,7 @@ if $(WX_AVAILABLE) = "yes" { Application glest_g3dviewer : $(GLEST_VIEWER_SOURCES) ; LinkWith glest_g3dviewer : glestlib strefloplib ; - ExternalLibs glest_g3dviewer : SDL GL GLU WX ; + ExternalLibs glest_g3dviewer : SDL GL GLU WX CURL ; IncludeDir glest_g3dviewer : ../shared_lib/include/$(LIB_INCLUDE_DIRS) $(GLEST_VIEWER_DIRS) ; } @@ -122,6 +122,6 @@ if $(WX_AVAILABLE) = "yes" { Application glest_configurator : $(GLEST_CONFIG_SOURCES) ; LinkWith glest_configurator : glestlib strefloplib ; - ExternalLibs glest_configurator : SDL GL GLU XERCES WX ; + ExternalLibs glest_configurator : SDL GL GLU XERCES WX CURL ; IncludeDir glest_configurator : ../shared_lib/include/$(LIB_INCLUDE_DIRS) $(GLEST_CONFIG_DIRS) ; diff --git a/mk/linux/configure.ac b/mk/linux/configure.ac index 813605ee8..8c3a2aa18 100644 --- a/mk/linux/configure.ac +++ b/mk/linux/configure.ac @@ -130,6 +130,14 @@ NP_FINDLIB([PNG], [LibPng], [LibPNG], [AC_MSG_ERROR([Please intall libpng])], [], []) +NP_FINDLIB([CURL], [LibCurl], [LibCurl], + NP_LANG_PROGRAM([#include ], + [curl_easy_init();]), + [], [-lcurl], + [], + [AC_MSG_ERROR([Please intall libcurl])], + [], []) + CHECK_LUA([], [AC_MSG_ERROR([Please install lua 5.1])]) AX_CHECK_GL diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index eac5d5459..bae1b3768 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -29,6 +29,7 @@ #include "simple_threads.h" #include #include "font.h" +#include #include "leak_dumper.h" diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index 313b5c9d6..7a602c643 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -23,6 +23,7 @@ #include "auto_test.h" #include "socket.h" #include "masterserver_info.h" +#include #include "leak_dumper.h" @@ -221,9 +222,44 @@ void MenuStateMasterserver::render(){ void MenuStateMasterserver::update(){ } -void MenuStateMasterserver::updateServerInfo(){ +void MenuStateMasterserver::updateServerInfo() { //MasterServerInfos masterServerInfos; clearServerLines(); + + std::string serverInfo = SystemFlags::getHTTP("http://soft-haus.com/glest/cgi-bin/mega-glest-master-query.php"); + + std::vector serverList; + Tokenize(serverInfo,serverList,"\n"); + for(int i=0; i < serverList.size(); i++) { + string &server = serverList[i]; + std::vector serverEntities; + Tokenize(server,serverEntities,"|"); + + const int MIN_FIELDS_EXPECTED = 11; + if(serverEntities.size() >= MIN_FIELDS_EXPECTED) { + MasterServerInfo *masterServerInfo=new MasterServerInfo(); + + //general info: + masterServerInfo->setGlestVersion(serverEntities[0]); + masterServerInfo->setPlatform(serverEntities[1]); + masterServerInfo->setBinaryCompileDate(serverEntities[2]); + + //game info: + masterServerInfo->setServerTitle(serverEntities[3]); + masterServerInfo->setIpAddress(serverEntities[4]); + + //game setup info: + masterServerInfo->setTech(serverEntities[5]); + masterServerInfo->setMap(serverEntities[6]); + masterServerInfo->setTileset(serverEntities[7]); + masterServerInfo->setActiveSlots(strToInt(serverEntities[8])); + masterServerInfo->setNetworkSlots(strToInt(serverEntities[9])); + masterServerInfo->setConnectedClients(strToInt(serverEntities[10])); + + serverLines.push_back(new ServerLine( masterServerInfo, i)); + } + } +/* for(int i=0; i<10;i++){ MasterServerInfo *masterServerInfo=new MasterServerInfo(); @@ -248,6 +284,7 @@ void MenuStateMasterserver::updateServerInfo(){ serverLines.push_back(new ServerLine( masterServerInfo, i)); } +*/ //masterServerInfos.push_back(masterServerInfo); } diff --git a/source/shared_lib/include/util/util.h b/source/shared_lib/include/util/util.h index a774f8bad..096415409 100644 --- a/source/shared_lib/include/util/util.h +++ b/source/shared_lib/include/util/util.h @@ -16,6 +16,7 @@ #include #include #include "thread.h" +#include using std::string; using namespace Shared::Platform; @@ -28,6 +29,11 @@ class SystemFlags { public: + struct httpMemoryStruct { + char *memory; + size_t size; + }; + enum DebugType { debugSystem, debugNetwork, @@ -100,11 +106,15 @@ protected: public: + static CURL *curl_handle; + SystemFlags(); ~SystemFlags(); static void init(); static SystemFlagsType & getSystemSettingType(DebugType type) { return debugLogFileList[type]; } + static size_t httpWriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data); + static std::string getHTTP(std::string URL); // Let the macro call into this when require.. NEVER call it automatically. static void handleDebug(DebugType type, const char *fmt, ...); diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index 4ee100323..7b61dee8e 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -37,7 +37,63 @@ using namespace Shared::PlatformCommon; namespace Shared{ namespace Util{ +// Init statics std::map SystemFlags::debugLogFileList; +int SystemFlags::lockFile = -1; +string SystemFlags::lockfilename = ""; +CURL *SystemFlags::curl_handle = NULL; +// + +static void *myrealloc(void *ptr, size_t size) +{ + /* There might be a realloc() out there that doesn't like reallocing + NULL pointers, so we take care of it here */ + if(ptr) + return realloc(ptr, size); + else + return malloc(size); +} + +size_t SystemFlags::httpWriteMemoryCallback(void *ptr, size_t size, size_t nmemb, void *data) +{ + size_t realsize = size * nmemb; + struct httpMemoryStruct *mem = (struct httpMemoryStruct *)data; + + mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1); + if (mem->memory) { + memcpy(&(mem->memory[mem->size]), ptr, realsize); + mem->size += realsize; + mem->memory[mem->size] = 0; + } + return realsize; +} + +std::string SystemFlags::getHTTP(std::string URL) { + curl_easy_setopt(SystemFlags::curl_handle, CURLOPT_URL, URL.c_str()); + + /* send all data to this function */ + curl_easy_setopt(SystemFlags::curl_handle, CURLOPT_WRITEFUNCTION, SystemFlags::httpWriteMemoryCallback); + + struct SystemFlags::httpMemoryStruct chunk; + chunk.memory=NULL; /* we expect realloc(NULL, size) to work */ + chunk.size = 0; /* no data at this point */ + + /* we pass our 'chunk' struct to the callback function */ + curl_easy_setopt(SystemFlags::curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); + + /* some servers don't like requests that are made without a user-agent + field, so we provide one */ + curl_easy_setopt(SystemFlags::curl_handle, CURLOPT_USERAGENT, "mega-glest-agent/1.0"); + + /* get contents from the URL */ + curl_easy_perform(SystemFlags::curl_handle); + std::string serverResponse = chunk.memory; + if(chunk.memory) { + free(chunk.memory); + } + + return serverResponse; +} void SystemFlags::init() { if(SystemFlags::debugLogFileList.size() == 0) { @@ -46,10 +102,12 @@ void SystemFlags::init() { SystemFlags::debugLogFileList[SystemFlags::debugPerformance] = SystemFlags::SystemFlagsType(SystemFlags::debugPerformance); SystemFlags::debugLogFileList[SystemFlags::debugWorldSynch] = SystemFlags::SystemFlagsType(SystemFlags::debugWorldSynch); } -} -int SystemFlags::lockFile = -1; -string SystemFlags::lockfilename = ""; + if(curl_handle == NULL) { + curl_global_init(CURL_GLOBAL_ALL); + curl_handle = curl_easy_init(); + } +} inline bool acquire_file_lock(int hnd) { @@ -71,6 +129,12 @@ SystemFlags::SystemFlags() { } SystemFlags::~SystemFlags() { SystemFlags::Close(); + + if(curl_handle != NULL) { + curl_easy_cleanup(curl_handle); + curl_handle = NULL; + curl_global_cleanup(); + } } void SystemFlags::Close() {