userver: userver/components/tcp_acceptor_base.hpp Source File
Loading...
Searching...
No Matches
tcp_acceptor_base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/components/tcp_acceptor_base.hpp
4/// @brief @copybrief components::TcpAcceptorBase
5
6#include <userver/components/loggable_component_base.hpp>
7#include <userver/concurrent/background_task_storage.hpp>
8#include <userver/engine/io/socket.hpp>
9#include <userver/engine/task/task.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace server::net {
14struct ListenerConfig;
15}
16
17namespace components {
18
19// clang-format off
20
21/// @ingroup userver_base_classes userver_components
22///
23/// @brief Component for accepting incoming TCP connections.
24///
25/// Each accepted socket is processed in a new coroutine by ProcessSocket of
26/// the derived class.
27///
28/// ## Static options:
29/// Name | Description | Default value
30/// ---- | ----------- | -------------
31/// port | port to listen on | -
32/// unix-socket | unix socket to listen on instead of listening on a port | ''
33/// task_processor | task processor to accept incoming connections | -
34/// backlog | max count of new connections pending acceptance | 1024
35/// no_delay | whether to set the `TCP_NODELAY` option on incoming sockets | true
36/// sockets_task_processor | task processor to process accepted sockets | value of `task_processor`
37///
38/// @see @ref scripts/docs/en/userver/tutorial/tcp_service.md
39
40// clang-format on
42 public:
43 TcpAcceptorBase(const ComponentConfig&, const ComponentContext&);
44 ~TcpAcceptorBase() override;
45
46 static yaml_config::Schema GetStaticConfigSchema();
47
48 protected:
49 /// Override this function to process incoming sockets.
50 ///
51 /// @warning The function is called concurrently from multiple threads on
52 /// each new socket.
53 virtual void ProcessSocket(engine::io::Socket&& sock) = 0;
54
55 private:
56 TcpAcceptorBase(const ComponentConfig& config,
57 const ComponentContext& context,
58 const server::net::ListenerConfig& acceptor_config);
59
60 void KeepAccepting();
61
62 void OnAllComponentsLoaded() final;
63 void OnAllComponentsAreStopping() final;
64
65 const bool no_delay_;
66 engine::TaskProcessor& acceptor_task_processor_;
67 engine::TaskProcessor& sockets_task_processor_;
68 concurrent::BackgroundTaskStorageCore tasks_;
69 engine::io::Socket listen_sock_;
70 engine::Task acceptor_;
71};
72
73} // namespace components
74
75USERVER_NAMESPACE_END