File indexing completed on 2025-01-05 04:37:20

0001 /*
0002     SPDX-FileCopyrightText: 2005 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 #ifndef NETSOCKETMONITOR_H
0007 #define NETSOCKETMONITOR_H
0008 
0009 #include <ktorrent_export.h>
0010 #include <list>
0011 #include <util/constants.h>
0012 
0013 namespace net
0014 {
0015 using bt::Uint32;
0016 
0017 class TrafficShapedSocket;
0018 
0019 /**
0020  * @author Joris Guisson <joris.guisson@gmail.com>
0021  *
0022  * Monitors all sockets for upload and download traffic.
0023  * It uses two threads to do this.
0024  */
0025 class KTORRENT_EXPORT SocketMonitor
0026 {
0027     SocketMonitor();
0028 
0029 public:
0030     virtual ~SocketMonitor();
0031 
0032     /// Add a new socket, will start the threads if necessary
0033     void add(TrafficShapedSocket *sock);
0034 
0035     /// Remove a socket, will stop threads if no more sockets are left
0036     void remove(TrafficShapedSocket *sock);
0037 
0038     typedef std::list<TrafficShapedSocket *>::iterator Itr;
0039 
0040     /// Get the begin of the list of sockets
0041     Itr begin()
0042     {
0043         return sockets.begin();
0044     }
0045 
0046     /// Get the end of the list of sockets
0047     Itr end()
0048     {
0049         return sockets.end();
0050     }
0051 
0052     /// lock the monitor
0053     void lock();
0054 
0055     /// unlock the monitor
0056     void unlock();
0057 
0058     /// Tell upload thread a packet is ready
0059     void signalPacketReady();
0060 
0061     enum GroupType {
0062         UPLOAD_GROUP,
0063         DOWNLOAD_GROUP,
0064     };
0065 
0066     /**
0067      * Shutdown the socketmonitor and all the networking threads.
0068      */
0069     void shutdown();
0070 
0071     /**
0072      * Creata a new upload or download group
0073      * @param type Whether it is an upload or download group
0074      * @param limit Limit of group in bytes/s
0075      * @param assured_rate The assured rate in bytes/s
0076      * @return The group ID
0077      */
0078     Uint32 newGroup(GroupType type, Uint32 limit, Uint32 assured_rate);
0079 
0080     /**
0081      * Change the group limit
0082      * @param type The group type
0083      * @param gid The group id
0084      * @param limit The limit
0085      */
0086     void setGroupLimit(GroupType type, Uint32 gid, Uint32 limit);
0087 
0088     /**
0089      * Change the group assured rate
0090      * @param type The group type
0091      * @param gid The group id
0092      * @param limit The limit
0093      */
0094     void setGroupAssuredRate(GroupType type, Uint32 gid, Uint32 as);
0095 
0096     /**
0097      * Remove a group
0098      * @param type The group type
0099      * @param gid The group id
0100      */
0101     void removeGroup(GroupType type, Uint32 gid);
0102 
0103     static void setDownloadCap(Uint32 bytes_per_sec);
0104     static Uint32 getDownloadCap();
0105     static void setUploadCap(Uint32 bytes_per_sec);
0106     static Uint32 getUploadCap();
0107     static void setSleepTime(Uint32 sleep_time);
0108     static SocketMonitor &instance()
0109     {
0110         return self;
0111     }
0112 
0113 private:
0114     class Private;
0115     Private *d;
0116     std::list<TrafficShapedSocket *> sockets;
0117     static SocketMonitor self;
0118 };
0119 
0120 }
0121 
0122 #endif