File indexing completed on 2025-01-05 04:37:21
0001 /* 0002 SPDX-FileCopyrightText: 2011 Joris Guisson <joris.guisson@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #ifndef NET_TRAFFICSHAPEDSOCKET_H 0008 #define NET_TRAFFICSHAPEDSOCKET_H 0009 0010 #include <QRecursiveMutex> 0011 #include <net/socketdevice.h> 0012 #include <util/constants.h> 0013 0014 namespace net 0015 { 0016 class Speed; 0017 0018 class SocketReader 0019 { 0020 public: 0021 SocketReader() 0022 { 0023 } 0024 virtual ~SocketReader() 0025 { 0026 } 0027 0028 /** 0029 * Function which will be called whenever data has been read from the socket. 0030 * This data should be dealt with, otherwise it will be discarded. 0031 * @param buf The buffer 0032 * @param size The size of the buffer 0033 */ 0034 virtual void onDataReady(bt::Uint8 *buf, bt::Uint32 size) = 0; 0035 }; 0036 0037 /** 0038 * Socket which supports traffic shaping 0039 */ 0040 class TrafficShapedSocket 0041 { 0042 public: 0043 TrafficShapedSocket(SocketDevice *sock); 0044 TrafficShapedSocket(int fd, int ip_version); 0045 TrafficShapedSocket(bool tcp, int ip_version); 0046 virtual ~TrafficShapedSocket(); 0047 0048 /// Get the SocketDevice 0049 SocketDevice *socketDevice() 0050 { 0051 return sock; 0052 } 0053 0054 /// Get the SocketDevice (const vesion) 0055 const SocketDevice *socketDevice() const 0056 { 0057 return sock; 0058 } 0059 0060 /// Set the reader 0061 void setReader(SocketReader *r) 0062 { 0063 rdr = r; 0064 } 0065 0066 /** 0067 * Reads data from the socket and pass it to the SocketReader. 0068 * @param max_bytes_to_read Maximum number of bytes to read (0 is no limit) 0069 * @param now Current time stamp 0070 * @return The number of bytes read 0071 */ 0072 virtual Uint32 read(Uint32 max_bytes_to_read, bt::TimeStamp now); 0073 0074 /** 0075 * Writes data to the socket. Subclasses should implement the data source. 0076 * @param max The maximum number of bytes to send over the socket (0 = no limit) 0077 * @param now Current time stamp 0078 * @return The number of bytes written 0079 */ 0080 virtual Uint32 write(Uint32 max, bt::TimeStamp now) = 0; 0081 0082 /// See if the socket has something ready to write 0083 virtual bool bytesReadyToWrite() const = 0; 0084 0085 /// Get the current download rate 0086 int getDownloadRate() const; 0087 0088 /// Get the current download rate 0089 int getUploadRate() const; 0090 0091 /// Update up and down speed 0092 void updateSpeeds(bt::TimeStamp now); 0093 0094 /** 0095 * Set the group ID of the socket 0096 * @param gid THe ID (0 is default group) 0097 * @param upload Whether this is an upload group or a download group 0098 */ 0099 void setGroupID(Uint32 gid, bool upload); 0100 0101 /// Get the download group ID 0102 Uint32 downloadGroupID() const 0103 { 0104 return down_gid; 0105 } 0106 0107 /// Get the upload group ID 0108 Uint32 uploadGroupID() const 0109 { 0110 return up_gid; 0111 } 0112 0113 protected: 0114 /** 0115 * Post process received data. Default implementation does nothing. 0116 * @param data The data 0117 * @param size The size of the data 0118 **/ 0119 virtual void postProcess(bt::Uint8 *data, bt::Uint32 size); 0120 0121 protected: 0122 SocketReader *rdr; 0123 Speed *down_speed; 0124 Speed *up_speed; 0125 Uint32 up_gid; 0126 Uint32 down_gid; // group id which this torrent belongs to, group 0 means the default group 0127 SocketDevice *sock; 0128 mutable QRecursiveMutex mutex; 0129 }; 0130 0131 } 0132 0133 #endif // NET_TRAFFICSHAPEDSOCKET_H