tool_cb_rea.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "tool_setup.h"
  25. #ifdef HAVE_SYS_SELECT_H
  26. #include <sys/select.h>
  27. #endif
  28. #define ENABLE_CURLX_PRINTF
  29. /* use our own printf() functions */
  30. #include "curlx.h"
  31. #include "tool_cfgable.h"
  32. #include "tool_cb_rea.h"
  33. #include "tool_operate.h"
  34. #include "tool_util.h"
  35. #include "tool_msgs.h"
  36. #include "tool_sleep.h"
  37. #include "memdebug.h" /* keep this as LAST include */
  38. /*
  39. ** callback for CURLOPT_READFUNCTION
  40. */
  41. size_t tool_read_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
  42. {
  43. ssize_t rc = 0;
  44. struct per_transfer *per = userdata;
  45. struct OperationConfig *config = per->config;
  46. if((per->uploadfilesize != -1) &&
  47. (per->uploadedsofar == per->uploadfilesize)) {
  48. /* done */
  49. return 0;
  50. }
  51. if(config->timeout_ms) {
  52. struct timeval now = tvnow();
  53. long msdelta = tvdiff(now, per->start);
  54. if(msdelta > config->timeout_ms)
  55. /* timeout */
  56. return 0;
  57. #ifndef _WIN32
  58. /* this logic waits on read activity on a file descriptor that is not a
  59. socket which makes it not work with select() on Windows */
  60. else {
  61. fd_set bits;
  62. struct timeval timeout;
  63. long wait = config->timeout_ms - msdelta;
  64. /* wait this long at the most */
  65. timeout.tv_sec = wait/1000;
  66. timeout.tv_usec = (int)((wait%1000)*1000);
  67. FD_ZERO(&bits);
  68. FD_SET(per->infd, &bits);
  69. if(!select(per->infd + 1, &bits, NULL, NULL, &timeout))
  70. return 0; /* timeout */
  71. }
  72. #endif
  73. }
  74. rc = read(per->infd, buffer, sz*nmemb);
  75. if(rc < 0) {
  76. if(errno == EAGAIN) {
  77. errno = 0;
  78. config->readbusy = TRUE;
  79. return CURL_READFUNC_PAUSE;
  80. }
  81. /* since size_t is unsigned we can't return negative values fine */
  82. rc = 0;
  83. }
  84. if((per->uploadfilesize != -1) &&
  85. (per->uploadedsofar + rc > per->uploadfilesize)) {
  86. /* do not allow uploading more than originally set out to do */
  87. curl_off_t delta = per->uploadedsofar + rc - per->uploadfilesize;
  88. warnf(per->config->global, "File size larger in the end than when "
  89. "started. Dropping at least %" CURL_FORMAT_CURL_OFF_T " bytes",
  90. delta);
  91. rc = (ssize_t)(per->uploadfilesize - per->uploadedsofar);
  92. }
  93. config->readbusy = FALSE;
  94. /* when select() returned zero here, it timed out */
  95. return (size_t)rc;
  96. }
  97. /*
  98. ** callback for CURLOPT_XFERINFOFUNCTION used to unpause busy reads
  99. */
  100. int tool_readbusy_cb(void *clientp,
  101. curl_off_t dltotal, curl_off_t dlnow,
  102. curl_off_t ultotal, curl_off_t ulnow)
  103. {
  104. struct per_transfer *per = clientp;
  105. struct OperationConfig *config = per->config;
  106. (void)dltotal; /* unused */
  107. (void)dlnow; /* unused */
  108. (void)ultotal; /* unused */
  109. (void)ulnow; /* unused */
  110. if(config->readbusy) {
  111. /* lame code to keep the rate down because the input might not deliver
  112. anything, get paused again and come back here immediately */
  113. static long rate = 500;
  114. static struct timeval prev;
  115. static curl_off_t ulprev;
  116. if(ulprev == ulnow) {
  117. /* it did not upload anything since last call */
  118. struct timeval now = tvnow();
  119. if(prev.tv_sec)
  120. /* get a rolling average rate */
  121. /* rate = rate - rate/4 + tvdiff(now, prev)/4; */
  122. rate -= rate/4 - tvdiff(now, prev)/4;
  123. prev = now;
  124. }
  125. else {
  126. rate = 50;
  127. ulprev = ulnow;
  128. }
  129. if(rate >= 50) {
  130. /* keeps the looping down to 20 times per second in the crazy case */
  131. config->readbusy = FALSE;
  132. curl_easy_pause(per->curl, CURLPAUSE_CONT);
  133. }
  134. else
  135. /* sleep half a period */
  136. tool_go_sleep(25);
  137. }
  138. return per->noprogress? 0 : CURL_PROGRESSFUNC_CONTINUE;
  139. }