Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
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/iallocator.h"
20#include "roc_core/list.h"
21#include "roc_core/list_node.h"
24#include "roc_packet/iwriter.h"
26
27namespace roc {
28namespace 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
42 multicast_interface[0] = '\0';
43 }
44};
45
46//! UDP receiver.
47class UdpReceiverPort : public BasicPort {
48public:
49 //! Initialize.
51 packet::IWriter& writer,
52 uv_loop_t& event_loop,
53 packet::PacketFactory& packet_factory,
54 core::BufferFactory<uint8_t>& buffer_factory,
56
57 //! Destroy.
59
60 //! Get bind address.
62
63 //! Open receiver.
64 virtual bool open();
65
66 //! Asynchronously close receiver.
67 virtual AsyncOperationStatus async_close(ICloseHandler& handler, void* handler_arg);
68
69protected:
70 //! Format descriptor.
72
73private:
74 static void close_cb_(uv_handle_t* handle);
75 static void alloc_cb_(uv_handle_t* handle, size_t size, uv_buf_t* buf);
76 static void recv_cb_(uv_udp_t* handle,
77 ssize_t nread,
78 const uv_buf_t* buf,
79 const sockaddr* addr,
80 unsigned flags);
81
82 bool join_multicast_group_();
83 void leave_multicast_group_();
84
85 UdpReceiverConfig config_;
86 packet::IWriter& writer_;
87
88 ICloseHandler* close_handler_;
89 void* close_handler_arg_;
90
91 uv_loop_t& loop_;
92
93 uv_udp_t handle_;
94 bool handle_initialized_;
95
96 bool multicast_group_joined_;
97 bool recv_started_;
98 bool closed_;
99
100 packet::PacketFactory& packet_factory_;
101 core::BufferFactory<uint8_t>& buffer_factory_;
102
103 unsigned packet_counter_;
104};
105
106} // namespace netio
107} // namespace roc
108
109#endif // ROC_NETIO_UDP_RECEIVER_PORT_H_
Base class for ports.
Buffer factory.
Memory allocator interface.
Definition iallocator.h:23
IAllocator & allocator() const
Get allocator.
Base class for ports.
Definition basic_port.h:40
Close handler interface.
virtual ~UdpReceiverPort()
Destroy.
const address::SocketAddr & bind_address() const
Get bind address.
virtual void format_descriptor(core::StringBuilder &b)
Format descriptor.
virtual AsyncOperationStatus async_close(ICloseHandler &handler, void *handler_arg)
Asynchronously close receiver.
virtual bool open()
Open receiver.
UdpReceiverPort(const UdpReceiverConfig &config, packet::IWriter &writer, uv_loop_t &event_loop, packet::PacketFactory &packet_factory, core::BufferFactory< uint8_t > &buffer_factory, core::IAllocator &allocator)
Initialize.
Packet writer interface.
Definition iwriter.h:21
Memory allocator 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.
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....