Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
basic_port.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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/basic_port.h
10 //! @brief Base class for ports.
11 
12 #ifndef ROC_NETIO_BASIC_PORT_H_
13 #define ROC_NETIO_BASIC_PORT_H_
14 
16 #include "roc_core/iarena.h"
17 #include "roc_core/list_node.h"
18 #include "roc_core/ref_counted.h"
22 
23 namespace roc {
24 namespace netio {
25 
26 //! Base class for ports.
27 //!
28 //! Port is a transport-level endpoint, sending or receiving data from remote
29 //! peer, like UDP sender or receiver, TCP listening socket, or TCP connection.
30 //!
31 //! The following rules must be followed:
32 //!
33 //! - if you called open(), you're responsible for calling async_close(),
34 //! even if open() failed
35 //! - if async_close() returned AsyncOp_Completed, the port was closed
36 //! immediately, and you can now destroy it
37 //! - if async_close() returned AsyncOp_Started, you should wait until
38 //! close handler callback is invoked before destroying port
39 class BasicPort : public core::RefCounted<BasicPort, core::ArenaAllocation>,
40  public core::ListNode {
41 public:
42  //! Initialize.
43  explicit BasicPort(core::IArena&);
44 
45  //! Destroy.
46  virtual ~BasicPort();
47 
48  //! Get a human-readable port description.
49  //!
50  //! @note
51  //! Port descriptor may change during initial configuration.
52  const char* descriptor() const;
53 
54  //! Open port.
55  //!
56  //! @remarks
57  //! Should be called from the event loop thread.
58  virtual bool open() = 0;
59 
60  //! Asynchronous close.
61  //!
62  //! @remarks
63  //! Should be called from the event loop thread.
64  //!
65  //! @returns
66  //! status code indicating whether operation was completed immediately or
67  //! is scheduled for asynchronous execution
69  void* handler_arg) = 0;
70 
71 protected:
72  //! Format descriptor and store into internal buffer.
74 
75  //! Implementation of descriptor formatting.
76  virtual void format_descriptor(core::StringBuilder& b) = 0;
77 
78 private:
79  enum { MaxDescriptorLen = address::SocketAddr::MaxStrLen * 2 + 48 };
80 
81  char descriptor_[MaxDescriptorLen];
82 };
83 
84 } // namespace netio
85 } // namespace roc
86 
87 #endif // ROC_NETIO_BASIC_PORT_H_
Memory arena interface.
Definition: iarena.h:23
Base class for list element.
Definition: list_node.h:26
Base class for object with reference counter.
Definition: ref_counted.h:40
Base class for ports.
Definition: basic_port.h:40
const char * descriptor() const
Get a human-readable port description.
virtual bool open()=0
Open port.
virtual AsyncOperationStatus async_close(ICloseHandler &handler, void *handler_arg)=0
Asynchronous close.
virtual void format_descriptor(core::StringBuilder &b)=0
Implementation of descriptor formatting.
BasicPort(core::IArena &)
Initialize.
void update_descriptor()
Format descriptor and store into internal buffer.
virtual ~BasicPort()
Destroy.
Close handler interface.
Memory arena interface.
Close handler interface.
Linked list node.
AsyncOperationStatus
Asynchronous operation status.
Root namespace.
Asynchronous operation status.
Base class for object with reference counter.
Socket address.
String builder.