]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/asio/include/boost/asio/detail/win_iocp_socket_service_base.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / asio / include / boost / asio / detail / win_iocp_socket_service_base.hpp
CommitLineData
7c673cae
FG
1//
2// detail/win_iocp_socket_service_base.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2016 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_BASE_HPP
12#define BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_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 <boost/asio/error.hpp>
23#include <boost/asio/io_service.hpp>
24#include <boost/asio/socket_base.hpp>
25#include <boost/asio/detail/addressof.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/mutex.hpp>
32#include <boost/asio/detail/operation.hpp>
33#include <boost/asio/detail/reactor.hpp>
34#include <boost/asio/detail/reactor_op.hpp>
35#include <boost/asio/detail/socket_holder.hpp>
36#include <boost/asio/detail/socket_ops.hpp>
37#include <boost/asio/detail/socket_types.hpp>
38#include <boost/asio/detail/win_iocp_io_service.hpp>
39#include <boost/asio/detail/win_iocp_null_buffers_op.hpp>
40#include <boost/asio/detail/win_iocp_socket_connect_op.hpp>
41#include <boost/asio/detail/win_iocp_socket_send_op.hpp>
42#include <boost/asio/detail/win_iocp_socket_recv_op.hpp>
43#include <boost/asio/detail/win_iocp_socket_recvmsg_op.hpp>
44
45#include <boost/asio/detail/push_options.hpp>
46
47namespace boost {
48namespace asio {
49namespace detail {
50
51class win_iocp_socket_service_base
52{
53public:
54 // The implementation type of the socket.
55 struct base_implementation_type
56 {
57 // The native socket representation.
58 socket_type socket_;
59
60 // The current state of the socket.
61 socket_ops::state_type state_;
62
63 // We use a shared pointer as a cancellation token here to work around the
64 // broken Windows support for cancellation. MSDN says that when you call
65 // closesocket any outstanding WSARecv or WSASend operations will complete
66 // with the error ERROR_OPERATION_ABORTED. In practice they complete with
67 // ERROR_NETNAME_DELETED, which means you can't tell the difference between
68 // a local cancellation and the socket being hard-closed by the peer.
69 socket_ops::shared_cancel_token_type cancel_token_;
70
71 // Per-descriptor data used by the reactor.
72 reactor::per_descriptor_data reactor_data_;
73
74#if defined(BOOST_ASIO_ENABLE_CANCELIO)
75 // The ID of the thread from which it is safe to cancel asynchronous
76 // operations. 0 means no asynchronous operations have been started yet.
77 // ~0 means asynchronous operations have been started from more than one
78 // thread, and cancellation is not supported for the socket.
79 DWORD safe_cancellation_thread_id_;
80#endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
81
82 // Pointers to adjacent socket implementations in linked list.
83 base_implementation_type* next_;
84 base_implementation_type* prev_;
85 };
86
87 // Constructor.
88 BOOST_ASIO_DECL win_iocp_socket_service_base(
89 boost::asio::io_service& io_service);
90
91 // Destroy all user-defined handler objects owned by the service.
92 BOOST_ASIO_DECL void shutdown_service();
93
94 // Construct a new socket implementation.
95 BOOST_ASIO_DECL void construct(base_implementation_type& impl);
96
97 // Move-construct a new socket implementation.
98 BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl,
99 base_implementation_type& other_impl);
100
101 // Move-assign from another socket implementation.
102 BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl,
103 win_iocp_socket_service_base& other_service,
104 base_implementation_type& other_impl);
105
106 // Destroy a socket implementation.
107 BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
108
109 // Determine whether the socket is open.
110 bool is_open(const base_implementation_type& impl) const
111 {
112 return impl.socket_ != invalid_socket;
113 }
114
115 // Destroy a socket implementation.
116 BOOST_ASIO_DECL boost::system::error_code close(
117 base_implementation_type& impl, boost::system::error_code& ec);
118
119 // Cancel all operations associated with the socket.
120 BOOST_ASIO_DECL boost::system::error_code cancel(
121 base_implementation_type& impl, boost::system::error_code& ec);
122
123 // Determine whether the socket is at the out-of-band data mark.
124 bool at_mark(const base_implementation_type& impl,
125 boost::system::error_code& ec) const
126 {
127 return socket_ops::sockatmark(impl.socket_, ec);
128 }
129
130 // Determine the number of bytes available for reading.
131 std::size_t available(const base_implementation_type& impl,
132 boost::system::error_code& ec) const
133 {
134 return socket_ops::available(impl.socket_, ec);
135 }
136
137 // Place the socket into the state where it will listen for new connections.
138 boost::system::error_code listen(base_implementation_type& impl,
139 int backlog, boost::system::error_code& ec)
140 {
141 socket_ops::listen(impl.socket_, backlog, ec);
142 return ec;
143 }
144
145 // Perform an IO control command on the socket.
146 template <typename IO_Control_Command>
147 boost::system::error_code io_control(base_implementation_type& impl,
148 IO_Control_Command& command, boost::system::error_code& ec)
149 {
150 socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
151 static_cast<ioctl_arg_type*>(command.data()), ec);
152 return ec;
153 }
154
155 // Gets the non-blocking mode of the socket.
156 bool non_blocking(const base_implementation_type& impl) const
157 {
158 return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
159 }
160
161 // Sets the non-blocking mode of the socket.
162 boost::system::error_code non_blocking(base_implementation_type& impl,
163 bool mode, boost::system::error_code& ec)
164 {
165 socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
166 return ec;
167 }
168
169 // Gets the non-blocking mode of the native socket implementation.
170 bool native_non_blocking(const base_implementation_type& impl) const
171 {
172 return (impl.state_ & socket_ops::internal_non_blocking) != 0;
173 }
174
175 // Sets the non-blocking mode of the native socket implementation.
176 boost::system::error_code native_non_blocking(base_implementation_type& impl,
177 bool mode, boost::system::error_code& ec)
178 {
179 socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
180 return ec;
181 }
182
183 // Disable sends or receives on the socket.
184 boost::system::error_code shutdown(base_implementation_type& impl,
185 socket_base::shutdown_type what, boost::system::error_code& ec)
186 {
187 socket_ops::shutdown(impl.socket_, what, ec);
188 return ec;
189 }
190
191 // Send the given data to the peer. Returns the number of bytes sent.
192 template <typename ConstBufferSequence>
193 size_t send(base_implementation_type& impl,
194 const ConstBufferSequence& buffers,
195 socket_base::message_flags flags, boost::system::error_code& ec)
196 {
197 buffer_sequence_adapter<boost::asio::const_buffer,
198 ConstBufferSequence> bufs(buffers);
199
200 return socket_ops::sync_send(impl.socket_, impl.state_,
201 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
202 }
203
204 // Wait until data can be sent without blocking.
205 size_t send(base_implementation_type& impl, const null_buffers&,
206 socket_base::message_flags, boost::system::error_code& ec)
207 {
208 // Wait for socket to become ready.
209 socket_ops::poll_write(impl.socket_, impl.state_, ec);
210
211 return 0;
212 }
213
214 // Start an asynchronous send. The data being sent must be valid for the
215 // lifetime of the asynchronous operation.
216 template <typename ConstBufferSequence, typename Handler>
217 void async_send(base_implementation_type& impl,
218 const ConstBufferSequence& buffers,
219 socket_base::message_flags flags, Handler& handler)
220 {
221 // Allocate and construct an operation to wrap the handler.
222 typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
223 typename op::ptr p = { boost::asio::detail::addressof(handler),
224 boost_asio_handler_alloc_helpers::allocate(
225 sizeof(op), handler), 0 };
226 p.p = new (p.v) op(impl.cancel_token_, buffers, handler);
227
228 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send"));
229
230 buffer_sequence_adapter<boost::asio::const_buffer,
231 ConstBufferSequence> bufs(buffers);
232
233 start_send_op(impl, bufs.buffers(), bufs.count(), flags,
234 (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
235 p.p);
236 p.v = p.p = 0;
237 }
238
239 // Start an asynchronous wait until data can be sent without blocking.
240 template <typename Handler>
241 void async_send(base_implementation_type& impl, const null_buffers&,
242 socket_base::message_flags, Handler& handler)
243 {
244 // Allocate and construct an operation to wrap the handler.
245 typedef win_iocp_null_buffers_op<Handler> op;
246 typename op::ptr p = { boost::asio::detail::addressof(handler),
247 boost_asio_handler_alloc_helpers::allocate(
248 sizeof(op), handler), 0 };
249 p.p = new (p.v) op(impl.cancel_token_, handler);
250
251 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
252 &impl, "async_send(null_buffers)"));
253
254 start_reactor_op(impl, reactor::write_op, p.p);
255 p.v = p.p = 0;
256 }
257
258 // Receive some data from the peer. Returns the number of bytes received.
259 template <typename MutableBufferSequence>
260 size_t receive(base_implementation_type& impl,
261 const MutableBufferSequence& buffers,
262 socket_base::message_flags flags, boost::system::error_code& ec)
263 {
264 buffer_sequence_adapter<boost::asio::mutable_buffer,
265 MutableBufferSequence> bufs(buffers);
266
267 return socket_ops::sync_recv(impl.socket_, impl.state_,
268 bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
269 }
270
271 // Wait until data can be received without blocking.
272 size_t receive(base_implementation_type& impl, const null_buffers&,
273 socket_base::message_flags, boost::system::error_code& ec)
274 {
275 // Wait for socket to become ready.
276 socket_ops::poll_read(impl.socket_, impl.state_, ec);
277
278 return 0;
279 }
280
281 // Start an asynchronous receive. The buffer for the data being received
282 // must be valid for the lifetime of the asynchronous operation.
283 template <typename MutableBufferSequence, typename Handler>
284 void async_receive(base_implementation_type& impl,
285 const MutableBufferSequence& buffers,
286 socket_base::message_flags flags, Handler& handler)
287 {
288 // Allocate and construct an operation to wrap the handler.
289 typedef win_iocp_socket_recv_op<MutableBufferSequence, Handler> op;
290 typename op::ptr p = { boost::asio::detail::addressof(handler),
291 boost_asio_handler_alloc_helpers::allocate(
292 sizeof(op), handler), 0 };
293 p.p = new (p.v) op(impl.state_, impl.cancel_token_, buffers, handler);
294
295 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_receive"));
296
297 buffer_sequence_adapter<boost::asio::mutable_buffer,
298 MutableBufferSequence> bufs(buffers);
299
300 start_receive_op(impl, bufs.buffers(), bufs.count(), flags,
301 (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
302 p.p);
303 p.v = p.p = 0;
304 }
305
306 // Wait until data can be received without blocking.
307 template <typename Handler>
308 void async_receive(base_implementation_type& impl, const null_buffers&,
309 socket_base::message_flags flags, Handler& handler)
310 {
311 // Allocate and construct an operation to wrap the handler.
312 typedef win_iocp_null_buffers_op<Handler> op;
313 typename op::ptr p = { boost::asio::detail::addressof(handler),
314 boost_asio_handler_alloc_helpers::allocate(
315 sizeof(op), handler), 0 };
316 p.p = new (p.v) op(impl.cancel_token_, handler);
317
318 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
319 &impl, "async_receive(null_buffers)"));
320
321 start_null_buffers_receive_op(impl, flags, p.p);
322 p.v = p.p = 0;
323 }
324
325 // Receive some data with associated flags. Returns the number of bytes
326 // received.
327 template <typename MutableBufferSequence>
328 size_t receive_with_flags(base_implementation_type& impl,
329 const MutableBufferSequence& buffers,
330 socket_base::message_flags in_flags,
331 socket_base::message_flags& out_flags, boost::system::error_code& ec)
332 {
333 buffer_sequence_adapter<boost::asio::mutable_buffer,
334 MutableBufferSequence> bufs(buffers);
335
336 return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
337 bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
338 }
339
340 // Wait until data can be received without blocking.
341 size_t receive_with_flags(base_implementation_type& impl,
342 const null_buffers&, socket_base::message_flags,
343 socket_base::message_flags& out_flags, boost::system::error_code& ec)
344 {
345 // Wait for socket to become ready.
346 socket_ops::poll_read(impl.socket_, impl.state_, ec);
347
348 // Clear out_flags, since we cannot give it any other sensible value when
349 // performing a null_buffers operation.
350 out_flags = 0;
351
352 return 0;
353 }
354
355 // Start an asynchronous receive. The buffer for the data being received
356 // must be valid for the lifetime of the asynchronous operation.
357 template <typename MutableBufferSequence, typename Handler>
358 void async_receive_with_flags(base_implementation_type& impl,
359 const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
360 socket_base::message_flags& out_flags, Handler& handler)
361 {
362 // Allocate and construct an operation to wrap the handler.
363 typedef win_iocp_socket_recvmsg_op<MutableBufferSequence, Handler> op;
364 typename op::ptr p = { boost::asio::detail::addressof(handler),
365 boost_asio_handler_alloc_helpers::allocate(
366 sizeof(op), handler), 0 };
367 p.p = new (p.v) op(impl.cancel_token_, buffers, out_flags, handler);
368
369 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
370 &impl, "async_receive_with_flags"));
371
372 buffer_sequence_adapter<boost::asio::mutable_buffer,
373 MutableBufferSequence> bufs(buffers);
374
375 start_receive_op(impl, bufs.buffers(), bufs.count(), in_flags, false, p.p);
376 p.v = p.p = 0;
377 }
378
379 // Wait until data can be received without blocking.
380 template <typename Handler>
381 void async_receive_with_flags(base_implementation_type& impl,
382 const null_buffers&, socket_base::message_flags in_flags,
383 socket_base::message_flags& out_flags, Handler& handler)
384 {
385 // Allocate and construct an operation to wrap the handler.
386 typedef win_iocp_null_buffers_op<Handler> op;
387 typename op::ptr p = { boost::asio::detail::addressof(handler),
388 boost_asio_handler_alloc_helpers::allocate(
389 sizeof(op), handler), 0 };
390 p.p = new (p.v) op(impl.cancel_token_, handler);
391
392 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl,
393 "async_receive_with_flags(null_buffers)"));
394
395 // Reset out_flags since it can be given no sensible value at this time.
396 out_flags = 0;
397
398 start_null_buffers_receive_op(impl, in_flags, p.p);
399 p.v = p.p = 0;
400 }
401
402 // Helper function to restart an asynchronous accept operation.
403 BOOST_ASIO_DECL void restart_accept_op(socket_type s,
404 socket_holder& new_socket, int family, int type, int protocol,
405 void* output_buffer, DWORD address_length, operation* op);
406
407protected:
408 // Open a new socket implementation.
409 BOOST_ASIO_DECL boost::system::error_code do_open(
410 base_implementation_type& impl, int family, int type,
411 int protocol, boost::system::error_code& ec);
412
413 // Assign a native socket to a socket implementation.
414 BOOST_ASIO_DECL boost::system::error_code do_assign(
415 base_implementation_type& impl, int type,
416 socket_type native_socket, boost::system::error_code& ec);
417
418 // Helper function to start an asynchronous send operation.
419 BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl,
420 WSABUF* buffers, std::size_t buffer_count,
421 socket_base::message_flags flags, bool noop, operation* op);
422
423 // Helper function to start an asynchronous send_to operation.
424 BOOST_ASIO_DECL void start_send_to_op(base_implementation_type& impl,
425 WSABUF* buffers, std::size_t buffer_count,
426 const socket_addr_type* addr, int addrlen,
427 socket_base::message_flags flags, operation* op);
428
429 // Helper function to start an asynchronous receive operation.
430 BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl,
431 WSABUF* buffers, std::size_t buffer_count,
432 socket_base::message_flags flags, bool noop, operation* op);
433
434 // Helper function to start an asynchronous null_buffers receive operation.
435 BOOST_ASIO_DECL void start_null_buffers_receive_op(
436 base_implementation_type& impl,
437 socket_base::message_flags flags, reactor_op* op);
438
439 // Helper function to start an asynchronous receive_from operation.
440 BOOST_ASIO_DECL void start_receive_from_op(base_implementation_type& impl,
441 WSABUF* buffers, std::size_t buffer_count, socket_addr_type* addr,
442 socket_base::message_flags flags, int* addrlen, operation* op);
443
444 // Helper function to start an asynchronous accept operation.
445 BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
446 bool peer_is_open, socket_holder& new_socket, int family, int type,
447 int protocol, void* output_buffer, DWORD address_length, operation* op);
448
449 // Start an asynchronous read or write operation using the reactor.
450 BOOST_ASIO_DECL void start_reactor_op(base_implementation_type& impl,
451 int op_type, reactor_op* op);
452
453 // Start the asynchronous connect operation using the reactor.
454 BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl,
455 int family, int type, const socket_addr_type* remote_addr,
456 std::size_t remote_addrlen, win_iocp_socket_connect_op_base* op);
457
458 // Helper function to close a socket when the associated object is being
459 // destroyed.
460 BOOST_ASIO_DECL void close_for_destruction(base_implementation_type& impl);
461
462 // Update the ID of the thread from which cancellation is safe.
463 BOOST_ASIO_DECL void update_cancellation_thread_id(
464 base_implementation_type& impl);
465
466 // Helper function to get the reactor. If no reactor has been created yet, a
467 // new one is obtained from the io_service and a pointer to it is cached in
468 // this service.
469 BOOST_ASIO_DECL reactor& get_reactor();
470
471 // The type of a ConnectEx function pointer, as old SDKs may not provide it.
472 typedef BOOL (PASCAL *connect_ex_fn)(SOCKET,
473 const socket_addr_type*, int, void*, DWORD, DWORD*, OVERLAPPED*);
474
475 // Helper function to get the ConnectEx pointer. If no ConnectEx pointer has
476 // been obtained yet, one is obtained using WSAIoctl and the pointer is
477 // cached. Returns a null pointer if ConnectEx is not available.
478 BOOST_ASIO_DECL connect_ex_fn get_connect_ex(
479 base_implementation_type& impl, int type);
480
481 // Helper function to emulate InterlockedCompareExchangePointer functionality
482 // for:
483 // - very old Platform SDKs; and
484 // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
485 BOOST_ASIO_DECL void* interlocked_compare_exchange_pointer(
486 void** dest, void* exch, void* cmp);
487
488 // Helper function to emulate InterlockedExchangePointer functionality for:
489 // - very old Platform SDKs; and
490 // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
491 BOOST_ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
492
493 // The io_service used to obtain the reactor, if required.
494 boost::asio::io_service& io_service_;
495
496 // The IOCP service used for running asynchronous operations and dispatching
497 // handlers.
498 win_iocp_io_service& iocp_service_;
499
500 // The reactor used for performing connect operations. This object is created
501 // only if needed.
502 reactor* reactor_;
503
504 // Pointer to ConnectEx implementation.
505 void* connect_ex_;
506
507 // Mutex to protect access to the linked list of implementations.
508 boost::asio::detail::mutex mutex_;
509
510 // The head of a linked list of all implementations.
511 base_implementation_type* impl_list_;
512};
513
514} // namespace detail
515} // namespace asio
516} // namespace boost
517
518#include <boost/asio/detail/pop_options.hpp>
519
520#if defined(BOOST_ASIO_HEADER_ONLY)
521# include <boost/asio/detail/impl/win_iocp_socket_service_base.ipp>
522#endif // defined(BOOST_ASIO_HEADER_ONLY)
523
524#endif // defined(BOOST_ASIO_HAS_IOCP)
525
526#endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP