]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | // |
2 | // detail/io_uring_socket_service_base.ipp | |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2022 Christopher M. Kohlhoff (chris at kohlhoff dot com) | |
6 | // | |
7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
9 | // | |
10 | ||
11 | #ifndef BOOST_ASIO_DETAIL_IMPL_IO_URING_SOCKET_SERVICE_BASE_IPP | |
12 | #define BOOST_ASIO_DETAIL_IMPL_IO_URING_SOCKET_SERVICE_BASE_IPP | |
13 | ||
14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200) | |
15 | # pragma once | |
16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) | |
17 | ||
18 | #include <boost/asio/detail/config.hpp> | |
19 | ||
20 | #if defined(BOOST_ASIO_HAS_IO_URING) | |
21 | ||
22 | #include <boost/asio/detail/io_uring_socket_service_base.hpp> | |
23 | ||
24 | #include <boost/asio/detail/push_options.hpp> | |
25 | ||
26 | namespace boost { | |
27 | namespace asio { | |
28 | namespace detail { | |
29 | ||
30 | io_uring_socket_service_base::io_uring_socket_service_base( | |
31 | execution_context& context) | |
32 | : io_uring_service_(boost::asio::use_service<io_uring_service>(context)) | |
33 | { | |
34 | io_uring_service_.init_task(); | |
35 | } | |
36 | ||
37 | void io_uring_socket_service_base::base_shutdown() | |
38 | { | |
39 | } | |
40 | ||
41 | void io_uring_socket_service_base::construct( | |
42 | io_uring_socket_service_base::base_implementation_type& impl) | |
43 | { | |
44 | impl.socket_ = invalid_socket; | |
45 | impl.state_ = 0; | |
46 | impl.io_object_data_ = 0; | |
47 | } | |
48 | ||
49 | void io_uring_socket_service_base::base_move_construct( | |
50 | io_uring_socket_service_base::base_implementation_type& impl, | |
51 | io_uring_socket_service_base::base_implementation_type& other_impl) | |
52 | BOOST_ASIO_NOEXCEPT | |
53 | { | |
54 | impl.socket_ = other_impl.socket_; | |
55 | other_impl.socket_ = invalid_socket; | |
56 | ||
57 | impl.state_ = other_impl.state_; | |
58 | other_impl.state_ = 0; | |
59 | ||
60 | impl.io_object_data_ = other_impl.io_object_data_; | |
61 | other_impl.io_object_data_ = 0; | |
62 | } | |
63 | ||
64 | void io_uring_socket_service_base::base_move_assign( | |
65 | io_uring_socket_service_base::base_implementation_type& impl, | |
66 | io_uring_socket_service_base& /*other_service*/, | |
67 | io_uring_socket_service_base::base_implementation_type& other_impl) | |
68 | { | |
69 | destroy(impl); | |
70 | ||
71 | impl.socket_ = other_impl.socket_; | |
72 | other_impl.socket_ = invalid_socket; | |
73 | ||
74 | impl.state_ = other_impl.state_; | |
75 | other_impl.state_ = 0; | |
76 | ||
77 | impl.io_object_data_ = other_impl.io_object_data_; | |
78 | other_impl.io_object_data_ = 0; | |
79 | } | |
80 | ||
81 | void io_uring_socket_service_base::destroy( | |
82 | io_uring_socket_service_base::base_implementation_type& impl) | |
83 | { | |
84 | if (impl.socket_ != invalid_socket) | |
85 | { | |
86 | BOOST_ASIO_HANDLER_OPERATION((io_uring_service_.context(), | |
87 | "socket", &impl, impl.socket_, "close")); | |
88 | ||
89 | io_uring_service_.deregister_io_object(impl.io_object_data_); | |
90 | boost::system::error_code ignored_ec; | |
91 | socket_ops::close(impl.socket_, impl.state_, true, ignored_ec); | |
92 | io_uring_service_.cleanup_io_object(impl.io_object_data_); | |
93 | } | |
94 | } | |
95 | ||
96 | boost::system::error_code io_uring_socket_service_base::close( | |
97 | io_uring_socket_service_base::base_implementation_type& impl, | |
98 | boost::system::error_code& ec) | |
99 | { | |
100 | if (is_open(impl)) | |
101 | { | |
102 | BOOST_ASIO_HANDLER_OPERATION((io_uring_service_.context(), | |
103 | "socket", &impl, impl.socket_, "close")); | |
104 | ||
105 | io_uring_service_.deregister_io_object(impl.io_object_data_); | |
106 | socket_ops::close(impl.socket_, impl.state_, false, ec); | |
107 | io_uring_service_.cleanup_io_object(impl.io_object_data_); | |
108 | } | |
109 | else | |
110 | { | |
111 | ec = success_ec_; | |
112 | } | |
113 | ||
114 | // The descriptor is closed by the OS even if close() returns an error. | |
115 | // | |
116 | // (Actually, POSIX says the state of the descriptor is unspecified. On | |
117 | // Linux the descriptor is apparently closed anyway; e.g. see | |
118 | // http://lkml.org/lkml/2005/9/10/129 | |
119 | construct(impl); | |
120 | ||
121 | return ec; | |
122 | } | |
123 | ||
124 | socket_type io_uring_socket_service_base::release( | |
125 | io_uring_socket_service_base::base_implementation_type& impl, | |
126 | boost::system::error_code& ec) | |
127 | { | |
128 | if (!is_open(impl)) | |
129 | { | |
130 | ec = boost::asio::error::bad_descriptor; | |
131 | return invalid_socket; | |
132 | } | |
133 | ||
134 | BOOST_ASIO_HANDLER_OPERATION((io_uring_service_.context(), | |
135 | "socket", &impl, impl.socket_, "release")); | |
136 | ||
137 | io_uring_service_.deregister_io_object(impl.io_object_data_); | |
138 | io_uring_service_.cleanup_io_object(impl.io_object_data_); | |
139 | socket_type sock = impl.socket_; | |
140 | construct(impl); | |
141 | ec = success_ec_; | |
142 | return sock; | |
143 | } | |
144 | ||
145 | boost::system::error_code io_uring_socket_service_base::cancel( | |
146 | io_uring_socket_service_base::base_implementation_type& impl, | |
147 | boost::system::error_code& ec) | |
148 | { | |
149 | if (!is_open(impl)) | |
150 | { | |
151 | ec = boost::asio::error::bad_descriptor; | |
152 | return ec; | |
153 | } | |
154 | ||
155 | BOOST_ASIO_HANDLER_OPERATION((io_uring_service_.context(), | |
156 | "socket", &impl, impl.socket_, "cancel")); | |
157 | ||
158 | io_uring_service_.cancel_ops(impl.io_object_data_); | |
159 | ec = success_ec_; | |
160 | return ec; | |
161 | } | |
162 | ||
163 | boost::system::error_code io_uring_socket_service_base::do_open( | |
164 | io_uring_socket_service_base::base_implementation_type& impl, | |
165 | int af, int type, int protocol, boost::system::error_code& ec) | |
166 | { | |
167 | if (is_open(impl)) | |
168 | { | |
169 | ec = boost::asio::error::already_open; | |
170 | return ec; | |
171 | } | |
172 | ||
173 | socket_holder sock(socket_ops::socket(af, type, protocol, ec)); | |
174 | if (sock.get() == invalid_socket) | |
175 | return ec; | |
176 | ||
177 | io_uring_service_.register_io_object(impl.io_object_data_); | |
178 | ||
179 | impl.socket_ = sock.release(); | |
180 | switch (type) | |
181 | { | |
182 | case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break; | |
183 | case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break; | |
184 | default: impl.state_ = 0; break; | |
185 | } | |
186 | ec = success_ec_; | |
187 | return ec; | |
188 | } | |
189 | ||
190 | boost::system::error_code io_uring_socket_service_base::do_assign( | |
191 | io_uring_socket_service_base::base_implementation_type& impl, int type, | |
192 | const io_uring_socket_service_base::native_handle_type& native_socket, | |
193 | boost::system::error_code& ec) | |
194 | { | |
195 | if (is_open(impl)) | |
196 | { | |
197 | ec = boost::asio::error::already_open; | |
198 | return ec; | |
199 | } | |
200 | ||
201 | io_uring_service_.register_io_object(impl.io_object_data_); | |
202 | ||
203 | impl.socket_ = native_socket; | |
204 | switch (type) | |
205 | { | |
206 | case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break; | |
207 | case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break; | |
208 | default: impl.state_ = 0; break; | |
209 | } | |
210 | impl.state_ |= socket_ops::possible_dup; | |
211 | ec = success_ec_; | |
212 | return ec; | |
213 | } | |
214 | ||
215 | void io_uring_socket_service_base::start_op( | |
216 | io_uring_socket_service_base::base_implementation_type& impl, | |
217 | int op_type, io_uring_operation* op, bool is_continuation, bool noop) | |
218 | { | |
219 | if (!noop) | |
220 | { | |
221 | io_uring_service_.start_op(op_type, | |
222 | impl.io_object_data_, op, is_continuation); | |
223 | } | |
224 | else | |
225 | { | |
226 | io_uring_service_.post_immediate_completion(op, is_continuation); | |
227 | } | |
228 | } | |
229 | ||
230 | void io_uring_socket_service_base::start_accept_op( | |
231 | io_uring_socket_service_base::base_implementation_type& impl, | |
232 | io_uring_operation* op, bool is_continuation, bool peer_is_open) | |
233 | { | |
234 | if (!peer_is_open) | |
235 | start_op(impl, io_uring_service::read_op, op, is_continuation, false); | |
236 | else | |
237 | { | |
238 | op->ec_ = boost::asio::error::already_open; | |
239 | io_uring_service_.post_immediate_completion(op, is_continuation); | |
240 | } | |
241 | } | |
242 | ||
243 | } // namespace detail | |
244 | } // namespace asio | |
245 | } // namespace boost | |
246 | ||
247 | #include <boost/asio/detail/pop_options.hpp> | |
248 | ||
249 | #endif // defined(BOOST_ASIO_HAS_IO_URING) | |
250 | ||
251 | #endif // BOOST_ASIO_DETAIL_IMPL_IO_URING_SOCKET_SERVICE_BASE_IPP |