libktorrent  2.2.0
trafficshapedsocket.h
1 /***************************************************************************
2  * Copyright (C) 2011 by Joris Guisson *
3  * joris.guisson@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program 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 *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 
22 #ifndef NET_TRAFFICSHAPEDSOCKET_H
23 #define NET_TRAFFICSHAPEDSOCKET_H
24 
25 #include <QMutex>
26 #include <net/socketdevice.h>
27 #include <util/constants.h>
28 
29 namespace net
30 {
31  class Speed;
32 
34  {
35  public:
36  SocketReader() {}
37  virtual ~SocketReader() {}
38 
45  virtual void onDataReady(bt::Uint8* buf, bt::Uint32 size) = 0;
46  };
47 
52  {
53  public:
55  TrafficShapedSocket(int fd,int ip_version);
56  TrafficShapedSocket(bool tcp,int ip_version);
57  virtual ~TrafficShapedSocket();
58 
60  SocketDevice* socketDevice() {return sock;}
61 
63  const SocketDevice* socketDevice() const {return sock;}
64 
66  void setReader(SocketReader* r) {rdr = r;}
67 
74  virtual Uint32 read(Uint32 max_bytes_to_read, bt::TimeStamp now);
75 
82  virtual Uint32 write(Uint32 max, bt::TimeStamp now) = 0;
83 
85  virtual bool bytesReadyToWrite() const = 0;
86 
88  int getDownloadRate() const;
89 
91  int getUploadRate() const;
92 
94  void updateSpeeds(bt::TimeStamp now);
95 
101  void setGroupID(Uint32 gid,bool upload);
102 
104  Uint32 downloadGroupID() const {return down_gid;}
105 
107  Uint32 uploadGroupID() const {return up_gid;}
108  protected:
114  virtual void postProcess(bt::Uint8* data, bt::Uint32 size);
115 
116  protected:
117  SocketReader* rdr;
118  Speed* down_speed;
119  Speed* up_speed;
120  Uint32 up_gid;
121  Uint32 down_gid; // group id which this torrent belongs to, group 0 means the default group
122  SocketDevice* sock;
123  mutable QMutex mutex;
124  };
125 
126 }
127 
128 #endif // NET_TRAFFICSHAPEDSOCKET_H
net::TrafficShapedSocket::updateSpeeds
void updateSpeeds(bt::TimeStamp now)
Update up and down speed.
net::TrafficShapedSocket::postProcess
virtual void postProcess(bt::Uint8 *data, bt::Uint32 size)
net::TrafficShapedSocket::setGroupID
void setGroupID(Uint32 gid, bool upload)
net::SocketDevice
Definition: socketdevice.h:32
net::TrafficShapedSocket::write
virtual Uint32 write(Uint32 max, bt::TimeStamp now)=0
net::TrafficShapedSocket::read
virtual Uint32 read(Uint32 max_bytes_to_read, bt::TimeStamp now)
net::Speed
Definition: speed.h:37
net::TrafficShapedSocket::bytesReadyToWrite
virtual bool bytesReadyToWrite() const =0
See if the socket has something ready to write.
net::TrafficShapedSocket::downloadGroupID
Uint32 downloadGroupID() const
Get the download group ID.
Definition: trafficshapedsocket.h:104
net::TrafficShapedSocket::setReader
void setReader(SocketReader *r)
Set the reader.
Definition: trafficshapedsocket.h:66
net::TrafficShapedSocket::getDownloadRate
int getDownloadRate() const
Get the current download rate.
net::SocketReader::onDataReady
virtual void onDataReady(bt::Uint8 *buf, bt::Uint32 size)=0
net::TrafficShapedSocket::getUploadRate
int getUploadRate() const
Get the current download rate.
net::TrafficShapedSocket::uploadGroupID
Uint32 uploadGroupID() const
Get the upload group ID.
Definition: trafficshapedsocket.h:107
net::TrafficShapedSocket::socketDevice
SocketDevice * socketDevice()
Get the SocketDevice.
Definition: trafficshapedsocket.h:60
net::TrafficShapedSocket::socketDevice
const SocketDevice * socketDevice() const
Get the SocketDevice (const vesion)
Definition: trafficshapedsocket.h:63
net::SocketReader
Definition: trafficshapedsocket.h:34
net::TrafficShapedSocket
Definition: trafficshapedsocket.h:52