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 <chrono>
20 : : #include <functional>
21 : : #include <future>
22 : : #include <memory>
23 : : #include <string>
24 : : #include <string_view>
25 : : #include <vector>
26 : :
27 : : #include "wirestead/base/constants.hpp"
28 : : #include "wirestead/base/visibility.hpp"
29 : : #include "wirestead/wrapper/ichannel.hpp"
30 : :
31 : : namespace boost {
32 : : namespace asio {
33 : : class io_context;
34 : : }
35 : : } // namespace boost
36 : :
37 : : namespace wirestead {
38 : :
39 : : namespace interface {
40 : : class Channel;
41 : : }
42 : :
43 : : namespace wrapper {
44 : :
45 : : /**
46 : : * @brief Modernized TCP Client Wrapper
47 : : */
48 : : class WIRESTEAD_API TcpClient : public ChannelInterface {
49 : : public:
50 : : TcpClient(const std::string& host, uint16_t port);
51 : : TcpClient(const std::string& host, uint16_t port, std::shared_ptr<boost::asio::io_context> external_ioc);
52 : : explicit TcpClient(std::shared_ptr<interface::Channel> channel);
53 : : ~TcpClient() override;
54 : :
55 : : // Move semantics
56 : : TcpClient(TcpClient&&) noexcept;
57 : : TcpClient& operator=(TcpClient&&) noexcept;
58 : :
59 : : // Disable copy
60 : : TcpClient(const TcpClient&) = delete;
61 : : TcpClient& operator=(const TcpClient&) = delete;
62 : :
63 : : // ChannelInterface implementation
64 : : [[nodiscard]] std::future<bool> start() override;
65 : : void stop() override;
66 : : bool send(std::string_view data) override;
67 : : bool send_line(std::string_view line) override;
68 : : bool send_blocking(std::string_view data) override;
69 : : bool send_line_blocking(std::string_view line) override;
70 : : bool try_send(std::string_view data) override;
71 : : bool try_send_line(std::string_view line) override;
72 : : bool send_move(std::vector<uint8_t>&& data) override;
73 : : bool try_send_move(std::vector<uint8_t>&& data) override;
74 : : bool send_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
75 : : bool try_send_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
76 : : bool connected() const override;
77 : : RuntimeStats stats() const override;
78 : : void reset_stats() override;
79 : :
80 : : TcpClient& on_data(MessageHandler handler) override;
81 : : TcpClient& on_data_batch(BatchMessageHandler handler) override;
82 : : TcpClient& on_connect(ConnectionHandler handler) override;
83 : : TcpClient& on_disconnect(ConnectionHandler handler) override;
84 : : TcpClient& on_error(ErrorHandler handler) override;
85 : : TcpClient& on_backpressure(std::function<void(size_t)> handler) override;
86 : :
87 : : TcpClient& framer(std::unique_ptr<framer::IFramer> framer) override;
88 : : TcpClient& on_message(MessageHandler handler) override;
89 : : TcpClient& on_message_batch(BatchMessageHandler handler) override;
90 : :
91 : : TcpClient& auto_start(bool manage = true) override;
92 : :
93 : : // Configuration
94 : : TcpClient& batch_size(size_t size);
95 : : TcpClient& batch_latency(std::chrono::milliseconds latency);
96 : : TcpClient& retry_interval(std::chrono::milliseconds interval);
97 : : TcpClient& max_retries(int max_retries);
98 : : /**
99 : : * @brief Connect over TLS, verifying the server's certificate.
100 : : *
101 : : * Verification is not optional: an unverified TLS connection encrypts traffic
102 : : * to whoever answered. With no argument the system trust store is used; pass
103 : : * a PEM to trust a private CA or a self-signed certificate instead. The host
104 : : * given to the constructor is the name the certificate must match.
105 : : *
106 : : * Must be set before start(). Requires a build with WIRESTEAD_ENABLE_TLS.
107 : : */
108 : : TcpClient& tls(const std::string& ca_file = "");
109 : :
110 : : TcpClient& connection_timeout(std::chrono::milliseconds timeout);
111 : : /**
112 : : * @brief Configure application-level idle timeout.
113 : : *
114 : : * A value of 0ms disables idle timeout. When enabled, inbound or outbound
115 : : * activity resets the timer.
116 : : */
117 : : TcpClient& idle_timeout(std::chrono::milliseconds timeout);
118 : : /**
119 : : * @brief Configure what happens when an enabled idle timeout expires.
120 : : *
121 : : * The default is IdleTimeoutAction::Reconnect. This setting has no effect
122 : : * while idle_timeout is 0ms.
123 : : */
124 : : TcpClient& idle_timeout_action(IdleTimeoutAction action);
125 : : TcpClient& backpressure_threshold(size_t threshold);
126 : : TcpClient& backpressure_strategy(base::constants::BackpressureStrategy strategy);
127 : :
128 : : /// Returns the configured backpressure threshold in bytes.
129 : : size_t backpressure_threshold() const;
130 : : /// Returns the configured backpressure strategy.
131 : : base::constants::BackpressureStrategy backpressure_strategy() const;
132 : : // Socket-option setters below have no live/runtime effect on an already-open
133 : : // socket: they stage a value that is only applied the next time start()
134 : : // builds a fresh connection (e.g. after stop() + start() again). Calling
135 : : // one of these while already connected is a deferred no-op until restart,
136 : : // not an immediate change (#436).
137 : : TcpClient& tcp_no_delay(bool enable = true);
138 : : TcpClient& keep_alive(bool enable = true);
139 : : TcpClient& send_buffer_size(size_t bytes);
140 : : TcpClient& receive_buffer_size(size_t bytes);
141 : :
142 : : /**
143 : : * @brief Size of the userspace buffer each read fills, in bytes.
144 : : *
145 : : * Distinct from receive_buffer_size(), which sets the kernel's SO_RCVBUF.
146 : : * Raising this reduces read completions and callback dispatches on bulk
147 : : * transfers, at the cost of that much memory per connection. Clamped to
148 : : * [MIN_READ_BUFFER_SIZE, MAX_READ_BUFFER_SIZE]; takes effect on the next
149 : : * start().
150 : : */
151 : : TcpClient& read_buffer_size(size_t bytes);
152 : : TcpClient& manage_external_context(bool manage);
153 : :
154 : : private:
155 : : struct Impl;
156 : 3064 : const Impl* get_impl() const { return impl_.get(); }
157 : : Impl* get_impl() { return impl_.get(); }
158 : : // #450: shared_ptr (not unique_ptr) so in-flight callbacks on an
159 : : // externally-owned io_context can extend Impl's lifetime for the
160 : : // duration of their invocation via weak_from_this(), rather than only
161 : : // checking a staleness flag that says nothing about whether Impl itself
162 : : // still exists.
163 : : std::shared_ptr<Impl> impl_;
164 : : };
165 : :
166 : : } // namespace wrapper
167 : : } // namespace wirestead
|