Bayonne2 / Common C++ 2 Framework
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
43 #ifndef WIN32
44 #include <sys/ioctl.h>
45 inline size_t ccioctl(SOCKET so, int request, size_t& len)
46 { return ::ioctl(so,request,&len); }
47 #else
48 inline size_t ccioctl(SOCKET so, int request, size_t& len )
49 {
50  unsigned long l;
51  size_t result = 0;
52  ::ioctlsocket(so,request,&l);
53  len = l;
54  return result;
55 }
56 #endif
57 
58 #ifdef CCXX_NAMESPACES
59 namespace ost {
60 #endif
61 
97 {
98 public:
103  UDPSocket(ia,port)
104  { }
105 
107  { endSocket(); }
108 
109  inline bool
111  { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
112 
113  inline InetHostAddress
114  getSender(tpport_t& port) const
115  { return UDPSocket::getSender(&port); }
116 
117  inline size_t
118  recv(unsigned char* buffer, size_t len)
119  { return UDPSocket::receive(buffer, len); }
120 
124  inline size_t
126  { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; }
127 
129  setMulticast(bool enable)
130  { return UDPSocket::setMulticast(enable); }
131 
132  inline Socket::Error
133  join(const InetMcastAddress& ia, uint32 iface)
134  { return UDPSocket::join(ia,iface); }
135 
136  inline Socket::Error
138  { return UDPSocket::drop(ia); }
139 
140  inline Socket::Error
141  setTimeToLive(unsigned char ttl)
142  { return UDPSocket::setTimeToLive(ttl); }
143 
148  UDPSocket()
149  { }
150 
151  inline void
152  setPeer(const InetAddress &ia, tpport_t port)
153  {UDPSocket::setPeer((InetHostAddress&)ia, port);}
154 
155  inline size_t
156  send(const unsigned char* const buffer, size_t len)
157  { return UDPSocket::send(buffer, len); }
158 
159  inline SOCKET getRecvSocket() const
160  { return UDPSocket::so; }
161 
162  // common
163  inline void
165  { UDPSocket::endSocket(); }
166 };
167 
188 template<class BaseSocket>
190 {
191 public:
193  {
194  recvSocket = new BaseSocket(ia,port);
195  sendSocket = new BaseSocket;
196  }
197 
199  { delete sendSocket; delete recvSocket; }
200 
201  inline bool
203  { return recvSocket->isPendingRecv(timeout); }
204 
205  inline InetHostAddress
206  getSender(tpport_t& port) const
207  { return recvSocket->getSender(port); }
208 
209  inline size_t
210  recv(unsigned char* buffer, size_t len)
211  { return recvSocket->recv(buffer, len); }
212 
213  inline size_t
215  { return recvSocket->getNextPacketSize(); }
216 
217  inline Socket::Error
218  setMulticast(bool enable)
219  { Socket::Error error = recvSocket->setMulticast(enable);
220  if (error) return error;
221  return sendSocket->setMulticast(enable); }
222 
223  inline Socket::Error
224  join(const InetMcastAddress& ia, uint32 iface)
225  { return recvSocket->join(ia,iface); }
226 
227  inline Socket::Error
229  { return recvSocket->drop(ia); }
230 
231  inline Socket::Error
232  setTimeToLive(unsigned char ttl)
233  { return sendSocket->setTimeToLive(ttl); }
234 
235  inline void
236  setPeer(const InetAddress& host, tpport_t port)
237  { sendSocket->setPeer(host,port); }
238 
239  inline size_t
240  send(const unsigned char* const buffer, size_t len)
241  { return sendSocket->send(buffer, len); }
242 
243  inline SOCKET getRecvSocket() const
244  { return recvSocket->getRecvSocket(); }
245 
246  // common.
247  inline void
249  { sendSocket->endSocket(); recvSocket->endSocket(); }
250 
251 private:
252  BaseSocket* sendSocket;
253  BaseSocket* recvSocket;
254 };
255 
256 #ifdef CCXX_IPV6
257 
279 class RTPBaseUDPIPv6Socket : private UDPSocket
280 {
281 public:
285  RTPBaseUDPIPv6Socket(const IPV6Address& ia, tpport_t port) :
286  UDPSocket(ia,port)
287  { }
288 
289  inline ~RTPBaseUDPIPv6Socket()
290  { endSocket(); }
291 
292  inline bool
293  isPendingRecv(microtimeout_t timeout)
294  { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
295 
296  inline IPV6Host
297  getSender(tpport_t& port) const
298  { return UDPSocket::getIPV6Sender(&port); }
299 
300  inline size_t
301  recv(unsigned char* buffer, size_t len)
302  { return UDPSocket::receive(buffer, len); }
303 
307  inline size_t
308  getNextPacketSize() const
309  { size_t len; ccioctl(UDPSocket::so,FIONREAD,len); return len; }
310 
312  setMulticast(bool enable)
313  { return UDPSocket::setMulticast(enable); }
314 
315  inline Socket::Error
316  join(const IPV6Multicast& ia, uint32 iface)
317  { return Socket::join(ia); }
318 
319  inline Socket::Error
320  drop(const IPV6Multicast& ia)
321  { return UDPSocket::drop(ia); }
322 
323  inline Socket::Error
324  setTimeToLive(unsigned char ttl)
325  { return UDPSocket::setTimeToLive(ttl); }
326 
330  RTPBaseUDPIPv6Socket() :
331  UDPSocket()
332  { }
333 
334  inline void
335  setPeer(const IPV6Host &ia, tpport_t port)
336  {UDPSocket::setPeer(ia, port);}
337 
338  inline size_t
339  send(const unsigned char* const buffer, size_t len)
340  { return UDPSocket::send(buffer, len); }
341 
342  inline SOCKET getRecvSocket() const
343  { return UDPSocket::so; }
344 
345  // common
346  inline void
347  endSocket()
348  { UDPSocket::endSocket(); }
349 };
350 
371 template<class BaseSocket>
372 class DualRTPChannelIPV6
373 {
374 public:
375  DualRTPChannelIPV6(const IPV6Host& ia, tpport_t port)
376  {
377  recvSocket = new BaseSocket(ia,port);
378  sendSocket = new BaseSocket;
379  }
380 
381  inline ~DualRTPChannelIPV6()
382  { delete sendSocket; delete recvSocket; }
383 
384  inline bool
385  isPendingRecv(microtimeout_t timeout) const
386  { return recvSocket->isPendingRecv(timeout); }
387 
388  inline IPV6Host
389  getSender(tpport_t& port) const
390  { return recvSocket->getIPV6Sender(port); }
391 
392  inline size_t
393  recv(unsigned char* buffer, size_t len)
394  { return recvSocket->recv(buffer, len); }
395 
396  inline size_t
397  getNextPacketSize() const
398  { return recvSocket->getNextPacketSize(); }
399 
400  inline Socket::Error
401  setMulticast(bool enable)
402  { Socket::Error error = recvSocket->setMulticast(enable);
403  if (error) return error;
404  return sendSocket->setMulticast(enable); }
405 
406  inline Socket::Error
407  join(const IPV6Multicast& ia, uint32 iface)
408  { return recvSocket->join(ia,iface); }
409 
410  inline Socket::Error
411  drop(const IPV6Multicast& ia)
412  { return recvSocket->drop(ia); }
413 
414  inline Socket::Error
415  setTimeToLive(unsigned char ttl)
416  { return sendSocket->setTimeToLive(ttl); }
417 
418  inline void
419  setPeer(const IPV6Host& host, tpport_t port)
420  { sendSocket->setPeer(host,port); }
421 
422  inline size_t
423  send(const unsigned char* const buffer, size_t len)
424  { return sendSocket->send(buffer, len); }
425 
426  inline SOCKET getRecvSocket() const
427  { return recvSocket->getRecvSocket(); }
428 
429  // common.
430  inline void
431  endSocket()
432  { sendSocket->endSocket(); recvSocket->endSocket(); }
433 
434 private:
435  BaseSocket* sendSocket;
436  BaseSocket* recvSocket;
437 };
438 
439 
440 typedef DualRTPChannelIPV6<RTPBaseUDPIPv6Socket> DualRTPUDPIPv6Channel;
441 typedef RTPBaseUDPIPv6Socket SingleRTPChannelIPV6;
442 typedef SingleRTPChannelIPV6 SymmetricRTPChannelIPV6;
443 
444 #endif
445 
447 
453 
458  // sockets
460 
461 #ifdef CCXX_NAMESPACES
462 }
463 #endif
464 
465 #endif //CCRTP_CHANNEL_H_
466 
virtual bool isPending(Pending pend, timeout_t timeout=TIMEOUT_INF)
Get the status of pending operations.
Socket::Error setTimeToLive(unsigned char ttl)
Definition: channel.h:232
RTPBaseUDPIPv4Socket()
Constructor for transmitter.
Definition: channel.h:147
Socket::Error join(const IPV4Multicast &ia, int InterfaceIndex)
join a multicast group on a particular interface
UDP sockets implement the TCP SOCK_DGRAM UDP protocol.
Definition: socket.h:884
Socket::Error drop(const InetMcastAddress &ia)
Definition: channel.h:228
Error join(const IPV4Multicast &ia)
Join a multicast group.
DualRTPChannel< RTPBaseUDPIPv4Socket > DualRTPUDPIPv4Channel
Definition: channel.h:446
__EXPORT AppLog & error(AppLog &sl)
Manipulator for error level.
Definition: applog.h:541
uint32 microtimeout_t
Time interval expressed in microseconds.
Definition: base.h:69
Error
Definition: socket.h:131
unsigned short tpport_t
Transport Protocol Ports.
Definition: address.h:86
Socket::Error drop(const InetMcastAddress &ia)
Definition: channel.h:137
SOCKET getRecvSocket() const
Definition: channel.h:159
SOCKET volatile so
the actual socket descriptor, in Windows, unlike posix it cannot be used as an file descriptor that w...
Definition: socket.h:221
RTPBaseUDPIPv4Socket(const InetAddress &ia, tpport_t port)
Constructor for receiver.
Definition: channel.h:102
SingleRTPChannel SymmetricRTPChannel
Actually, RTP with a single channel can be called 'Symmetric RTP'.
Definition: channel.h:457
bool isPendingRecv(microtimeout_t timeout) const
Definition: channel.h:202
size_t ccioctl(SOCKET so, int request, size_t &len)
Definition: channel.h:45
ssize_t send(const void *buf, size_t len)
Send a message packet to a peer host.
SOCKET getRecvSocket() const
Definition: channel.h:243
Error setMulticast(bool enable)
Set the multicast.
Definition: socket.h:943
Socket::Error join(const InetMcastAddress &ia, uint32 iface)
Definition: channel.h:133
Socket::Error setTimeToLive(unsigned char ttl)
Definition: channel.h:141
Error setTimeToLive(char ttl)
Set time to live.
Definition: socket.h:949
BaseSocket * sendSocket
Definition: channel.h:252
Error drop(const IPV4Multicast &ia)
Drop membership from a multicast group.
Socket::Error setMulticast(bool enable)
Definition: channel.h:218
#define InetHostAddress
Definition: address.h:76
A UDP/IPv4 socket class targetted at RTP stacks.
Definition: channel.h:96
void endSocket(void)
Used as the default destructor for ending a socket.
size_t getNextPacketSize() const
Definition: channel.h:214
bool isPendingRecv(microtimeout_t timeout)
Definition: channel.h:110
size_t getNextPacketSize() const
Get size of next datagram waiting to be read.
Definition: channel.h:125
InetHostAddress getSender(tpport_t &port) const
Definition: channel.h:114
RTPBaseUDPIPv4Socket SingleRTPChannel
May be used in applications where using the same socket for both sending and receiving is not a limit...
Definition: channel.h:452
Socket::Error setMulticast(bool enable)
Definition: channel.h:129
Socket::Error join(const InetMcastAddress &ia, uint32 iface)
Definition: channel.h:224
#define InetAddress
Definition: address.h:75
IPV4Host getSender(tpport_t *port=NULL) const
Definition: socket.h:505
size_t recv(unsigned char *buffer, size_t len)
Definition: channel.h:118
DualRTPChannel(const InetAddress &ia, tpport_t port)
Definition: channel.h:192
#define InetMcastAddress
Definition: address.h:78
ssize_t receive(void *buf, size_t len, bool reply=false)
Receive a message from any host.
Base elements for RTP stacks: constants, types and global functions.
size_t send(const unsigned char *const buffer, size_t len)
Definition: channel.h:240
int SOCKET
Definition: socket.h:60
void setPeer(const InetAddress &ia, tpport_t port)
Definition: channel.h:152
void endSocket()
Definition: channel.h:248
void setPeer(const IPV4Host &host, tpport_t port)
set the peer address to send message packets to.
void setPeer(const InetAddress &host, tpport_t port)
Definition: channel.h:236
size_t recv(unsigned char *buffer, size_t len)
Definition: channel.h:210
InetHostAddress getSender(tpport_t &port) const
Definition: channel.h:206
BaseSocket * recvSocket
Definition: channel.h:253
size_t send(const unsigned char *const buffer, size_t len)
Definition: channel.h:156