LCOV - code coverage report
Current view: top level - wirestead/wrapper/tcp_client - tcp_client.hpp (source / functions) Coverage Total Hit
Test: Wirestead Coverage Report Lines: 100.0 % 1 1
Test Date: 2026-10-29 11:36:05 Functions: 100.0 % 1 1
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

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

Generated by: LCOV version 2.0-1