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 440x local_stream_socket::~local_stream_socket()
34 {
35 440x close();
36 440x }
37
38 378x local_stream_socket::local_stream_socket(capy::execution_context& ctx)
39 378x : io_object(handle(
40 ctx,
41 detail::use_backend_service<
42 detail::local_stream_service_of,
43 756x detail::local_stream_service>(ctx)))
44 {
45 378x }
46
47 std::error_code
48 53x local_stream_socket::open() noexcept
49 {
50 53x if (is_open())
51 ✗ return {};
52 53x return open_for_family(AF_UNIX, SOCK_STREAM, 0);
53 }
54
55 std::error_code
56 53x local_stream_socket::open_for_family(
57 int family, int type, int protocol) noexcept
58 {
59 53x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
60 53x std::error_code ec = svc.open_socket(
61 53x static_cast<local_stream_socket::implementation&>(*h_.get()), family,
62 type, protocol);
63 53x return ec;
64 }
65
66 void
67 461x local_stream_socket::close() noexcept
68 {
69 461x if (!is_open())
70 221x return;
71 240x h_.service().close(h_);
72 }
73
74 void
75 10x local_stream_socket::cancel() noexcept
76 {
77 10x if (!is_open())
78 2x return;
79 8x get().cancel();
80 }
81
82 std::error_code
83 6x local_stream_socket::shutdown(shutdown_type what) noexcept
84 {
85 6x if (!is_open())
86 2x return make_error_code(std::errc::bad_file_descriptor);
87 4x return get().shutdown(what);
88 }
89
90 std::error_code
91 191x local_stream_socket::assign(native_handle_type fd) noexcept
92 {
93 191x if (is_open())
94 8x return make_error_code(error::already_open);
95 183x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
96 183x std::error_code ec = svc.assign_socket(
97 183x static_cast<local_stream_socket::implementation&>(*h_.get()), fd);
98 183x return ec;
99 }
100
101 native_handle_type
102 31x local_stream_socket::native_handle() const noexcept
103 {
104 31x if (!is_open())
105 #if BOOST_COROSIO_HAS_IOCP
106 return ~native_handle_type(0);
107 #else
108 2x return -1;
109 #endif
110 29x return get().native_handle();
111 }
112
113 capy::results<native_handle_type>
114 8x local_stream_socket::release() noexcept
115 {
116 8x if (!is_open())
117 return {
118 4x make_error_code(std::errc::bad_file_descriptor),
119 2x detail::invalid_native_handle};
120 6x return {std::error_code{}, get().release_socket()};
121 }
122
123 capy::results<std::size_t>
124 11x local_stream_socket::available() const noexcept
125 {
126 11x if (!is_open())
127 2x 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 9x int value = 0;
138 9x if (::ioctl(native_handle(), FIONREAD, &value) < 0)
139 5x return {detail::make_err(errno), 0};
140 4x return {std::error_code{}, static_cast<std::size_t>(value)};
141 #endif
142 }
143
144 local_endpoint
145 6x local_stream_socket::local_endpoint() const noexcept
146 {
147 6x if (!is_open())
148 2x return corosio::local_endpoint{};
149 4x return get().local_endpoint();
150 }
151
152 local_endpoint
153 6x local_stream_socket::remote_endpoint() const noexcept
154 {
155 6x if (!is_open())
156 2x return corosio::local_endpoint{};
157 4x return get().remote_endpoint();
158 }
159
160 } // namespace boost::corosio
161
162 #endif // BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP
163