Skip to content

Commit 3271b03

Browse files
1.add curlpp::Multi::poll/wait
2.add option:TimeoutMs 3.add example26,which use wait and TimeoutMs
1 parent 8840ec8 commit 3271b03

File tree

5 files changed

+139
-1
lines changed

5 files changed

+139
-1
lines changed

examples/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ usage.
3333
of curlpp options.
3434
Example 24: Binded method functor for DebugFunction example.
3535
Example 25: Send e-mail over SMTP.
36-
36+
Example 26: Multi interface using multi poll/wait
3737

3838

examples/example26.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* Copyright (c) <2002-2005> <Jean-Philippe Barrette-LaPierre>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining
5+
* a copy of this software and associated documentation files
6+
* (curlpp), to deal in the Software without restriction,
7+
* including without limitation the rights to use, copy, modify, merge,
8+
* publish, distribute, sublicense, and/or sell copies of the Software,
9+
* and to permit persons to whom the Software is furnished to do so,
10+
* subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included
13+
* in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16+
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18+
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19+
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20+
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
/**
25+
* \file
26+
* Multi demo using poll/wait
27+
*
28+
*/
29+
30+
#include <iostream>
31+
#include <sstream>
32+
#include <cstdlib>
33+
34+
#include <curlpp/Easy.hpp>
35+
#include <curlpp/Exception.hpp>
36+
#include <curlpp/Multi.hpp>
37+
#include <curlpp/Options.hpp>
38+
#include <curlpp/cURLpp.hpp>
39+
40+
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
41+
#pragma comment(lib, "Ws2_32.lib")
42+
#endif // WIN32
43+
44+
int main(int argc, char *argv[]) {
45+
const char *url1 = "http://www.baidu.com";
46+
const char *url2 = "https://cn.bing.com";
47+
48+
try {
49+
curlpp::initialize();
50+
51+
curlpp::Easy request1;
52+
curlpp::Easy request2;
53+
54+
request1.setOpt(new curlpp::options::Url(url1));
55+
std::ostringstream os1;
56+
curlpp::options::WriteStream ws1(&os1);
57+
request1.setOpt(ws1);
58+
59+
std::list<std::string> headers1;
60+
headers1.push_back("content-type:text/html; charset=utf-8");
61+
request1.setOpt(new curlpp::Options::HttpHeader(headers1));
62+
request1.setOpt(new curlpp::options::TimeoutMs(1000));
63+
64+
65+
request2.setOpt(new curlpp::options::Url(url2));
66+
std::ostringstream os2;
67+
curlpp::options::WriteStream ws2(&os2);
68+
request2.setOpt(ws2);
69+
request2.setOpt(new curlpp::options::TimeoutMs(1000));
70+
71+
curlpp::Multi requests;
72+
requests.add(&request1);
73+
requests.add(&request2);
74+
75+
/* we start some action by calling perform right away */
76+
int nbLeft = 0;
77+
while (!requests.perform(&nbLeft)) {
78+
};
79+
80+
while (nbLeft) {
81+
// block until activity is detected on at least one of the handles or timeout_ms has passed
82+
requests.wait(100);
83+
while (!requests.perform(&nbLeft)) {
84+
};
85+
}
86+
87+
std::cout << "NB lefts: " << nbLeft << std::endl;
88+
89+
/* See how the transfers went */
90+
/*
91+
Multi::info returns a list of:
92+
std::pair< curlpp::Easy, curlpp::Multi::Info >
93+
*/
94+
const curlpp::Multi::Msgs msgs = requests.info();
95+
for (auto pos = msgs.begin(); pos != msgs.end(); pos++) {
96+
if (pos->second.msg == CURLMSG_DONE) {
97+
/* Find out which handle this message is about */
98+
if (pos->first == &request1) {
99+
std::cout << "First request completed with status " << pos->second.code << ",data:" << os1.str() << std::endl;
100+
} else if (pos->first == &request2) {
101+
std::cout << "Second request completed with status " << pos->second.code << ",data:" << os2.str() << std::endl;
102+
}
103+
}
104+
}
105+
106+
curlpp::terminate();
107+
108+
} catch (curlpp::LogicError &e) {
109+
std::cout << e.what() << std::endl;
110+
} catch (curlpp::RuntimeError &e) {
111+
std::cout << e.what() << std::endl;
112+
}
113+
114+
return EXIT_SUCCESS;
115+
}

include/curlpp/Multi.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ namespace curlpp
6262
fd_set * exc_fd_set,
6363
int * max_fd);
6464

65+
void wait(int timeout_ms, struct curl_waitfd extra_fds[] = nullptr, unsigned int extra_nfds = 0, int * numfds = nullptr);
66+
67+
#if LIBCURL_VERSION_NUM >= 0x074200
68+
void poll(int timeout_ms, struct curl_waitfd extra_fds[] = nullptr, unsigned int extra_nfds = 0, int * numfds = nullptr);
69+
#endif
70+
6571
typedef std::list<std::pair<const curlpp::Easy *, Multi::Info> >
6672
Msgs;
6773

include/curlpp/Options.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ namespace options
305305
*/
306306

307307
typedef curlpp::OptionTrait<long, CURLOPT_TIMEOUT> Timeout;
308+
typedef curlpp::OptionTrait<long, CURLOPT_TIMEOUT_MS> TimeoutMs;
308309
typedef curlpp::OptionTrait<long, CURLOPT_LOW_SPEED_LIMIT> LowSpeedLimit;
309310
typedef curlpp::OptionTrait<long, CURLOPT_LOW_SPEED_TIME> LowSpeedTime;
310311
typedef curlpp::OptionTrait<long, CURLOPT_MAXCONNECTS> MaxConnects;

src/curlpp/Multi.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ curlpp::Multi::fdset(fd_set * read, fd_set * write, fd_set * exc, int * max)
9696
}
9797
}
9898

99+
void curlpp::Multi::wait(int timeout_ms, struct curl_waitfd extra_fds[], unsigned int extra_nfds, int * numfds) {
100+
CURLMcode code = curl_multi_wait(mMultiHandle, extra_fds, extra_nfds, timeout_ms, numfds);
101+
if (code != CURLM_OK) {
102+
throw curlpp::RuntimeError(curl_multi_strerror(code));
103+
}
104+
}
105+
106+
#if LIBCURL_VERSION_NUM >= 0x074200
107+
void curlpp::Multi::poll(int timeout_ms, struct curl_waitfd extra_fds[], unsigned int extra_nfds, int * numfds) {
108+
CURLMcode code = curl_multi_poll(mMultiHandle, extra_fds, extra_nfds, timeout_ms, numfds);
109+
if (code != CURLM_OK) {
110+
throw curlpp::RuntimeError(curl_multi_strerror(code));
111+
}
112+
}
113+
#endif
114+
99115
curlpp::Multi::Msgs
100116
curlpp::Multi::info()
101117
{

0 commit comments

Comments
 (0)