Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
udp_sender_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_sender_port.h
10//! @brief UDP sender.
11
12#ifndef ROC_NETIO_UDP_SENDER_PORT_H_
13#define ROC_NETIO_UDP_SENDER_PORT_H_
14
15#include <uv.h>
16
18#include "roc_core/atomic.h"
19#include "roc_core/iarena.h"
20#include "roc_core/mpsc_queue.h"
24#include "roc_packet/iwriter.h"
25
26namespace roc {
27namespace netio {
28
29//! UDP sender parameters.
31 //! Sender will bind to this address.
32 //! If IP is zero, INADDR_ANY is used, i.e. the socket is bound to all network
33 //! interfaces. If port is zero, a random free port is selected.
35
36 //! If set, enable SO_REUSEADDR when binding socket to non-ephemeral port.
37 //! If not set, SO_REUSEADDR is not enabled.
39
40 //! If true, allow non-blocking writes directly in write() method.
41 //! If non-blocking write can't be performed, sender falls back to
42 //! regular asynchronous write.
44
46 : reuseaddr(false)
47 , non_blocking_enabled(true) {
48 }
49
50 //! Check two configs for equality.
51 bool operator==(const UdpSenderConfig& other) const {
52 return bind_address == other.bind_address
54 }
55};
56
57//! UDP sender.
58class UdpSenderPort : public BasicPort, public packet::IWriter {
59public:
60 //! Initialize.
62 uv_loop_t& event_loop,
64
65 //! Destroy.
67
68 //! Get bind address.
70
71 //! Open sender.
72 virtual bool open();
73
74 //! Asynchronously close sender.
75 virtual AsyncOperationStatus async_close(ICloseHandler& handler, void* handler_arg);
76
77 //! Write packet.
78 //! @remarks
79 //! May be called from any thread.
81
82protected:
83 //! Format descriptor.
85
86private:
87 static void close_cb_(uv_handle_t* handle);
88 static void write_sem_cb_(uv_async_t* handle);
89 static void send_cb_(uv_udp_send_t* req, int status);
90
91 void write_(const packet::PacketPtr&);
92
93 bool fully_closed_() const;
94 void start_closing_();
95
96 bool try_nonblocking_send_(const packet::PacketPtr& pp);
97 void report_stats_();
98
99 UdpSenderConfig config_;
100
101 ICloseHandler* close_handler_;
102 void* close_handler_arg_;
103
104 uv_loop_t& loop_;
105
106 uv_async_t write_sem_;
107 bool write_sem_initialized_;
108
109 uv_udp_t handle_;
110 bool handle_initialized_;
111
112 address::SocketAddr address_;
113
115
116 core::Atomic<int> pending_packets_;
117 core::Atomic<int> sent_packets_;
118 core::Atomic<int> sent_packets_blk_;
119
120 bool stopped_;
121 bool closed_;
122
123 uv_os_fd_t fd_;
124
125 core::RateLimiter rate_limiter_;
126};
127
128} // namespace netio
129} // namespace roc
130
131#endif // ROC_NETIO_UDP_SENDER_PORT_H_
Atomic.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition attributes.h:31
Base class for ports.
IArena & arena() const
Get arena.
Memory arena interface.
Definition iarena.h:23
Base class for ports.
Definition basic_port.h:40
Close handler interface.
const address::SocketAddr & bind_address() const
Get bind address.
virtual AsyncOperationStatus async_close(ICloseHandler &handler, void *handler_arg)
Asynchronously close sender.
virtual ROC_ATTR_NODISCARD status::StatusCode write(const packet::PacketPtr &)
Write packet.
virtual bool open()
Open sender.
virtual void format_descriptor(core::StringBuilder &b)
Format descriptor.
UdpSenderPort(const UdpSenderConfig &config, uv_loop_t &event_loop, core::IArena &arena)
Initialize.
Packet writer interface.
Definition iwriter.h:23
Memory arena interface.
Close handler interface.
Packet writer interface.
Multi-producer single-consumer queue.
AsyncOperationStatus
Asynchronous operation status.
Root namespace.
Rate limiter.
Socket address.
StatusCode
Status code.
Definition status_code.h:19
UDP sender parameters.
bool non_blocking_enabled
If true, allow non-blocking writes directly in write() method. If non-blocking write can't be perform...
address::SocketAddr bind_address
Sender will bind to this address. If IP is zero, INADDR_ANY is used, i.e. the socket is bound to all ...
bool operator==(const UdpSenderConfig &other) const
Check two configs for equality.
bool reuseaddr
If set, enable SO_REUSEADDR when binding socket to non-ephemeral port. If not set,...