unilink  0.4.3
A simple C++ library for unified async communication
tcp_client_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 <chrono>
20 #include <cstdint>
21 #include <string>
22 
26 
27 namespace unilink {
28 namespace builder {
29 
33 #ifdef _MSC_VER
34 #pragma warning(push)
35 #pragma warning(disable : 4251)
36 #endif
37 class UNILINK_API TcpClientBuilder : public BuilderInterface<wrapper::TcpClient, TcpClientBuilder> {
38  public:
39  TcpClientBuilder(const std::string& host, uint16_t port);
40 
41  // Delete copy, allow move
46 
47  std::unique_ptr<wrapper::TcpClient> build() override;
48 
49  TcpClientBuilder& auto_manage(bool auto_manage = true) override;
50 
51  // Modernized event handlers
52  TcpClientBuilder& on_data(std::function<void(const wrapper::MessageContext&)> handler) override;
53  TcpClientBuilder& on_connect(std::function<void(const wrapper::ConnectionContext&)> handler) override;
54  TcpClientBuilder& on_disconnect(std::function<void(const wrapper::ConnectionContext&)> handler) override;
55  TcpClientBuilder& on_error(std::function<void(const wrapper::ErrorContext&)> handler) override;
56 
60  TcpClientBuilder& retry_interval(uint32_t milliseconds);
61 
65  TcpClientBuilder& max_retries(int max_retries);
66 
70  TcpClientBuilder& connection_timeout(uint32_t milliseconds);
71 
75  TcpClientBuilder& use_independent_context(bool use_independent = true);
76 
77  private:
78  std::string host_;
79  uint16_t port_;
80  bool auto_manage_;
81  bool use_independent_context_;
82 
83  // Configuration
84  std::chrono::milliseconds retry_interval_;
85  int max_retries_;
86  std::chrono::milliseconds connection_timeout_;
87 
88  // Callbacks
89  std::function<void(const wrapper::MessageContext&)> on_data_;
90  std::function<void(const wrapper::ConnectionContext&)> on_connect_;
91  std::function<void(const wrapper::ConnectionContext&)> on_disconnect_;
92  std::function<void(const wrapper::ErrorContext&)> on_error_;
93 };
94 
95 #ifdef _MSC_VER
96 #pragma warning(pop)
97 #endif
98 
99 } // namespace builder
100 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37