]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/win_iocp_socket_service.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / detail / win_iocp_socket_service.hpp
1 //
2 // detail/win_iocp_socket_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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_WIN_IOCP_SOCKET_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP
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_IOCP)
21
22 #include <cstring>
23 #include <boost/asio/error.hpp>
24 #include <boost/asio/io_context.hpp>
25 #include <boost/asio/socket_base.hpp>
26 #include <boost/asio/detail/bind_handler.hpp>
27 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
28 #include <boost/asio/detail/fenced_block.hpp>
29 #include <boost/asio/detail/handler_alloc_helpers.hpp>
30 #include <boost/asio/detail/handler_invoke_helpers.hpp>
31 #include <boost/asio/detail/memory.hpp>
32 #include <boost/asio/detail/mutex.hpp>
33 #include <boost/asio/detail/operation.hpp>
34 #include <boost/asio/detail/reactor_op.hpp>
35 #include <boost/asio/detail/select_reactor.hpp>
36 #include <boost/asio/detail/socket_holder.hpp>
37 #include <boost/asio/detail/socket_ops.hpp>
38 #include <boost/asio/detail/socket_types.hpp>
39 #include <boost/asio/detail/win_iocp_io_context.hpp>
40 #include <boost/asio/detail/win_iocp_null_buffers_op.hpp>
41 #include <boost/asio/detail/win_iocp_socket_accept_op.hpp>
42 #include <boost/asio/detail/win_iocp_socket_connect_op.hpp>
43 #include <boost/asio/detail/win_iocp_socket_recvfrom_op.hpp>
44 #include <boost/asio/detail/win_iocp_socket_send_op.hpp>
45 #include <boost/asio/detail/win_iocp_socket_service_base.hpp>
46
47 #include <boost/asio/detail/push_options.hpp>
48
49 namespace boost {
50 namespace asio {
51 namespace detail {
52
53 template <typename Protocol>
54 class win_iocp_socket_service :
55 public service_base<win_iocp_socket_service<Protocol> >,
56 public win_iocp_socket_service_base
57 {
58 public:
59 // The protocol type.
60 typedef Protocol protocol_type;
61
62 // The endpoint type.
63 typedef typename Protocol::endpoint endpoint_type;
64
65 // The native type of a socket.
66 class native_handle_type
67 {
68 public:
69 native_handle_type(socket_type s)
70 : socket_(s),
71 have_remote_endpoint_(false)
72 {
73 }
74
75 native_handle_type(socket_type s, const endpoint_type& ep)
76 : socket_(s),
77 have_remote_endpoint_(true),
78 remote_endpoint_(ep)
79 {
80 }
81
82 void operator=(socket_type s)
83 {
84 socket_ = s;
85 have_remote_endpoint_ = false;
86 remote_endpoint_ = endpoint_type();
87 }
88
89 operator socket_type() const
90 {
91 return socket_;
92 }
93
94 bool have_remote_endpoint() const
95 {
96 return have_remote_endpoint_;
97 }
98
99 endpoint_type remote_endpoint() const
100 {
101 return remote_endpoint_;
102 }
103
104 private:
105 socket_type socket_;
106 bool have_remote_endpoint_;
107 endpoint_type remote_endpoint_;
108 };
109
110 // The implementation type of the socket.
111 struct implementation_type :
112 win_iocp_socket_service_base::base_implementation_type
113 {
114 // Default constructor.
115 implementation_type()
116 : protocol_(endpoint_type().protocol()),
117 have_remote_endpoint_(false),
118 remote_endpoint_()
119 {
120 }
121
122 // The protocol associated with the socket.
123 protocol_type protocol_;
124
125 // Whether we have a cached remote endpoint.
126 bool have_remote_endpoint_;
127
128 // A cached remote endpoint.
129 endpoint_type remote_endpoint_;
130 };
131
132 // Constructor.
133 win_iocp_socket_service(boost::asio::io_context& io_context)
134 : service_base<win_iocp_socket_service<Protocol> >(io_context),
135 win_iocp_socket_service_base(io_context)
136 {
137 }
138
139 // Destroy all user-defined handler objects owned by the service.
140 void shutdown()
141 {
142 this->base_shutdown();
143 }
144
145 // Move-construct a new socket implementation.
146 void move_construct(implementation_type& impl,
147 implementation_type& other_impl)
148 {
149 this->base_move_construct(impl, other_impl);
150
151 impl.protocol_ = other_impl.protocol_;
152 other_impl.protocol_ = endpoint_type().protocol();
153
154 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
155 other_impl.have_remote_endpoint_ = false;
156
157 impl.remote_endpoint_ = other_impl.remote_endpoint_;
158 other_impl.remote_endpoint_ = endpoint_type();
159 }
160
161 // Move-assign from another socket implementation.
162 void move_assign(implementation_type& impl,
163 win_iocp_socket_service_base& other_service,
164 implementation_type& other_impl)
165 {
166 this->base_move_assign(impl, other_service, other_impl);
167
168 impl.protocol_ = other_impl.protocol_;
169 other_impl.protocol_ = endpoint_type().protocol();
170
171 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
172 other_impl.have_remote_endpoint_ = false;
173
174 impl.remote_endpoint_ = other_impl.remote_endpoint_;
175 other_impl.remote_endpoint_ = endpoint_type();
176 }
177
178 // Move-construct a new socket implementation from another protocol type.
179 template <typename Protocol1>
180 void converting_move_construct(implementation_type& impl,
181 win_iocp_socket_service<Protocol1>&,
182 typename win_iocp_socket_service<
183 Protocol1>::implementation_type& other_impl)
184 {
185 this->base_move_construct(impl, other_impl);
186
187 impl.protocol_ = protocol_type(other_impl.protocol_);
188 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
189
190 impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_;
191 other_impl.have_remote_endpoint_ = false;
192
193 impl.remote_endpoint_ = other_impl.remote_endpoint_;
194 other_impl.remote_endpoint_ = typename Protocol1::endpoint();
195 }
196
197 // Open a new socket implementation.
198 boost::system::error_code open(implementation_type& impl,
199 const protocol_type& protocol, boost::system::error_code& ec)
200 {
201 if (!do_open(impl, protocol.family(),
202 protocol.type(), protocol.protocol(), ec))
203 {
204 impl.protocol_ = protocol;
205 impl.have_remote_endpoint_ = false;
206 impl.remote_endpoint_ = endpoint_type();
207 }
208 return ec;
209 }
210
211 // Assign a native socket to a socket implementation.
212 boost::system::error_code assign(implementation_type& impl,
213 const protocol_type& protocol, const native_handle_type& native_socket,
214 boost::system::error_code& ec)
215 {
216 if (!do_assign(impl, protocol.type(), native_socket, ec))
217 {
218 impl.protocol_ = protocol;
219 impl.have_remote_endpoint_ = native_socket.have_remote_endpoint();
220 impl.remote_endpoint_ = native_socket.remote_endpoint();
221 }
222 return ec;
223 }
224
225 // Get the native socket representation.
226 native_handle_type native_handle(implementation_type& impl)
227 {
228 if (impl.have_remote_endpoint_)
229 return native_handle_type(impl.socket_, impl.remote_endpoint_);
230 return native_handle_type(impl.socket_);
231 }
232
233 // Bind the socket to the specified local endpoint.
234 boost::system::error_code bind(implementation_type& impl,
235 const endpoint_type& endpoint, boost::system::error_code& ec)
236 {
237 socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
238 return ec;
239 }
240
241 // Set a socket option.
242 template <typename Option>
243 boost::system::error_code set_option(implementation_type& impl,
244 const Option& option, boost::system::error_code& ec)
245 {
246 socket_ops::setsockopt(impl.socket_, impl.state_,
247 option.level(impl.protocol_), option.name(impl.protocol_),
248 option.data(impl.protocol_), option.size(impl.protocol_), ec);
249 return ec;
250 }
251
252 // Set a socket option.
253 template <typename Option>
254 boost::system::error_code get_option(const implementation_type& impl,
255 Option& option, boost::system::error_code& ec) const
256 {
257 std::size_t size = option.size(impl.protocol_);
258 socket_ops::getsockopt(impl.socket_, impl.state_,
259 option.level(impl.protocol_), option.name(impl.protocol_),
260 option.data(impl.protocol_), &size, ec);
261 if (!ec)
262 option.resize(impl.protocol_, size);
263 return ec;
264 }
265
266 // Get the local endpoint.
267 endpoint_type local_endpoint(const implementation_type& impl,
268 boost::system::error_code& ec) const
269 {
270 endpoint_type endpoint;
271 std::size_t addr_len = endpoint.capacity();
272 if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
273 return endpoint_type();
274 endpoint.resize(addr_len);
275 return endpoint;
276 }
277
278 // Get the remote endpoint.
279 endpoint_type remote_endpoint(const implementation_type& impl,
280 boost::system::error_code& ec) const
281 {
282 endpoint_type endpoint = impl.remote_endpoint_;
283 std::size_t addr_len = endpoint.capacity();
284 if (socket_ops::getpeername(impl.socket_, endpoint.data(),
285 &addr_len, impl.have_remote_endpoint_, ec))
286 return endpoint_type();
287 endpoint.resize(addr_len);
288 return endpoint;
289 }
290
291 // Disable sends or receives on the socket.
292 boost::system::error_code shutdown(base_implementation_type& impl,
293 socket_base::shutdown_type what, boost::system::error_code& ec)
294 {
295 socket_ops::shutdown(impl.socket_, what, ec);
296 return ec;
297 }
298
299 // Send a datagram to the specified endpoint. Returns the number of bytes
300 // sent.
301 template <typename ConstBufferSequence>
302 size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
303 const endpoint_type& destination, socket_base::message_flags flags,
304 boost::system::error_code& ec)
305 {
306 buffer_sequence_adapter<boost::asio::const_buffer,
307 ConstBufferSequence> bufs(buffers);
308
309 return socket_ops::sync_sendto(impl.socket_, impl.state_,
310 bufs.buffers(), bufs.count(), flags,
311 destination.data(), destination.size(), ec);
312 }
313
314 // Wait until data can be sent without blocking.
315 size_t send_to(implementation_type& impl, const null_buffers&,
316 const endpoint_type&, socket_base::message_flags,
317 boost::system::error_code& ec)
318 {
319 // Wait for socket to become ready.
320 socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
321
322 return 0;
323 }
324
325 // Start an asynchronous send. The data being sent must be valid for the
326 // lifetime of the asynchronous operation.
327 template <typename ConstBufferSequence, typename Handler>
328 void async_send_to(implementation_type& impl,
329 const ConstBufferSequence& buffers, const endpoint_type& destination,
330 socket_base::message_flags flags, Handler& handler)
331 {
332 // Allocate and construct an operation to wrap the handler.
333 typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
334 typename op::ptr p = { boost::asio::detail::addressof(handler),
335 op::ptr::allocate(handler), 0 };
336 p.p = new (p.v) op(impl.cancel_token_, buffers, handler);
337
338 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
339 &impl, impl.socket_, "async_send_to"));
340
341 buffer_sequence_adapter<boost::asio::const_buffer,
342 ConstBufferSequence> bufs(buffers);
343
344 start_send_to_op(impl, bufs.buffers(), bufs.count(),
345 destination.data(), static_cast<int>(destination.size()),
346 flags, p.p);
347 p.v = p.p = 0;
348 }
349
350 // Start an asynchronous wait until data can be sent without blocking.
351 template <typename Handler>
352 void async_send_to(implementation_type& impl, const null_buffers&,
353 const endpoint_type&, socket_base::message_flags, Handler& handler)
354 {
355 // Allocate and construct an operation to wrap the handler.
356 typedef win_iocp_null_buffers_op<Handler> op;
357 typename op::ptr p = { boost::asio::detail::addressof(handler),
358 op::ptr::allocate(handler), 0 };
359 p.p = new (p.v) op(impl.cancel_token_, handler);
360
361 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
362 &impl, impl.socket_, "async_send_to(null_buffers)"));
363
364 start_reactor_op(impl, select_reactor::write_op, p.p);
365 p.v = p.p = 0;
366 }
367
368 // Receive a datagram with the endpoint of the sender. Returns the number of
369 // bytes received.
370 template <typename MutableBufferSequence>
371 size_t receive_from(implementation_type& impl,
372 const MutableBufferSequence& buffers,
373 endpoint_type& sender_endpoint, socket_base::message_flags flags,
374 boost::system::error_code& ec)
375 {
376 buffer_sequence_adapter<boost::asio::mutable_buffer,
377 MutableBufferSequence> bufs(buffers);
378
379 std::size_t addr_len = sender_endpoint.capacity();
380 std::size_t bytes_recvd = socket_ops::sync_recvfrom(
381 impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
382 flags, sender_endpoint.data(), &addr_len, ec);
383
384 if (!ec)
385 sender_endpoint.resize(addr_len);
386
387 return bytes_recvd;
388 }
389
390 // Wait until data can be received without blocking.
391 size_t receive_from(implementation_type& impl,
392 const null_buffers&, endpoint_type& sender_endpoint,
393 socket_base::message_flags, boost::system::error_code& ec)
394 {
395 // Wait for socket to become ready.
396 socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
397
398 // Reset endpoint since it can be given no sensible value at this time.
399 sender_endpoint = endpoint_type();
400
401 return 0;
402 }
403
404 // Start an asynchronous receive. The buffer for the data being received and
405 // the sender_endpoint object must both be valid for the lifetime of the
406 // asynchronous operation.
407 template <typename MutableBufferSequence, typename Handler>
408 void async_receive_from(implementation_type& impl,
409 const MutableBufferSequence& buffers, endpoint_type& sender_endp,
410 socket_base::message_flags flags, Handler& handler)
411 {
412 // Allocate and construct an operation to wrap the handler.
413 typedef win_iocp_socket_recvfrom_op<
414 MutableBufferSequence, endpoint_type, Handler> op;
415 typename op::ptr p = { boost::asio::detail::addressof(handler),
416 op::ptr::allocate(handler), 0 };
417 p.p = new (p.v) op(sender_endp, impl.cancel_token_, buffers, handler);
418
419 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
420 &impl, impl.socket_, "async_receive_from"));
421
422 buffer_sequence_adapter<boost::asio::mutable_buffer,
423 MutableBufferSequence> bufs(buffers);
424
425 start_receive_from_op(impl, bufs.buffers(), bufs.count(),
426 sender_endp.data(), flags, &p.p->endpoint_size(), p.p);
427 p.v = p.p = 0;
428 }
429
430 // Wait until data can be received without blocking.
431 template <typename Handler>
432 void async_receive_from(implementation_type& impl,
433 const null_buffers&, endpoint_type& sender_endpoint,
434 socket_base::message_flags flags, Handler& handler)
435 {
436 // Allocate and construct an operation to wrap the handler.
437 typedef win_iocp_null_buffers_op<Handler> op;
438 typename op::ptr p = { boost::asio::detail::addressof(handler),
439 op::ptr::allocate(handler), 0 };
440 p.p = new (p.v) op(impl.cancel_token_, handler);
441
442 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
443 &impl, impl.socket_, "async_receive_from(null_buffers)"));
444
445 // Reset endpoint since it can be given no sensible value at this time.
446 sender_endpoint = endpoint_type();
447
448 start_null_buffers_receive_op(impl, flags, p.p);
449 p.v = p.p = 0;
450 }
451
452 // Accept a new connection.
453 template <typename Socket>
454 boost::system::error_code accept(implementation_type& impl, Socket& peer,
455 endpoint_type* peer_endpoint, boost::system::error_code& ec)
456 {
457 // We cannot accept a socket that is already open.
458 if (peer.is_open())
459 {
460 ec = boost::asio::error::already_open;
461 return ec;
462 }
463
464 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
465 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
466 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
467 peer_endpoint ? &addr_len : 0, ec));
468
469 // On success, assign new connection to peer socket object.
470 if (new_socket.get() != invalid_socket)
471 {
472 if (peer_endpoint)
473 peer_endpoint->resize(addr_len);
474 peer.assign(impl.protocol_, new_socket.get(), ec);
475 if (!ec)
476 new_socket.release();
477 }
478
479 return ec;
480 }
481
482 #if defined(BOOST_ASIO_HAS_MOVE)
483 // Accept a new connection.
484 typename Protocol::socket accept(implementation_type& impl,
485 io_context* peer_io_context, endpoint_type* peer_endpoint,
486 boost::system::error_code& ec)
487 {
488 typename Protocol::socket peer(
489 peer_io_context ? *peer_io_context : io_context_);
490
491 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
492 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
493 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
494 peer_endpoint ? &addr_len : 0, ec));
495
496 // On success, assign new connection to peer socket object.
497 if (new_socket.get() != invalid_socket)
498 {
499 if (peer_endpoint)
500 peer_endpoint->resize(addr_len);
501 peer.assign(impl.protocol_, new_socket.get(), ec);
502 if (!ec)
503 new_socket.release();
504 }
505
506 return peer;
507 }
508 #endif // defined(BOOST_ASIO_HAS_MOVE)
509
510 // Start an asynchronous accept. The peer and peer_endpoint objects
511 // must be valid until the accept's handler is invoked.
512 template <typename Socket, typename Handler>
513 void async_accept(implementation_type& impl, Socket& peer,
514 endpoint_type* peer_endpoint, Handler& handler)
515 {
516 // Allocate and construct an operation to wrap the handler.
517 typedef win_iocp_socket_accept_op<Socket, protocol_type, Handler> op;
518 typename op::ptr p = { boost::asio::detail::addressof(handler),
519 op::ptr::allocate(handler), 0 };
520 bool enable_connection_aborted =
521 (impl.state_ & socket_ops::enable_connection_aborted) != 0;
522 p.p = new (p.v) op(*this, impl.socket_, peer, impl.protocol_,
523 peer_endpoint, enable_connection_aborted, handler);
524
525 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
526 &impl, impl.socket_, "async_accept"));
527
528 start_accept_op(impl, peer.is_open(), p.p->new_socket(),
529 impl.protocol_.family(), impl.protocol_.type(),
530 impl.protocol_.protocol(), p.p->output_buffer(),
531 p.p->address_length(), p.p);
532 p.v = p.p = 0;
533 }
534
535 #if defined(BOOST_ASIO_HAS_MOVE)
536 // Start an asynchronous accept. The peer and peer_endpoint objects
537 // must be valid until the accept's handler is invoked.
538 template <typename Handler>
539 void async_accept(implementation_type& impl,
540 boost::asio::io_context* peer_io_context,
541 endpoint_type* peer_endpoint, Handler& handler)
542 {
543 // Allocate and construct an operation to wrap the handler.
544 typedef win_iocp_socket_move_accept_op<protocol_type, Handler> op;
545 typename op::ptr p = { boost::asio::detail::addressof(handler),
546 op::ptr::allocate(handler), 0 };
547 bool enable_connection_aborted =
548 (impl.state_ & socket_ops::enable_connection_aborted) != 0;
549 p.p = new (p.v) op(*this, impl.socket_, impl.protocol_,
550 peer_io_context ? *peer_io_context : io_context_,
551 peer_endpoint, enable_connection_aborted, handler);
552
553 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
554 &impl, impl.socket_, "async_accept"));
555
556 start_accept_op(impl, false, p.p->new_socket(),
557 impl.protocol_.family(), impl.protocol_.type(),
558 impl.protocol_.protocol(), p.p->output_buffer(),
559 p.p->address_length(), p.p);
560 p.v = p.p = 0;
561 }
562 #endif // defined(BOOST_ASIO_HAS_MOVE)
563
564 // Connect the socket to the specified endpoint.
565 boost::system::error_code connect(implementation_type& impl,
566 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
567 {
568 socket_ops::sync_connect(impl.socket_,
569 peer_endpoint.data(), peer_endpoint.size(), ec);
570 return ec;
571 }
572
573 // Start an asynchronous connect.
574 template <typename Handler>
575 void async_connect(implementation_type& impl,
576 const endpoint_type& peer_endpoint, Handler& handler)
577 {
578 // Allocate and construct an operation to wrap the handler.
579 typedef win_iocp_socket_connect_op<Handler> op;
580 typename op::ptr p = { boost::asio::detail::addressof(handler),
581 op::ptr::allocate(handler), 0 };
582 p.p = new (p.v) op(impl.socket_, handler);
583
584 BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
585 &impl, impl.socket_, "async_connect"));
586
587 start_connect_op(impl, impl.protocol_.family(), impl.protocol_.type(),
588 peer_endpoint.data(), static_cast<int>(peer_endpoint.size()), p.p);
589 p.v = p.p = 0;
590 }
591 };
592
593 } // namespace detail
594 } // namespace asio
595 } // namespace boost
596
597 #include <boost/asio/detail/pop_options.hpp>
598
599 #endif // defined(BOOST_ASIO_HAS_IOCP)
600
601 #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP