LCOV - code coverage report
Current view: top level - wirestead/transport/tcp_server - tcp_server.hpp (source / functions) Coverage Total Hit
Test: Wirestead Coverage Report Lines: 100.0 % 2 2
Test Date: 2026-08-30 10:35:09 Functions: 100.0 % 2 2
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 <memory>
      20                 :             : #include <optional>
      21                 :             : #include <string>
      22                 :             : #include <string_view>
      23                 :             : #include <vector>
      24                 :             : 
      25                 :             : #include "wirestead/base/visibility.hpp"
      26                 :             : #include "wirestead/config/tcp_server_config.hpp"
      27                 :             : #include "wirestead/diagnostics/error_types.hpp"
      28                 :             : #include "wirestead/interface/channel.hpp"
      29                 :             : 
      30                 :             : namespace boost {
      31                 :             : namespace asio {
      32                 :             : class io_context;
      33                 :             : }
      34                 :             : }  // namespace boost
      35                 :             : 
      36                 :             : namespace wirestead {
      37                 :             : 
      38                 :             : namespace interface {
      39                 :             : class TcpAcceptorInterface;
      40                 :             : }
      41                 :             : 
      42                 :             : namespace transport {
      43                 :             : 
      44                 :             : /**
      45                 :             :  * @brief Thread-safe TCP Server implementation
      46                 :             :  */
      47                 :             : class WIRESTEAD_API TcpServer : public interface::Channel, public std::enable_shared_from_this<TcpServer> {
      48                 :             :  public:
      49                 :             :   // use_shared_context: opt into the shared IoContextManager singleton
      50                 :             :   // instead of the default dedicated io_context + thread. Only meaningful
      51                 :             :   // for multi-server-per-process deployments deliberately trading
      52                 :             :   // parallelism for reduced thread/memory overhead (#440); most callers
      53                 :             :   // should leave this false.
      54                 :             :   static std::shared_ptr<TcpServer> create(const config::TcpServerConfig& cfg, bool use_shared_context = false);
      55                 :             :   static std::shared_ptr<TcpServer> create(const config::TcpServerConfig& cfg,
      56                 :             :                                            std::unique_ptr<interface::TcpAcceptorInterface> acceptor,
      57                 :             :                                            boost::asio::io_context& ioc);
      58                 :             :   ~TcpServer() override;
      59                 :             : 
      60                 :             :   // Move semantics
      61                 :             :   TcpServer(TcpServer&&) noexcept;
      62                 :             :   TcpServer& operator=(TcpServer&&) noexcept;
      63                 :             : 
      64                 :             :   // Non-copyable
      65                 :             :   TcpServer(const TcpServer&) = delete;
      66                 :             :   TcpServer& operator=(const TcpServer&) = delete;
      67                 :             : 
      68                 :             :   // Channel implementation
      69                 :             :   void start() override;
      70                 :             :   void stop() override;
      71                 :             :   bool is_connected() const override;
      72                 :             :   bool is_backpressure_active() const override;
      73                 :             :   bool is_backpressure_active(ClientId client_id) const;
      74                 :             :   boost::asio::any_io_executor get_executor() override;
      75                 :             :   bool async_write_copy(memory::ConstByteSpan data) override;
      76                 :             :   bool async_write_move(std::vector<uint8_t>&& data) override;
      77                 :             :   bool async_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
      78                 :             :   bool async_try_write_copy(memory::ConstByteSpan data) override;
      79                 :             :   bool async_try_write_move(std::vector<uint8_t>&& data) override;
      80                 :             :   bool async_try_write_shared(std::shared_ptr<const std::vector<uint8_t>> data) override;
      81                 :             :   void on_bytes(OnBytes cb) override;
      82                 :             :   void on_state(OnState cb) override;
      83                 :             :   void on_backpressure(OnBackpressure cb) override;
      84                 :             :   wrapper::RuntimeStats stats() const override;
      85                 :             :   void reset_stats() override;
      86                 :             :   std::optional<diagnostics::ErrorInfo> last_error_info() const override;
      87                 :             : 
      88                 :             :   // Multi-client support
      89                 :             :   bool broadcast(std::string_view message);
      90                 :             :   bool broadcast(memory::ConstByteSpan data);
      91                 :             :   bool send_to_client(ClientId client_id, std::string_view message);
      92                 :             :   bool send_to_client(ClientId client_id, memory::ConstByteSpan data);
      93                 :             :   bool try_send_to_client(ClientId client_id, std::string_view message);
      94                 :             :   bool try_send_to_client(ClientId client_id, memory::ConstByteSpan data);
      95                 :             :   size_t client_count() const;
      96                 :             :   std::vector<ClientId> connected_clients() const;
      97                 :             :   std::optional<wrapper::RuntimeStats> client_stats(ClientId client_id) const;
      98                 :             : 
      99                 :             :   void request_stop();
     100                 :             : 
     101                 :             :   using MultiClientConnectHandler = std::function<void(ClientId client_id, const std::string& client_info)>;
     102                 :             :   using MultiClientDataHandler = std::function<void(ClientId client_id, memory::ConstByteSpan data)>;
     103                 :             :   using MultiClientDisconnectHandler = std::function<void(ClientId client_id)>;
     104                 :             : 
     105                 :             :   void on_multi_connect(MultiClientConnectHandler handler);
     106                 :             :   void on_multi_data(MultiClientDataHandler handler);
     107                 :             :   void on_multi_disconnect(MultiClientDisconnectHandler handler);
     108                 :             : 
     109                 :             :   void set_client_limit(size_t max_clients);
     110                 :             : 
     111                 :             :   base::LinkState state() const;
     112                 :             : 
     113                 :             :  private:
     114                 :             :   explicit TcpServer(const config::TcpServerConfig& cfg, bool use_shared_context);
     115                 :             :   TcpServer(const config::TcpServerConfig& cfg, std::unique_ptr<interface::TcpAcceptorInterface> acceptor,
     116                 :             :             boost::asio::io_context& ioc);
     117                 :             : 
     118                 :             :   struct Impl;
     119                 :        2056 :   const Impl* get_impl() const { return impl_.get(); }
     120                 :        6662 :   Impl* get_impl() { return impl_.get(); }
     121                 :             :   std::unique_ptr<Impl> impl_;
     122                 :             : };
     123                 :             : }  // namespace transport
     124                 :             : }  // namespace wirestead
        

Generated by: LCOV version 2.0-1