Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.add curlpp::Multi::poll/wait 2.add option:TimeoutMs #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/README
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ usage.
of curlpp options.
Example 24: Binded method functor for DebugFunction example.
Example 25: Send e-mail over SMTP.

Example 26: Multi interface using multi poll/wait


115 changes: 115 additions & 0 deletions examples/example26.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) <2002-2005> <Jean-Philippe Barrette-LaPierre>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (curlpp), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* \file
* Multi demo using poll/wait
*
*/

#include <iostream>
#include <sstream>
#include <cstdlib>

#include <curlpp/Easy.hpp>
#include <curlpp/Exception.hpp>
#include <curlpp/Multi.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/cURLpp.hpp>

#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
#pragma comment(lib, "Ws2_32.lib")
#endif // WIN32

int main(int argc, char *argv[]) {
const char *url1 = "http://www.baidu.com";
const char *url2 = "https://cn.bing.com";

try {
curlpp::initialize();

curlpp::Easy request1;
curlpp::Easy request2;

request1.setOpt(new curlpp::options::Url(url1));
std::ostringstream os1;
curlpp::options::WriteStream ws1(&os1);
request1.setOpt(ws1);

std::list<std::string> headers1;
headers1.push_back("content-type:text/html; charset=utf-8");
request1.setOpt(new curlpp::Options::HttpHeader(headers1));
request1.setOpt(new curlpp::options::TimeoutMs(1000));


request2.setOpt(new curlpp::options::Url(url2));
std::ostringstream os2;
curlpp::options::WriteStream ws2(&os2);
request2.setOpt(ws2);
request2.setOpt(new curlpp::options::TimeoutMs(1000));

curlpp::Multi requests;
requests.add(&request1);
requests.add(&request2);

/* we start some action by calling perform right away */
int nbLeft = 0;
while (!requests.perform(&nbLeft)) {
};

while (nbLeft) {
// block until activity is detected on at least one of the handles or timeout_ms has passed
requests.wait(100);
while (!requests.perform(&nbLeft)) {
};
}

std::cout << "NB lefts: " << nbLeft << std::endl;

/* See how the transfers went */
/*
Multi::info returns a list of:
std::pair< curlpp::Easy, curlpp::Multi::Info >
*/
const curlpp::Multi::Msgs msgs = requests.info();
for (auto pos = msgs.begin(); pos != msgs.end(); pos++) {
if (pos->second.msg == CURLMSG_DONE) {
/* Find out which handle this message is about */
if (pos->first == &request1) {
std::cout << "First request completed with status " << pos->second.code << ",data:" << os1.str() << std::endl;
} else if (pos->first == &request2) {
std::cout << "Second request completed with status " << pos->second.code << ",data:" << os2.str() << std::endl;
}
}
}

curlpp::terminate();

} catch (curlpp::LogicError &e) {
std::cout << e.what() << std::endl;
} catch (curlpp::RuntimeError &e) {
std::cout << e.what() << std::endl;
}

return EXIT_SUCCESS;
}
6 changes: 6 additions & 0 deletions include/curlpp/Multi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ namespace curlpp
fd_set * exc_fd_set,
int * max_fd);

void wait(int timeout_ms, struct curl_waitfd extra_fds[] = nullptr, unsigned int extra_nfds = 0, int * numfds = nullptr);

#if LIBCURL_VERSION_NUM >= 0x074200
void poll(int timeout_ms, struct curl_waitfd extra_fds[] = nullptr, unsigned int extra_nfds = 0, int * numfds = nullptr);
#endif

typedef std::list<std::pair<const curlpp::Easy *, Multi::Info> >
Msgs;

Expand Down
1 change: 1 addition & 0 deletions include/curlpp/Options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ namespace options
*/

typedef curlpp::OptionTrait<long, CURLOPT_TIMEOUT> Timeout;
typedef curlpp::OptionTrait<long, CURLOPT_TIMEOUT_MS> TimeoutMs;
typedef curlpp::OptionTrait<long, CURLOPT_LOW_SPEED_LIMIT> LowSpeedLimit;
typedef curlpp::OptionTrait<long, CURLOPT_LOW_SPEED_TIME> LowSpeedTime;
typedef curlpp::OptionTrait<long, CURLOPT_MAXCONNECTS> MaxConnects;
Expand Down
16 changes: 16 additions & 0 deletions src/curlpp/Multi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,22 @@ curlpp::Multi::fdset(fd_set * read, fd_set * write, fd_set * exc, int * max)
}
}

void curlpp::Multi::wait(int timeout_ms, struct curl_waitfd extra_fds[], unsigned int extra_nfds, int * numfds) {
CURLMcode code = curl_multi_wait(mMultiHandle, extra_fds, extra_nfds, timeout_ms, numfds);
if (code != CURLM_OK) {
throw curlpp::RuntimeError(curl_multi_strerror(code));
}
}

#if LIBCURL_VERSION_NUM >= 0x074200
void curlpp::Multi::poll(int timeout_ms, struct curl_waitfd extra_fds[], unsigned int extra_nfds, int * numfds) {
CURLMcode code = curl_multi_poll(mMultiHandle, extra_fds, extra_nfds, timeout_ms, numfds);
if (code != CURLM_OK) {
throw curlpp::RuntimeError(curl_multi_strerror(code));
}
}
#endif

curlpp::Multi::Msgs
curlpp::Multi::info()
{
Expand Down