Add net-limit module for limiting network bandwidth.
[tairon.git] / src / net-limit / reader.h
blob89d4e0e4cc8f6d5b90d779ebec2a2067e562d4a2
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_reader_h
18 #define _tairon_net_limit_reader_h
20 #include <tairon/core/signals.h>
22 namespace Tairon
25 namespace Net
28 class Limiter;
29 class Socket;
31 /** \brief This class provides base class for readers.
33 * Reader is an object that reads data from a socket and limits incoming
34 * bandwidth according to its Limiter.
36 * This class must not be used directly because the internal buffer must be set
37 * correctly.
39 class Reader
41 public:
42 /** Creates a Reader object.
44 * \param s Socket from which the Reader is reading.
45 * \param l Limiter used for limiting bandwidth.
47 Reader(Socket *s, Limiter *l);
49 /** Destroys the object.
51 virtual ~Reader();
53 /** Returns pointer to the internal buffer.
55 const char *getBuffer() {
56 return buffer;
59 /** Reads data from the socket.
61 void read();
63 /** Sets offset to the internal buffer to zero. It is useful for
64 * reusing this reader.
66 void reset();
68 public:
69 /** Emitted when the internal buffer is full.
71 Tairon::Core::Signal1<void, Reader *> bufferFullSignal;
73 /** Emitted when there are new data in the buffer.
75 * The receiver gets pointer to this Reader as the first parameter and
76 * number of bytes read as the second one.
78 Tairon::Core::Signal2<void, Reader *, size_t> dataReadSignal;
80 /** This signal is emitted when a socket error occurs. It could also
81 * mean that the connection has been closed. The socket is already
82 * closed when the signal is emitted.
84 Tairon::Core::Signal2<void, Reader *, Socket *> errorSignal;
86 protected:
87 /** Buffer for storing incoming data.
89 char *buffer;
91 /** Size of the buffer;
93 unsigned int bufSize;
95 /** Limiter used for limiting bandwidth.
97 Limiter *limiter;
99 /** Offset from the beginning of the buffer.
101 unsigned int offset;
103 /** Functor for calling read method.
105 Tairon::Core::Functor0<void> *readFunctor;
107 /** Socket from which the Reader is reading.
109 Socket *socket;
112 }; // namespace Net
114 }; // namespace Tairon
116 #endif
118 // vim: ai sw=4 ts=4 noet fdm=marker