From a5e2c45653a79cf137c78e31d1cb2b0be4e85f1e Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Tue, 2 Jun 2020 19:13:44 +0200 Subject: [PATCH] support closures --- HTTPSStream.cc | 11 ++++++----- HTTPSStream.hh | 9 +++++---- UpdateStream.cc | 2 +- UpdateStream.hh | 2 +- downloadclient.cc | 4 ++-- downloadclient.hh | 4 ++-- test_noupdate.cc | 3 ++- test_updater.cc | 9 +++------ 8 files changed, 22 insertions(+), 22 deletions(-) diff --git a/HTTPSStream.cc b/HTTPSStream.cc index 5780f0c..83f1018 100644 --- a/HTTPSStream.cc +++ b/HTTPSStream.cc @@ -45,7 +45,7 @@ namespace pEp { HTTPSDevice::HTTPSDevice(string url, string user_agent, int version) : _url(url), _user_agent(user_agent), _version(version), - _stream(nullptr) + _stream(nullptr), notifyRead(nullptr) { if (version != 11) throw logic_error("only HTTP version 1.1 implemented"); @@ -54,7 +54,8 @@ namespace pEp { HTTPSDevice::HTTPSDevice(const HTTPSDevice& second) : _url(second._url), _user_agent(second._user_agent), - _version(second._version), _stream(nullptr) + _version(second._version), _stream(nullptr), + notifyRead(second.notifyRead) { _parser.body_limit(1000000000); } @@ -64,7 +65,7 @@ namespace pEp { close(); } - void HTTPSDevice::open(string url, notifyRead_t notifyRead) + void HTTPSDevice::open(string url, notifyRead_t *notifyRead) { if (url == "") { if (_url != "") @@ -125,7 +126,7 @@ namespace pEp { get(u.path, notifyRead); } - void HTTPSDevice::get(string path, notifyRead_t notifyRead) + void HTTPSDevice::get(string path, notifyRead_t *notifyRead) { if (path == "") throw invalid_argument("path needed for GET"); @@ -151,7 +152,7 @@ namespace pEp { http::read_header(*_stream, b, _parser); if (header()["Content-Length"] != "0" && notifyRead) - notifyRead(); + (*notifyRead)(); http::read(*_stream, b, _parser); diff --git a/HTTPSStream.hh b/HTTPSStream.hh index da580a5..582ed4a 100644 --- a/HTTPSStream.hh +++ b/HTTPSStream.hh @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -20,7 +21,7 @@ namespace pEp { namespace ssl = boost::asio::ssl; namespace http = boost::beast::http; - typedef void (*notifyRead_t)(void); + typedef function< void() > notifyRead_t; struct url_split { string protocol; @@ -47,15 +48,15 @@ namespace pEp { string _filename; public: - notifyRead_t notifyRead; + notifyRead_t *notifyRead; HTTPSDevice(string url = "", string user_agent="pEp/1.0", int version=11); HTTPSDevice(const HTTPSDevice& second); ~HTTPSDevice(); - void open(string url = "", notifyRead_t notifyRead = nullptr); - void get(string path, notifyRead_t notifyRead = nullptr); + void open(string url = "", notifyRead_t *notifyRead = nullptr); + void get(string path, notifyRead_t *notifyRead = nullptr); void close(); const http::fields& header() const { return _parser.get(); } diff --git a/UpdateStream.cc b/UpdateStream.cc index d413422..a47a4d6 100644 --- a/UpdateStream.cc +++ b/UpdateStream.cc @@ -32,7 +32,7 @@ namespace pEp { delete _str; } - void UpdateDevice::open(CryptoPP::RSA::PublicKey update_key, notifyRead_t notifyRead) + void UpdateDevice::open(CryptoPP::RSA::PublicKey update_key, notifyRead_t *notifyRead) { CryptoPP::RSAES< CryptoPP::OAEP >::Encryptor update_encryptor(update_key); diff --git a/UpdateStream.hh b/UpdateStream.hh index e9c7d96..5f6052c 100644 --- a/UpdateStream.hh +++ b/UpdateStream.hh @@ -41,7 +41,7 @@ namespace pEp { streamsize read(char* s, streamsize n); - void open(PublicKey update_key, notifyRead_t notifyRead = nullptr); + void open(PublicKey update_key, notifyRead_t *notifyRead = nullptr); void close(); string filename() { return _https->filename(); } diff --git a/downloadclient.cc b/downloadclient.cc index 67a9069..0406680 100644 --- a/downloadclient.cc +++ b/downloadclient.cc @@ -26,7 +26,7 @@ namespace pEp { return key; } - string update(product p, PublicKey update_key, notifyRead_t notifyRead) + string update(product p, PublicKey update_key, notifyRead_t *notifyRead) { UpdateStream us { UpdateDevice(p) }; @@ -41,7 +41,7 @@ namespace pEp { return us->filename(); } - string update(product p, string keyfile, notifyRead_t notifyRead) + string update(product p, string keyfile, notifyRead_t *notifyRead) { PublicKey update_key = load_key(keyfile); return update(p, update_key, notifyRead); diff --git a/downloadclient.hh b/downloadclient.hh index d19a66c..33db55d 100644 --- a/downloadclient.hh +++ b/downloadclient.hh @@ -56,11 +56,11 @@ namespace pEp { // // see sample in test_updater.cc - string update(product p, PublicKey update_key, notifyRead_t notifyRead = nullptr); + string update(product p, PublicKey update_key, notifyRead_t *notifyRead = nullptr); // update() - convenience function doing load_key() and update() - string update(product p, string keyfile, notifyRead_t notifyRead = nullptr); + string update(product p, string keyfile, notifyRead_t *notifyRead = nullptr); } } diff --git a/test_noupdate.cc b/test_noupdate.cc index 2987a0c..aafaa6e 100644 --- a/test_noupdate.cc +++ b/test_noupdate.cc @@ -23,7 +23,8 @@ int main() product p { "pEp for Something", "https://fdik.org/cgidownload?hash=66232323234242" }; try { - string filename = update(p, "public.der", notifyRead); + pEp::notifyRead_t notifyRead = [=]()->void{cout << "notify: read\n";exit(1);}; + string filename = update(p, "public.der", ¬ifyRead); cout << "downloaded " << filename << "\n"; return 1; } diff --git a/test_updater.cc b/test_updater.cc index f66a978..3198b39 100644 --- a/test_updater.cc +++ b/test_updater.cc @@ -2,17 +2,13 @@ // see LICENSE.txt #include +#include #include #include "downloadclient.cc" using namespace std; using namespace pEp::UpdateClient; -void notifyRead() -{ - cout << "notify: read\n"; -} - int main() { cout << "downloading...\n"; @@ -20,7 +16,8 @@ int main() product p { "pEp for Something", "https://fdik.org/cgidownload?hash=42232323234242" }; try { - string filename = update(p, "public.der", notifyRead); + pEp::notifyRead_t notifyRead = [=]()->void{cout << "notify: read\n";}; + string filename = update(p, "public.der", ¬ifyRead); cout << "downloaded " << filename << "\n"; return 0; }