Skip to content

Commit 594a009

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

File tree

5 files changed

+184
-51
lines changed

5 files changed

+184
-51
lines changed

examples/README

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
configure.in is a good example to add curlpp to your project
22

3-
Note that example 22 is contains the easiest and shorter examples of cURLpp
3+
Note that example 22 is contains the easiest and shorter examples of cURLpp
44
usage.
55

66
Example 00: Minimal example for fetching a site.
@@ -13,14 +13,14 @@ usage.
1313
Example 07: Cookie interface example via getInfo.
1414
Demonstrates usage of Infos::CookieList which example04 did not.
1515
Example 08: verbose callback example, with exception safe handling.
16-
Example 09: verbose callback example, with exception safe handling,
16+
Example 09: verbose callback example, with exception safe handling,
1717
but without raiseException function.
1818
Example 10: Binded function functor for WriteFunction example.
1919
Example 11: Plain write function example.
2020
Example 12: HTTP POST example.
2121
Example 13: Simple Multi interface example.
2222
Example 14: Multi interface example with info function example.
23-
Example 15: Simple example for demonstrating the NoValueOptionTrait.
23+
Example 15: Simple example for demonstrating the NoValueOptionTrait.
2424
(SslEngineDefault)
2525
Example 16: HTTP POST example with HTTP Authentification.
2626
Example 17: Binded method functor for WriteFunction example.
@@ -29,10 +29,8 @@ usage.
2929
Example 20: std::ostream usage.
3030
Example 21: upload example with std::istream.
3131
Example 22: Real easy and quick examples.
32-
Example 23: Setting request options using iterators to custom container
32+
Example 23: Setting request options using iterators to custom container
3333
of curlpp options.
3434
Example 24: Binded method functor for DebugFunction example.
3535
Example 25: Send e-mail over SMTP.
36-
37-
38-
36+
Example 26: Multi interface using multi poll/wait

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: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/*
22
* Copyright (c) <2002-2009> <Jean-Philippe Barrette-LaPierre>
3-
*
3+
*
44
* 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,
5+
* a copy of this software and associated documentation files
6+
* (curlpp), to deal in the Software without restriction,
77
* including without limitation the rights to use, copy, modify, merge,
88
* publish, distribute, sublicense, and/or sell copies of the Software,
9-
* and to permit persons to whom the Software is furnished to do so,
9+
* and to permit persons to whom the Software is furnished to do so,
1010
* subject to the following conditions:
11-
*
11+
*
1212
* The above copyright notice and this permission notice shall be included
1313
* in all copies or substantial portions of the Software.
14-
*
14+
*
1515
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1616
* 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,
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,
2020
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2121
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
@@ -42,7 +42,7 @@ namespace curlpp
4242

4343
public:
4444

45-
struct Info
45+
struct Info
4646
{
4747
CURLcode code;
4848
CURLMSG msg;
@@ -61,10 +61,14 @@ namespace curlpp
6161
fd_set * write_fd_set,
6262
fd_set * exc_fd_set,
6363
int * max_fd);
64+
void wait(int timeout_ms, struct curl_waitfd extra_fds[] = nullptr, unsigned int extra_nfds = 0, int * numfds = nullptr);
65+
#if LIBCURL_VERSION_NUM >= 0x074200
66+
void poll(int timeout_ms, struct curl_waitfd extra_fds[] = nullptr, unsigned int extra_nfds = 0, int * numfds = nullptr);
67+
#endif
6468

6569
typedef std::list<std::pair<const curlpp::Easy *, Multi::Info> >
6670
Msgs;
67-
71+
6872
Msgs info();
6973

7074
private:

include/curlpp/Options.hpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/*
22
* Copyright (c) <2002-2009> <Jean-Philippe Barrette-LaPierre>
3-
*
3+
*
44
* 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,
5+
* a copy of this software and associated documentation files
6+
* (curlpp), to deal in the Software without restriction,
77
* including without limitation the rights to use, copy, modify, merge,
88
* publish, distribute, sublicense, and/or sell copies of the Software,
9-
* and to permit persons to whom the Software is furnished to do so,
9+
* and to permit persons to whom the Software is furnished to do so,
1010
* subject to the following conditions:
11-
*
11+
*
1212
* The above copyright notice and this permission notice shall be included
1313
* in all copies or substantial portions of the Software.
14-
*
14+
*
1515
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1616
* 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,
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,
2020
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2121
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
@@ -27,15 +27,15 @@
2727

2828
#include "Option.hpp"
2929

30-
#include <iostream>
30+
#include <iostream>
3131

3232

3333
#ifdef CURLPP_ALLOW_NOT_AVAILABLE
3434
#define DEF_IF_ALLOW_AVAILABLE (type,option,name) typedef curlpp::NotAvailableOptionTrait<type, option> name;
3535
#endif
3636

37-
// #begin define OPTION(version,type,option,name)
38-
// #if LIBCURL_VERSION_NUM >= version
37+
// #begin define OPTION(version,type,option,name)
38+
// #if LIBCURL_VERSION_NUM >= version
3939
// typedef curlpp::OptionTrait<type, option> name;
4040
// #else
4141
// DEF_IF_ALLOW_AVAILABLE(type,option,name)
@@ -81,7 +81,7 @@ namespace options
8181

8282

8383
/**
84-
* Callback options.
84+
* Callback options.
8585
*/
8686

