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/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
42public:
43 TcpAcceptorBase(const ComponentConfig&, const ComponentContext&);
44 ~TcpAcceptorBase() override;
45
46 static yaml_config::Schema GetStaticConfigSchema();
47
48protected:
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
55private:
56 TcpAcceptorBase(
57 const ComponentConfig& config,
58 const ComponentContext& context,
59 const server::net::ListenerConfig& acceptor_config
60 );
61
62 void KeepAccepting();
63
64 void OnAllComponentsLoaded() final;
65 void OnAllComponentsAreStopping() final;
66
67 const bool no_delay_;
68 engine::TaskProcessor& acceptor_task_processor_;
69 engine::TaskProcessor& sockets_task_processor_;
70 concurrent::BackgroundTaskStorageCore tasks_;
71 engine::io::Socket listen_sock_;
72 engine::Task acceptor_;
73};
74
75} // namespace components
76
77USERVER_NAMESPACE_END