LCOV - code coverage report
Current view: top level - wirestead/interface - itcp_socket.hpp (source / functions) Coverage Total Hit
Test: Wirestead Coverage Report Lines: 100.0 % 16 16
Test Date: 2026-08-30 10:35:09 Functions: 100.0 % 3 3
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 66.7 % 12 8

             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 <boost/asio.hpp>
      20                 :             : #include <functional>
      21                 :             : #include <memory>
      22                 :             : #include <vector>
      23                 :             : 
      24                 :             : #include "wirestead/base/platform.hpp"
      25                 :             : #include "wirestead/base/visibility.hpp"
      26                 :             : 
      27                 :             : namespace wirestead {
      28                 :             : namespace interface {
      29                 :             : 
      30                 :             : namespace net = boost::asio;
      31                 :             : 
      32                 :             : /**
      33                 :             :  * @brief An interface abstracting Boost.Asio's tcp::socket for testability.
      34                 :             :  * This is an internal interface used for dependency injection and mocking.
      35                 :             :  */
      36                 :             : class WIRESTEAD_API TcpSocketInterface {
      37                 :             :  public:
      38                 :             :   virtual ~TcpSocketInterface();
      39                 :             : 
      40                 :             :   virtual void async_read_some(const net::mutable_buffer& buffer,
      41                 :             :                                std::function<void(const boost::system::error_code&, std::size_t)> handler) = 0;
      42                 :             :   virtual void async_write(const net::const_buffer& buffer,
      43                 :             :                            std::function<void(const boost::system::error_code&, std::size_t)> handler) = 0;
      44                 :             : 
      45                 :             :   // Scatter-gather write: sends every buffer in `buffers` as one operation,
      46                 :             :   // completing once with the total byte count or the first error. Draining
      47                 :             :   // several queued messages this way turns N send syscalls into one.
      48                 :             :   //
      49                 :             :   // The memory the buffers point at must stay valid until the handler runs.
      50                 :             :   //
      51                 :             :   // The default flattens into one buffer and delegates to the single-buffer
      52                 :             :   // overload above: correct, but it copies, so implementations backed by a real
      53                 :             :   // socket override it. Test doubles can rely on the default.
      54                 :             :   virtual void async_write(const std::vector<net::const_buffer>& buffers,
      55                 :             :                            std::function<void(const boost::system::error_code&, std::size_t)> handler);
      56                 :             :   virtual void shutdown(net::ip::tcp::socket::shutdown_type what, boost::system::error_code& ec) = 0;
      57                 :             :   virtual void close(boost::system::error_code& ec) = 0;
      58                 :             :   virtual net::ip::tcp::endpoint remote_endpoint(boost::system::error_code& ec) const = 0;
      59                 :             : 
      60                 :             :   // Runs whatever has to happen after accept and before the first read. A plain
      61                 :             :   // socket has nothing to do, so the default reports success and the caller
      62                 :             :   // proceeds exactly as it did before this existed; a TLS socket performs the
      63                 :             :   // handshake here. Failure is the session's cue to close rather than read.
      64                 :             :   //
      65                 :             :   // The handler runs on the caller's executor, not necessarily inline - bind it
      66                 :             :   // to the session strand the same way a read completion is bound.
      67                 :             :   virtual void async_handshake(std::function<void(const boost::system::error_code&)> handler);
      68                 :             : };
      69                 :             : 
      70                 :             : // Nothing to negotiate on a plain socket. Posting rather than calling inline
      71                 :             : // would need an executor this interface does not carry, and every caller
      72                 :             : // already dispatches onto its own strand before getting here.
      73                 :         132 : inline void TcpSocketInterface::async_handshake(std::function<void(const boost::system::error_code&)> handler) {
      74   [ +  -  +  - ]:         132 :   if (handler) handler(boost::system::error_code());
      75                 :         132 : }
      76                 :             : 
      77                 :             : // Flattens into one contiguous buffer and delegates. Copies, which is why a
      78                 :             : // socket-backed implementation overrides this; kept here so test doubles and
      79                 :             : // any not-yet-converted implementation stay correct for free. `flat` is
      80                 :             : // owned by the completion lambda, so it outlives the delegated write.
      81                 :          17 : inline void TcpSocketInterface::async_write(
      82                 :             :     const std::vector<net::const_buffer>& buffers,
      83                 :             :     std::function<void(const boost::system::error_code&, std::size_t)> handler) {
      84         [ +  - ]:          17 :   auto flat = std::make_shared<std::vector<unsigned char>>();
      85                 :          17 :   std::size_t total = 0;
      86         [ +  + ]:          34 :   for (const auto& b : buffers) total += b.size();
      87                 :          17 :   flat->reserve(total);
      88         [ +  + ]:          34 :   for (const auto& b : buffers) {
      89                 :          17 :     const auto* p = static_cast<const unsigned char*>(b.data());
      90                 :          17 :     flat->insert(flat->end(), p, p + b.size());
      91                 :             :   }
      92                 :          17 :   async_write(net::const_buffer(flat->data(), flat->size()),
      93                 :          34 :               [flat, handler = std::move(handler)](const boost::system::error_code& ec, std::size_t n) {
      94         [ +  - ]:           9 :                 if (handler) handler(ec, n);
      95                 :           9 :               });
      96                 :          17 : }
      97                 :             : 
      98                 :             : }  // namespace interface
      99                 :             : }  // namespace wirestead
        

Generated by: LCOV version 2.0-1