5
5
6
6
#include " curl/curl.h"
7
7
8
- struct completion_job_data
8
+ template <typename ResultType>
9
+ struct handler_wrapper
9
10
{
10
- static void do_completion_job (void * completion_data)
11
+ static void invoke_once (void * completion_data, ResultType arg )
11
12
{
12
- (*reinterpret_cast <completion_job_data *>(completion_data))();
13
- delete reinterpret_cast <completion_job_data *>(completion_data);
13
+ (*reinterpret_cast <handler_wrapper *>(completion_data))(arg );
14
+ delete reinterpret_cast <handler_wrapper *>(completion_data);
14
15
}
15
16
16
- virtual ~completion_job_data ()
17
+ virtual ~handler_wrapper ()
17
18
{
18
19
}
19
20
20
- virtual void operator () ()
21
+ virtual void operator () (ResultType )
21
22
{
22
23
}
23
24
};
24
25
25
26
26
- template <typename continuation_t >
27
- struct completion_job_data_for_coro : public completion_job_data
27
+ template <typename T, typename continuation_t >
28
+ struct handler_wrapper_impl : public handler_wrapper <T>
28
29
{
29
30
continuation_t continuation;
30
31
31
- completion_job_data_for_coro (continuation_t && continuation)
32
+ handler_wrapper_impl (continuation_t && continuation)
32
33
: continuation(continuation)
33
34
{
34
35
}
35
36
36
- virtual void operator () () override
37
+ virtual void operator () (T t ) override
37
38
{
38
- continuation ();
39
+ continuation (std::move (t) );
39
40
}
40
41
};
41
42
42
- ucoro::awaitable<void > async_curl_http_post (std::string url)
43
+ ucoro::awaitable<CURLcode > async_curl_http_post (std::string url)
43
44
{
44
45
CURLM* curl = co_await ucoro::local_storage_t <CURLM*>{};
45
46
46
- co_await callback_awaitable<void >([curl, url](auto continuation)
47
+ auto curl_code = co_await callback_awaitable<CURLcode >([curl, url](auto continuation)
47
48
{
48
49
auto http_handle = curl_easy_init ();
49
50
50
- auto complete_op = new completion_job_data_for_coro< decltype (continuation)>(std::move (continuation));
51
+ auto complete_op = new handler_wrapper_impl<CURLcode, decltype (continuation)>(std::move (continuation));
51
52
52
53
/* set the options (I left out a few, you get the point anyway) */
53
54
curl_easy_setopt (http_handle, CURLOPT_URL, url.c_str ());
@@ -60,14 +61,12 @@ ucoro::awaitable<void> async_curl_http_post(std::string url)
60
61
});
61
62
62
63
// NOTE: 记住这个位置,叫 1 号位
63
- co_return ;
64
+ co_return curl_code ;
64
65
}
65
66
66
67
ucoro::awaitable<int > coro_compute_int (std::string url)
67
68
{
68
- co_await async_curl_http_post (url);
69
-
70
- co_return url.size ();
69
+ co_return co_await async_curl_http_post (url);
71
70
}
72
71
73
72
ucoro::awaitable<void > coro_compute_exec (std::string url)
@@ -124,7 +123,7 @@ int main(int argc, char** argv)
124
123
curl_easy_getinfo (e, CURLINFO_PRIVATE, &completion_job_data);
125
124
if (completion_job_data)
126
125
{
127
- completion_job_data::do_completion_job (completion_job_data);
126
+ handler_wrapper<CURLcode>:: invoke_once (completion_job_data, msg-> data . result );
128
127
}
129
128
130
129
curl_multi_remove_handle (curl, e);
0 commit comments