From 3a1b334193f430e7f4d49a7776d0c83b361aeda2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Sun, 19 May 2013 15:10:22 +0000 Subject: [PATCH] [GB.NET.CURL] * NEW: New Progress event raised while the FtpClient is downloading or uploading some data. The event is not raised by the HttpClient yet. * NEW: FtpClient has four new read-only properties to get information on uploading and downloading progress: Downloaded, Uploaded, TotalDownloaded and TotalUploaded. git-svn-id: svn://localhost/gambas/trunk@5663 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- gb.net.curl/src/CCurl.c | 57 ++++++++++++++++++++++++++++++++++++ gb.net.curl/src/CCurl.h | 6 ++++ gb.net.curl/src/CFtpClient.c | 14 +++++---- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/gb.net.curl/src/CCurl.c b/gb.net.curl/src/CCurl.c index 0154532cd..a56c0a7de 100644 --- a/gb.net.curl/src/CCurl.c +++ b/gb.net.curl/src/CCurl.c @@ -43,6 +43,7 @@ DECLARE_EVENT(EVENT_Finished); DECLARE_EVENT(EVENT_Error); DECLARE_EVENT(EVENT_Connect); DECLARE_EVENT(EVENT_Read); +DECLARE_EVENT(EVENT_Progress); static CCURL *_async_list = NULL; @@ -251,6 +252,18 @@ void CURL_init_stream(void *_object) GB.Stream.SetAvailableNow(&THIS->stream, TRUE); } + +static int curl_progress(void *_object, double dltotal, double dlnow, double ultotal, double ulnow) +{ + THIS->dltotal = (int64_t)dltotal; + THIS->dlnow = (int64_t)dlnow; + THIS->ultotal = (int64_t)ultotal; + THIS->ulnow = (int64_t)ulnow; + GB.Raise(THIS, EVENT_Progress, 0); + return 0; +} + + /*************************************************************** This CallBack is called each event loop by Gambas to test the status of curl descriptors @@ -364,6 +377,20 @@ bool CURL_check_active(void *_object) return FALSE; } +void CURL_set_progress(void *_object, bool progress) +{ + curl_easy_setopt(THIS_CURL, CURLOPT_NOPROGRESS, progress ? 0 : 1); + if (progress) + { + curl_easy_setopt(THIS_CURL, CURLOPT_PROGRESSFUNCTION , curl_progress); + curl_easy_setopt(THIS_CURL, CURLOPT_PROGRESSDATA , _object); + } +} + + +//--------------------------------------------------------------------------- + + BEGIN_PROPERTY(Curl_User) if (READ_PROPERTY) @@ -575,6 +602,30 @@ BEGIN_PROPERTY(Curl_Debug) END_PROPERTY +BEGIN_PROPERTY(Curl_Downloaded) + + GB.ReturnLong(THIS->dlnow); + +END_PROPERTY + +BEGIN_PROPERTY(Curl_Uploaded) + + GB.ReturnLong(THIS->ulnow); + +END_PROPERTY + +BEGIN_PROPERTY(Curl_TotalDownloaded) + + GB.ReturnLong(THIS->dltotal); + +END_PROPERTY + +BEGIN_PROPERTY(Curl_TotalUploaded) + + GB.ReturnLong(THIS->ultotal); + +END_PROPERTY + //************************************************************************* //#################### GAMBAS INTERFACE ################################### //************************************************************************* @@ -601,10 +652,16 @@ GB_DESC CCurlDesc[] = GB_PROPERTY_READ("ErrorText", "s", Curl_ErrorText), GB_PROPERTY("Debug", "b", Curl_Debug), + GB_PROPERTY_READ("Downloaded", "l", Curl_Downloaded), + GB_PROPERTY_READ("Uploaded", "l", Curl_Uploaded), + GB_PROPERTY_READ("TotalDownloaded", "l", Curl_TotalDownloaded), + GB_PROPERTY_READ("TotalUploaded", "l", Curl_TotalUploaded), + GB_EVENT("Finished", NULL, NULL, &EVENT_Finished), GB_EVENT("Connect", NULL, NULL, &EVENT_Connect), GB_EVENT("Read", NULL, NULL, &EVENT_Read), GB_EVENT("Error", NULL, NULL, &EVENT_Error), + GB_EVENT("Progress", NULL, NULL, &EVENT_Progress), GB_END_DECLARE }; diff --git a/gb.net.curl/src/CCurl.h b/gb.net.curl/src/CCurl.h index 52572b2f3..5de6778f1 100644 --- a/gb.net.curl/src/CCurl.h +++ b/gb.net.curl/src/CCurl.h @@ -68,6 +68,10 @@ typedef int timeout; int method; // 0->Get, 1->Put char *data; + int64_t dltotal; + int64_t dlnow; + int64_t ultotal; + int64_t ulnow; unsigned async : 1; unsigned in_list : 1; unsigned debug : 1; @@ -102,4 +106,6 @@ void CURL_init_stream(void *_object); bool CURL_check_active(void *_object); +void CURL_set_progress(void *_object, bool progress); + #endif diff --git a/gb.net.curl/src/CFtpClient.c b/gb.net.curl/src/CFtpClient.c index f0eb4a4fd..5aeba7726 100644 --- a/gb.net.curl/src/CFtpClient.c +++ b/gb.net.curl/src/CFtpClient.c @@ -43,7 +43,7 @@ #define EXEC_CMD 2 -static int ftp_read_curl (void *buffer, size_t size, size_t nmemb, void *_object) +static int ftp_read_curl(void *buffer, size_t size, size_t nmemb, void *_object) { FILE *file = THIS_FILE; THIS_STATUS = NET_RECEIVING_DATA; @@ -80,11 +80,11 @@ static int ftp_write_curl(void *buffer, size_t size, size_t nmemb, void *_object return nmemb; } - static void ftp_reset(void *_object) { GB.FreeString(&THIS->data); GB.Unref(&THIS_FTP->commands); + CURL_set_progress(THIS_CURL, FALSE); } @@ -112,13 +112,13 @@ static void ftp_initialize_curl_handle(void *_object) if (!THIS->async) { - curl_easy_setopt(THIS_CURL, CURLOPT_NOSIGNAL,1); - curl_easy_setopt(THIS_CURL, CURLOPT_TIMEOUT,THIS->timeout); + curl_easy_setopt(THIS_CURL, CURLOPT_NOSIGNAL, 1); + curl_easy_setopt(THIS_CURL, CURLOPT_TIMEOUT, THIS->timeout); } curl_easy_setopt(THIS_CURL, CURLOPT_VERBOSE, (bool)THIS->debug); curl_easy_setopt(THIS_CURL, CURLOPT_PRIVATE,(char*)_object); - + CURL_proxy_set(&THIS->proxy.proxy,THIS_CURL); CURL_user_set(&THIS->user, THIS_CURL); curl_easy_setopt(THIS_CURL, CURLOPT_URL,THIS_URL); @@ -150,6 +150,8 @@ static int ftp_exec(void *_object, int what, GB_ARRAY commands) curl_easy_setopt(THIS_CURL, CURLOPT_WRITEDATA , _object); curl_easy_setopt(THIS_CURL, CURLOPT_UPLOAD , 0); + CURL_set_progress(THIS_CURL, TRUE); + break; case EXEC_PUT: @@ -158,6 +160,8 @@ static int ftp_exec(void *_object, int what, GB_ARRAY commands) curl_easy_setopt(THIS_CURL, CURLOPT_READDATA , _object); curl_easy_setopt(THIS_CURL, CURLOPT_UPLOAD , 1); + CURL_set_progress(THIS_CURL, TRUE); + break; case EXEC_CMD: