Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
udp_receiver_port.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_netio/target_libuv/roc_netio/udp_receiver_port.h
10 //! @brief UDP receiver.
11 
12 #ifndef ROC_NETIO_UDP_RECEIVER_PORT_H_
13 #define ROC_NETIO_UDP_RECEIVER_PORT_H_
14 
15 #include <uv.h>
16 
19 #include "roc_core/iarena.h"
20 #include "roc_core/list.h"
21 #include "roc_core/list_node.h"
22 #include "roc_netio/basic_port.h"
24 #include "roc_packet/iwriter.h"
26 
27 namespace roc {
28 namespace netio {
29 
30 //! UDP receiver parameters.
32  //! Receiver will bind to this address.
33  //! If IP is zero, INADDR_ANY is used, i.e. the socket is bound to all network
34  //! interfaces. If port is zero, a random free port is selected.
36 
37  //! If not empty, receiver will join multicast group on the interface
38  //! with given address. May be "0.0.0.0" or "[::]" to join on all interfaces.
40 
41  //! If set, enable SO_REUSEADDR when binding socket to non-ephemeral port.
42  //! If not set, SO_REUSEADDR is enabled only for multicast sockets when
43  //! binding to non-ephemeral port.
44  bool reuseaddr;
45 
47  : reuseaddr(false) {
48  multicast_interface[0] = '\0';
49  }
50 };
51 
52 //! UDP receiver.
53 class UdpReceiverPort : public BasicPort {
54 public:
55  //! Initialize.
57  packet::IWriter& writer,
58  uv_loop_t& event_loop,
59  packet::PacketFactory& packet_factory,
60  core::BufferFactory<uint8_t>& buffer_factory,
62 
63  //! Destroy.
64  virtual ~UdpReceiverPort();
65 
66  //! Get bind address.
68 
69  //! Open receiver.
70  virtual bool open();
71 
72  //! Asynchronously close receiver.
73  virtual AsyncOperationStatus async_close(ICloseHandler& handler, void* handler_arg);
74 
75 protected:
76  //! Format descriptor.
78 
79 private:
80  static void close_cb_(uv_handle_t* handle);
81  static void alloc_cb_(uv_handle_t* handle, size_t size, uv_buf_t* buf);
82  static void recv_cb_(uv_udp_t* handle,
83  ssize_t nread,
84  const uv_buf_t* buf,
85  const sockaddr* addr,
86  unsigned flags);
87 
88  bool join_multicast_group_();
89  void leave_multicast_group_();
90 
91  UdpReceiverConfig config_;
92  packet::IWriter& writer_;
93 
94  ICloseHandler* close_handler_;
95  void* close_handler_arg_;
96 
97  uv_loop_t& loop_;
98 
99  uv_udp_t handle_;
100  bool handle_initialized_;
101 
102  bool multicast_group_joined_;
103  bool recv_started_;
104  bool closed_;
105 
106  packet::PacketFactory& packet_factory_;
107  core::BufferFactory<uint8_t>& buffer_factory_;
108 
109  unsigned packet_counter_;
110 };
111 
112 } // namespace netio
113 } // namespace roc
114 
115 #endif // ROC_NETIO_UDP_RECEIVER_PORT_H_
Base class for ports.
Buffer factory.
Socket address.
Definition: socket_addr.h:26
IArena & arena() const
Get arena.
Memory arena interface.
Definition: iarena.h:23
Base class for ports.
Definition: basic_port.h:40
Close handler interface.
UdpReceiverPort(const UdpReceiverConfig &config, packet::IWriter &writer, uv_loop_t &event_loop, packet::PacketFactory &packet_factory, core::BufferFactory< uint8_t > &buffer_factory, core::IArena &arena)
Initialize.
virtual ~UdpReceiverPort()
Destroy.
virtual void format_descriptor(core::StringBuilder &b)
Format descriptor.
virtual AsyncOperationStatus async_close(ICloseHandler &handler, void *handler_arg)
Asynchronously close receiver.
const address::SocketAddr & bind_address() const
Get bind address.
virtual bool open()
Open receiver.
Packet writer interface.
Definition: iwriter.h:23
Memory arena interface.
Close handler interface.
Packet writer interface.
Intrusive doubly-linked list.
Linked list node.
AsyncOperationStatus
Asynchronous operation status.
Root namespace.
Packet factory.
Socket address.
UDP receiver parameters.
bool reuseaddr
If set, enable SO_REUSEADDR when binding socket to non-ephemeral port. If not set,...
address::SocketAddr bind_address
Receiver will bind to this address. If IP is zero, INADDR_ANY is used, i.e. the socket is bound to al...
char multicast_interface[64]
If not empty, receiver will join multicast group on the interface with given address....