example14.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) <2002-2006> <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. * \file
  25. * Multi demo.
  26. *
  27. */
  28. #include <iostream>
  29. #include <cstdlib>
  30. #include <curlpp/cURLpp.hpp>
  31. #include <curlpp/Easy.hpp>
  32. #include <curlpp/Multi.hpp>
  33. #include <curlpp/Options.hpp>
  34. #include <curlpp/Exception.hpp>
  35. #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  36. #pragma comment(lib, "Ws2_32.lib")
  37. #endif // WIN32
  38. int main(int argc, char *argv[])
  39. {
  40. if(argc < 3) {
  41. std::cerr << "Example 13: Wrong number of arguments" << std::endl
  42. << "Example 13: Usage: example13 url1 url2"
  43. << std::endl;
  44. return EXIT_FAILURE;
  45. }
  46. char *url1 = argv[1];
  47. char *url2 = argv[2];
  48. try {
  49. curlpp::Cleanup cleaner;
  50. curlpp::Easy request1;
  51. curlpp::Easy request2;
  52. request1.setOpt(new curlpp::options::Url(url1));
  53. request1.setOpt(new curlpp::options::Verbose(true));
  54. request2.setOpt(new curlpp::options::Url(url2));
  55. request2.setOpt(new curlpp::options::Verbose(true));
  56. int nbLeft;
  57. curlpp::Multi requests;
  58. requests.add(&request1);
  59. requests.add(&request2);
  60. /* we start some action by calling perform right away */
  61. while(!requests.perform(&nbLeft)) {};
  62. while(nbLeft) {
  63. struct timeval timeout;
  64. int rc; /* select() return code */
  65. fd_set fdread;
  66. fd_set fdwrite;
  67. fd_set fdexcep;
  68. int maxfd;
  69. FD_ZERO(&fdread);
  70. FD_ZERO(&fdwrite);
  71. FD_ZERO(&fdexcep);
  72. /* set a suitable timeout to play around with */
  73. timeout.tv_sec = 1;
  74. timeout.tv_usec = 0;
  75. /* get file descriptors from the transfers */
  76. requests.fdset(&fdread, &fdwrite, &fdexcep, &maxfd);
  77. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  78. switch(rc) {
  79. case -1:
  80. /* select error */
  81. nbLeft = 0;
  82. printf("select() returns error, this is badness\n");
  83. break;
  84. case 0:
  85. /* timeout, do something else */
  86. break;
  87. default:
  88. /* one or more of curl's file descriptors say there's data to read
  89. or write */
  90. while(!requests.perform(&nbLeft)) {};
  91. break;
  92. }
  93. }
  94. std::cout << "NB lefts: " << nbLeft << std::endl;
  95. /* See how the transfers went */
  96. /*
  97. Multi::info returns a list of:
  98. std::pair< curlpp::Easy, curlpp::Multi::Info >
  99. */
  100. curlpp::Multi::Msgs msgs = requests.info();
  101. for(curlpp::Multi::Msgs::iterator pos = msgs.begin();
  102. pos != msgs.end();
  103. pos++) {
  104. if(pos->second.msg == CURLMSG_DONE) {
  105. /* Find out which handle this message is about */
  106. if(pos->first == &request1) {
  107. printf("First request completed with status %d\n", pos->second.code);
  108. }
  109. else if(pos->first == &request2) {
  110. printf("Second request completed with status %d\n", pos->second.code);
  111. }
  112. }
  113. }
  114. }
  115. catch ( curlpp::LogicError & e ) {
  116. std::cout << e.what() << std::endl;
  117. }
  118. catch ( curlpp::RuntimeError & e ) {
  119. std::cout << e.what() << std::endl;
  120. }
  121. return EXIT_SUCCESS;
  122. }