src/corosio/src/local_stream_socket.cpp

98.3% Lines (58 / 59) 100.0% Functions (13 / 13)
local_stream_socket.cpp
f(x) Functions (13)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Michael Vandeberg
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/corosio
8 //
9
10 #include <boost/corosio/detail/platform.hpp>
11
12 #if BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP
13
14 #include <boost/corosio/local_stream_socket.hpp>
15 #include <boost/corosio/error.hpp>
16 #include <boost/corosio/detail/local_stream_service.hpp>
17
18 #include "src/detail/use_backend_service.hpp"
19 #include <boost/corosio/native/detail/make_err.hpp>
20
21 #if BOOST_COROSIO_POSIX
22 #include <sys/ioctl.h>
23 #include <sys/socket.h>
24 #elif BOOST_COROSIO_HAS_IOCP
25 #include <boost/corosio/native/detail/iocp/win_windows.hpp>
26 #ifndef AF_UNIX
27 #define AF_UNIX 1
28 #endif
29 #endif
30
31 namespace boost::corosio {
32
33 724x local_stream_socket::~local_stream_socket()
34 {
35 724x close();
36 724x }
37
38 631x local_stream_socket::local_stream_socket(capy::execution_context& ctx)
39 631x : io_object(handle(
40 ctx,
41 detail::use_backend_service<
42 detail::local_stream_service_of,
43 1262x detail::local_stream_service>(ctx)))
44 {
45 631x }
46
47 std::error_code
48 83x local_stream_socket::open() noexcept
49 {
50 83x if (is_open())
51 ✗ return {};
52 83x return open_for_family(AF_UNIX, SOCK_STREAM, 0);
53 }
54
55 std::error_code
56 83x local_stream_socket::open_for_family(
57 int family, int type, int protocol) noexcept
58 {
59 83x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
60 83x std::error_code ec = svc.open_socket(
61 83x static_cast<local_stream_socket::implementation&>(*h_.get()), family,
62 type, protocol);
63 83x return ec;
64 }
65
66 void
67 807x local_stream_socket::close() noexcept
68 {
69 807x if (!is_open())
70 397x return;
71 410x h_.service().close(h_);
72 }
73
74 void
75 14x local_stream_socket::cancel() noexcept
76 {
77 14x if (!is_open())
78 3x return;
79 11x get().cancel();
80 }
81
82 std::error_code
83 10x local_stream_socket::shutdown(shutdown_type what) noexcept
84 {
85 10x if (!is_open())
86 3x return make_error_code(std::errc::bad_file_descriptor);
87 7x return get().shutdown(what);
88 }
89
90 std::error_code
91 336x local_stream_socket::assign(native_handle_type fd) noexcept
92 {
93 336x if (is_open())
94 12x return make_error_code(error::already_open);
95 324x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
96 324x std::error_code ec = svc.assign_socket(
97 324x static_cast<local_stream_socket::implementation&>(*h_.get()), fd);
98 324x return ec;
99 }
100
101 native_handle_type
102 48x local_stream_socket::native_handle() const noexcept
103 {
104 48x if (!is_open())
105 #if BOOST_COROSIO_HAS_IOCP
106 return ~native_handle_type(0);
107 #else
108 3x return -1;
109 #endif
110 45x return get().native_handle();
111 }
112
113 capy::results<native_handle_type>
114 12x local_stream_socket::release() noexcept
115 {
116 12x if (!is_open())
117 return {
118 6x make_error_code(std::errc::bad_file_descriptor),
119 3x detail::invalid_native_handle};
120 9x return {std::error_code{}, get().release_socket()};
121 }
122
123 capy::results<std::size_t>
124 18x local_stream_socket::available() const noexcept
125 {
126 18x if (!is_open())
127 3x return {make_error_code(std::errc::bad_file_descriptor), 0};
128 #if BOOST_COROSIO_HAS_IOCP
129 u_long value = 0;
130 if (::ioctlsocket(static_cast<SOCKET>(native_handle()), FIONREAD, &value) !=
131 0)
132 return {
133 detail::make_err(static_cast<unsigned long>(::WSAGetLastError())),
134 0};
135 return {std::error_code{}, static_cast<std::size_t>(value)};
136 #else
137 15x int value = 0;
138 15x if (::ioctl(native_handle(), FIONREAD, &value) < 0)
139 9x return {detail::make_err(errno), 0};
140 6x return {std::error_code{}, static_cast<std::size_t>(value)};
141 #endif
142 }
143
144 local_endpoint
145 9x local_stream_socket::local_endpoint() const noexcept
146 {
147 9x if (!is_open())
148 3x return corosio::local_endpoint{};
149 6x return get().local_endpoint();
150 }
151
152 local_endpoint
153 9x local_stream_socket::remote_endpoint() const noexcept
154 {
155 9x if (!is_open())
156 3x return corosio::local_endpoint{};
157 6x return get().remote_endpoint();
158 }
159
160 } // namespace boost::corosio
161
162 #endif // BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP
163