1 // Copyright (C) 2001-2015 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 Lesser General Public License
14 // along with GNU ccRTP. If not, see <http://www.gnu.org/licenses/>.
15 //
16 // As a special exception, you may use this file as part of a free software
17 // library without restriction. Specifically, if other files instantiate
18 // templates or use macros or inline functions from this file, or you compile
19 // this file and link it with other files to produce an executable, this
20 // file does not by itself cause the resulting executable to be covered by
21 // the GNU General Public License. This exception does not however
22 // invalidate any other reasons why the executable file might be covered by
23 // the GNU General Public License.
24 //
25 // This exception applies only to the code released under the name GNU
26 // ccRTP. If you copy code from other releases into a copy of GNU
27 // ccRTP, as the General Public License permits, the exception does
28 // not apply to the code that you add in this way. To avoid misleading
29 // anyone as to the status of such modified files, you must delete
30 // this exception notice from them.
31 //
32 // If you write modifications of your own for GNU ccRTP, it is your choice
33 // whether to permit this exception to apply to your modifications.
34 // If you do not wish that, delete this exception notice.
35 //
36
37 #ifndef CCRTP_CHANNEL_H_
38 #define CCRTP_CHANNEL_H_
39
41 #include <commoncpp/socket.h>
42
43 #ifndef _MSWINDOWS_
44 #include <sys/ioctl.h>
45 inline size_t ccioctl(
int 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 NAMESPACE_COMMONCPP
59
95 {
96 public:
102 { }
103
106
107 inline bool
109 { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
110
111 inline InetHostAddress
113 { return UDPSocket::getSender(&port); }
114
115 inline size_t
116 recv(
unsigned char* buffer,
size_t len)
117 { return UDPSocket::receive(buffer, len); }
118
122 inline size_t
124 {
size_t len;
ccioctl(UDPSocket::so,FIONREAD,len);
return len; }
125
126 Socket::Error
128 { return UDPSocket::setMulticast(enable); }
129
130 inline Socket::Error
131 join(
const InetMcastAddress& ia, uint32 iface)
132 { return UDPSocket::join(ia,iface); }
133
134 inline Socket::Error
135 drop(
const InetMcastAddress& ia)
137
138 inline Socket::Error
140 { return UDPSocket::setTimeToLive(ttl); }
141
147 { }
148
149 inline void
151 {UDPSocket::setPeer((InetHostAddress&)ia, port);}
152
153 inline size_t
154 send(
const unsigned char*
const buffer,
size_t len)
155 { return UDPSocket::send(buffer, len); }
156
158 { return UDPSocket::so; }
159
160 // common
161 inline void
163 { UDPSocket::endSocket(); }
164 };
165
186 template<class BaseSocket>
188 {
189 public:
191 {
194 }
195
198
199 inline bool
201 {
return recvSocket->isPendingRecv(timeout); }
202
203 inline InetHostAddress
206
207 inline size_t
208 recv(
unsigned char* buffer,
size_t len)
210
211 inline size_t
214
215 inline Socket::Error
217 { Socket::Error error =
recvSocket->setMulticast(enable);
218 if (error) return error;
220
221 inline Socket::Error
222 join(
const InetMcastAddress& ia, uint32 iface)
224
225 inline Socket::Error
226 drop(
const InetMcastAddress& ia)
228
229 inline Socket::Error
232
233 inline void
234 setPeer(
const InetAddress& host, tpport_t port)
236
237 inline size_t
238 send(
const unsigned char*
const buffer,
size_t len)
240
243
244 // common.
245 inline void
248
249 private:
252 };
253
254 #ifdef CCXX_IPV6
255
277 class RTPBaseUDPIPv6Socket :
private UDPSocket
278 {
279 public:
283 RTPBaseUDPIPv6Socket(const IPV6Address& ia, tpport_t port) :
285 { }
286
287 inline ~RTPBaseUDPIPv6Socket()
288 { endSocket(); }
289
290 inline bool
292 { return UDPSocket::isPending(UDPSocket::pendingInput, timeout); }
293
294 inline IPV6Host
295 getSender(tpport_t& port) const
296 { return UDPSocket::getIPV6Sender(&port); }
297
298 inline size_t
299 recv(unsigned char* buffer, size_t len)
300 { return UDPSocket::receive(buffer, len); }
301
305 inline size_t
306 getNextPacketSize() const
307 {
size_t len;
ccioctl(UDPSocket::so,FIONREAD,len);
return len; }
308
309 Socket::Error
310 setMulticast(bool enable)
311 { return UDPSocket::setMulticast(enable); }
312
313 inline Socket::Error
314 join(const IPV6Multicast& ia, uint32 iface)
315 { return Socket::join(ia); }
316
317 inline Socket::Error
318 drop(
const IPV6Multicast& ia)
320
321 inline Socket::Error
322 setTimeToLive(unsigned char ttl)
323 { return UDPSocket::setTimeToLive(ttl); }
324
328 RTPBaseUDPIPv6Socket() :
330 { }
331
332 inline void
333 setPeer(const IPV6Host &ia, tpport_t port)
334 {UDPSocket::setPeer(ia, port);}
335
336 inline size_t
337 send(const unsigned char* const buffer, size_t len)
338 { return UDPSocket::send(buffer, len); }
339
340 inline SOCKET getRecvSocket() const
341 { return UDPSocket::so; }
342
343 // common
344 inline void
345 endSocket()
346 { UDPSocket::endSocket(); }
347 };
348
369 template<class BaseSocket>
370 class DualRTPChannelIPV6
371 {
372 public:
373 DualRTPChannelIPV6(const IPV6Host& ia, tpport_t port)
374 {
375 recvSocket = new BaseSocket(ia,port);
376 sendSocket = new BaseSocket;
377 }
378
379 inline ~DualRTPChannelIPV6()
380 { delete sendSocket; delete recvSocket; }
381
382 inline bool
384 { return recvSocket->isPendingRecv(timeout); }
385
386 inline IPV6Host
387 getSender(tpport_t& port) const
388 { return recvSocket->getIPV6Sender(port); }
389
390 inline size_t
391 recv(unsigned char* buffer, size_t len)
392 { return recvSocket->recv(buffer, len); }
393
394 inline size_t
395 getNextPacketSize() const
396 { return recvSocket->getNextPacketSize(); }
397
398 inline Socket::Error
399 setMulticast(bool enable)
400 { Socket::Error error = recvSocket->setMulticast(enable);
401 if (error) return error;
402 return sendSocket->setMulticast(enable); }
403
404 inline Socket::Error
405 join(const IPV6Multicast& ia, uint32 iface)
406 { return recvSocket->join(ia,iface); }
407
408 inline Socket::Error
409 drop(
const IPV6Multicast& ia)
410 { return recvSocket->drop(ia); }
411
412 inline Socket::Error
413 setTimeToLive(unsigned char ttl)
414 { return sendSocket->setTimeToLive(ttl); }
415
416 inline void
417 setPeer(const IPV6Host& host, tpport_t port)
418 { sendSocket->setPeer(host,port); }
419
420 inline size_t
421 send(const unsigned char* const buffer, size_t len)
422 { return sendSocket->send(buffer, len); }
423
424 inline SOCKET getRecvSocket() const
425 { return recvSocket->getRecvSocket(); }
426
427 // common.
428 inline void
429 endSocket()
430 { sendSocket->endSocket(); recvSocket->endSocket(); }
431
432 private:
433 BaseSocket* sendSocket;
434 BaseSocket* recvSocket;
435 };
436
437
438 typedef DualRTPChannelIPV6<RTPBaseUDPIPv6Socket> DualRTPUDPIPv6Channel;
439 typedef RTPBaseUDPIPv6Socket SingleRTPChannelIPV6;
440 typedef SingleRTPChannelIPV6 SymmetricRTPChannelIPV6;
441
442 #endif
443
445
451
456 // sockets
458
459 END_NAMESPACE
460
461 #endif //CCRTP_CHANNEL_H_
462
Socket::Error setTimeToLive(unsigned char ttl)
RTPBaseUDPIPv4Socket()
Constructor for transmitter.
Socket::Error drop(const InetMcastAddress &ia)
DualRTPChannel< RTPBaseUDPIPv4Socket > DualRTPUDPIPv4Channel
uint32 microtimeout_t
Time interval expressed in microseconds.
Socket::Error drop(const InetMcastAddress &ia)
SOCKET getRecvSocket() const
RTPBaseUDPIPv4Socket(const InetAddress &ia, tpport_t port)
Constructor for receiver.
SingleRTPChannel SymmetricRTPChannel
Actually, RTP with a single channel can be called 'Symmetric RTP'.
bool isPendingRecv(microtimeout_t timeout) const
SOCKET getRecvSocket() const
Socket::Error join(const InetMcastAddress &ia, uint32 iface)
Socket::Error setTimeToLive(unsigned char ttl)
Socket::Error setMulticast(bool enable)
A UDP/IPv4 socket class targetted at RTP stacks.
size_t getNextPacketSize() const
bool isPendingRecv(microtimeout_t timeout)
size_t getNextPacketSize() const
Get size of next datagram waiting to be read.
InetHostAddress getSender(tpport_t &port) const
RTPBaseUDPIPv4Socket SingleRTPChannel
May be used in applications where using the same socket for both sending and receiving is not a limit...
Socket::Error setMulticast(bool enable)
Socket::Error join(const InetMcastAddress &ia, uint32 iface)
size_t recv(unsigned char *buffer, size_t len)
DualRTPChannel(const InetAddress &ia, tpport_t port)
Base elements for RTP stacks: constants, types and global functions.
size_t send(const unsigned char *const buffer, size_t len)
void setPeer(const InetAddress &ia, tpport_t port)
size_t ccioctl(int so, int request, size_t &len)
void setPeer(const InetAddress &host, tpport_t port)
size_t recv(unsigned char *buffer, size_t len)
InetHostAddress getSender(tpport_t &port) const
size_t send(const unsigned char *const buffer, size_t len)