summaryrefslogtreecommitdiff
path: root/includes
diff options
context:
space:
mode:
authorNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-06-19 15:38:02 -0700
committerNicolas "Pixel" Noble <pixel@nobis-crew.org>2014-06-19 15:38:02 -0700
commitc83a8a951cce9b7ed541c293993f708f720bf28d (patch)
treee2c92a5778de9829eef0b03bb49bca4acdba2396 /includes
parent948dd878a4060bac728f0af1cf7c0d0048ddace0 (diff)
Finishing up basic curl integration and adding a basic DownloadTask.
Diffstat (limited to 'includes')
-rw-r--r--includes/CurlTask.h39
-rw-r--r--includes/Exceptions.h10
-rw-r--r--includes/TaskMan.h5
3 files changed, 38 insertions, 16 deletions
diff --git a/includes/CurlTask.h b/includes/CurlTask.h
index 0aa62a0..41243ec 100644
--- a/includes/CurlTask.h
+++ b/includes/CurlTask.h
@@ -7,20 +7,45 @@
namespace Balau {
class CurlTask : public StacklessTask {
-public:
- CurlTask();
+ public:
+ CurlTask();
+ ~CurlTask();
friend class TaskMan;
protected:
CURL * m_curlHandle;
-private:
- void curlDone(CURLcode result);
+ void registerCurlHandle() { getTaskMan()->registerCurlHandle(this); }
+ void unregisterCurlHandle() { getTaskMan()->unregisterCurlHandle(this); }
+ private:
+ virtual size_t writeFunction(char * ptr, size_t size, size_t nmemb) { return size * nmemb; }
+ virtual size_t readFunction(void * ptr, size_t size, size_t nmemb) { return CURL_READFUNC_ABORT; }
+ virtual int debugFunction(curl_infotype info, char * str, size_t str_len) { return 0; }
+ virtual void curlDone(CURLcode result) { }
static size_t writeFunctionStatic(char * ptr, size_t size, size_t nmemb, void * userdata);
- virtual size_t writeFunction(char * ptr, size_t size, size_t nmemb) { return size * nmemb; }
static size_t readFunctionStatic(void * ptr, size_t size, size_t nmemb, void * userdata);
- virtual size_t readFunction(void * ptr, size_t size, size_t nmemb) { return CURL_READFUNC_ABORT; }
static int debugFunctionStatic(CURL * easy, curl_infotype info, char * str, size_t str_len, void * userdata);
- virtual int debugFunction(curl_infotype info, char * str, size_t str_len) { return 0; }
+};
+
+class DownloadTask : public CurlTask {
+ public:
+ DownloadTask(const String & url);
+ const String & getData() const { return m_data; }
+ bool isDone() { return m_done; }
+ long responseCode() { return m_responseCode; }
+
+ protected:
+ String m_data;
+ CURLcode m_curlResult;
+ long m_responseCode;
+
+ private:
+ virtual const char * getName() const override { return m_name.to_charp(); }
+ virtual void Do() override;
+ virtual void curlDone(CURLcode result) override;
+ virtual size_t writeFunction(char * ptr, size_t size, size_t nmemb) override { m_data += ptr; return size * nmemb; }
+ String m_name;
+ Events::Custom m_evt;
+ bool m_done = false;
};
};
diff --git a/includes/Exceptions.h b/includes/Exceptions.h
index 1c66968..11d0d7b 100644
--- a/includes/Exceptions.h
+++ b/includes/Exceptions.h
@@ -97,15 +97,7 @@ static inline void * realloc(void * previous, size_t size) {
};
-static inline void AssertHelperInner(const String & msg, const char * details = NULL) throw (GeneralException) {
-#if defined(_MSC_VER) && defined(_DEBUG)
- if (IsDebuggerPresent())
- __debugbreak();
- else
-#endif
- throw GeneralException(msg, details);
-}
-
+void AssertHelperInner(const String & msg, const char * details = NULL) throw (GeneralException);
static inline void AssertHelper(const String & msg, const char * fmt, ...) printfwarning(2, 3);
static inline void AssertHelper(const String & msg, const char * fmt, ...) {
diff --git a/includes/TaskMan.h b/includes/TaskMan.h
index a51653b..1f35c1e 100644
--- a/includes/TaskMan.h
+++ b/includes/TaskMan.h
@@ -24,6 +24,7 @@ namespace gnu = __gnu_cxx;
namespace Balau {
class TaskScheduler;
+class CurlTask;
namespace Events {
@@ -85,6 +86,7 @@ class TaskMan {
void * m_fiber;
#endif
friend class Task;
+ friend class CurlTask;
friend class TaskScheduler;
template<class T>
friend T * createAsyncOp(T * op);
@@ -106,12 +108,15 @@ class TaskMan {
ev::timer m_curlTimer;
CURLM * m_curlMulti = false;
int m_curlStillRunning = 0;
+ bool m_curlGotNewHandles = false;
static int curlSocketCallbackStatic(CURL * easy, curl_socket_t s, int what, void * userp, void * socketp);
int curlSocketCallback(CURL * easy, curl_socket_t s, int what, void * socketp);
void curlSocketEventCallback(ev::io & w, int revents);
static int curlMultiTimerCallbackStatic(CURLM * multi, long timeout_ms, void * userp);
int curlMultiTimerCallback(CURLM * multi, long timeout_ms);
void curlMultiTimerEventCallback(ev::timer & w, int revents);
+ void registerCurlHandle(CurlTask * curlTask);
+ void unregisterCurlHandle(CurlTask * curlTask);
TaskMan(const TaskMan &) = delete;
TaskMan & operator=(const TaskMan &) = delete;