ccRTP
channel.h
Go to the documentation of this file.
1 // Copyright (C) 2001-2005 Federico Montesino Pouzols <fedemp@altern.org>
2 //
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // ccRTP. If you copy code from other releases into a copy of GNU
28 // ccRTP, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU ccRTP, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
38 #ifndef CCRTP_CHANNEL_H_
39 #define CCRTP_CHANNEL_H_
40 
41 #include <ccrtp/base.h>
42 #include <commoncpp/socket.h>
43 
44 #ifndef _MSWINDOWS_
45 #include <sys/ioctl.h>
46 inline size_t ccioctl(int so, int request, size_t& len)
47  { return ioctl(so,request,&len); }
48 #else
49 inline size_t ccioctl(SOCKET so, int request, size_t& len )
50 {
51  unsigned long l;
52  size_t result = 0;
53  ::ioctlsocket(so,request,&l);
54  len = l;
55  return result;
56 }
57 #endif
58 
59 NAMESPACE_COMMONCPP
60 
95 class RTPBaseUDPIPv4Socket : private UDPSocket
96 {
97 public:
101  RTPBaseUDPIPv4Socket(const InetAddress& ia, tpport_t port) :
102  UDPSocket(ia,port)
103  { }
104 
106  { endSocket(); }
107 
108  inline bool
110  { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
111 
112  inline InetHostAddress
113  getSender(tpport_t& port) const
114  { return UDPSocket::getSender(&port); }
115 
116  inline size_t
117  recv(unsigned char* buffer, size_t len)
118  { return UDPSocket::receive(buffer, len); }
119 
123  inline size_t
125  { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; }
126 
127  Socket::Error
128  setMulticast(bool enable)
129  { return UDPSocket::setMulticast(enable); }
130 
131  inline Socket::Error
132  join(const InetMcastAddress& ia, uint32 iface)
133  { return UDPSocket::join(ia,iface); }
134 
135  inline Socket::Error
136  drop(const InetMcastAddress& ia)
137  { return UDPSocket::drop(ia); }
138 
139  inline Socket::Error
140  setTimeToLive(unsigned char ttl)
141  { return UDPSocket::setTimeToLive(ttl); }
142 
147  UDPSocket()
148  { }
149 
150  inline void
151  setPeer(const InetAddress &ia, tpport_t port)
152  {UDPSocket::setPeer((InetHostAddress&)ia, port);}
153 
154  inline size_t
155  send(const unsigned char* const buffer, size_t len)
156  { return UDPSocket::send(buffer, len); }
157 
158  inline SOCKET getRecvSocket() const
159  { return UDPSocket::so; }
160 
161  // common
162  inline void
164  { UDPSocket::endSocket(); }
165 };
166 
187 template<class BaseSocket>
189 {
190 public:
191  DualRTPChannel(const InetAddress& ia, tpport_t port)
192  {
193  recvSocket = new BaseSocket(ia,port);
194  sendSocket = new BaseSocket;
195  }
196 
198  { delete sendSocket; delete recvSocket; }
199 
200  inline bool
202  { return recvSocket->isPendingRecv(timeout); }
203 
204  inline InetHostAddress
205  getSender(tpport_t& port) const
206  { return recvSocket->getSender(port); }
207 
208  inline size_t
209  recv(unsigned char* buffer, size_t len)
210  { return recvSocket->recv(buffer, len); }
211 
212  inline size_t
214  { return recvSocket->getNextPacketSize(); }
215 
216  inline Socket::Error
217  setMulticast(bool enable)
218  { Socket::Error error = recvSocket->setMulticast(enable);
219  if (error) return error;
220  return sendSocket->setMulticast(enable); }
221 
222  inline Socket::Error
223  join(const InetMcastAddress& ia, uint32 iface)
224  { return recvSocket->join(ia,iface); }
225 
226  inline Socket::Error
227  drop(const InetMcastAddress& ia)
228  { return recvSocket->drop(ia); }
229 
230  inline Socket::Error
231  setTimeToLive(unsigned char ttl)
232  { return sendSocket->setTimeToLive(ttl); }
233 
234  inline void
235  setPeer(const InetAddress& host, tpport_t port)
236  { sendSocket->setPeer(host,port); }
237 
238  inline size_t
239  send(const unsigned char* const buffer, size_t len)
240  { return sendSocket->send(buffer, len); }
241 
242  inline SOCKET getRecvSocket() const
243  { return recvSocket->getRecvSocket(); }
244 
245  // common.
246  inline void
248  { sendSocket->endSocket(); recvSocket->endSocket(); }
249 
250 private:
251  BaseSocket* sendSocket;
252  BaseSocket* recvSocket;
253 };
254 
255 #ifdef CCXX_IPV6
256 
278 class RTPBaseUDPIPv6Socket : private UDPSocket
279 {
280 public:
284  RTPBaseUDPIPv6Socket(const IPV6Address& ia, tpport_t port) :
285  UDPSocket(ia,port)
286  { }
287 
288  inline ~RTPBaseUDPIPv6Socket()
289  { endSocket(); }
290 
291  inline bool
293  { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
294 
295  inline IPV6Host
296  getSender(tpport_t& port) const
297  { return UDPSocket::getIPV6Sender(&port); }
298 
299  inline size_t
300  recv(unsigned char* buffer, size_t len)
301  { return UDPSocket::receive(buffer, len); }
302 
306  inline size_t
307  getNextPacketSize() const
308  { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; }
309 
310  Socket::Error
311  setMulticast(bool enable)
312  { return UDPSocket::setMulticast(enable); }
313 
314  inline Socket::Error
315  join(const IPV6Multicast& ia, uint32 iface)
316  { return Socket::join(ia); }
317 
318  inline Socket::Error
319  drop(const IPV6Multicast& ia)
320  { return UDPSocket::drop(ia); }
321 
322  inline Socket::Error
323  setTimeToLive(unsigned char ttl)
324  { return UDPSocket::setTimeToLive(ttl); }
325 
329  RTPBaseUDPIPv6Socket() :
330  UDPSocket()
331  { }
332 
333  inline void
334  setPeer(const IPV6Host &ia, tpport_t port)
335  {UDPSocket::setPeer(ia, port);}
336 
337  inline size_t
338  send(const unsigned char* const buffer, size_t len)
339  { return UDPSocket::send(buffer, len); }
340 
341  inline SOCKET getRecvSocket() const
342  { return UDPSocket::so; }
343 
344  // common
345  inline void
346  endSocket()
347  { UDPSocket::endSocket(); }
348 };
349 
370 template<class BaseSocket>
371 class DualRTPChannelIPV6
372 {
373 public:
374  DualRTPChannelIPV6(const IPV6Host& ia, tpport_t port)
375  {
376  recvSocket = new BaseSocket(ia,port);
377  sendSocket = new BaseSocket;
378  }
379 
380  inline ~DualRTPChannelIPV6()
381  { delete sendSocket; delete recvSocket; }
382 
383  inline bool
384  isPendingRecv(microtimeout_t timeout) const
385  { return recvSocket->isPendingRecv(timeout); }
386 
387  inline IPV6Host
388  getSender(tpport_t& port) const
389  { return recvSocket->getIPV6Sender(port); }
390 
391  inline size_t
392  recv(unsigned char* buffer, size_t len)
393  { return recvSocket->recv(buffer, len); }
394 
395  inline size_t
396  getNextPacketSize() const
397  { return recvSocket->getNextPacketSize(); }
398 
399  inline Socket::Error
400  setMulticast(bool enable)
401  { Socket::Error error = recvSocket->setMulticast(enable);
402  if (error) return error;
403  return sendSocket->setMulticast(enable); }
404 
405  inline Socket::Error
406  join(const IPV6Multicast& ia, uint32 iface)
407  { return recvSocket->join(ia,iface); }
408 
409  inline Socket::Error
410  drop(const IPV6Multicast& ia)
411  { return recvSocket->drop(ia); }
412 
413  inline Socket::Error
414  setTimeToLive(unsigned char ttl)
415  { return sendSocket->setTimeToLive(ttl); }
416 
417  inline void
418  setPeer(const IPV6Host& host, tpport_t port)
419  { sendSocket->setPeer(host,port); }
420 
421  inline size_t
422  send(const unsigned char* const buffer, size_t len)
423  { return sendSocket->send(buffer, len); }
424 
425  inline SOCKET getRecvSocket() const
426  { return recvSocket->getRecvSocket(); }
427 
428  // common.
429  inline void
430  endSocket()
431  { sendSocket->endSocket(); recvSocket->endSocket(); }
432 
433 private:
434  BaseSocket* sendSocket;
435  BaseSocket* recvSocket;
436 };
437 
438 
439 typedef DualRTPChannelIPV6<RTPBaseUDPIPv6Socket> DualRTPUDPIPv6Channel;
440 typedef RTPBaseUDPIPv6Socket SingleRTPChannelIPV6;
441 typedef SingleRTPChannelIPV6 SymmetricRTPChannelIPV6;
442 
443 #endif
444 
446 
452 
457  // sockets
459 
460 END_NAMESPACE
461 
462 #endif //CCRTP_CHANNEL_H_
463 
InetHostAddress getSender(tpport_t &port) const
Definition: channel.h:205
bool isPendingRecv(microtimeout_t timeout) const
Definition: channel.h:201
size_t recv(unsigned char *buffer, size_t len)
Definition: channel.h:209
DualRTPChannel< RTPBaseUDPIPv4Socket > DualRTPUDPIPv4Channel
Definition: channel.h:445
uint32 microtimeout_t
Time interval expressed in microseconds.
Definition: base.h:68
~RTPBaseUDPIPv4Socket()
Definition: channel.h:105
size_t send(const unsigned char *const buffer, size_t len)
Definition: channel.h:239
DualRTPChannel(const InetAddress &ia, tpport_t port)
Definition: channel.h:191
SOCKET getRecvSocket() const
Definition: channel.h:242
size_t getNextPacketSize() const
Definition: channel.h:213
void endSocket()
Definition: channel.h:163
RTPBaseUDPIPv4Socket(const InetAddress &ia, tpport_t port)
Constructor for receiver.
Definition: channel.h:101
Socket::Error setTimeToLive(unsigned char ttl)
Definition: channel.h:231
SingleRTPChannel SymmetricRTPChannel
Actually, RTP with a single channel can be called &#39;Symmetric RTP&#39;.
Definition: channel.h:456
size_t recv(unsigned char *buffer, size_t len)
Definition: channel.h:117
size_t send(const unsigned char *const buffer, size_t len)
Definition: channel.h:155
void setPeer(const InetAddress &ia, tpport_t port)
Definition: channel.h:151
Socket::Error setMulticast(bool enable)
Definition: channel.h:217
Socket::Error drop(const InetMcastAddress &ia)
Definition: channel.h:136
size_t getNextPacketSize() const
Get size of next datagram waiting to be read.
Definition: channel.h:124
RTPBaseUDPIPv4Socket()
Constructor for transmitter.
Definition: channel.h:146
Definition: channel.h:188
Socket::Error join(const InetMcastAddress &ia, uint32 iface)
Definition: channel.h:132
RTPBaseUDPIPv4Socket SingleRTPChannel
May be used in applications where using the same socket for both sending and receiving is not a limit...
Definition: channel.h:451
A UDP/IPv4 socket class targeted at RTP stacks.
Definition: channel.h:95
~DualRTPChannel()
Definition: channel.h:197
InetHostAddress getSender(tpport_t &port) const
Definition: channel.h:113
Socket::Error setMulticast(bool enable)
Definition: channel.h:128
Socket::Error join(const InetMcastAddress &ia, uint32 iface)
Definition: channel.h:223
void setPeer(const InetAddress &host, tpport_t port)
Definition: channel.h:235
Base elements for RTP stacks: constants, types and global functions.
void endSocket()
Definition: channel.h:247
bool isPendingRecv(microtimeout_t timeout)
Definition: channel.h:109
size_t ccioctl(int so, int request, size_t &len)
Definition: channel.h:46
SOCKET getRecvSocket() const
Definition: channel.h:158
Socket::Error setTimeToLive(unsigned char ttl)
Definition: channel.h:140
Socket::Error drop(const InetMcastAddress &ia)
Definition: channel.h:227