src/corosio/src/local_stream_socket.cpp

98.3% Lines (57 / 58) 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 #include <boost/corosio/native/detail/make_err.hpp>
18
19 #if BOOST_COROSIO_POSIX
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #elif BOOST_COROSIO_HAS_IOCP
23 #include <boost/corosio/native/detail/iocp/win_windows.hpp>
24 #ifndef AF_UNIX
25 #define AF_UNIX 1
26 #endif
27 #endif
28
29 namespace boost::corosio {
30
31 342x local_stream_socket::~local_stream_socket()
32 {
33 342x close();
34 342x }
35
36 284x local_stream_socket::local_stream_socket(capy::execution_context& ctx)
37 284x : io_object(create_handle<detail::local_stream_service>(ctx))
38 {
39 284x }
40
41 std::error_code
42 51x local_stream_socket::open() noexcept
43 {
44 51x if (is_open())
45 ✗ return {};
46 51x return open_for_family(AF_UNIX, SOCK_STREAM, 0);
47 }
48
49 std::error_code
50 51x local_stream_socket::open_for_family(
51 int family, int type, int protocol) noexcept
52 {
53 51x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
54 51x std::error_code ec = svc.open_socket(
55 51x static_cast<local_stream_socket::implementation&>(*h_.get()), family,
56 type, protocol);
57 51x return ec;
58 }
59
60 void
61 355x local_stream_socket::close() noexcept
62 {
63 355x if (!is_open())
64 133x return;
65 222x h_.service().close(h_);
66 }
67
68 void
69 8x local_stream_socket::cancel() noexcept
70 {
71 8x if (!is_open())
72 2x return;
73 6x get().cancel();
74 }
75
76 std::error_code
77 6x local_stream_socket::shutdown(shutdown_type what) noexcept
78 {
79 6x if (!is_open())
80 2x return make_error_code(std::errc::bad_file_descriptor);
81 4x return get().shutdown(what);
82 }
83
84 std::error_code
85 173x local_stream_socket::assign(native_handle_type fd) noexcept
86 {
87 173x auto& svc = static_cast<detail::local_stream_service&>(h_.service());
88 173x std::error_code ec = svc.assign_socket(
89 173x static_cast<local_stream_socket::implementation&>(*h_.get()), fd);
90 173x return ec;
91 }
92
93 native_handle_type
94 25x local_stream_socket::native_handle() const noexcept
95 {
96 25x if (!is_open())
97 #if BOOST_COROSIO_HAS_IOCP
98 return ~native_handle_type(0);
99 #else
100 2x return -1;
101 #endif
102 23x return get().native_handle();
103 }
104
105 native_handle_type
106 6x local_stream_socket::release()
107 {
108 6x if (!is_open())
109 2x detail::throw_system_error(
110 2x make_error_code(std::errc::bad_file_descriptor),
111 "local_stream_socket::release");
112 4x return get().release_socket();
113 }
114
115 std::size_t
116 11x local_stream_socket::available() const
117 {
118 11x if (!is_open())
119 2x detail::throw_system_error(
120 4x make_error_code(std::errc::bad_file_descriptor),
121 "local_stream_socket::available");
122 #if BOOST_COROSIO_HAS_IOCP
123 u_long value = 0;
124 if (::ioctlsocket(static_cast<SOCKET>(native_handle()), FIONREAD, &value) !=
125 0)
126 detail::throw_system_error(
127 detail::make_err(static_cast<unsigned long>(::WSAGetLastError())),
128 "local_stream_socket::available");
129 return static_cast<std::size_t>(value);
130 #else
131 9x int value = 0;
132 9x if (::ioctl(native_handle(), FIONREAD, &value) < 0)
133 5x detail::throw_system_error(
134 10x detail::make_err(errno), "local_stream_socket::available");
135 4x return static_cast<std::size_t>(value);
136 #endif
137 }
138
139 local_endpoint
140 6x local_stream_socket::local_endpoint() const noexcept
141 {
142 6x if (!is_open())
143 2x return corosio::local_endpoint{};
144 4x return get().local_endpoint();
145 }
146
147 local_endpoint
148 6x local_stream_socket::remote_endpoint() const noexcept
149 {
150 6x if (!is_open())
151 2x return corosio::local_endpoint{};
152 4x return get().remote_endpoint();
153 }
154
155 } // namespace boost::corosio
156
157 #endif // BOOST_COROSIO_POSIX || BOOST_COROSIO_HAS_IOCP
158