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 NETSOCKETGROUP_H
0007 #define NETSOCKETGROUP_H
0008 
0009 #include <list>
0010 #include <util/constants.h>
0011 
0012 namespace net
0013 {
0014 using bt::Uint32;
0015 
0016 class TrafficShapedSocket;
0017 
0018 /**
0019     @author Joris Guisson <joris.guisson@gmail.com>
0020 */
0021 class SocketGroup
0022 {
0023     Uint32 limit;
0024     Uint32 assured_rate;
0025     std::list<TrafficShapedSocket *> sockets;
0026     bt::TimeStamp prev_run_time;
0027     Uint32 group_allowance;
0028     Uint32 group_assured;
0029 
0030 public:
0031     SocketGroup(Uint32 limit, Uint32 assured_rate);
0032     virtual ~SocketGroup();
0033 
0034     /// Clear the lists of sockets
0035     void clear()
0036     {
0037         sockets.clear();
0038     }
0039 
0040     /// Add a socket for processing
0041     void add(TrafficShapedSocket *s)
0042     {
0043         sockets.push_back(s);
0044     }
0045 
0046     /**
0047         Process all the sockets in the vector for download.
0048         @param global_allowance How much the group can do, this will be updated, 0 means no limit
0049         @param now Current time
0050         @return true if we can download more data, false otherwise
0051     */
0052     bool download(Uint32 &global_allowance, bt::TimeStamp now);
0053 
0054     /**
0055         Process all the sockets in the vector for upload
0056         @param global_allowance How much the group can do, this will be updated, 0 means no limit
0057         @param now Current time
0058         @return true if we can upload more data, false otherwise
0059      */
0060     bool upload(Uint32 &global_allowance, bt::TimeStamp now);
0061 
0062     /**
0063      * Set the group limit in bytes per sec
0064      * @param lim The limit
0065      */
0066     void setLimit(Uint32 lim)
0067     {
0068         limit = lim;
0069     }
0070 
0071     /**
0072      * Set the assured rate for the gorup in bytes per sec
0073      * @param as The assured rate
0074      */
0075     void setAssuredRate(Uint32 as)
0076     {
0077         assured_rate = as;
0078     }
0079 
0080     /// Get the number of sockets
0081     Uint32 numSockets() const
0082     {
0083         return sockets.size();
0084     }
0085 
0086     /**
0087      * Calculate the allowance for this group
0088      * @param now Current timestamp
0089      */
0090     void calcAllowance(bt::TimeStamp now);
0091 
0092     /**
0093      * Get the assured allowance .
0094      */
0095     Uint32 getAssuredAllowance() const
0096     {
0097         return group_assured;
0098     }
0099 
0100 private:
0101     void processUnlimited(bool up, bt::TimeStamp now);
0102     bool processLimited(bool up, bt::TimeStamp now, Uint32 &allowance);
0103     bool process(bool up, bt::TimeStamp now, Uint32 &global_allowance);
0104 };
0105 
0106 }
0107 
0108 #endif