1 /*
2  * lftp - file transfer program
3  *
4  * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef RATELIMIT_H
21 #define RATELIMIT_H
22 
23 #include "TimeDate.h"
24 #include "buffer.h"
25 
26 class RateLimit
27 {
28 public:
29    class BytesPool
30    {
31       friend class RateLimit;
32 
33       int pool;
34       int rate;
35       int pool_max;
36       Time t;
37 
38       void AdjustTime();
39       void Reset();
40       void Used(int);
41    };
42 
43 private:
44    static xmap_p<RateLimit> *total;
45 
46    enum level_e { PER_CONN, PER_HOST, TOTAL } level;
47    RateLimit *parent;
48    int xfer_number;
49    BytesPool pool[2];
50 
51    void init(level_e lvl,const char *closure);
RateLimit(level_e lvl,const char * closure)52    RateLimit(level_e lvl,const char *closure) { init(lvl,closure); }
53 
54    void AddXfer(int add);
55 
56 public:
RateLimit(const char * closure)57    RateLimit(const char *closure) { init(PER_CONN,closure); }
58    ~RateLimit();
59 
60    enum dir_t { GET=0, PUT=1 };
61 
62    int BytesAllowed(dir_t how);
BytesAllowedToGet()63    int BytesAllowedToGet() { return BytesAllowed(GET); }
BytesAllowedToPut()64    int BytesAllowedToPut() { return BytesAllowed(PUT); }
65    void BytesUsed(int,dir_t);
BytesGot(int b)66    void BytesGot(int b) { BytesUsed(b,GET); }
BytesPut(int b)67    void BytesPut(int b) { BytesUsed(b,PUT); }
68    bool Relaxed(dir_t dir);
69    void Reset();
70 
71    void Reconfig(const char *name,const char *c);
72 
73    int LimitBufferSize(int size,dir_t d) const;
74    void SetBufferSize(IOBuffer *buf,int size) const;
SetBufferSize(SMTaskRef<IOBuffer> & buf,int size)75    void SetBufferSize(SMTaskRef<IOBuffer>& buf,int size) const { SetBufferSize(buf.get_non_const(),size); }
76 
77    static void ClassCleanup();
78 };
79 
80 #endif // RATELIMIT_H
81