unilink  0.4.3
A simple C++ library for unified async communication
tcp_client_builder.cc
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 
18 
19 #include <boost/asio/io_context.hpp>
20 
23 
24 namespace unilink {
25 namespace builder {
26 
27 TcpClientBuilder::TcpClientBuilder(const std::string& host, uint16_t port)
28  : host_(host),
29  port_(port),
30  auto_manage_(false),
31  use_independent_context_(false),
32  retry_interval_(3000),
33  max_retries_(-1),
34  connection_timeout_(5000) {
35  if (port == 0) throw diagnostics::BuilderException("Invalid port number: 0");
36  if (host.empty()) throw diagnostics::BuilderException("Host cannot be empty");
37 
38  // Ensure background IO service is running
40 }
41 
42 std::unique_ptr<wrapper::TcpClient> TcpClientBuilder::build() {
43  std::unique_ptr<wrapper::TcpClient> client;
44  if (use_independent_context_) {
45  client = std::make_unique<wrapper::TcpClient>(host_, port_, std::make_shared<boost::asio::io_context>());
46  client->set_manage_external_context(true);
47  } else {
48  client = std::make_unique<wrapper::TcpClient>(host_, port_);
49  }
50 
51  if (on_data_) client->on_data(on_data_);
52  if (on_connect_) client->on_connect(on_connect_);
53  if (on_disconnect_) client->on_disconnect(on_disconnect_);
54  if (on_error_) client->on_error(on_error_);
55 
56  client->set_retry_interval(retry_interval_);
57  client->set_max_retries(max_retries_);
58  client->set_connection_timeout(connection_timeout_);
59 
60  if (auto_manage_) {
61  client->auto_manage(true);
62  }
63 
64  return client;
65 }
66 
68  auto_manage_ = auto_manage;
69  return *this;
70 }
71 
73  on_data_ = std::move(handler);
74  return *this;
75 }
76 
78  on_connect_ = std::move(handler);
79  return *this;
80 }
81 
83  on_disconnect_ = std::move(handler);
84  return *this;
85 }
86 
87 TcpClientBuilder& TcpClientBuilder::on_error(std::function<void(const wrapper::ErrorContext&)> handler) {
88  on_error_ = std::move(handler);
89  return *this;
90 }
91 
93  retry_interval_ = std::chrono::milliseconds(milliseconds);
94  return *this;
95 }
96 
98  max_retries_ = max_retries;
99  return *this;
100 }
101 
103  connection_timeout_ = std::chrono::milliseconds(milliseconds);
104  return *this;
105 }
106 
108  use_independent_context_ = use_independent;
109  return *this;
110 }
111 
112 } // namespace builder
113 } // namespace unilink