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 <memory>
21 : :
22 : : #include "wirestead/base/platform.hpp"
23 : : #include "wirestead/base/visibility.hpp"
24 : : #include "wirestead/interface/iuds_socket.hpp"
25 : :
26 : : namespace wirestead {
27 : : namespace transport {
28 : :
29 : : namespace net = boost::asio;
30 : : using uds = net::local::stream_protocol;
31 : :
32 : : /**
33 : : * @brief Boost.Asio implementation of UdsSocketInterface interface.
34 : : */
35 : : class WIRESTEAD_API BoostUdsSocket : public interface::UdsSocketInterface {
36 : : public:
37 : : explicit BoostUdsSocket(uds::socket sock);
38 : 82 : ~BoostUdsSocket() override = default;
39 : :
40 : : void async_read_some(const net::mutable_buffer& buffer,
41 : : std::function<void(const boost::system::error_code&, std::size_t)> handler) override;
42 : : void async_write(const net::const_buffer& buffer,
43 : : std::function<void(const boost::system::error_code&, std::size_t)> handler) override;
44 : :
45 : : // Real scatter-gather write: hands the sequence to asio, which issues one
46 : : // sendmsg/writev instead of one send per queued buffer.
47 : 31 : void async_write(const std::vector<net::const_buffer>& buffers,
48 : : std::function<void(const boost::system::error_code&, std::size_t)> handler) override {
49 : 31 : net::async_write(socket_, buffers, std::move(handler));
50 : 31 : }
51 : : void async_connect(const uds::endpoint& endpoint,
52 : : std::function<void(const boost::system::error_code&)> handler) override;
53 : : void shutdown(uds::socket::shutdown_type what, boost::system::error_code& ec) override;
54 : : void close(boost::system::error_code& ec) override;
55 : : uds::endpoint remote_endpoint(boost::system::error_code& ec) const override;
56 : :
57 : : private:
58 : : uds::socket socket_;
59 : : };
60 : :
61 : : } // namespace transport
62 : : } // namespace wirestead
|