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.
This commit is contained in:
Alexander Tkachev 2016-05-15 00:31:02 +06:00
parent 6ad983bb72
commit 8b585d631b
6 changed files with 118 additions and 26 deletions

View file

@ -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 <curl/curl.h>
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

View file

@ -20,33 +20,29 @@
* *
*/ */
#ifndef BACKENDS_CLOUD_DROPBOX_FINALCOUNTDOWNREQUEST_H #ifndef BACKENDS_CLOUD_DROPBOX_CURLREQUEST_H
#define BACKENDS_CLOUD_DROPBOX_FINALCOUNTDOWNREQUEST_H #define BACKENDS_CLOUD_DROPBOX_CURLREQUEST_H
#include "backends/cloud/request.h" #include "backends/cloud/request.h"
typedef void CURLM;
namespace Cloud { namespace Cloud {
namespace Dropbox { namespace Dropbox {
class FinalCountdownRequest : public Cloud::Request { class CurlRequest : public Cloud::Request {
int _times; bool _firstTime;
CURLM *_curlm;
char *_url;
public: public:
FinalCountdownRequest(Callback cb) : Request(cb), _times(5) {}; CurlRequest(Callback cb, char *url);
virtual ~CurlRequest();
virtual bool handle() { 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 Dropbox
} //end of namespace Cloud::Dropbox } //end of namespace Cloud
#endif #endif

View file

@ -19,18 +19,26 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * 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/dropboxstorage.h"
#include "backends/cloud/dropbox/finalcountdownrequest.h" #include "backends/cloud/dropbox/curlrequest.h"
#include "common/debug.h" #include "common/debug.h"
#include <curl/curl.h>
namespace Cloud { namespace Cloud {
namespace Dropbox { namespace Dropbox {
static void finalCountdownCallback(void *ptr) { static void curlCallback(void *ptr) {
warning("ladies and gentlemen, this is your callback speaking"); debug("--- curl request is complete ---");
warning("the returned pointer is %d", ptr); }
warning("thank you for your attention");
DropboxStorage::DropboxStorage() {
curl_global_init(CURL_GLOBAL_ALL);
}
DropboxStorage::~DropboxStorage() {
curl_global_cleanup();
} }
void DropboxStorage::listDirectory(Common::String path) { void DropboxStorage::listDirectory(Common::String path) {
@ -38,7 +46,8 @@ void DropboxStorage::listDirectory(Common::String path) {
} }
void DropboxStorage::syncSaves() { void DropboxStorage::syncSaves() {
addRequest(new FinalCountdownRequest(finalCountdownCallback)); addRequest(new CurlRequest(curlCallback, "tkachov.ru"));
addRequest(new CurlRequest(curlCallback, "bash.im"));
} }
} //end of namespace Dropbox } //end of namespace Dropbox

View file

@ -29,7 +29,8 @@ namespace Cloud { namespace Dropbox {
class DropboxStorage: public Cloud::Storage { class DropboxStorage: public Cloud::Storage {
public: public:
DropboxStorage() {}; DropboxStorage();
virtual ~DropboxStorage();
virtual void listDirectory(Common::String path); virtual void listDirectory(Common::String path);
virtual void syncSaves(); virtual void syncSaves();

View file

@ -42,7 +42,10 @@ void Storage::handler() {
//TODO: lock mutex here (in case another handler() would be called before this one ends) //TODO: lock mutex here (in case another handler() would be called before this one ends)
warning("handler's here"); warning("handler's here");
for (Common::Array<Request *>::iterator i = _requests.begin(); i != _requests.end();) { for (Common::Array<Request *>::iterator i = _requests.begin(); i != _requests.end();) {
if ((*i)->handle()) _requests.erase(i); if ((*i)->handle()) {
delete (*i);
_requests.erase(i);
}
else ++i; else ++i;
} }
if (_requests.empty()) stopTimer(); if (_requests.empty()) stopTimer();

View file

@ -23,7 +23,8 @@ ifdef USE_CLOUD
MODULE_OBJS += \ MODULE_OBJS += \
cloud/manager.o \ cloud/manager.o \
cloud/storage.o \ cloud/storage.o \
cloud/dropbox/dropboxstorage.o cloud/dropbox/dropboxstorage.o \
cloud/dropbox/curlrequest.o
endif endif
ifdef USE_ELF_LOADER ifdef USE_ELF_LOADER