8787
typedef curlpp::OptionTrait<curl_write_callback, CURLOPT_WRITEFUNCTION>
@@ -92,10 +92,10 @@ namespace options
9292

9393

9494
/**
95-
* Using this option will reset CURLOPT_WRITEFUNCTION to
96-
* default callback. In fact, use only this option if you only
97-
* want libcURL to use the FILE * given in argument instead
98-
* of stdout.
95+
* Using this option will reset CURLOPT_WRITEFUNCTION to
96+
* default callback. In fact, use only this option if you only
97+
* want libcURL to use the FILE * given in argument instead
98+
* of stdout.
9999
*/
100100

101101
#if LIBCURL_VERSION_NUM >= 0x070907
@@ -133,10 +133,10 @@ namespace options
133133

134134

135135
/**
136-
* Using this option will reset CURLOPT_READFUNCTION to
137-
* default callback. In fact, use only this option if you only
138-
* want libcURL to use the FILE * given in argument instead
139-
* of stdout.
136+
* Using this option will reset CURLOPT_READFUNCTION to
137+
* default callback. In fact, use only this option if you only
138+
* want libcURL to use the FILE * given in argument instead
139+
* of stdout.
140140
*/
141141

142142
#if LIBCURL_VERSION_NUM >= 0x070907
@@ -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: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/*
22
* Copyright (c) <2002-2009> <Jean-Philippe Barrette-LaPierre>
3-
*
3+
*
44
* 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,
5+
* a copy of this software and associated documentation files
6+
* (curlpp), to deal in the Software without restriction,
77
* including without limitation the rights to use, copy, modify, merge,
88
* publish, distribute, sublicense, and/or sell copies of the Software,
9-
* and to permit persons to whom the Software is furnished to do so,
9+
* and to permit persons to whom the Software is furnished to do so,
1010
* subject to the following conditions:
11-
*
11+
*
1212
* The above copyright notice and this permission notice shall be included
1313
* in all copies or substantial portions of the Software.
14-
*
14+
*
1515
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1616
* 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,
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,
2020
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2121
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
*/
@@ -36,7 +36,7 @@ curlpp::Multi::Multi()
3636
curlpp::Multi::~Multi()
3737
{
3838
// remove all the remaining easy handles
39-
while (!mHandles.empty())
39+
while (!mHandles.empty())
4040
{
4141
std::map<CURL *, const curlpp::Easy *>::iterator handle = mHandles.begin();
4242
curl_multi_remove_handle(mMultiHandle, handle->second->getHandle());
@@ -96,11 +96,27 @@ 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
{
102118
CURLMsg * msg; /* for picking up messages with the transfer status */
103-
119+
104120
int msgsInQueue;
105121
Msgs result;
106122
while ((msg = curl_multi_info_read(mMultiHandle, &msgsInQueue)) != NULL) {
@@ -112,4 +128,3 @@ curlpp::Multi::info()
112128

113129
return result;
114130
}
115-

0 commit comments

Comments
 (0)