ccRTP
pool.h
Go to the documentation of this file.
00001 // Copyright (C) 2001-2015 Federico Montesino Pouzols <fedemp@altern.org>
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public License
00014 // along with GNU ccRTP.  If not, see <http://www.gnu.org/licenses/>.
00015 //
00016 // As a special exception, you may use this file as part of a free software
00017 // library without restriction.  Specifically, if other files instantiate
00018 // templates or use macros or inline functions from this file, or you compile
00019 // this file and link it with other files to produce an executable, this
00020 // file does not by itself cause the resulting executable to be covered by
00021 // the GNU General Public License.  This exception does not however
00022 // invalidate any other reasons why the executable file might be covered by
00023 // the GNU General Public License.
00024 //
00025 // This exception applies only to the code released under the name GNU
00026 // ccRTP.  If you copy code from other releases into a copy of GNU
00027 // ccRTP, as the General Public License permits, the exception does
00028 // not apply to the code that you add in this way.  To avoid misleading
00029 // anyone as to the status of such modified files, you must delete
00030 // this exception notice from them.
00031 //
00032 // If you write modifications of your own for GNU ccRTP, it is your choice
00033 // whether to permit this exception to apply to your modifications.
00034 // If you do not wish that, delete this exception notice.
00035 //
00036 
00042 #ifndef CCXX_RTP_POOL_H
00043 #define CCXX_RTP_POOL_H
00044 
00045 #include <list>
00046 #include <ccrtp/rtp.h>
00047 
00048 NAMESPACE_COMMONCPP
00049 using std::list;
00050 
00051 typedef TRTPSessionBase<> RTPSessionBase;
00052 
00053 class RTPSessionBaseHandler
00054 {
00055 public:
00056     inline microtimeout_t getSchedulingTimeout(RTPSessionBase& s)
00057     { return s.getSchedulingTimeout(); }
00058 
00059     inline timeval getRTCPCheckInterval(RTPSessionBase& s)
00060     { return s.getRTCPCheckInterval(); }
00061 
00062     size_t
00063     takeInDataPacket(RTPSessionBase& s)
00064     { return s.takeInDataPacket(); }
00065 
00066     size_t
00067     dispatchDataPacket(RTPSessionBase& s)
00068     { return s.dispatchDataPacket(); }
00069 
00070     void
00071     controlReceptionService(RTPSessionBase& s)
00072     { s.controlReceptionService(); }
00073 
00074     void
00075     controlTransmissionService(RTPSessionBase& s)
00076     { s.controlTransmissionService(); }
00077 
00078     inline SOCKET getDataRecvSocket(RTPSessionBase& s) const
00079     { return s.getDataRecvSocket(); }
00080 
00081     inline SOCKET getControlRecvSocket(RTPSessionBase& s) const
00082     { return s.getControlRecvSocket(); }
00083 };
00084 
00092 class SessionListElement {
00093 private:
00094     RTPSessionBase* elem;
00095     bool cleared;
00096 
00097 public:
00098     SessionListElement(RTPSessionBase* e);
00099     void clear();
00100     bool isCleared();
00101     RTPSessionBase* get();
00102 };
00103 
00104 
00105 inline SessionListElement::SessionListElement(RTPSessionBase* e)
00106     : elem(e), cleared(false) {
00107 }
00108 
00109 inline void SessionListElement::clear() {
00110     cleared = true;
00111     delete elem;
00112     elem = 0;
00113 }
00114 
00115 inline bool SessionListElement::isCleared() {
00116     return cleared;
00117 }
00118 
00119 inline RTPSessionBase* SessionListElement::get() {
00120     return elem;
00121 }
00122 
00128 class PredEquals
00129 {
00130 protected:
00131     RTPSessionBase* elem;
00132 public:
00133     PredEquals(RTPSessionBase* e) : elem(e) {}
00134 
00135     bool operator() (SessionListElement* e)
00136     {
00137         return e->get() == elem;
00138     }
00139 };
00140 
00154 class __EXPORT RTPSessionPool: public RTPSessionBaseHandler
00155 {
00156 public:
00157     RTPSessionPool();
00158 
00159     inline virtual ~RTPSessionPool()
00160     { }
00161 
00162     bool
00163     addSession(RTPSessionBase& session);
00164 
00165     bool
00166     removeSession(RTPSessionBase& session);
00167 
00168     size_t
00169     getPoolLength() const;
00170 
00171     virtual void startRunning() = 0;
00172 
00173     inline bool isActive()
00174     { return poolActive; }
00175 
00176 protected:
00177     inline void setActive()
00178     { poolActive = true; }
00179 
00180     inline timeval getPoolTimeout()
00181     { return poolTimeout; }
00182 
00183     inline void setPoolTimeout(int sec, int usec)
00184     { poolTimeout.tv_sec = sec; poolTimeout.tv_usec = usec; }
00185 
00186     inline void setPoolTimeout(struct timeval to)
00187     { poolTimeout = to; }
00188 
00189     std::list<SessionListElement*> sessionList;
00190     typedef std::list<SessionListElement*>::iterator PoolIterator;
00191 
00192     mutable ThreadLock poolLock;
00193 
00194 #ifndef _MSWINDOWS_
00195     fd_set recvSocketSet;
00196     SOCKET highestSocket;  // highest socket number + 1
00197 #endif
00198 
00199 private:
00200     timeval poolTimeout;
00201     mutable bool poolActive;
00202 };
00203 
00204 
00205 class __EXPORT SingleRTPSessionPool :
00206         public RTPSessionPool,
00207         public Thread
00208 {
00209 public:
00213     SingleRTPSessionPool(int pri = 0) :
00214         RTPSessionPool(),
00215         Thread(pri)
00216     { }
00217 
00218     ~SingleRTPSessionPool()
00219     { }
00220 
00221     void startRunning()
00222     { setActive(); Thread::start(); }
00223 
00224 protected:
00229     void run();
00230 };
00231 
00232 END_NAMESPACE
00233 
00234 #endif //CCXX_RTP_POOL_H
00235