made the speedcheck actually work again
[platform/upstream/curl.git] / lib / speedcheck.c
1 /*****************************************************************************
2  *                                  _   _ ____  _     
3  *  Project                     ___| | | |  _ \| |    
4  *                             / __| | | | |_) | |    
5  *                            | (__| |_| |  _ <| |___ 
6  *                             \___|\___/|_| \_\_____|
7  *
8  *  The contents of this file are subject to the Mozilla Public License
9  *  Version 1.0 (the "License"); you may not use this file except in
10  *  compliance with the License. You may obtain a copy of the License at
11  *  http://www.mozilla.org/MPL/
12  *
13  *  Software distributed under the License is distributed on an "AS IS"
14  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15  *  License for the specific language governing rights and limitations
16  *  under the License.
17  *
18  *  The Original Code is Curl.
19  *
20  *  The Initial Developer of the Original Code is Daniel Stenberg.
21  *
22  *  Portions created by the Initial Developer are Copyright (C) 1998.
23  *  All Rights Reserved.
24  *
25  * ------------------------------------------------------------
26  * Main author:
27  * - Daniel Stenberg <daniel@haxx.se>
28  *
29  *      http://curl.haxx.se
30  *
31  * $Source$
32  * $Revision$
33  * $Date$
34  * $Author$
35  * $State$
36  * $Locker$
37  *
38  * ------------------------------------------------------------
39  ****************************************************************************/
40
41 #include "setup.h"
42
43 #include <stdio.h>
44 #if defined(__MINGW32__)
45 #include <winsock.h>
46 #endif
47
48 #include <curl/curl.h>
49 #include "urldata.h"
50 #include "sendf.h"
51 #include "speedcheck.h"
52
53 void speedinit(struct UrlData *data)
54 {
55   memset(&data->keeps_speed, 0, sizeof(struct timeval));
56 }
57
58 CURLcode speedcheck(struct UrlData *data,
59                     struct timeval now)
60 {
61   if((data->progress.current_speed >= 0) &&
62      data->low_speed_time &&
63      (tvlong(data->keeps_speed) != 0) &&
64      (data->progress.current_speed < data->low_speed_limit)) {
65
66     /* We are now below the "low speed limit". If we are below it
67        for "low speed time" seconds we consider that enough reason
68        to abort the download. */
69     
70     if( tvdiff(now, data->keeps_speed) > data->low_speed_time) {
71       /* we have been this slow for long enough, now die */
72       failf(data,
73             "Operation too slow. "
74             "Less than %d bytes/sec transfered the last %d seconds",
75             data->low_speed_limit,
76             data->low_speed_time);
77       return CURLE_OPERATION_TIMEOUTED;
78     }
79   }
80   else {
81     /* we keep up the required speed all right */
82     data->keeps_speed = now;
83   }
84   return CURLE_OK;
85 }
86