speedcheck.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://curl.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "urldata.h"
  25. #include "sendf.h"
  26. #include "multiif.h"
  27. #include "speedcheck.h"
  28. void Curl_speedinit(struct SessionHandle *data)
  29. {
  30. memset(&data->state.keeps_speed, 0, sizeof(struct timeval));
  31. }
  32. CURLcode Curl_speedcheck(struct SessionHandle *data,
  33. struct timeval now)
  34. {
  35. if((data->progress.current_speed >= 0) &&
  36. data->set.low_speed_time &&
  37. (Curl_tvlong(data->state.keeps_speed) != 0) &&
  38. (data->progress.current_speed < data->set.low_speed_limit)) {
  39. long howlong = Curl_tvdiff(now, data->state.keeps_speed);
  40. long nextcheck = (data->set.low_speed_time * 1000) - howlong;
  41. /* We are now below the "low speed limit". If we are below it
  42. for "low speed time" seconds we consider that enough reason
  43. to abort the download. */
  44. if(nextcheck <= 0) {
  45. /* we have been this slow for long enough, now die */
  46. failf(data,
  47. "Operation too slow. "
  48. "Less than %ld bytes/sec transferred the last %ld seconds",
  49. data->set.low_speed_limit,
  50. data->set.low_speed_time);
  51. return CURLE_OPERATION_TIMEDOUT;
  52. }
  53. else {
  54. /* wait complete low_speed_time */
  55. Curl_expire(data, nextcheck);
  56. }
  57. }
  58. else {
  59. /* we keep up the required speed all right */
  60. data->state.keeps_speed = now;
  61. if(data->set.low_speed_limit)
  62. /* if there is a low speed limit enabled, we set the expire timer to
  63. make this connection's speed get checked again no later than when
  64. this time is up */
  65. Curl_expire(data, data->set.low_speed_time*1000);
  66. }
  67. return CURLE_OK;
  68. }