Branch data Line data Source code
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 : : #include <memory>
21 : : #include <optional>
22 : : #include <string>
23 : : #include <variant>
24 : : #include <vector>
25 : :
26 : : #include "wirestead/base/constants.hpp"
27 : : #include "wirestead/base/platform.hpp"
28 : : #include "wirestead/base/visibility.hpp"
29 : : #include "wirestead/config/tcp_client_config.hpp"
30 : : #include "wirestead/diagnostics/error_types.hpp"
31 : : #include "wirestead/interface/result_channel.hpp"
32 : : #include "wirestead/memory/memory_pool.hpp"
33 : : #include "wirestead/transport/base/reconnect_policy.hpp"
34 : : #include "wirestead/wrapper/send_result.hpp"
35 : :
36 : : // Forward declare boost components
37 : : namespace boost {
38 : : namespace asio {
39 : : class io_context;
40 : : }
41 : : } // namespace boost
42 : :
43 : : namespace wirestead {
44 : : namespace wrapper {
45 : : class TcpClient;
46 : : } // namespace wrapper
47 : : namespace transport {
48 : : namespace detail {
49 : : struct TcpWriteWait;
50 : : }
51 : :
52 : : using base::LinkState;
53 : : using config::TcpClientConfig;
54 : : using interface::Channel;
55 : :
56 : : // Use static create() helpers to construct safely
57 : : class WIRESTEAD_API TcpClient : public interface::ResultChannel, public std::enable_shared_from_this<TcpClient> {
58 : : public:
59 : : using BufferVariant =
60 : : std::variant<memory::PooledBuffer, std::vector<uint8_t>, std::shared_ptr<const std::vector<uint8_t>>>;
61 : :
62 : : static std::shared_ptr<TcpClient> create(const TcpClientConfig& cfg);
63 : : static std::shared_ptr<TcpClient> create(const TcpClientConfig& cfg, boost::asio::io_context& ioc);
64 : : ~TcpClient() override;
65 : :
66 : : // Move semantics
67 : : TcpClient(TcpClient&&) noexcept;
68 : : TcpClient& operator=(TcpClient&&) noexcept;
69 : :
70 : : // Disable copy (should be already disabled by unique_ptr, but being explicit)
71 : : TcpClient(const TcpClient&) = delete;
72 : : TcpClient& operator=(const TcpClient&) = delete;
73 : :
74 : : void start() override;
75 : : void stop() override;
76 : : bool is_connected() const override;
77 : : bool is_backpressure_active() const override;
78 : : std::optional<size_t> write_queue_limit() const override;
79 : : wrapper::RuntimeStats stats() const override;
80 : : void reset_stats() override;
81 : : boost::asio::any_io_executor get_executor() override;
82 : :
83 : : [[nodiscard]] wrapper::SendResult async_write_copy_result(memory::ConstByteSpan data) override;
84 : : [[nodiscard]] wrapper::SendResult async_write_move_result(std::vector<uint8_t>&& data) override;
85 : : [[nodiscard]] wrapper::SendResult async_write_shared_result(
86 : : std::shared_ptr<const std::vector<uint8_t>> data) override;
87 : : [[nodiscard]] wrapper::SendResult async_try_write_copy_result(memory::ConstByteSpan data) override;
88 : : [[nodiscard]] wrapper::SendResult async_try_write_move_result(std::vector<uint8_t>&& data) override;
89 : : [[nodiscard]] wrapper::SendResult async_try_write_shared_result(
90 : : std::shared_ptr<const std::vector<uint8_t>> data) override;
91 : :
92 : : // Thread-safe: may be called at any time, including after start(). Each
93 : : // setter takes effect for subsequent operations (callback replacement is
94 : : // synchronized against concurrent reads on the io thread; see #436).
95 : : void on_bytes(OnBytes cb) override;
96 : : void on_state(OnState cb) override;
97 : : void on_backpressure(OnBackpressure cb) override;
98 : :
99 : : std::optional<diagnostics::ErrorInfo> last_error_info() const override;
100 : :
101 : : // Dynamic configuration methods. Thread-safe; take effect for the next
102 : : // reconnect/idle-timeout decision, not retroactively for one already in
103 : : // flight.
104 : : void set_backpressure_strategy(base::constants::BackpressureStrategy strategy);
105 : : void set_retry_interval(unsigned interval_ms);
106 : : void set_max_retries(int max_retries);
107 : : void set_connection_timeout(unsigned timeout_ms);
108 : : void set_idle_timeout(unsigned timeout_ms);
109 : : void set_idle_timeout_action(IdleTimeoutAction action);
110 : : void set_reconnect_policy(ReconnectPolicy policy);
111 : :
112 : : private:
113 : : // The built-in wrapper pins capacity waits without extending Channel's ABI.
114 : : friend class wrapper::TcpClient;
115 : : void fail_receive();
116 : : std::optional<uint64_t> write_connection() const;
117 : : std::shared_ptr<detail::TcpWriteWait> capture_write_wait() const;
118 : : std::optional<wrapper::SendResult> poll_write_wait(const std::shared_ptr<detail::TcpWriteWait>& wait) const;
119 : : void cancel_write_waits();
120 : : wrapper::SendResult write_state();
121 : : // Native admission only: wrapper lifecycle, validation precedence and
122 : : // strategy-level reason mapping remain the wrapper's responsibility.
123 : : wrapper::SendResult write_copy(memory::ConstByteSpan data, std::optional<uint64_t> expected_connection);
124 : : wrapper::SendResult write_move(std::vector<uint8_t>&& data, std::optional<uint64_t> expected_connection);
125 : : wrapper::SendResult write_shared(std::shared_ptr<const std::vector<uint8_t>> data,
126 : : std::optional<uint64_t> expected_connection);
127 : :
128 : : wrapper::SendResult try_write_copy(memory::ConstByteSpan data);
129 : : wrapper::SendResult try_write_move(std::vector<uint8_t>&& data);
130 : : wrapper::SendResult try_write_shared(std::shared_ptr<const std::vector<uint8_t>> data);
131 : :
132 : : explicit TcpClient(const TcpClientConfig& cfg);
133 : : explicit TcpClient(const TcpClientConfig& cfg, boost::asio::io_context& ioc);
134 : :
135 : : struct Impl;
136 : 5661 : const Impl* get_impl() const { return impl_.get(); }
137 : : Impl* get_impl() { return impl_.get(); }
138 : : std::unique_ptr<Impl> impl_;
139 : : };
140 : : } // namespace transport
141 : : } // namespace wirestead
|