ccRTP
|
00001 // Copyright (C) 2001-2005 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 General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00016 // 00017 // As a special exception, you may use this file as part of a free software 00018 // library without restriction. Specifically, if other files instantiate 00019 // templates or use macros or inline functions from this file, or you compile 00020 // this file and link it with other files to produce an executable, this 00021 // file does not by itself cause the resulting executable to be covered by 00022 // the GNU General Public License. This exception does not however 00023 // invalidate any other reasons why the executable file might be covered by 00024 // the GNU General Public License. 00025 // 00026 // This exception applies only to the code released under the name GNU 00027 // ccRTP. If you copy code from other releases into a copy of GNU 00028 // ccRTP, as the General Public License permits, the exception does 00029 // not apply to the code that you add in this way. To avoid misleading 00030 // anyone as to the status of such modified files, you must delete 00031 // this exception notice from them. 00032 // 00033 // If you write modifications of your own for GNU ccRTP, it is your choice 00034 // whether to permit this exception to apply to your modifications. 00035 // If you do not wish that, delete this exception notice. 00036 // 00037 00038 #ifndef CCRTP_CHANNEL_H_ 00039 #define CCRTP_CHANNEL_H_ 00040 00041 #include <ccrtp/base.h> 00042 00043 #ifndef WIN32 00044 #include <sys/ioctl.h> 00045 inline size_t ccioctl(SOCKET 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 #ifdef CCXX_NAMESPACES 00059 namespace ost { 00060 #endif 00061 00096 class RTPBaseUDPIPv4Socket : private UDPSocket 00097 { 00098 public: 00102 RTPBaseUDPIPv4Socket(const InetAddress& ia, tpport_t port) : 00103 UDPSocket(ia,port) 00104 { } 00105 00106 inline ~RTPBaseUDPIPv4Socket() 00107 { endSocket(); } 00108 00109 inline bool 00110 isPendingRecv(microtimeout_t timeout) 00111 { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); } 00112 00113 inline InetHostAddress 00114 getSender(tpport_t& port) const 00115 { return UDPSocket::getSender(&port); } 00116 00117 inline size_t 00118 recv(unsigned char* buffer, size_t len) 00119 { return UDPSocket::receive(buffer, len); } 00120 00124 inline size_t 00125 getNextPacketSize() const 00126 { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; } 00127 00128 Socket::Error 00129 setMulticast(bool enable) 00130 { return UDPSocket::setMulticast(enable); } 00131 00132 inline Socket::Error 00133 join(const InetMcastAddress& ia, uint32 iface) 00134 { return UDPSocket::join(ia,iface); } 00135 00136 inline Socket::Error 00137 drop(const InetMcastAddress& ia) 00138 { return UDPSocket::drop(ia); } 00139 00140 inline Socket::Error 00141 setTimeToLive(unsigned char ttl) 00142 { return UDPSocket::setTimeToLive(ttl); } 00143 00147 RTPBaseUDPIPv4Socket() : 00148 UDPSocket() 00149 { } 00150 00151 inline void 00152 setPeer(const InetAddress &ia, tpport_t port) 00153 {UDPSocket::setPeer((InetHostAddress&)ia, port);} 00154 00155 inline size_t 00156 send(const unsigned char* const buffer, size_t len) 00157 { return UDPSocket::send(buffer, len); } 00158 00159 inline SOCKET getRecvSocket() const 00160 { return UDPSocket::so; } 00161 00162 // common 00163 inline void 00164 endSocket() 00165 { UDPSocket::endSocket(); } 00166 }; 00167 00188 template<class BaseSocket> 00189 class DualRTPChannel 00190 { 00191 public: 00192 DualRTPChannel(const InetAddress& ia, tpport_t port) 00193 { 00194 recvSocket = new BaseSocket(ia,port); 00195 sendSocket = new BaseSocket; 00196 } 00197 00198 inline ~DualRTPChannel() 00199 { delete sendSocket; delete recvSocket; } 00200 00201 inline bool 00202 isPendingRecv(microtimeout_t timeout) const 00203 { return recvSocket->isPendingRecv(timeout); } 00204 00205 inline InetHostAddress 00206 getSender(tpport_t& port) const 00207 { return recvSocket->getSender(port); } 00208 00209 inline size_t 00210 recv(unsigned char* buffer, size_t len) 00211 { return recvSocket->recv(buffer, len); } 00212 00213 inline size_t 00214 getNextPacketSize() const 00215 { return recvSocket->getNextPacketSize(); } 00216 00217 inline Socket::Error 00218 setMulticast(bool enable) 00219 { Socket::Error error = recvSocket->setMulticast(enable); 00220 if (error) return error; 00221 return sendSocket->setMulticast(enable); } 00222 00223 inline Socket::Error 00224 join(const InetMcastAddress& ia, uint32 iface) 00225 { return recvSocket->join(ia,iface); } 00226 00227 inline Socket::Error 00228 drop(const InetMcastAddress& ia) 00229 { return recvSocket->drop(ia); } 00230 00231 inline Socket::Error 00232 setTimeToLive(unsigned char ttl) 00233 { return sendSocket->setTimeToLive(ttl); } 00234 00235 inline void 00236 setPeer(const InetAddress& host, tpport_t port) 00237 { sendSocket->setPeer(host,port); } 00238 00239 inline size_t 00240 send(const unsigned char* const buffer, size_t len) 00241 { return sendSocket->send(buffer, len); } 00242 00243 inline SOCKET getRecvSocket() const 00244 { return recvSocket->getRecvSocket(); } 00245 00246 // common. 00247 inline void 00248 endSocket() 00249 { sendSocket->endSocket(); recvSocket->endSocket(); } 00250 00251 private: 00252 BaseSocket* sendSocket; 00253 BaseSocket* recvSocket; 00254 }; 00255 00256 #ifdef CCXX_IPV6 00257 00279 class RTPBaseUDPIPv6Socket : private UDPSocket 00280 { 00281 public: 00285 RTPBaseUDPIPv6Socket(const IPV6Address& ia, tpport_t port) : 00286 UDPSocket(ia,port) 00287 { } 00288 00289 inline ~RTPBaseUDPIPv6Socket() 00290 { endSocket(); } 00291 00292 inline bool 00293 isPendingRecv(microtimeout_t timeout) 00294 { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); } 00295 00296 inline IPV6Host 00297 getSender(tpport_t& port) const 00298 { return UDPSocket::getIPV6Sender(&port); } 00299 00300 inline size_t 00301 recv(unsigned char* buffer, size_t len) 00302 { return UDPSocket::receive(buffer, len); } 00303 00307 inline size_t 00308 getNextPacketSize() const 00309 { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; } 00310 00311 Socket::Error 00312 setMulticast(bool enable) 00313 { return UDPSocket::setMulticast(enable); } 00314 00315 inline Socket::Error 00316 join(const IPV6Multicast& ia, uint32 iface) 00317 { return Socket::join(ia); } 00318 00319 inline Socket::Error 00320 drop(const IPV6Multicast& ia) 00321 { return UDPSocket::drop(ia); } 00322 00323 inline Socket::Error 00324 setTimeToLive(unsigned char ttl) 00325 { return UDPSocket::setTimeToLive(ttl); } 00326 00330 RTPBaseUDPIPv6Socket() : 00331 UDPSocket() 00332 { } 00333 00334 inline void 00335 setPeer(const IPV6Host &ia, tpport_t port) 00336 {UDPSocket::setPeer(ia, port);} 00337 00338 inline size_t 00339 send(const unsigned char* const buffer, size_t len) 00340 { return UDPSocket::send(buffer, len); } 00341 00342 inline SOCKET getRecvSocket() const 00343 { return UDPSocket::so; } 00344 00345 // common 00346 inline void 00347 endSocket() 00348 { UDPSocket::endSocket(); } 00349 }; 00350 00371 template<class BaseSocket> 00372 class DualRTPChannelIPV6 00373 { 00374 public: 00375 DualRTPChannelIPV6(const IPV6Host& ia, tpport_t port) 00376 { 00377 recvSocket = new BaseSocket(ia,port); 00378 sendSocket = new BaseSocket; 00379 } 00380 00381 inline ~DualRTPChannelIPV6() 00382 { delete sendSocket; delete recvSocket; } 00383 00384 inline bool 00385 isPendingRecv(microtimeout_t timeout) const 00386 { return recvSocket->isPendingRecv(timeout); } 00387 00388 inline IPV6Host 00389 getSender(tpport_t& port) const 00390 { return recvSocket->getIPV6Sender(port); } 00391 00392 inline size_t 00393 recv(unsigned char* buffer, size_t len) 00394 { return recvSocket->recv(buffer, len); } 00395 00396 inline size_t 00397 getNextPacketSize() const 00398 { return recvSocket->getNextPacketSize(); } 00399 00400 inline Socket::Error 00401 setMulticast(bool enable) 00402 { Socket::Error error = recvSocket->setMulticast(enable); 00403 if (error) return error; 00404 return sendSocket->setMulticast(enable); } 00405 00406 inline Socket::Error 00407 join(const IPV6Multicast& ia, uint32 iface) 00408 { return recvSocket->join(ia,iface); } 00409 00410 inline Socket::Error 00411 drop(const IPV6Multicast& ia) 00412 { return recvSocket->drop(ia); } 00413 00414 inline Socket::Error 00415 setTimeToLive(unsigned char ttl) 00416 { return sendSocket->setTimeToLive(ttl); } 00417 00418 inline void 00419 setPeer(const IPV6Host& host, tpport_t port) 00420 { sendSocket->setPeer(host,port); } 00421 00422 inline size_t 00423 send(const unsigned char* const buffer, size_t len) 00424 { return sendSocket->send(buffer, len); } 00425 00426 inline SOCKET getRecvSocket() const 00427 { return recvSocket->getRecvSocket(); } 00428 00429 // common. 00430 inline void 00431 endSocket() 00432 { sendSocket->endSocket(); recvSocket->endSocket(); } 00433 00434 private: 00435 BaseSocket* sendSocket; 00436 BaseSocket* recvSocket; 00437 }; 00438 00439 00440 typedef DualRTPChannelIPV6<RTPBaseUDPIPv6Socket> DualRTPUDPIPv6Channel; 00441 typedef RTPBaseUDPIPv6Socket SingleRTPChannelIPV6; 00442 typedef SingleRTPChannelIPV6 SymmetricRTPChannelIPV6; 00443 00444 #endif 00445 00446 typedef DualRTPChannel<RTPBaseUDPIPv4Socket> DualRTPUDPIPv4Channel; 00447 00452 typedef RTPBaseUDPIPv4Socket SingleRTPChannel; 00453 00457 typedef SingleRTPChannel SymmetricRTPChannel; 00458 // sockets 00460 00461 #ifdef CCXX_NAMESPACES 00462 } 00463 #endif 00464 00465 #endif //CCRTP_CHANNEL_H_ 00466