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 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 
17 #include "roc_core/buffer_pool.h"
18 #include "roc_core/iallocator.h"
19 #include "roc_core/list.h"
20 #include "roc_core/list_node.h"
21 #include "roc_core/refcnt.h"
22 #include "roc_netio/basic_port.h"
24 #include "roc_packet/address.h"
25 #include "roc_packet/iwriter.h"
26 #include "roc_packet/packet_pool.h"
27 
28 namespace roc {
29 namespace netio {
30 
31 //! UDP receiver.
32 class UDPReceiverPort : public BasicPort {
33 public:
34  //! Initialize.
35  UDPReceiverPort(ICloseHandler& close_handler,
36  const packet::Address&,
37  uv_loop_t& event_loop,
38  packet::IWriter& writer,
39  packet::PacketPool& packet_pool,
40  core::BufferPool<uint8_t>& buffer_pool,
41  core::IAllocator& allocator);
42 
43  //! Destroy.
45 
46  //! Get bind address.
47  virtual const packet::Address& address() const;
48 
49  //! Open receiver.
50  virtual bool open();
51 
52  //! Asynchronously close receiver.
53  virtual void async_close();
54 
55 private:
56  static void close_cb_(uv_handle_t* handle);
57  static void alloc_cb_(uv_handle_t* handle, size_t size, uv_buf_t* buf);
58  static void recv_cb_(uv_udp_t* handle,
59  ssize_t nread,
60  const uv_buf_t* buf,
61  const sockaddr* addr,
62  unsigned flags);
63 
64  ICloseHandler& close_handler_;
65 
66  uv_loop_t& loop_;
67 
68  uv_udp_t handle_;
69  bool handle_initialized_;
70 
71  bool recv_started_;
72  bool closed_;
73 
74  packet::Address address_;
75  packet::IWriter& writer_;
76 
77  packet::PacketPool& packet_pool_;
78  core::BufferPool<uint8_t>& buffer_pool_;
79 
80  unsigned packet_counter_;
81 };
82 
83 } // namespace netio
84 } // namespace roc
85 
86 #endif // ROC_NETIO_UDP_RECEIVER_PORT_H_
Memory allocator interface.
Definition: iallocator.h:23
virtual void async_close()
Asynchronously close receiver.
virtual bool open()
Open receiver.
virtual const packet::Address & address() const
Get bind address.
Packet pool.
Root namespace.
Close handler interface.
Linked list node.
Close handler.
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.
Intrusive doubly-linked list.
UDPReceiverPort(ICloseHandler &close_handler, const packet::Address &, uv_loop_t &event_loop, packet::IWriter &writer, packet::PacketPool &packet_pool, core::BufferPool< uint8_t > &buffer_pool, core::IAllocator &allocator)
Initialize.
Base class for reference countable objects.
Buffer pool.
Basic network port.
Memory allocator interface.