Add net-limit module for limiting network bandwidth.
[tairon.git] / src / net-limit / limiter.h
blobed6d1f0a70d1388ec3ffc9c2245b19b030c0535e
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 David Brodsky *
4 * *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Library General Public *
7 * License as published by the Free Software Foundation and appearing *
8 * in the file LICENSE.LGPL included in the packaging of this file. *
9 * *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Library General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #ifndef _tairon_net_limit_limiter_h
18 #define _tairon_net_limit_limiter_h
20 #include <list>
22 #include <tairon/core/signals.h>
24 namespace Tairon
27 namespace Net
30 /** \brief Limiter is used for limiting network bandwidth.
32 class Limiter
34 public:
35 /** Constructs a Limiter object.
37 * \param r Maximum number of bytes to by transferred per second.
38 * \param p Parent of this limiter. Transfer rate of this limiter won't
39 * exceed its parent's rate. If no parent is specified then this
40 * limiter notices its child about free bandwidth to use. An instance
41 * of Tairon::Net::LimitManager has to be created before Limiter can be
42 * used.
44 Limiter(size_t r, Limiter *p = 0);
46 /** Destroys the object.
48 ~Limiter();
50 /** Returns maximum number of bytes that can be transferred.
52 size_t getAllowedSize(size_t max);
54 /** Removes a functor from queue of functors waiting for free transfer.
56 void removeFromQueue(Tairon::Core::Functor0<void> *f);
58 /** Sets number of transferred bytes to zero and calls waiting functors.
60 void reset();
62 /** Informs this Limiter about transferred data.
64 void transferred(size_t s);
66 /** Someone wants to be notified when there is a possibility to transfer
67 * more data.
69 * \param f Functor that will be called when there are data to read.
71 void wantMore(Tairon::Core::Functor0<void> *f);
73 private:
74 /** Parent of this limiter.
76 Limiter *parent;
78 /** Maximum number of bytes to be transferred per second.
80 size_t rate;
82 /** Number of bytes that can be transferred.
84 size_t rest;
86 /** List of functors that will be called when a transfer can be done.
88 std::list<Tairon::Core::Functor0<void> *> waiting;
90 /** Functor for calling wantMore() method.
92 Tairon::Core::Functor0<void> *wantMoreFunctor;
95 }; // namespace Net
97 }; // namespace Tairon
99 #endif
101 // vim: ai sw=4 ts=4 noet fdm=marker