ccRTP
channel.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 
00037 #ifndef CCRTP_CHANNEL_H_
00038 #define CCRTP_CHANNEL_H_
00039 
00040 #include <ccrtp/base.h>
00041 #include <commoncpp/socket.h>
00042 
00043 #ifndef _MSWINDOWS_
00044 #include <sys/ioctl.h>
00045 inline size_t ccioctl(int so, int request, size_t& len)
00046     { return ioctl(so,request,&len); }
00047 #else
00048 inline size_t ccioctl(SOCKET so, int request, size_t& len )
00049 {
00050     unsigned long l;
00051     size_t result = 0;
00052     ::ioctlsocket(so,request,&l);
00053     len = l;
00054     return result;
00055 }
00056 #endif
00057 
00058 NAMESPACE_COMMONCPP
00059 
00094 class RTPBaseUDPIPv4Socket : private UDPSocket
00095 {
00096 public:
00100     RTPBaseUDPIPv4Socket(const InetAddress& ia, tpport_t port) :
00101         UDPSocket(ia,port)
00102     { }
00103 
00104     inline ~RTPBaseUDPIPv4Socket()
00105     { endSocket(); }
00106 
00107     inline bool
00108     isPendingRecv(microtimeout_t timeout)
00109     { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
00110 
00111     inline InetHostAddress
00112     getSender(tpport_t& port) const
00113     { return UDPSocket::getSender(&port); }
00114 
00115     inline size_t
00116     recv(unsigned char* buffer, size_t len)
00117     { return UDPSocket::receive(buffer, len); }
00118 
00122     inline size_t
00123     getNextPacketSize() const
00124     { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; }
00125 
00126     Socket::Error
00127     setMulticast(bool enable)
00128     { return UDPSocket::setMulticast(enable); }
00129 
00130     inline Socket::Error
00131     join(const InetMcastAddress& ia, uint32 iface)
00132     { return UDPSocket::join(ia,iface); }
00133 
00134     inline Socket::Error
00135     drop(const InetMcastAddress& ia)
00136     { return UDPSocket::drop(ia); }
00137 
00138         inline Socket::Error
00139     setTimeToLive(unsigned char ttl)
00140     { return UDPSocket::setTimeToLive(ttl); }
00141 
00145     RTPBaseUDPIPv4Socket() :
00146         UDPSocket()
00147     { }
00148 
00149     inline void
00150     setPeer(const InetAddress &ia, tpport_t port)
00151         {UDPSocket::setPeer((InetHostAddress&)ia, port);}
00152 
00153     inline size_t
00154     send(const unsigned char* const buffer, size_t len)
00155     { return UDPSocket::send(buffer, len); }
00156 
00157     inline SOCKET getRecvSocket() const
00158     { return UDPSocket::so; }
00159 
00160     // common
00161     inline void
00162     endSocket()
00163     { UDPSocket::endSocket(); }
00164 };
00165 
00186 template<class BaseSocket>
00187 class DualRTPChannel
00188 {
00189 public:
00190     DualRTPChannel(const InetAddress& ia, tpport_t port)
00191     {
00192         recvSocket = new BaseSocket(ia,port);
00193         sendSocket = new BaseSocket;
00194     }
00195 
00196     inline ~DualRTPChannel()
00197     { delete sendSocket; delete recvSocket; }
00198 
00199     inline bool
00200     isPendingRecv(microtimeout_t timeout) const
00201     { return recvSocket->isPendingRecv(timeout); }
00202 
00203     inline InetHostAddress
00204     getSender(tpport_t& port) const
00205     { return recvSocket->getSender(port); }
00206 
00207     inline size_t
00208     recv(unsigned char* buffer, size_t len)
00209     { return recvSocket->recv(buffer, len); }
00210 
00211     inline size_t
00212     getNextPacketSize() const
00213     { return recvSocket->getNextPacketSize(); }
00214 
00215     inline Socket::Error
00216     setMulticast(bool enable)
00217     { Socket::Error error = recvSocket->setMulticast(enable);
00218       if (error) return error;
00219       return sendSocket->setMulticast(enable); }
00220 
00221     inline Socket::Error
00222     join(const InetMcastAddress& ia, uint32 iface)
00223     { return recvSocket->join(ia,iface); }
00224 
00225     inline Socket::Error
00226     drop(const InetMcastAddress& ia)
00227     { return recvSocket->drop(ia); }
00228 
00229         inline Socket::Error
00230     setTimeToLive(unsigned char ttl)
00231     { return sendSocket->setTimeToLive(ttl); }
00232 
00233     inline void
00234     setPeer(const InetAddress& host, tpport_t port)
00235     { sendSocket->setPeer(host,port); }
00236 
00237     inline size_t
00238     send(const unsigned char* const buffer, size_t len)
00239     { return sendSocket->send(buffer, len); }
00240 
00241     inline SOCKET getRecvSocket() const
00242     { return recvSocket->getRecvSocket(); }
00243 
00244     // common.
00245     inline void
00246     endSocket()
00247     { sendSocket->endSocket(); recvSocket->endSocket(); }
00248 
00249 private:
00250     BaseSocket* sendSocket;
00251     BaseSocket* recvSocket;
00252 };
00253 
00254 #ifdef  CCXX_IPV6
00255 
00277 class RTPBaseUDPIPv6Socket : private UDPSocket
00278 {
00279 public:
00283     RTPBaseUDPIPv6Socket(const IPV6Address& ia, tpport_t port) :
00284         UDPSocket(ia,port)
00285     { }
00286 
00287     inline ~RTPBaseUDPIPv6Socket()
00288     { endSocket(); }
00289 
00290     inline bool
00291     isPendingRecv(microtimeout_t timeout)
00292     { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
00293 
00294     inline IPV6Host
00295     getSender(tpport_t& port) const
00296     { return UDPSocket::getIPV6Sender(&port); }
00297 
00298     inline size_t
00299     recv(unsigned char* buffer, size_t len)
00300     { return UDPSocket::receive(buffer, len); }
00301 
00305     inline size_t
00306     getNextPacketSize() const
00307     { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; }
00308 
00309     Socket::Error
00310     setMulticast(bool enable)
00311     { return UDPSocket::setMulticast(enable); }
00312 
00313     inline Socket::Error
00314     join(const IPV6Multicast& ia, uint32 iface)
00315     { return Socket::join(ia); }
00316 
00317     inline Socket::Error
00318     drop(const IPV6Multicast& ia)
00319     { return UDPSocket::drop(ia); }
00320 
00321         inline Socket::Error
00322     setTimeToLive(unsigned char ttl)
00323     { return UDPSocket::setTimeToLive(ttl); }
00324 
00328     RTPBaseUDPIPv6Socket() :
00329         UDPSocket()
00330     { }
00331 
00332     inline void
00333     setPeer(const IPV6Host &ia, tpport_t port)
00334         {UDPSocket::setPeer(ia, port);}
00335 
00336     inline size_t
00337     send(const unsigned char* const buffer, size_t len)
00338     { return UDPSocket::send(buffer, len); }
00339 
00340     inline SOCKET getRecvSocket() const
00341     { return UDPSocket::so; }
00342 
00343     // common
00344     inline void
00345     endSocket()
00346     { UDPSocket::endSocket(); }
00347 };
00348 
00369 template<class BaseSocket>
00370 class DualRTPChannelIPV6
00371 {
00372 public:
00373     DualRTPChannelIPV6(const IPV6Host& ia, tpport_t port)
00374     {
00375         recvSocket = new BaseSocket(ia,port);
00376         sendSocket = new BaseSocket;
00377     }
00378 
00379     inline ~DualRTPChannelIPV6()
00380     { delete sendSocket; delete recvSocket; }
00381 
00382     inline bool
00383     isPendingRecv(microtimeout_t timeout) const
00384     { return recvSocket->isPendingRecv(timeout); }
00385 
00386     inline IPV6Host
00387     getSender(tpport_t& port) const
00388     { return recvSocket->getIPV6Sender(port); }
00389 
00390     inline size_t
00391     recv(unsigned char* buffer, size_t len)
00392     { return recvSocket->recv(buffer, len); }
00393 
00394     inline size_t
00395     getNextPacketSize() const
00396     { return recvSocket->getNextPacketSize(); }
00397 
00398     inline Socket::Error
00399     setMulticast(bool enable)
00400     { Socket::Error error = recvSocket->setMulticast(enable);
00401       if (error) return error;
00402       return sendSocket->setMulticast(enable); }
00403 
00404     inline Socket::Error
00405     join(const IPV6Multicast& ia, uint32 iface)
00406     { return recvSocket->join(ia,iface); }
00407 
00408     inline Socket::Error
00409     drop(const IPV6Multicast& ia)
00410     { return recvSocket->drop(ia); }
00411 
00412         inline Socket::Error
00413     setTimeToLive(unsigned char ttl)
00414     { return sendSocket->setTimeToLive(ttl); }
00415 
00416     inline void
00417     setPeer(const IPV6Host& host, tpport_t port)
00418     { sendSocket->setPeer(host,port); }
00419 
00420     inline size_t
00421     send(const unsigned char* const buffer, size_t len)
00422     { return sendSocket->send(buffer, len); }
00423 
00424     inline SOCKET getRecvSocket() const
00425     { return recvSocket->getRecvSocket(); }
00426 
00427     // common.
00428     inline void
00429     endSocket()
00430     { sendSocket->endSocket(); recvSocket->endSocket(); }
00431 
00432 private:
00433     BaseSocket* sendSocket;
00434     BaseSocket* recvSocket;
00435 };
00436 
00437 
00438 typedef DualRTPChannelIPV6<RTPBaseUDPIPv6Socket> DualRTPUDPIPv6Channel;
00439 typedef RTPBaseUDPIPv6Socket SingleRTPChannelIPV6;
00440 typedef SingleRTPChannelIPV6 SymmetricRTPChannelIPV6;
00441 
00442 #endif
00443 
00444 typedef DualRTPChannel<RTPBaseUDPIPv4Socket> DualRTPUDPIPv4Channel;
00445 
00450 typedef RTPBaseUDPIPv4Socket SingleRTPChannel;
00451 
00455 typedef SingleRTPChannel SymmetricRTPChannel;
00456  // sockets
00458 
00459 END_NAMESPACE
00460 
00461 #endif  //CCRTP_CHANNEL_H_
00462