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 local::stream_protocol::socket for testability.
34 : : * This is an internal interface used for dependency injection and mocking.
35 : : */
36 : : class WIRESTEAD_API UdsSocketInterface {
37 : : public:
38 : 73 : virtual ~UdsSocketInterface() = default;
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 async_connect(const net::local::stream_protocol::endpoint& endpoint,
57 : : std::function<void(const boost::system::error_code&)> handler) = 0;
58 : : virtual void shutdown(net::local::stream_protocol::socket::shutdown_type what, boost::system::error_code& ec) = 0;
59 : : virtual void close(boost::system::error_code& ec) = 0;
60 : : virtual net::local::stream_protocol::endpoint remote_endpoint(boost::system::error_code& ec) const = 0;
61 : : };
62 : :
63 : : // Flattens into one contiguous buffer and delegates. Copies, which is why a
64 : : // socket-backed implementation overrides this; kept here so test doubles and
65 : : // any not-yet-converted implementation stay correct for free. `flat` is
66 : : // owned by the completion lambda, so it outlives the delegated write.
67 : 14 : inline void UdsSocketInterface::async_write(
68 : : const std::vector<net::const_buffer>& buffers,
69 : : std::function<void(const boost::system::error_code&, std::size_t)> handler) {
70 [ + - ]: 14 : auto flat = std::make_shared<std::vector<unsigned char>>();
71 : 14 : std::size_t total = 0;
72 [ + + ]: 28 : for (const auto& b : buffers) total += b.size();
73 : 14 : flat->reserve(total);
74 [ + + ]: 28 : for (const auto& b : buffers) {
75 : 14 : const auto* p = static_cast<const unsigned char*>(b.data());
76 : 14 : flat->insert(flat->end(), p, p + b.size());
77 : : }
78 : 14 : async_write(net::const_buffer(flat->data(), flat->size()),
79 : 28 : [flat, handler = std::move(handler)](const boost::system::error_code& ec, std::size_t n) {
80 [ + - ]: 12 : if (handler) handler(ec, n);
81 : 12 : });
82 : 14 : }
83 : :
84 : : } // namespace interface
85 : : } // namespace wirestead
|