From 4de4290898e678b1545a0f6b611b07b935951cd5 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Thu, 14 Apr 2011 18:19:15 -0700 Subject: [PATCH] Use libcurl instead of WinHTTP for plClient's server status message --- CMakeLists.txt | 7 ++ Sources/Plasma/Apps/plClient/CMakeLists.txt | 3 +- Sources/Plasma/Apps/plClient/winmain.cpp | 95 ++++++++------------- 3 files changed, 44 insertions(+), 61 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e8e174dc..83cb950b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,9 +14,16 @@ find_package(Ogg REQUIRED) #TODO: Not required if we aren't building the clie find_package(Vorbis REQUIRED) #TODO: Not required if we aren't building the client find_package(Speex REQUIRED) #TODO: Not required if we aren't building the client find_package(DirectX REQUIRED) +find_package(CURL REQUIRED) find_package(Bink) #TODO: Find Bink, but don't require it if plPipeline isn't built... # Or better yet, just eliminate bink altogether +# libCurl isn't smart enough to detect this for us, so we have to configure it ourselves +option(CURL_IS_STATIC "Using the static version of libcurl?" ON) +if(CURL_IS_STATIC) + add_definitions(-DCURL_STATICLIB) +endif(CURL_IS_STATIC) + option(PLASMA_EXTERNAL_RELEASE "Is this release intended for the general public?" OFF) if(PLASMA_EXTERNAL_RELEASE) diff --git a/Sources/Plasma/Apps/plClient/CMakeLists.txt b/Sources/Plasma/Apps/plClient/CMakeLists.txt index 618f03f5..834a95e6 100644 --- a/Sources/Plasma/Apps/plClient/CMakeLists.txt +++ b/Sources/Plasma/Apps/plClient/CMakeLists.txt @@ -13,6 +13,7 @@ endif() include_directories(${OPENAL_INCLUDE_DIR}) include_directories(${OPENSSL_INCLUDE_DIR}) include_directories(${PYTHON_INCLUDE_DIR}) +include_directories(${CURL_INCLUDE_DIR}) set(plClient_HEADERS plClient.h @@ -193,6 +194,7 @@ target_link_libraries(plClient ${PHYSX_LIBRARIES}) target_link_libraries(plClient ${Ogg_LIBRARIES}) target_link_libraries(plClient ${Vorbis_LIBRARIES}) target_link_libraries(plClient ${DirectX_LIBRARIES}) +target_link_libraries(plClient ${CURL_LIBRARY}) if(Bink_SDK_AVAILABLE) target_link_libraries(plClient ${Bink_LIBRARIES}) @@ -200,7 +202,6 @@ endif() if (WIN32) target_link_libraries(plClient Rpcrt4) - target_link_libraries(plClient Winhttp) target_link_libraries(plClient Version) target_link_libraries(plClient Vfw32) target_link_libraries(plClient Ws2_32) diff --git a/Sources/Plasma/Apps/plClient/winmain.cpp b/Sources/Plasma/Apps/plClient/winmain.cpp index d3400396..8263e083 100644 --- a/Sources/Plasma/Apps/plClient/winmain.cpp +++ b/Sources/Plasma/Apps/plClient/winmain.cpp @@ -33,9 +33,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include // Windows Load EXE into memory suff #endif -#include -#include -#include +#include #include "HeadSpin.h" #include "hsStream.h" @@ -1198,73 +1196,48 @@ static void LoadUserPass (const wchar *accountName, char *username, ShaDigest *p } } +size_t CurlCallback(void *buffer, size_t size, size_t nmemb, void *param) +{ + static char status[256]; + + HWND hwnd = (HWND)param; + + strncpy(status, (const char *)buffer, std::min(size * nmemb, 256)); + status[255] = 0; + PostMessage(hwnd, WM_USER_SETSTATUSMSG, 0, (LPARAM) status); + return size * nmemb; +} + void StatusCallback(void *param) { HWND hwnd = (HWND)param; + char *statusUrl = hsWStringToString(GetServerStatusUrl()); + CURL *hCurl = curl_easy_init(); + + // For reporting errors + char curlError[CURL_ERROR_SIZE]; + curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, curlError); + while(s_loginDlgRunning) { - // get status message from webpage and display in status area. - const wchar *path = BuildTypeServerStatusPath(); - if(path) - { - HINTERNET hSession = 0; - HINTERNET hConnect = 0; - HINTERNET hRequest = 0; - - hSession = WinHttpOpen( - L"UruClient/1.0", - WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, - WINHTTP_NO_PROXY_NAME, - WINHTTP_NO_PROXY_BYPASS, 0 - ); - if(hSession) - { - HINTERNET hConnect = WinHttpConnect( hSession, STATUS_PATH, INTERNET_DEFAULT_HTTP_PORT, 0); - if(hConnect) - { - HINTERNET hRequest = WinHttpOpenRequest( - hConnect, - L"GET", - path, - NULL, - WINHTTP_NO_REFERER, - WINHTTP_DEFAULT_ACCEPT_TYPES, - 0 - ); - if(hRequest) - { - static char data[256] = {0}; - DWORD bytesRead; - WinHttpSendRequest( - hRequest, - WINHTTP_NO_ADDITIONAL_HEADERS, - 0, - WINHTTP_NO_REQUEST_DATA, - 0, - 0, - 0 - ); - WinHttpReceiveResponse(hRequest, 0); - WinHttpReadData(hRequest, data, 255, &bytesRead); - data[bytesRead] = 0; - if(bytesRead) - PostMessage(hwnd, WM_USER_SETSTATUSMSG, 0, (LPARAM) data); - } - } - } - WinHttpCloseHandle(hRequest); - WinHttpCloseHandle(hConnect); - WinHttpCloseHandle(hSession); - } - else - break; // no status message + curl_easy_setopt(hCurl, CURLOPT_URL, statusUrl); + curl_easy_setopt(hCurl, CURLOPT_USERAGENT, "UruClient/1.0"); + curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, &CurlCallback); + curl_easy_setopt(hCurl, CURLOPT_WRITEDATA, param); + + if (curl_easy_perform(hCurl) != 0) + PostMessage(hwnd, WM_USER_SETSTATUSMSG, 0, (LPARAM) curlError); for(unsigned i = 0; i < UPDATE_STATUSMSG_SECONDS && s_loginDlgRunning; ++i) { Sleep(1000); } } + + delete [] statusUrl; + curl_easy_cleanup(hCurl); + s_statusEvent.Signal(); } @@ -1665,7 +1638,6 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nC return PARABLE_NORMAL_EXIT; } - /////////<<<<<<<< FILE *serverini = _wfopen(serverIni, L"rb"); if (serverini) { @@ -1678,13 +1650,14 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nC hsMessageBox("No server.ini file found. Please check your URU installation.", "Error", hsMessageBoxNormal); return PARABLE_NORMAL_EXIT; } - /////////<<<<<<<< NetCliAuthAutoReconnectEnable(false); NetCommSetReadIniAccountInfo(!doIntroDialogs); InitNetClientComm(); + curl_global_init(CURL_GLOBAL_ALL); + wchar acctName[kMaxAccountNameLength]; // if we're being launched from gametap then don't use the intro dialogs @@ -1771,6 +1744,8 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nC } } + curl_global_cleanup(); + if (needExit) { DeInitNetClientComm(); return PARABLE_NORMAL_EXIT;