From eae58c6f56a90ef5c464a22e77eff71863716a75 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 9 Oct 2015 02:22:28 +0100 Subject: Get rid of class Curl for being a) used wrong and b) just adding complication --- project2/url/curlHelper.cpp | 49 ++--------------------------------------- project2/url/curlHelper.h | 21 +----------------- project2/url/downloadToFile.cpp | 6 +++-- project2/url/urlStream.cpp | 13 +++++++++-- 4 files changed, 18 insertions(+), 71 deletions(-) diff --git a/project2/url/curlHelper.cpp b/project2/url/curlHelper.cpp index d6ca64b..bd331e8 100644 --- a/project2/url/curlHelper.cpp +++ b/project2/url/curlHelper.cpp @@ -22,31 +22,14 @@ CurlHelper::CurlHelper() { } -Curl::Curl(const std::string & url) : - CurlHandle(url) -{ -} - CurlHelper::~CurlHelper() { } -size_t -Curl::curlReadHelperHandle(const char * ptr, size_t size, size_t nmemb, void *stream) -{ - return (*static_cast(stream))(ptr, size * nmemb); -} - -size_t -Curl::curlSendHelperHandle(char * ptr, size_t size, size_t nmemb, void *stream) -{ - return (*static_cast(stream))(ptr, size * nmemb); -} - -CurlPtr +AdHoc::Net::CurlHandlePtr CurlHelper::newCurl(ExecContext * ec) const { - CurlPtr c = new Curl(getUrl(ec)); + AdHoc::Net::CurlHandlePtr c = new AdHoc::Net::CurlHandle(getUrl(ec)); setCurlOpts(c.get(), ec); return c; } @@ -71,34 +54,6 @@ VariableCurlHelper::setCurlOpts(AdHoc::Net::CurlHandle * c, ExecContext * ec) co c->setopt(CURLOPT_TIMEOUT_MS, timeout(ec)); } -void -Curl::performRead(const ReadHandler & h) -{ - setReadHandler(h); - CurlHandle::perform(); -} - -void -Curl::setReadHandler(const ReadHandler & h) -{ - setopt(CURLOPT_WRITEDATA, &h); - setopt(CURLOPT_WRITEFUNCTION, &curlReadHelperHandle); -} - -void -Curl::performSend(const SendHandler & h) -{ - setSendHandler(h); - CurlHandle::perform(); -} - -void -Curl::setSendHandler(const SendHandler & h) -{ - setopt(CURLOPT_READDATA, &h); - setopt(CURLOPT_READFUNCTION, &curlSendHelperHandle); -} - std::string VariableCurlHelper::getUrl(ExecContext * ec) const { diff --git a/project2/url/curlHelper.h b/project2/url/curlHelper.h index ad73a31..d56173c 100644 --- a/project2/url/curlHelper.h +++ b/project2/url/curlHelper.h @@ -4,32 +4,13 @@ #include "variables.h" #include -class Curl : public AdHoc::Net::CurlHandle { - public: - Curl(const std::string & url); - virtual ~Curl() { } - - typedef boost::function2 ReadHandler; - typedef boost::function2 SendHandler; - - void performRead(const ReadHandler &); - void setReadHandler(const ReadHandler &); - void performSend(const SendHandler &); - void setSendHandler(const SendHandler &); - - private: - static size_t curlReadHelperHandle(const char * ptr, size_t size, size_t nmemb, void *stream); - static size_t curlSendHelperHandle(char * ptr, size_t size, size_t nmemb, void *stream); -}; -typedef boost::intrusive_ptr CurlPtr; - /// Project2 helper component to provide common access to remote resources via libcurl class CurlHelper { public: CurlHelper(); virtual ~CurlHelper(); - CurlPtr newCurl(ExecContext *) const; + AdHoc::Net::CurlHandlePtr newCurl(ExecContext *) const; virtual void setCurlOpts(AdHoc::Net::CurlHandle *, ExecContext *) const; protected: diff --git a/project2/url/downloadToFile.cpp b/project2/url/downloadToFile.cpp index 3b90ffc..1d73cce 100644 --- a/project2/url/downloadToFile.cpp +++ b/project2/url/downloadToFile.cpp @@ -17,13 +17,15 @@ class Download : public Task, VariableCurlHelper { void execute(ExecContext * ec) const { - CurlPtr c = newCurl(ec); FILE * file = fopen(destination(ec), "w"); if (!file) { throw syscall_error(errno); } AdHoc::ScopeExit tidy([=]{ fclose(file); }); - c->performRead([=](const char * data, size_t len) -> size_t { return fwrite(data, 1, len, file); }); + auto c = newCurl(ec); + c->setopt(CURLOPT_READDATA, file); + c->setopt(CURLOPT_READFUNCTION, &fwrite); + c->perform(); } private: diff --git a/project2/url/urlStream.cpp b/project2/url/urlStream.cpp index 21bf5c2..1c681c7 100644 --- a/project2/url/urlStream.cpp +++ b/project2/url/urlStream.cpp @@ -14,8 +14,17 @@ class UrlStream : public Stream, VariableCurlHelper { void runStream(const Sink & sink, ExecContext * ec) const { - CurlPtr c = newCurl(ec); - c->performRead(sink); + auto c = newCurl(ec); + c->setopt(CURLOPT_READDATA, &sink); + c->setopt(CURLOPT_READFUNCTION, &readToSink); + c->perform(); + } + + static size_t readToSink(const void *ptr, size_t size, size_t nmemb, void * sink) + { + auto s = static_cast(sink); + auto d = static_cast(ptr); + return (*s)(d, size * nmemb); } }; -- cgit v1.2.3