Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
udp_sender_port.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc 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 
17 #include "roc_core/iallocator.h"
18 #include "roc_core/mutex.h"
19 #include "roc_core/refcnt.h"
20 #include "roc_netio/basic_port.h"
22 #include "roc_packet/address.h"
23 #include "roc_packet/iwriter.h"
24 
25 namespace roc {
26 namespace netio {
27 
28 //! UDP sender.
29 class UDPSenderPort : public BasicPort, public packet::IWriter {
30 public:
31  //! Initialize.
32  UDPSenderPort(ICloseHandler& close_handler,
33  const packet::Address&,
34  uv_loop_t& event_loop,
35  core::IAllocator& allocator);
36 
37  //! Destroy.
39 
40  //! Get bind address.
41  virtual const packet::Address& address() const;
42 
43  //! Open sender.
44  virtual bool open();
45 
46  //! Asynchronously close sender.
47  virtual void async_close();
48 
49  //! Write packet.
50  //! @remarks
51  //! May be called from any thread.
52  virtual void write(const packet::PacketPtr&);
53 
54 private:
55  static void close_cb_(uv_handle_t* handle);
56  static void write_sem_cb_(uv_async_t* handle);
57  static void send_cb_(uv_udp_send_t* req, int status);
58 
59  packet::PacketPtr read_();
60  void close_();
61 
62  ICloseHandler& close_handler_;
63 
64  uv_loop_t& loop_;
65 
66  uv_async_t write_sem_;
67  bool write_sem_initialized_;
68 
69  uv_udp_t handle_;
70  bool handle_initialized_;
71 
72  packet::Address address_;
73 
75  core::Mutex mutex_;
76 
77  size_t pending_;
78  bool stopped_;
79  bool closed_;
80 
81  unsigned packet_counter_;
82 };
83 
84 } // namespace netio
85 } // namespace roc
86 
87 #endif // ROC_NETIO_UDP_SENDER_PORT_H_
Memory allocator interface.
Definition: iallocator.h:23
virtual bool open()
Open sender.
Root namespace.
Close handler interface.
Close handler.
Mutex.
Definition: mutex.h:27
Packet writer interface.
Basic port interface.
Definition: basic_port.h:24
Network address.
Definition: address.h:24
Packet writer interface.
Definition: iwriter.h:21
Network address.
virtual const packet::Address & address() const
Get bind address.
UDPSenderPort(ICloseHandler &close_handler, const packet::Address &, uv_loop_t &event_loop, core::IAllocator &allocator)
Initialize.
Mutex.
Base class for reference countable objects.
Basic network port.
virtual void write(const packet::PacketPtr &)
Write packet.
virtual void async_close()
Asynchronously close sender.
Memory allocator interface.