unilink  0.4.3
A simple C++ library for unified async communication
tcp_server_builder.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Jinwoo Sung
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cstdint>
20 
24 
25 namespace unilink {
26 namespace builder {
27 
31 #ifdef _MSC_VER
32 #pragma warning(push)
33 #pragma warning(disable : 4251)
34 #endif
35 class UNILINK_API TcpServerBuilder : public BuilderInterface<wrapper::TcpServer, TcpServerBuilder> {
36  public:
37  explicit TcpServerBuilder(uint16_t port);
38 
39  // Delete copy, allow move
44 
49  std::unique_ptr<wrapper::TcpServer> build() override;
50 
56  TcpServerBuilder& auto_manage(bool auto_manage = true) override;
57 
58  // Modernized event handlers (Override BuilderInterface)
59  TcpServerBuilder& on_data(std::function<void(const wrapper::MessageContext&)> handler) override;
60  TcpServerBuilder& on_connect(std::function<void(const wrapper::ConnectionContext&)> handler) override;
61  TcpServerBuilder& on_disconnect(std::function<void(const wrapper::ConnectionContext&)> handler) override;
62  TcpServerBuilder& on_error(std::function<void(const wrapper::ErrorContext&)> handler) override;
63 
67  TcpServerBuilder& use_independent_context(bool use_independent = true);
68 
72  TcpServerBuilder& enable_port_retry(bool enable = true, int max_retries = 3, int retry_interval_ms = 1000);
73 
77  TcpServerBuilder& idle_timeout(int timeout_ms);
78 
82  TcpServerBuilder& max_clients(size_t max);
83 
87  TcpServerBuilder& single_client();
88 
92  TcpServerBuilder& multi_client(size_t max);
93 
97  TcpServerBuilder& unlimited_clients();
98 
99  private:
100  uint16_t port_;
101  bool auto_manage_;
102  bool use_independent_context_;
103 
104  // Configuration
105  bool enable_port_retry_;
106  int max_port_retries_;
107  int port_retry_interval_ms_;
108  int idle_timeout_ms_;
109  size_t max_clients_;
110  bool client_limit_set_;
111 
112  // Modernized callbacks
113  std::function<void(const wrapper::MessageContext&)> on_data_;
114  std::function<void(const wrapper::ConnectionContext&)> on_connect_;
115  std::function<void(const wrapper::ConnectionContext&)> on_disconnect_;
116  std::function<void(const wrapper::ErrorContext&)> on_error_;
117 };
118 
119 #ifdef _MSC_VER
120 #pragma warning(pop)
121 #endif
122 
123 } // namespace builder
124 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37