speedcheck.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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 "speedcheck.h"
  30. void Curl_speedinit(struct SessionHandle *data)
  31. {
  32. memset(&data->state.keeps_speed, 0, sizeof(struct timeval));
  33. }
  34. CURLcode Curl_speedcheck(struct SessionHandle *data,
  35. struct timeval now)
  36. {
  37. if((data->progress.current_speed >= 0) &&
  38. data->set.low_speed_time &&
  39. (Curl_tvlong(data->state.keeps_speed) != 0) &&
  40. (data->progress.current_speed < data->set.low_speed_limit)) {
  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( (Curl_tvdiff(now, data->state.keeps_speed)/1000) >
  45. data->set.low_speed_time) {
  46. /* we have been this slow for long enough, now die */
  47. failf(data,
  48. "Operation too slow. "
  49. "Less than %d bytes/sec transfered the last %d seconds",
  50. data->set.low_speed_limit,
  51. data->set.low_speed_time);
  52. return CURLE_OPERATION_TIMEOUTED;
  53. }
  54. }
  55. else {
  56. /* we keep up the required speed all right */
  57. data->state.keeps_speed = now;
  58. }
  59. return CURLE_OK;
  60. }