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/iallocator.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 true, allow non-blocking writes directly in write() method.
37 //! If non-blocking write can't be performed, sender falls back to
38 //! regular asynchronous write.
40
42 : non_blocking_enabled(true) {
43 }
44
45 //! Check two configs for equality.
46 bool operator==(const UdpSenderConfig& other) const {
47 return bind_address == other.bind_address
49 }
50};
51
52//! UDP sender.
53class UdpSenderPort : public BasicPort, public packet::IWriter {
54public:
55 //! Initialize.
57 uv_loop_t& event_loop,
59
60 //! Destroy.
62
63 //! Get bind address.
65
66 //! Open sender.
67 virtual bool open();
68
69 //! Asynchronously close sender.
70 virtual AsyncOperationStatus async_close(ICloseHandler& handler, void* handler_arg);
71
72 //! Write packet.
73 //! @remarks
74 //! May be called from any thread.
75 virtual void write(const packet::PacketPtr&);
76
77protected:
78 //! Format descriptor.
80
81private:
82 static void close_cb_(uv_handle_t* handle);
83 static void write_sem_cb_(uv_async_t* handle);
84 static void send_cb_(uv_udp_send_t* req, int status);
85
86 void write_(const packet::PacketPtr&);
87
88 bool fully_closed_() const;
89 void start_closing_();
90
91 bool try_nonblocking_send_(const packet::PacketPtr& pp);
92 void report_stats_();
93
94 UdpSenderConfig config_;
95
96 ICloseHandler* close_handler_;
97 void* close_handler_arg_;
98
99 uv_loop_t& loop_;
100
101 uv_async_t write_sem_;
102 bool write_sem_initialized_;
103
104 uv_udp_t handle_;
105 bool handle_initialized_;
106
107 address::SocketAddr address_;
108
110
111 core::Atomic<int> pending_packets_;
112 core::Atomic<int> sent_packets_;
113 core::Atomic<int> sent_packets_blk_;
114
115 bool stopped_;
116 bool closed_;
117
118 uv_os_fd_t fd_;
119
120 core::RateLimiter rate_limiter_;
121};
122
123} // namespace netio
124} // namespace roc
125
126#endif // ROC_NETIO_UDP_SENDER_PORT_H_
Atomic.
Base class for ports.
Atomic integer. Provides sequential consistency. For a fine-grained memory order control,...
Definition atomic.h:26
Memory allocator interface.
Definition iallocator.h:23
Thread-safe lock-free node-based intrusive multi-producer single-consumer queue.
Definition mpsc_queue.h:40
IAllocator & allocator() const
Get allocator.
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 bool open()
Open sender.
virtual void write(const packet::PacketPtr &)
Write packet.
UdpSenderPort(const UdpSenderConfig &config, uv_loop_t &event_loop, core::IAllocator &allocator)
Initialize.
virtual void format_descriptor(core::StringBuilder &b)
Format descriptor.
Packet writer interface.
Definition iwriter.h:21
Memory allocator interface.
Close handler interface.
Packet writer interface.
Multi-producer single-consumer queue.
AsyncOperationStatus
Asynchronous operation status.
Root namespace.
Rate limiter.
Socket address.
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.