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/detail/except.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 344x local_stream_socket::~local_stream_socket()
34 {
35 344x close();
36 344x }
37
38 286x local_stream_socket::local_stream_socket(capy::execution_context& ctx)
39 286x : io_object(handle(
40 ctx,
41 detail::use_backend_service<
42 detail::local_stream_service_of,
43 572x detail::local_stream_service>(ctx)))
44 {
45 286x }
46
47 std::error_code
48 51x local_stream_socket::open() noexcept
49 {
50 51x if (is_open())
51 ✗ return {};
52 51x return open_for_family(AF_UNIX, SOCK_STREAM, 0);
53 }
54
55 std::error_code
56 51x local_stream_socket::open_for_family(
57 int family, int type, int protocol) noexcept
58 {
59 51x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
60 51x std::error_code ec = svc.open_socket(
61 51x static_cast<local_stream_socket::implementation&>(*h_.get()), family,
62 type, protocol);
63 51x return ec;
64 }
65
66 void
67 357x local_stream_socket::close() noexcept
68 {
69 357x if (!is_open())
70 135x return;
71 222x h_.service().close(h_);
72 }
73
74 void
75 8x local_stream_socket::cancel() noexcept
76 {
77 8x if (!is_open())
78 2x return;
79 6x 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 173x local_stream_socket::assign(native_handle_type fd) noexcept
92 {
93 173x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
94 173x std::error_code ec = svc.assign_socket(
95 173x static_cast<local_stream_socket::implementation&>(*h_.get()), fd);
96 173x return ec;
97 }
98
99 native_handle_type
100 25x local_stream_socket::native_handle() const noexcept
101 {
102 25x if (!is_open())
103 #if BOOST_COROSIO_HAS_IOCP
104 return ~native_handle_type(0);
105 #else
106 2x return -1;
107 #endif
108 23x return get().native_handle();
109 }
110
111 native_handle_type
112 6x local_stream_socket::release()
113 {
114 6x if (!is_open())
115 2x detail::throw_system_error(
116 2x make_error_code(std::errc::bad_file_descriptor),
117 "local_stream_socket::release");
118 4x return get().release_socket();
119 }
120
121 std::size_t
122 11x local_stream_socket::available() const
123 {
124 11x if (!is_open())
125 2x detail::throw_system_error(
126 4x make_error_code(std::errc::bad_file_descriptor),
127 "local_stream_socket::available");
128 #if BOOST_COROSIO_HAS_IOCP
129 u_long value = 0;
130 if (::ioctlsocket(static_cast<SOCKET>(native_handle()), FIONREAD, &value) !=
131 0)
132 detail::throw_system_error(
133 detail::make_err(static_cast<unsigned long>(::WSAGetLastError())),
134 "local_stream_socket::available");
135 return static_cast<std::size_t>(value);
136 #else
137 9x int value = 0;
138 9x if (::ioctl(native_handle(), FIONREAD, &value) < 0)
139 5x detail::throw_system_error(
140 10x detail::make_err(errno), "local_stream_socket::available");
141 4x return static_cast<std::size_t>(value);
142 #endif
143 }
144
145 local_endpoint
146 6x local_stream_socket::local_endpoint() const noexcept
147 {
148 6x if (!is_open())
149 2x return corosio::local_endpoint{};
150 4x return get().local_endpoint();
151 }
152
153 local_endpoint
154 6x local_stream_socket::remote_endpoint() const noexcept
155 {
156 6x if (!is_open())
157 2x return corosio::local_endpoint{};
158 4x return get().remote_endpoint();
159 }
160
161 } // namespace boost::corosio
162
163 #endif // BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP
164