annotate DEPENDENCIES/generic/include/boost/asio/detail/reactive_socket_service.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents c530137014c0
children
rev   line source
Chris@16 1 //
Chris@16 2 // detail/reactive_socket_service.hpp
Chris@16 3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Chris@16 4 //
Chris@101 5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
Chris@16 6 //
Chris@16 7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
Chris@16 8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 9 //
Chris@16 10
Chris@16 11 #ifndef BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP
Chris@16 12 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP
Chris@16 13
Chris@16 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
Chris@16 15 # pragma once
Chris@16 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
Chris@16 17
Chris@16 18 #include <boost/asio/detail/config.hpp>
Chris@16 19
Chris@16 20 #if !defined(BOOST_ASIO_HAS_IOCP)
Chris@16 21
Chris@16 22 #include <boost/asio/buffer.hpp>
Chris@16 23 #include <boost/asio/error.hpp>
Chris@16 24 #include <boost/asio/io_service.hpp>
Chris@16 25 #include <boost/asio/socket_base.hpp>
Chris@16 26 #include <boost/asio/detail/addressof.hpp>
Chris@16 27 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
Chris@16 28 #include <boost/asio/detail/noncopyable.hpp>
Chris@16 29 #include <boost/asio/detail/reactive_null_buffers_op.hpp>
Chris@16 30 #include <boost/asio/detail/reactive_socket_accept_op.hpp>
Chris@16 31 #include <boost/asio/detail/reactive_socket_connect_op.hpp>
Chris@16 32 #include <boost/asio/detail/reactive_socket_recvfrom_op.hpp>
Chris@16 33 #include <boost/asio/detail/reactive_socket_sendto_op.hpp>
Chris@16 34 #include <boost/asio/detail/reactive_socket_service_base.hpp>
Chris@16 35 #include <boost/asio/detail/reactor.hpp>
Chris@16 36 #include <boost/asio/detail/reactor_op.hpp>
Chris@16 37 #include <boost/asio/detail/socket_holder.hpp>
Chris@16 38 #include <boost/asio/detail/socket_ops.hpp>
Chris@16 39 #include <boost/asio/detail/socket_types.hpp>
Chris@16 40
Chris@16 41 #include <boost/asio/detail/push_options.hpp>
Chris@16 42
Chris@16 43 namespace boost {
Chris@16 44 namespace asio {
Chris@16 45 namespace detail {
Chris@16 46
Chris@16 47 template <typename Protocol>
Chris@16 48 class reactive_socket_service :
Chris@16 49 public reactive_socket_service_base
Chris@16 50 {
Chris@16 51 public:
Chris@16 52 // The protocol type.
Chris@16 53 typedef Protocol protocol_type;
Chris@16 54
Chris@16 55 // The endpoint type.
Chris@16 56 typedef typename Protocol::endpoint endpoint_type;
Chris@16 57
Chris@16 58 // The native type of a socket.
Chris@16 59 typedef socket_type native_handle_type;
Chris@16 60
Chris@16 61 // The implementation type of the socket.
Chris@16 62 struct implementation_type :
Chris@16 63 reactive_socket_service_base::base_implementation_type
Chris@16 64 {
Chris@16 65 // Default constructor.
Chris@16 66 implementation_type()
Chris@16 67 : protocol_(endpoint_type().protocol())
Chris@16 68 {
Chris@16 69 }
Chris@16 70
Chris@16 71 // The protocol associated with the socket.
Chris@16 72 protocol_type protocol_;
Chris@16 73 };
Chris@16 74
Chris@16 75 // Constructor.
Chris@16 76 reactive_socket_service(boost::asio::io_service& io_service)
Chris@16 77 : reactive_socket_service_base(io_service)
Chris@16 78 {
Chris@16 79 }
Chris@16 80
Chris@16 81 // Move-construct a new socket implementation.
Chris@16 82 void move_construct(implementation_type& impl,
Chris@16 83 implementation_type& other_impl)
Chris@16 84 {
Chris@16 85 this->base_move_construct(impl, other_impl);
Chris@16 86
Chris@16 87 impl.protocol_ = other_impl.protocol_;
Chris@16 88 other_impl.protocol_ = endpoint_type().protocol();
Chris@16 89 }
Chris@16 90
Chris@16 91 // Move-assign from another socket implementation.
Chris@16 92 void move_assign(implementation_type& impl,
Chris@16 93 reactive_socket_service_base& other_service,
Chris@16 94 implementation_type& other_impl)
Chris@16 95 {
Chris@16 96 this->base_move_assign(impl, other_service, other_impl);
Chris@16 97
Chris@16 98 impl.protocol_ = other_impl.protocol_;
Chris@16 99 other_impl.protocol_ = endpoint_type().protocol();
Chris@16 100 }
Chris@16 101
Chris@16 102 // Move-construct a new socket implementation from another protocol type.
Chris@16 103 template <typename Protocol1>
Chris@16 104 void converting_move_construct(implementation_type& impl,
Chris@16 105 typename reactive_socket_service<
Chris@16 106 Protocol1>::implementation_type& other_impl)
Chris@16 107 {
Chris@16 108 this->base_move_construct(impl, other_impl);
Chris@16 109
Chris@16 110 impl.protocol_ = protocol_type(other_impl.protocol_);
Chris@16 111 other_impl.protocol_ = typename Protocol1::endpoint().protocol();
Chris@16 112 }
Chris@16 113
Chris@16 114 // Open a new socket implementation.
Chris@16 115 boost::system::error_code open(implementation_type& impl,
Chris@16 116 const protocol_type& protocol, boost::system::error_code& ec)
Chris@16 117 {
Chris@16 118 if (!do_open(impl, protocol.family(),
Chris@16 119 protocol.type(), protocol.protocol(), ec))
Chris@16 120 impl.protocol_ = protocol;
Chris@16 121 return ec;
Chris@16 122 }
Chris@16 123
Chris@16 124 // Assign a native socket to a socket implementation.
Chris@16 125 boost::system::error_code assign(implementation_type& impl,
Chris@16 126 const protocol_type& protocol, const native_handle_type& native_socket,
Chris@16 127 boost::system::error_code& ec)
Chris@16 128 {
Chris@16 129 if (!do_assign(impl, protocol.type(), native_socket, ec))
Chris@16 130 impl.protocol_ = protocol;
Chris@16 131 return ec;
Chris@16 132 }
Chris@16 133
Chris@16 134 // Get the native socket representation.
Chris@16 135 native_handle_type native_handle(implementation_type& impl)
Chris@16 136 {
Chris@16 137 return impl.socket_;
Chris@16 138 }
Chris@16 139
Chris@16 140 // Bind the socket to the specified local endpoint.
Chris@16 141 boost::system::error_code bind(implementation_type& impl,
Chris@16 142 const endpoint_type& endpoint, boost::system::error_code& ec)
Chris@16 143 {
Chris@16 144 socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
Chris@16 145 return ec;
Chris@16 146 }
Chris@16 147
Chris@16 148 // Set a socket option.
Chris@16 149 template <typename Option>
Chris@16 150 boost::system::error_code set_option(implementation_type& impl,
Chris@16 151 const Option& option, boost::system::error_code& ec)
Chris@16 152 {
Chris@16 153 socket_ops::setsockopt(impl.socket_, impl.state_,
Chris@16 154 option.level(impl.protocol_), option.name(impl.protocol_),
Chris@16 155 option.data(impl.protocol_), option.size(impl.protocol_), ec);
Chris@16 156 return ec;
Chris@16 157 }
Chris@16 158
Chris@16 159 // Set a socket option.
Chris@16 160 template <typename Option>
Chris@16 161 boost::system::error_code get_option(const implementation_type& impl,
Chris@16 162 Option& option, boost::system::error_code& ec) const
Chris@16 163 {
Chris@16 164 std::size_t size = option.size(impl.protocol_);
Chris@16 165 socket_ops::getsockopt(impl.socket_, impl.state_,
Chris@16 166 option.level(impl.protocol_), option.name(impl.protocol_),
Chris@16 167 option.data(impl.protocol_), &size, ec);
Chris@16 168 if (!ec)
Chris@16 169 option.resize(impl.protocol_, size);
Chris@16 170 return ec;
Chris@16 171 }
Chris@16 172
Chris@16 173 // Get the local endpoint.
Chris@16 174 endpoint_type local_endpoint(const implementation_type& impl,
Chris@16 175 boost::system::error_code& ec) const
Chris@16 176 {
Chris@16 177 endpoint_type endpoint;
Chris@16 178 std::size_t addr_len = endpoint.capacity();
Chris@16 179 if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
Chris@16 180 return endpoint_type();
Chris@16 181 endpoint.resize(addr_len);
Chris@16 182 return endpoint;
Chris@16 183 }
Chris@16 184
Chris@16 185 // Get the remote endpoint.
Chris@16 186 endpoint_type remote_endpoint(const implementation_type& impl,
Chris@16 187 boost::system::error_code& ec) const
Chris@16 188 {
Chris@16 189 endpoint_type endpoint;
Chris@16 190 std::size_t addr_len = endpoint.capacity();
Chris@16 191 if (socket_ops::getpeername(impl.socket_,
Chris@16 192 endpoint.data(), &addr_len, false, ec))
Chris@16 193 return endpoint_type();
Chris@16 194 endpoint.resize(addr_len);
Chris@16 195 return endpoint;
Chris@16 196 }
Chris@16 197
Chris@16 198 // Send a datagram to the specified endpoint. Returns the number of bytes
Chris@16 199 // sent.
Chris@16 200 template <typename ConstBufferSequence>
Chris@16 201 size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
Chris@16 202 const endpoint_type& destination, socket_base::message_flags flags,
Chris@16 203 boost::system::error_code& ec)
Chris@16 204 {
Chris@16 205 buffer_sequence_adapter<boost::asio::const_buffer,
Chris@16 206 ConstBufferSequence> bufs(buffers);
Chris@16 207
Chris@16 208 return socket_ops::sync_sendto(impl.socket_, impl.state_,
Chris@16 209 bufs.buffers(), bufs.count(), flags,
Chris@16 210 destination.data(), destination.size(), ec);
Chris@16 211 }
Chris@16 212
Chris@16 213 // Wait until data can be sent without blocking.
Chris@16 214 size_t send_to(implementation_type& impl, const null_buffers&,
Chris@16 215 const endpoint_type&, socket_base::message_flags,
Chris@16 216 boost::system::error_code& ec)
Chris@16 217 {
Chris@16 218 // Wait for socket to become ready.
Chris@16 219 socket_ops::poll_write(impl.socket_, impl.state_, ec);
Chris@16 220
Chris@16 221 return 0;
Chris@16 222 }
Chris@16 223
Chris@16 224 // Start an asynchronous send. The data being sent must be valid for the
Chris@16 225 // lifetime of the asynchronous operation.
Chris@16 226 template <typename ConstBufferSequence, typename Handler>
Chris@16 227 void async_send_to(implementation_type& impl,
Chris@16 228 const ConstBufferSequence& buffers,
Chris@16 229 const endpoint_type& destination, socket_base::message_flags flags,
Chris@16 230 Handler& handler)
Chris@16 231 {
Chris@16 232 bool is_continuation =
Chris@16 233 boost_asio_handler_cont_helpers::is_continuation(handler);
Chris@16 234
Chris@16 235 // Allocate and construct an operation to wrap the handler.
Chris@16 236 typedef reactive_socket_sendto_op<ConstBufferSequence,
Chris@16 237 endpoint_type, Handler> op;
Chris@16 238 typename op::ptr p = { boost::asio::detail::addressof(handler),
Chris@16 239 boost_asio_handler_alloc_helpers::allocate(
Chris@16 240 sizeof(op), handler), 0 };
Chris@16 241 p.p = new (p.v) op(impl.socket_, buffers, destination, flags, handler);
Chris@16 242
Chris@16 243 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_send_to"));
Chris@16 244
Chris@16 245 start_op(impl, reactor::write_op, p.p, is_continuation, true, false);
Chris@16 246 p.v = p.p = 0;
Chris@16 247 }
Chris@16 248
Chris@16 249 // Start an asynchronous wait until data can be sent without blocking.
Chris@16 250 template <typename Handler>
Chris@16 251 void async_send_to(implementation_type& impl, const null_buffers&,
Chris@16 252 const endpoint_type&, socket_base::message_flags, Handler& handler)
Chris@16 253 {
Chris@16 254 bool is_continuation =
Chris@16 255 boost_asio_handler_cont_helpers::is_continuation(handler);
Chris@16 256
Chris@16 257 // Allocate and construct an operation to wrap the handler.
Chris@16 258 typedef reactive_null_buffers_op<Handler> op;
Chris@16 259 typename op::ptr p = { boost::asio::detail::addressof(handler),
Chris@16 260 boost_asio_handler_alloc_helpers::allocate(
Chris@16 261 sizeof(op), handler), 0 };
Chris@16 262 p.p = new (p.v) op(handler);
Chris@16 263
Chris@16 264 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
Chris@16 265 &impl, "async_send_to(null_buffers)"));
Chris@16 266
Chris@16 267 start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
Chris@16 268 p.v = p.p = 0;
Chris@16 269 }
Chris@16 270
Chris@16 271 // Receive a datagram with the endpoint of the sender. Returns the number of
Chris@16 272 // bytes received.
Chris@16 273 template <typename MutableBufferSequence>
Chris@16 274 size_t receive_from(implementation_type& impl,
Chris@16 275 const MutableBufferSequence& buffers,
Chris@16 276 endpoint_type& sender_endpoint, socket_base::message_flags flags,
Chris@16 277 boost::system::error_code& ec)
Chris@16 278 {
Chris@16 279 buffer_sequence_adapter<boost::asio::mutable_buffer,
Chris@16 280 MutableBufferSequence> bufs(buffers);
Chris@16 281
Chris@16 282 std::size_t addr_len = sender_endpoint.capacity();
Chris@16 283 std::size_t bytes_recvd = socket_ops::sync_recvfrom(
Chris@16 284 impl.socket_, impl.state_, bufs.buffers(), bufs.count(),
Chris@16 285 flags, sender_endpoint.data(), &addr_len, ec);
Chris@16 286
Chris@16 287 if (!ec)
Chris@16 288 sender_endpoint.resize(addr_len);
Chris@16 289
Chris@16 290 return bytes_recvd;
Chris@16 291 }
Chris@16 292
Chris@16 293 // Wait until data can be received without blocking.
Chris@16 294 size_t receive_from(implementation_type& impl, const null_buffers&,
Chris@16 295 endpoint_type& sender_endpoint, socket_base::message_flags,
Chris@16 296 boost::system::error_code& ec)
Chris@16 297 {
Chris@16 298 // Wait for socket to become ready.
Chris@16 299 socket_ops::poll_read(impl.socket_, impl.state_, ec);
Chris@16 300
Chris@16 301 // Reset endpoint since it can be given no sensible value at this time.
Chris@16 302 sender_endpoint = endpoint_type();
Chris@16 303
Chris@16 304 return 0;
Chris@16 305 }
Chris@16 306
Chris@16 307 // Start an asynchronous receive. The buffer for the data being received and
Chris@16 308 // the sender_endpoint object must both be valid for the lifetime of the
Chris@16 309 // asynchronous operation.
Chris@16 310 template <typename MutableBufferSequence, typename Handler>
Chris@16 311 void async_receive_from(implementation_type& impl,
Chris@16 312 const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
Chris@16 313 socket_base::message_flags flags, Handler& handler)
Chris@16 314 {
Chris@16 315 bool is_continuation =
Chris@16 316 boost_asio_handler_cont_helpers::is_continuation(handler);
Chris@16 317
Chris@16 318 // Allocate and construct an operation to wrap the handler.
Chris@16 319 typedef reactive_socket_recvfrom_op<MutableBufferSequence,
Chris@16 320 endpoint_type, Handler> op;
Chris@16 321 typename op::ptr p = { boost::asio::detail::addressof(handler),
Chris@16 322 boost_asio_handler_alloc_helpers::allocate(
Chris@16 323 sizeof(op), handler), 0 };
Chris@16 324 int protocol = impl.protocol_.type();
Chris@16 325 p.p = new (p.v) op(impl.socket_, protocol,
Chris@16 326 buffers, sender_endpoint, flags, handler);
Chris@16 327
Chris@16 328 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
Chris@16 329 &impl, "async_receive_from"));
Chris@16 330
Chris@16 331 start_op(impl,
Chris@16 332 (flags & socket_base::message_out_of_band)
Chris@16 333 ? reactor::except_op : reactor::read_op,
Chris@16 334 p.p, is_continuation, true, false);
Chris@16 335 p.v = p.p = 0;
Chris@16 336 }
Chris@16 337
Chris@16 338 // Wait until data can be received without blocking.
Chris@16 339 template <typename Handler>
Chris@16 340 void async_receive_from(implementation_type& impl,
Chris@16 341 const null_buffers&, endpoint_type& sender_endpoint,
Chris@16 342 socket_base::message_flags flags, Handler& handler)
Chris@16 343 {
Chris@16 344 bool is_continuation =
Chris@16 345 boost_asio_handler_cont_helpers::is_continuation(handler);
Chris@16 346
Chris@16 347 // Allocate and construct an operation to wrap the handler.
Chris@16 348 typedef reactive_null_buffers_op<Handler> op;
Chris@16 349 typename op::ptr p = { boost::asio::detail::addressof(handler),
Chris@16 350 boost_asio_handler_alloc_helpers::allocate(
Chris@16 351 sizeof(op), handler), 0 };
Chris@16 352 p.p = new (p.v) op(handler);
Chris@16 353
Chris@16 354 BOOST_ASIO_HANDLER_CREATION((p.p, "socket",
Chris@16 355 &impl, "async_receive_from(null_buffers)"));
Chris@16 356
Chris@16 357 // Reset endpoint since it can be given no sensible value at this time.
Chris@16 358 sender_endpoint = endpoint_type();
Chris@16 359
Chris@16 360 start_op(impl,
Chris@16 361 (flags & socket_base::message_out_of_band)
Chris@16 362 ? reactor::except_op : reactor::read_op,
Chris@16 363 p.p, is_continuation, false, false);
Chris@16 364 p.v = p.p = 0;
Chris@16 365 }
Chris@16 366
Chris@16 367 // Accept a new connection.
Chris@16 368 template <typename Socket>
Chris@16 369 boost::system::error_code accept(implementation_type& impl,
Chris@16 370 Socket& peer, endpoint_type* peer_endpoint, boost::system::error_code& ec)
Chris@16 371 {
Chris@16 372 // We cannot accept a socket that is already open.
Chris@16 373 if (peer.is_open())
Chris@16 374 {
Chris@16 375 ec = boost::asio::error::already_open;
Chris@16 376 return ec;
Chris@16 377 }
Chris@16 378
Chris@16 379 std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
Chris@16 380 socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
Chris@16 381 impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
Chris@16 382 peer_endpoint ? &addr_len : 0, ec));
Chris@16 383
Chris@16 384 // On success, assign new connection to peer socket object.
Chris@16 385 if (new_socket.get() != invalid_socket)
Chris@16 386 {
Chris@16 387 if (peer_endpoint)
Chris@16 388 peer_endpoint->resize(addr_len);
Chris@16 389 if (!peer.assign(impl.protocol_, new_socket.get(), ec))
Chris@16 390 new_socket.release();
Chris@16 391 }
Chris@16 392
Chris@16 393 return ec;
Chris@16 394 }
Chris@16 395
Chris@16 396 // Start an asynchronous accept. The peer and peer_endpoint objects
Chris@16 397 // must be valid until the accept's handler is invoked.
Chris@16 398 template <typename Socket, typename Handler>
Chris@16 399 void async_accept(implementation_type& impl, Socket& peer,
Chris@16 400 endpoint_type* peer_endpoint, Handler& handler)
Chris@16 401 {
Chris@16 402 bool is_continuation =
Chris@16 403 boost_asio_handler_cont_helpers::is_continuation(handler);
Chris@16 404
Chris@16 405 // Allocate and construct an operation to wrap the handler.
Chris@16 406 typedef reactive_socket_accept_op<Socket, Protocol, Handler> op;
Chris@16 407 typename op::ptr p = { boost::asio::detail::addressof(handler),
Chris@16 408 boost_asio_handler_alloc_helpers::allocate(
Chris@16 409 sizeof(op), handler), 0 };
Chris@16 410 p.p = new (p.v) op(impl.socket_, impl.state_, peer,
Chris@16 411 impl.protocol_, peer_endpoint, handler);
Chris@16 412
Chris@16 413 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_accept"));
Chris@16 414
Chris@16 415 start_accept_op(impl, p.p, is_continuation, peer.is_open());
Chris@16 416 p.v = p.p = 0;
Chris@16 417 }
Chris@16 418
Chris@16 419 // Connect the socket to the specified endpoint.
Chris@16 420 boost::system::error_code connect(implementation_type& impl,
Chris@16 421 const endpoint_type& peer_endpoint, boost::system::error_code& ec)
Chris@16 422 {
Chris@16 423 socket_ops::sync_connect(impl.socket_,
Chris@16 424 peer_endpoint.data(), peer_endpoint.size(), ec);
Chris@16 425 return ec;
Chris@16 426 }
Chris@16 427
Chris@16 428 // Start an asynchronous connect.
Chris@16 429 template <typename Handler>
Chris@16 430 void async_connect(implementation_type& impl,
Chris@16 431 const endpoint_type& peer_endpoint, Handler& handler)
Chris@16 432 {
Chris@16 433 bool is_continuation =
Chris@16 434 boost_asio_handler_cont_helpers::is_continuation(handler);
Chris@16 435
Chris@16 436 // Allocate and construct an operation to wrap the handler.
Chris@16 437 typedef reactive_socket_connect_op<Handler> op;
Chris@16 438 typename op::ptr p = { boost::asio::detail::addressof(handler),
Chris@16 439 boost_asio_handler_alloc_helpers::allocate(
Chris@16 440 sizeof(op), handler), 0 };
Chris@16 441 p.p = new (p.v) op(impl.socket_, handler);
Chris@16 442
Chris@16 443 BOOST_ASIO_HANDLER_CREATION((p.p, "socket", &impl, "async_connect"));
Chris@16 444
Chris@16 445 start_connect_op(impl, p.p, is_continuation,
Chris@16 446 peer_endpoint.data(), peer_endpoint.size());
Chris@16 447 p.v = p.p = 0;
Chris@16 448 }
Chris@16 449 };
Chris@16 450
Chris@16 451 } // namespace detail
Chris@16 452 } // namespace asio
Chris@16 453 } // namespace boost
Chris@16 454
Chris@16 455 #include <boost/asio/detail/pop_options.hpp>
Chris@16 456
Chris@16 457 #endif // !defined(BOOST_ASIO_HAS_IOCP)
Chris@16 458
Chris@16 459 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_HPP