summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authortastytea2020-01-08 21:27:27 +0100
committertastytea2020-01-08 21:27:27 +0100
commitd2de78ff9e0ccfc39df8c7bdf2491773342eb11e (patch)
tree3eeab2f78d6ef6f645e59dc8f925cdd57d9073f7 /include
parenta724006854f7364dbf305f84ee440d9664bb6307 (diff)
downloadmastodonpp-d2de78ff9e0ccfc39df8c7bdf2491773342eb11e.tar
mastodonpp-d2de78ff9e0ccfc39df8c7bdf2491773342eb11e.tar.gz
mastodonpp-d2de78ff9e0ccfc39df8c7bdf2491773342eb11e.zip
Add streaming support.
Diffstat (limited to 'include')
-rw-r--r--include/connection.hpp7
-rw-r--r--include/curl_wrapper.hpp63
2 files changed, 66 insertions, 4 deletions
diff --git a/include/connection.hpp b/include/connection.hpp
index 5ffe969..4bcdfe9 100644
--- a/include/connection.hpp
+++ b/include/connection.hpp
@@ -106,6 +106,13 @@ public:
*/
void set_proxy(string_view proxy);
+ /*!
+ * @brief Get new stream contents and delete them.
+ *
+ * @since 0.1.0
+ */
+ string get_new_stream_contents();
+
private:
Instance &_instance;
const string_view _baseuri;
diff --git a/include/curl_wrapper.hpp b/include/curl_wrapper.hpp
index ddc74fb..1ff1d07 100644
--- a/include/curl_wrapper.hpp
+++ b/include/curl_wrapper.hpp
@@ -22,6 +22,7 @@
#include "curl/curl.h"
#include <map>
+#include <mutex>
#include <string>
#include <string_view>
#include <variant>
@@ -31,6 +32,7 @@ namespace mastodonpp
{
using std::map;
+using std::mutex;
using std::string;
using std::string_view;
using std::variant;
@@ -91,10 +93,10 @@ public:
CURLWrapper();
//! Copy constructor
- CURLWrapper(const CURLWrapper &other) = default;
+ CURLWrapper(const CURLWrapper &other) = delete;
//! Move constructor
- CURLWrapper(CURLWrapper &&other) noexcept = default;
+ CURLWrapper(CURLWrapper &&other) noexcept = delete;
/*!
* @brief Cleans up curl and connection.
@@ -108,10 +110,10 @@ public:
virtual ~CURLWrapper() noexcept;
//! Copy assignment operator
- CURLWrapper& operator=(const CURLWrapper &other) = default;
+ CURLWrapper& operator=(const CURLWrapper &other) = delete;
//! Move assignment operator
- CURLWrapper& operator=(CURLWrapper &&other) noexcept = default;
+ CURLWrapper& operator=(CURLWrapper &&other) noexcept = delete;
/*!
* @brief Returns pointer to the CURL easy handle.
@@ -139,8 +141,30 @@ public:
*/
void set_proxy(string_view proxy);
+ /*!
+ * @brief Cancel the stream.
+ *
+ * The stream will be cancelled, usually whithin a second. The @link
+ * answer_type::curl_error_code curl_error_code @endlink of the answer will
+ * be set to 42 (`CURLE_ABORTED_BY_CALLBACK`).
+ *
+ * @since 0.1.0
+ */
+ void cancel_stream();
+
protected:
/*!
+ * @brief Mutex for #get_buffer a.k.a. _curl_buffer_body.
+ *
+ * This mutex is locked in `writer_body()` and
+ * Connection::get_new_stream_contents before anything is read or written
+ * from/to _curl_buffer_body.
+ *
+ * @since 0.1.0
+ */
+ mutex buffer_mutex;
+
+ /*!
* @brief Make a HTTP request.
*
* @param method The HTTP method.
@@ -153,11 +177,23 @@ protected:
answer_type make_request(const http_method &method, string uri,
const parametermap &parameters);
+ /*!
+ * @brief Returns a reference to the buffer libcurl writes into.
+ *
+ * @since 0.1.0
+ */
+ [[nodiscard]]
+ string &get_buffer()
+ {
+ return _curl_buffer_body;
+ }
+
private:
CURL *_connection;
char _curl_buffer_error[CURL_ERROR_SIZE];
string _curl_buffer_headers;
string _curl_buffer_body;
+ bool _stream_cancelled;
/*!
* @brief libcurl write callback function.
@@ -191,6 +227,25 @@ private:
}
/*!
+ * @brief libcurl transfer info function.
+ *
+ * Used to cancel streams.
+ *
+ * @since 0.1.0
+ */
+ int progress(void *clientp, curl_off_t dltotal, curl_off_t dlnow,
+ curl_off_t ultotal, curl_off_t ulnow);
+
+ //! @copydoc writer_body_wrapper
+ static inline int progress_wrapper(void *f, void *clientp,
+ curl_off_t dltotal, curl_off_t dlnow,
+ curl_off_t ultotal, curl_off_t ulnow)
+ {
+ return static_cast<CURLWrapper*>(f)->progress(clientp, dltotal, dlnow,
+ ultotal, ulnow);
+ }
+
+ /*!
* @brief Setup libcurl connection.
*
* @since 0.1.0