RTP Audio System  2.0.0
trafficshaper.h
Go to the documentation of this file.
00001 // ##########################################################################
00002 // ####                                                                  ####
00003 // ####                      RTP Audio Server Project                    ####
00004 // ####                    ============================                  ####
00005 // ####                                                                  ####
00006 // #### Traffic Shaper                                                   ####
00007 // ####                                                                  ####
00008 // ####           Copyright (C) 1999-2012 by Thomas Dreibholz            ####
00009 // ####                                                                  ####
00010 // #### Contact:                                                         ####
00011 // ####    EMail: dreibh@iem.uni-due.de                                  ####
00012 // ####    WWW:   https://www.nntb.no/~dreibh/rtpaudio                ####
00013 // ####                                                                  ####
00014 // #### ---------------------------------------------------------------- ####
00015 // ####                                                                  ####
00016 // #### This program is free software: you can redistribute it and/or    ####
00017 // #### modify it under the terms of the GNU General Public License as   ####
00018 // #### published by the Free Software Foundation, either version 3 of   ####
00019 // #### the License, or (at your option) any later version.              ####
00020 // ####                                                                  ####
00021 // #### This program is distributed in the hope that it will be useful,  ####
00022 // #### but WITHOUT ANY WARRANTY; without even the implied warranty of   ####
00023 // #### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    ####
00024 // #### GNU General Public License for more details.                     ####
00025 // ####                                                                  ####
00026 // #### You should have received a copy of the GNU General Public        ####
00027 // #### License along with this program.  If not, see                    ####
00028 // #### <http://www.gnu.org/licenses/>.                                  ####
00029 // ####                                                                  ####
00030 // ##########################################################################
00031 // $Id: trafficshaper.h 1286 2011-12-18 13:43:16Z dreibh $
00032 
00033 
00034 #ifndef TRAFFICSHAPER_H
00035 #define TRAFFICSHAPER_H
00036 
00037 
00038 #include "tdsystem.h"
00039 #include "tdsocket.h"
00040 #include "internetaddress.h"
00041 #include "timedthread.h"
00042 #include "trafficclassvalues.h"
00043 
00044 
00045 #include <set>
00046 #include <deque>
00047 #include <vector>
00048 
00049 
00050 class TrafficShaper;
00051 
00052 
00060 class TrafficShaperSingleton : public TimedThread
00061 {
00062    // ====== Constructor/Destructor =========================================
00063    public:
00067    TrafficShaperSingleton();
00068 
00072    ~TrafficShaperSingleton();
00073 
00074 
00075    // ====== Add/remove traffic shapers =====================================
00081    void addTrafficShaper(TrafficShaper* ts);
00082 
00088    void removeTrafficShaper(TrafficShaper* ts);
00089 
00090 
00091    // ====== Private data ===================================================
00092    private:
00093    void timerEvent();
00094 
00095 
00096    std::vector<TrafficShaper*> ShaperSet;
00097    cardinal                    UserCount;
00098 };
00099 
00100 
00101 
00109 class TrafficShaper : public Synchronizable
00110 {
00111    // ====== Constructor/Destructor =========================================
00112    public:
00116    TrafficShaper();
00117 
00121    TrafficShaper(Socket* socket);
00122 
00126    ~TrafficShaper();
00127 
00128 
00129    // ====== Initialize =====================================================
00135    void init(Socket* socket);
00136 
00137 
00138    // ====== Set sender socket ==============================================
00144    inline void setSocket(Socket* socket);
00145 
00146 
00147    // ====== Settings =======================================================
00153    inline card64 getBandwidth() const;
00154 
00160    inline void setBandwidth(const card64 bandwidth);
00161 
00167    inline double getBufferDelay() const;
00168 
00174    inline void setBufferDelay(const double bufferDelay);
00175 
00176 
00177    // ====== Buffer manipulation ============================================
00181    void flush();
00182 
00190    bool refreshBuffer(const card8 trafficClass,
00191                       const bool doRemapping);
00192 
00198    inline cardinal getLastSeqNum();
00199 
00200 
00201    // ====== I/O functions ==================================================
00213     ssize_t sendTo(const void*         buffer,
00214                    const size_t        length,
00215                    const cardinal      seqNum,
00216                    const cardinal      flags,
00217                    const InternetFlow& receiver,
00218                    const card8         trafficClass = 0);
00219 
00233     ssize_t send(const void*    buffer,
00234                  const size_t   length,
00235                  const cardinal seqNum,
00236                  const cardinal flags        = 0,
00237                  const card8    trafficClass = 0);
00238 
00247    ssize_t write(const void*    buffer,
00248                  const size_t   length,
00249                  const cardinal seqNum);
00250 
00251 
00252    // ====== Private data ===================================================
00253    private:
00254    void sendAll();
00255    ssize_t addPacket(const void*    data,
00256                      const cardinal bytes,
00257                      const cardinal seqNum,
00258                      InternetFlow&  destination,
00259                      const cardinal flags,
00260                      const cardinal command);
00261 
00262 
00263    enum TrafficShaperCommand {
00264       TSC_Write  = 0,
00265       TSC_Send   = 1,
00266       TSC_SendTo = 2
00267    };
00268 
00269    struct TrafficShaperPacket {
00270       card64       SendTimeStamp;
00271       cardinal     HeaderSize;
00272       cardinal     PayloadSize;
00273       cardinal     Flags;
00274       cardinal     Command;
00275       InternetFlow Destination;
00276       char*        Data;
00277       cardinal     SeqNum;
00278 
00279       inline int operator<(const TrafficShaperPacket& packet) const {
00280          return(SendTimeStamp < packet.SendTimeStamp);
00281       }
00282    };
00283 
00284    friend class TrafficShaperSingleton;
00285 
00286 
00287    static TrafficShaperSingleton   Singleton;
00288    std::deque<TrafficShaperPacket> Queue;
00289    Socket*                         SenderSocket;
00290    card64                          SendTimeStamp;
00291    card64                          Bandwidth;
00292    double                          BufferDelay;
00293    integer                         LastError;
00294    cardinal                        LastSeqNum;
00295 };
00296 
00297 
00298 #include "trafficshaper.icc"
00299 
00300 
00301 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines