tool_cb_rea.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "memdebug.h" /* keep this as LAST include */
  36. /*
  37. ** callback for CURLOPT_READFUNCTION
  38. */
  39. size_t tool_read_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
  40. {
  41. ssize_t rc = 0;
  42. struct InStruct *in = userdata;
  43. struct OperationConfig *config = in->config;
  44. if(config->timeout_ms) {
  45. struct timeval now = tvnow();
  46. long msdelta = tvdiff(now, in->per->start);
  47. if(msdelta > config->timeout_ms)
  48. /* timeout */
  49. return 0;
  50. #ifndef WIN32
  51. /* this logic waits on read activity on a file descriptor that is not a
  52. socket which makes it not work with select() on Windows */
  53. else {
  54. fd_set bits;
  55. struct timeval timeout;
  56. long wait = config->timeout_ms - msdelta;
  57. /* wait this long at the most */
  58. timeout.tv_sec = wait/1000;
  59. timeout.tv_usec = (wait%1000)*1000;
  60. FD_ZERO(&bits);
  61. FD_SET(in->fd, &bits);
  62. if(!select(in->fd + 1, &bits, NULL, NULL, &timeout))
  63. return 0; /* timeout */
  64. }
  65. #endif
  66. }
  67. rc = read(in->fd, buffer, sz*nmemb);
  68. if(rc < 0) {
  69. if(errno == EAGAIN) {
  70. errno = 0;
  71. in->config->readbusy = TRUE;
  72. return CURL_READFUNC_PAUSE;
  73. }
  74. /* since size_t is unsigned we can't return negative values fine */
  75. rc = 0;
  76. }
  77. in->config->readbusy = FALSE;
  78. /* when select() rerturned zero here, it timed out */
  79. return (size_t)rc;
  80. }
  81. /*
  82. ** callback for CURLOPT_XFERINFOFUNCTION used to unpause busy reads
  83. */
  84. int tool_readbusy_cb(void *clientp,
  85. curl_off_t dltotal, curl_off_t dlnow,
  86. curl_off_t ultotal, curl_off_t ulnow)
  87. {
  88. struct per_transfer *per = clientp;
  89. struct OperationConfig *config = per->config;
  90. (void)dltotal; /* unused */
  91. (void)dlnow; /* unused */
  92. (void)ultotal; /* unused */
  93. (void)ulnow; /* unused */
  94. if(config->readbusy) {
  95. config->readbusy = FALSE;
  96. curl_easy_pause(per->curl, CURLPAUSE_CONT);
  97. }
  98. return per->noprogress? 0 : CURL_PROGRESSFUNC_CONTINUE;
  99. }