unilink  0.4.3
A simple C++ library for unified async communication
tcp_client.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 <memory>
20 #include <optional>
21 #include <string>
22 #include <variant>
23 #include <vector>
24 
33 
34 // Forward declare boost components
35 namespace boost {
36 namespace asio {
37 class io_context;
38 }
39 } // namespace boost
40 
41 namespace unilink {
42 namespace transport {
43 
44 using base::LinkState;
45 using config::TcpClientConfig;
46 using interface::Channel;
47 
48 // Use static create() helpers to construct safely
49 class UNILINK_API TcpClient : public Channel, public std::enable_shared_from_this<TcpClient> {
50  public:
51  using BufferVariant =
52  std::variant<memory::PooledBuffer, std::vector<uint8_t>, std::shared_ptr<const std::vector<uint8_t>>>;
53 
54  static std::shared_ptr<TcpClient> create(const TcpClientConfig& cfg);
55  static std::shared_ptr<TcpClient> create(const TcpClientConfig& cfg, boost::asio::io_context& ioc);
56  ~TcpClient();
57 
58  // Move semantics
59  TcpClient(TcpClient&&) noexcept;
60  TcpClient& operator=(TcpClient&&) noexcept;
61 
62  // Disable copy (should be already disabled by unique_ptr, but being explicit)
63  TcpClient(const TcpClient&) = delete;
64  TcpClient& operator=(const TcpClient&) = delete;
65 
66  void start() override;
67  void stop() override;
68  bool is_connected() const override;
69 
70  void async_write_copy(memory::ConstByteSpan data) override;
71  void async_write_move(std::vector<uint8_t>&& data) override;
72  void async_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
73 
74  // Callbacks must be configured before start() is invoked to avoid setter races.
75  void on_bytes(OnBytes cb) override;
76  void on_state(OnState cb) override;
77  void on_backpressure(OnBackpressure cb) override;
78 
79  std::optional<diagnostics::ErrorInfo> last_error_info() const;
80 
81  // Dynamic configuration methods
82  void set_retry_interval(unsigned interval_ms);
83  void set_reconnect_policy(ReconnectPolicy policy);
84 
85  private:
86  explicit TcpClient(const TcpClientConfig& cfg);
87  explicit TcpClient(const TcpClientConfig& cfg, boost::asio::io_context& ioc);
88 
89  struct Impl;
90  const Impl* get_impl() const { return impl_.get(); }
91  Impl* get_impl() { return impl_.get(); }
92  std::unique_ptr<Impl> impl_;
93 };
94 } // namespace transport
95 } // namespace unilink
Definition: serial.hpp:27
#define UNILINK_API
Definition: visibility.hpp:37