Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
tcp_server_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/tcp_server_port.h
10 //! @brief TCP server.
11 
12 #ifndef ROC_NETIO_TCP_SERVER_PORT_H_
13 #define ROC_NETIO_TCP_SERVER_PORT_H_
14 
15 #include <uv.h>
16 
18 #include "roc_core/iarena.h"
19 #include "roc_core/list.h"
20 #include "roc_core/shared_ptr.h"
21 #include "roc_core/stddefs.h"
22 #include "roc_netio/basic_port.h"
26 #include "roc_netio/socket_ops.h"
28 
29 namespace roc {
30 namespace netio {
31 
32 //! TCP server parameters.
34  //! Server will bind to this address.
35  //! If IP is zero, INADDR_ANY is used, i.e. the socket is bound to all network
36  //! interfaces. If port is zero, a random free port is selected.
38 
39  //! Maximum length to which the queue of pending connections may grow.
40  size_t backlog_limit;
41 
43  : backlog_limit(128) {
44  }
45 };
46 
47 //! TCP server.
48 class TcpServerPort : public BasicPort, private ITerminateHandler, private ICloseHandler {
49 public:
50  //! Initialize.
52  IConnAcceptor& conn_acceptor,
53  uv_loop_t& loop,
55 
56  //! Destroy.
57  virtual ~TcpServerPort();
58 
59  //! Get bind address.
61 
62  //! Open TCP server.
63  //!
64  //! @remarks
65  //! Should be called from the network loop thread.
66  virtual bool open();
67 
68  //! Asynchronously close TCP server.
69  //!
70  //! @remarks
71  //! Should be called from network loop thread.
72  virtual AsyncOperationStatus async_close(ICloseHandler& handler, void* handler_arg);
73 
74 protected:
75  //! Format descriptor.
77 
78 private:
79  static void poll_cb_(uv_poll_t* handle, int status, int events);
80  static void close_cb_(uv_handle_t* handle);
81 
82  virtual void handle_terminate_completed(IConn& conn, void* arg);
83  virtual void handle_close_completed(BasicPort& port, void* arg);
84 
85  AsyncOperationStatus async_close_server_();
86  void finish_closing_server_();
87 
88  size_t num_connections_() const;
89  void async_close_all_connections_();
90  void async_terminate_connection_(const core::SharedPtr<TcpConnectionPort>&);
91  void async_close_connection_(const core::SharedPtr<TcpConnectionPort>&);
92  void finish_closing_connection_(const core::SharedPtr<TcpConnectionPort>&);
93 
94  TcpServerConfig config_;
95 
96  IConnAcceptor& conn_acceptor_;
97 
98  ICloseHandler* close_handler_;
99  void* close_handler_arg_;
100 
101  uv_loop_t& loop_;
102 
103  SocketHandle socket_;
104 
105  uv_poll_t poll_handle_;
106  bool poll_handle_initialized_;
107  bool poll_handle_started_;
108 
109  core::List<TcpConnectionPort> open_conns_;
110  core::List<TcpConnectionPort> closing_conns_;
111 
112  bool want_close_;
113  bool closed_;
114 };
115 
116 } // namespace netio
117 } // namespace roc
118 
119 #endif // ROC_NETIO_TCP_SERVER_PORT_H_
Base class for ports.
Socket address.
Definition: socket_addr.h:26
IArena & arena() const
Get arena.
Memory arena interface.
Definition: iarena.h:23
Intrusive doubly-linked list.
Definition: list.h:40
Shared ownership intrusive pointer.
Definition: shared_ptr.h:32
Base class for ports.
Definition: basic_port.h:40
Close handler interface.
Connection acceptor interface.
Connection interface.
Definition: iconn.h:30
Termination handler interface.
TcpServerPort(const TcpServerConfig &config, IConnAcceptor &conn_acceptor, uv_loop_t &loop, core::IArena &arena)
Initialize.
const address::SocketAddr & bind_address() const
Get bind address.
virtual AsyncOperationStatus async_close(ICloseHandler &handler, void *handler_arg)
Asynchronously close TCP server.
virtual ~TcpServerPort()
Destroy.
virtual bool open()
Open TCP server.
virtual void format_descriptor(core::StringBuilder &b)
Format descriptor.
Memory arena interface.
Close handler interface.
Connection acceptor interface.
Termination handler interface.
Intrusive doubly-linked list.
int SocketHandle
Platform-specific socket handle.
Definition: socket_ops.h:51
AsyncOperationStatus
Asynchronous operation status.
Root namespace.
Shared ownership intrusive pointer.
Socket address.
Socket operations.
Commonly used types and functions.
TCP connection parameters.
TCP server parameters.
address::SocketAddr bind_address
Server will bind to this address. If IP is zero, INADDR_ANY is used, i.e. the socket is bound to all ...
size_t backlog_limit
Maximum length to which the queue of pending connections may grow.
TCP connection.