From 8b585d631b21441dfa3de69d2c7c848d172c73be Mon Sep 17 00:00:00 2001 From: Alexander Tkachev Date: Sun, 15 May 2016 00:31:02 +0600 Subject: CLOUD: Add CurlRequest CurlRequest uses own multi_handle, in which it creates an easy_handle to make a request. Every time `handle()` is called it checks whether request is complete and, if it is, stops. --- backends/cloud/dropbox/curlrequest.cpp | 82 ++++++++++++++++++++++++++ backends/cloud/dropbox/curlrequest.h | 48 +++++++++++++++ backends/cloud/dropbox/dropboxstorage.cpp | 21 +++++-- backends/cloud/dropbox/dropboxstorage.h | 3 +- backends/cloud/dropbox/finalcountdownrequest.h | 52 ---------------- backends/cloud/storage.cpp | 5 +- 6 files changed, 151 insertions(+), 60 deletions(-) create mode 100644 backends/cloud/dropbox/curlrequest.cpp create mode 100644 backends/cloud/dropbox/curlrequest.h delete mode 100644 backends/cloud/dropbox/finalcountdownrequest.h (limited to 'backends/cloud') diff --git a/backends/cloud/dropbox/curlrequest.cpp b/backends/cloud/dropbox/curlrequest.cpp new file mode 100644 index 0000000000..216b56c19e --- /dev/null +++ b/backends/cloud/dropbox/curlrequest.cpp @@ -0,0 +1,82 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#define FORBIDDEN_SYMBOL_ALLOW_ALL + +#include "backends/cloud/dropbox/curlrequest.h" +#include "common/debug.h" +#include + +namespace Cloud { +namespace Dropbox { + +static size_t curlDataCallback(char *d, size_t n, size_t l, void *p) { + debug("%p got %d more bytes", p, n * l); + return n * l; +} + +CurlRequest::CurlRequest(Callback cb, char *url) : Request(cb), _firstTime(true) { + _curlm = curl_multi_init(); + _url = url; +} + +CurlRequest::~CurlRequest() { + curl_multi_cleanup(_curlm); +} + +bool CurlRequest::handle() { + if (_firstTime) { + CURL *eh = curl_easy_init(); + curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, curlDataCallback); + curl_easy_setopt(eh, CURLOPT_WRITEDATA, this); + curl_easy_setopt(eh, CURLOPT_HEADER, 0L); + curl_easy_setopt(eh, CURLOPT_URL, _url); + curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L); + curl_multi_add_handle(_curlm, eh); + + _firstTime = false; + } + + int U; + curl_multi_perform(_curlm, &U); + + int Q; + CURLMsg *_curlMsg; + while ((_curlMsg = curl_multi_info_read(_curlm, &Q))) { + if (_curlMsg->msg == CURLMSG_DONE) { + CURL *e = _curlMsg->easy_handle; + debug("R: %d - %s\n", _curlMsg->data.result, curl_easy_strerror(_curlMsg->data.result)); + curl_multi_remove_handle(_curlm, e); + curl_easy_cleanup(e); + + _callback(0); + return true; + } else { + debug("E: CURLMsg (%d)\n", _curlMsg->msg); + } + } + + return false; +} + +} //end of namespace Dropbox +} //end of namespace Cloud diff --git a/backends/cloud/dropbox/curlrequest.h b/backends/cloud/dropbox/curlrequest.h new file mode 100644 index 0000000000..8453fbb90a --- /dev/null +++ b/backends/cloud/dropbox/curlrequest.h @@ -0,0 +1,48 @@ +/* ScummVM - Graphic Adventure Engine +* +* ScummVM is the legal property of its developers, whose names +* are too numerous to list here. Please refer to the COPYRIGHT +* file distributed with this source distribution. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +* +*/ + +#ifndef BACKENDS_CLOUD_DROPBOX_CURLREQUEST_H +#define BACKENDS_CLOUD_DROPBOX_CURLREQUEST_H + +#include "backends/cloud/request.h" + +typedef void CURLM; + +namespace Cloud { +namespace Dropbox { + +class CurlRequest : public Cloud::Request { + bool _firstTime; + CURLM *_curlm; + char *_url; + +public: + CurlRequest(Callback cb, char *url); + virtual ~CurlRequest(); + + virtual bool handle(); +}; + +} //end of namespace Dropbox +} //end of namespace Cloud + +#endif diff --git a/backends/cloud/dropbox/dropboxstorage.cpp b/backends/cloud/dropbox/dropboxstorage.cpp index 8a0772dc3f..142a059457 100644 --- a/backends/cloud/dropbox/dropboxstorage.cpp +++ b/backends/cloud/dropbox/dropboxstorage.cpp @@ -19,18 +19,26 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ +#define FORBIDDEN_SYMBOL_ALLOW_ALL #include "backends/cloud/dropbox/dropboxstorage.h" -#include "backends/cloud/dropbox/finalcountdownrequest.h" +#include "backends/cloud/dropbox/curlrequest.h" #include "common/debug.h" +#include namespace Cloud { namespace Dropbox { -static void finalCountdownCallback(void *ptr) { - warning("ladies and gentlemen, this is your callback speaking"); - warning("the returned pointer is %d", ptr); - warning("thank you for your attention"); +static void curlCallback(void *ptr) { + debug("--- curl request is complete ---"); +} + +DropboxStorage::DropboxStorage() { + curl_global_init(CURL_GLOBAL_ALL); +} + +DropboxStorage::~DropboxStorage() { + curl_global_cleanup(); } void DropboxStorage::listDirectory(Common::String path) { @@ -38,7 +46,8 @@ void DropboxStorage::listDirectory(Common::String path) { } void DropboxStorage::syncSaves() { - addRequest(new FinalCountdownRequest(finalCountdownCallback)); + addRequest(new CurlRequest(curlCallback, "tkachov.ru")); + addRequest(new CurlRequest(curlCallback, "bash.im")); } } //end of namespace Dropbox diff --git a/backends/cloud/dropbox/dropboxstorage.h b/backends/cloud/dropbox/dropboxstorage.h index f80854b987..a0229438be 100644 --- a/backends/cloud/dropbox/dropboxstorage.h +++ b/backends/cloud/dropbox/dropboxstorage.h @@ -29,7 +29,8 @@ namespace Cloud { namespace Dropbox { class DropboxStorage: public Cloud::Storage { public: - DropboxStorage() {}; + DropboxStorage(); + virtual ~DropboxStorage(); virtual void listDirectory(Common::String path); virtual void syncSaves(); diff --git a/backends/cloud/dropbox/finalcountdownrequest.h b/backends/cloud/dropbox/finalcountdownrequest.h deleted file mode 100644 index 5ef942180c..0000000000 --- a/backends/cloud/dropbox/finalcountdownrequest.h +++ /dev/null @@ -1,52 +0,0 @@ -/* ScummVM - Graphic Adventure Engine -* -* ScummVM is the legal property of its developers, whose names -* are too numerous to list here. Please refer to the COPYRIGHT -* file distributed with this source distribution. -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -* -* You should have received a copy of the GNU General Public License -* along with this program; if not, write to the Free Software -* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -* -*/ - -#ifndef BACKENDS_CLOUD_DROPBOX_FINALCOUNTDOWNREQUEST_H -#define BACKENDS_CLOUD_DROPBOX_FINALCOUNTDOWNREQUEST_H - -#include "backends/cloud/request.h" - -namespace Cloud { -namespace Dropbox { - -class FinalCountdownRequest : public Cloud::Request { - int _times; - -public: - FinalCountdownRequest(Callback cb) : Request(cb), _times(5) {}; - - virtual bool handle() { - if (--_times == 0) { - warning("It's the final countdown!"); - _callback(0); //meh, don't have anything for you, my caller - return true; - } - - warning("%d...", _times); - return false; - } -}; - -} -} //end of namespace Cloud::Dropbox - -#endif diff --git a/backends/cloud/storage.cpp b/backends/cloud/storage.cpp index 23dcd8c379..0c5f6a39e7 100644 --- a/backends/cloud/storage.cpp +++ b/backends/cloud/storage.cpp @@ -42,7 +42,10 @@ void Storage::handler() { //TODO: lock mutex here (in case another handler() would be called before this one ends) warning("handler's here"); for (Common::Array::iterator i = _requests.begin(); i != _requests.end();) { - if ((*i)->handle()) _requests.erase(i); + if ((*i)->handle()) { + delete (*i); + _requests.erase(i); + } else ++i; } if (_requests.empty()) stopTimer(); -- cgit v1.2.3