speedcheck.c 2.6 KB

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