11 #ifndef ASIO_BASIC_SOCKET_HPP 12 #define ASIO_BASIC_SOCKET_HPP 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 18 #include "asio/any_io_executor.hpp" 19 #include "asio/detail/config.hpp" 20 #include "asio/async_result.hpp" 21 #include "asio/detail/handler_type_requirements.hpp" 22 #include "asio/detail/io_object_impl.hpp" 23 #include "asio/detail/non_const_lvalue.hpp" 24 #include "asio/detail/throw_error.hpp" 25 #include "asio/detail/type_traits.hpp" 26 #include "asio/error.hpp" 27 #include "asio/execution_context.hpp" 28 #include "asio/post.hpp" 29 #include "asio/socket_base.hpp" 31 #if defined(ASIO_WINDOWS_RUNTIME) 32 # include "asio/detail/null_socket_service.hpp" 33 #elif defined(ASIO_HAS_IOCP) 34 # include "asio/detail/win_iocp_socket_service.hpp" 36 # include "asio/detail/reactive_socket_service.hpp" 39 #if defined(ASIO_HAS_MOVE) 41 #endif // defined(ASIO_HAS_MOVE) 43 #include "asio/detail/push_options.hpp" 47 #if !defined(ASIO_BASIC_SOCKET_FWD_DECL) 48 #define ASIO_BASIC_SOCKET_FWD_DECL 51 template <
typename Protocol,
typename Executor = any_io_executor>
54 #endif // !defined(ASIO_BASIC_SOCKET_FWD_DECL) 65 template <
typename Protocol,
typename Executor>
74 template <
typename Executor1>
82 #if defined(GENERATING_DOCUMENTATION) 84 #elif defined(ASIO_WINDOWS_RUNTIME) 85 typedef typename detail::null_socket_service<
87 #elif defined(ASIO_HAS_IOCP) 88 typedef typename detail::win_iocp_socket_service<
101 #if !defined(ASIO_NO_EXTENSIONS) 104 #endif // !defined(ASIO_NO_EXTENSIONS) 126 template <
typename ExecutionContext>
129 is_convertible<ExecutionContext&, execution_context&>::value
150 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
151 asio::detail::throw_error(ec,
"open");
166 template <
typename ExecutionContext>
169 is_convertible<ExecutionContext&, execution_context&>::value
174 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
175 asio::detail::throw_error(ec,
"open");
197 const protocol_type protocol = endpoint.protocol();
198 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
199 asio::detail::throw_error(ec,
"open");
200 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
201 asio::detail::throw_error(ec,
"bind");
220 template <
typename ExecutionContext>
223 is_convertible<ExecutionContext&, execution_context&>::value
228 const protocol_type protocol = endpoint.protocol();
229 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
230 asio::detail::throw_error(ec,
"open");
231 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
232 asio::detail::throw_error(ec,
"bind");
249 const native_handle_type& native_socket)
253 impl_.get_service().
assign(impl_.get_implementation(),
254 protocol, native_socket, ec);
255 asio::detail::throw_error(ec,
"assign");
272 template <
typename ExecutionContext>
274 const native_handle_type& native_socket,
276 is_convertible<ExecutionContext&, execution_context&>::value
281 impl_.get_service().
assign(impl_.get_implementation(),
282 protocol, native_socket, ec);
283 asio::detail::throw_error(ec,
"assign");
286 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) 298 : impl_(std::move(
other.impl_))
314 impl_ = std::move(
other.impl_);
319 template <
typename Protocol1,
typename Executor1>
332 template <
typename Protocol1,
typename Executor1>
335 is_convertible<Protocol1, Protocol>::value
336 && is_convertible<Executor1, Executor>::value
338 : impl_(std::move(
other.impl_))
352 template <
typename Protocol1,
typename Executor1>
354 is_convertible<Protocol1, Protocol>::value
355 && is_convertible<Executor1, Executor>::value,
360 impl_ = std::move(tmp.impl_);
363 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) 368 return impl_.get_executor();
371 #if !defined(ASIO_NO_EXTENSIONS) 399 #endif // !defined(ASIO_NO_EXTENSIONS) 418 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
419 asio::detail::throw_error(ec,
"open");
441 ASIO_SYNC_OP_VOID
open(
const protocol_type& protocol,
444 impl_.get_service().open(impl_.get_implementation(), protocol, ec);
445 ASIO_SYNC_OP_VOID_RETURN(ec);
458 void assign(
const protocol_type& protocol,
459 const native_handle_type& native_socket)
462 impl_.get_service().
assign(impl_.get_implementation(),
463 protocol, native_socket, ec);
464 asio::detail::throw_error(ec,
"assign");
477 ASIO_SYNC_OP_VOID
assign(
const protocol_type& protocol,
480 impl_.get_service().assign(impl_.get_implementation(),
481 protocol, native_socket, ec);
482 ASIO_SYNC_OP_VOID_RETURN(ec);
488 return impl_.get_service().is_open(impl_.get_implementation());
506 impl_.get_service().close(impl_.get_implementation(), ec);
507 asio::detail::throw_error(ec,
"close");
536 impl_.get_service().close(impl_.get_implementation(), ec);
537 ASIO_SYNC_OP_VOID_RETURN(ec);
553 #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \ 554 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603) 555 __declspec(deprecated(
"This function always fails with " 556 "operation_not_supported when used on Windows versions " 557 "prior to Windows 8.1."))
562 native_handle_type
s = impl_.get_service().release(
563 impl_.get_implementation(), ec);
564 asio::detail::throw_error(ec,
"release");
581 #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \ 582 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0603) 583 __declspec(deprecated(
"This function always fails with " 584 "operation_not_supported when used on Windows versions " 585 "prior to Windows 8.1."))
589 return impl_.get_service().release(impl_.get_implementation(), ec);
600 return impl_.get_service().native_handle(impl_.get_implementation());
637 #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \ 638 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \ 639 && !defined(ASIO_ENABLE_CANCELIO) 640 __declspec(deprecated(
"By default, this function always fails with " 641 "operation_not_supported when used on Windows XP, Windows Server 2003, " 642 "or earlier. Consult documentation for details."))
647 impl_.get_service().cancel(impl_.get_implementation(), ec);
648 asio::detail::throw_error(ec,
"cancel");
685 #if defined(ASIO_MSVC) && (ASIO_MSVC >= 1400) \ 686 && (!defined(_WIN32_WINNT) || _WIN32_WINNT < 0x0600) \ 687 && !defined(ASIO_ENABLE_CANCELIO) 688 __declspec(deprecated(
"By default, this function always fails with " 689 "operation_not_supported when used on Windows XP, Windows Server 2003, " 690 "or earlier. Consult documentation for details."))
694 impl_.get_service().cancel(impl_.get_implementation(), ec);
695 ASIO_SYNC_OP_VOID_RETURN(ec);
711 bool b = impl_.get_service().at_mark(impl_.get_implementation(), ec);
712 asio::detail::throw_error(ec,
"at_mark");
728 return impl_.get_service().at_mark(impl_.get_implementation(), ec);
744 std::size_t
s = impl_.get_service().available(
745 impl_.get_implementation(), ec);
746 asio::detail::throw_error(ec,
"available");
762 return impl_.get_service().available(impl_.get_implementation(), ec);
783 void bind(
const endpoint_type& endpoint)
786 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
787 asio::detail::throw_error(ec,
"bind");
813 ASIO_SYNC_OP_VOID
bind(
const endpoint_type& endpoint,
816 impl_.get_service().bind(impl_.get_implementation(), endpoint, ec);
817 ASIO_SYNC_OP_VOID_RETURN(ec);
843 void connect(
const endpoint_type& peer_endpoint)
848 impl_.get_service().open(impl_.get_implementation(),
849 peer_endpoint.protocol(), ec);
850 asio::detail::throw_error(ec,
"connect");
852 impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
853 asio::detail::throw_error(ec,
"connect");
884 ASIO_SYNC_OP_VOID
connect(
const endpoint_type& peer_endpoint,
889 impl_.get_service().open(impl_.get_implementation(),
890 peer_endpoint.protocol(), ec);
893 ASIO_SYNC_OP_VOID_RETURN(ec);
897 impl_.get_service().connect(impl_.get_implementation(), peer_endpoint, ec);
898 ASIO_SYNC_OP_VOID_RETURN(ec);
944 ConnectHandler ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
947 async_connect(
const endpoint_type& peer_endpoint,
948 ASIO_MOVE_ARG(ConnectHandler) handler
949 ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
954 const protocol_type protocol = peer_endpoint.protocol();
955 impl_.get_service().open(impl_.get_implementation(), protocol, open_ec);
958 return async_initiate<ConnectHandler, void (asio::error_code)>(
959 initiate_async_connect(
this), handler, peer_endpoint, open_ec);
996 template <
typename SettableSocketOption>
1000 impl_.get_service().set_option(impl_.get_implementation(), option, ec);
1001 asio::detail::throw_error(ec,
"set_option");
1043 template <
typename SettableSocketOption>
1047 impl_.get_service().set_option(impl_.get_implementation(), option, ec);
1048 ASIO_SYNC_OP_VOID_RETURN(ec);
1086 template <
typename GettableSocketOption>
1090 impl_.get_service().get_option(impl_.get_implementation(), option, ec);
1091 asio::detail::throw_error(ec,
"get_option");
1134 template <
typename GettableSocketOption>
1138 impl_.get_service().get_option(impl_.get_implementation(), option, ec);
1139 ASIO_SYNC_OP_VOID_RETURN(ec);
1164 template <
typename IoControlCommand>
1168 impl_.get_service().io_control(impl_.get_implementation(), command, ec);
1169 asio::detail::throw_error(ec,
"io_control");
1199 template <
typename IoControlCommand>
1203 impl_.get_service().io_control(impl_.get_implementation(), command, ec);
1204 ASIO_SYNC_OP_VOID_RETURN(ec);
1220 return impl_.get_service().non_blocking(impl_.get_implementation());
1239 impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
1240 asio::detail::throw_error(ec,
"non_blocking");
1259 impl_.get_service().non_blocking(impl_.get_implementation(), mode, ec);
1260 ASIO_SYNC_OP_VOID_RETURN(ec);
1349 return impl_.get_service().native_non_blocking(impl_.get_implementation());
1440 impl_.get_service().native_non_blocking(
1441 impl_.get_implementation(), mode, ec);
1442 asio::detail::throw_error(ec,
"native_non_blocking");
1533 impl_.get_service().native_non_blocking(
1534 impl_.get_implementation(), mode, ec);
1535 ASIO_SYNC_OP_VOID_RETURN(ec);
1556 endpoint_type ep = impl_.get_service().local_endpoint(
1557 impl_.get_implementation(), ec);
1558 asio::detail::throw_error(ec,
"local_endpoint");
1585 return impl_.get_service().local_endpoint(impl_.get_implementation(), ec);
1606 endpoint_type ep = impl_.get_service().remote_endpoint(
1607 impl_.get_implementation(), ec);
1608 asio::detail::throw_error(ec,
"remote_endpoint");
1635 return impl_.get_service().remote_endpoint(impl_.get_implementation(), ec);
1658 impl_.get_service().shutdown(impl_.get_implementation(), what, ec);
1659 asio::detail::throw_error(ec,
"shutdown");
1687 impl_.get_service().shutdown(impl_.get_implementation(), what, ec);
1688 ASIO_SYNC_OP_VOID_RETURN(ec);
1710 impl_.get_service().wait(impl_.get_implementation(), w, ec);
1711 asio::detail::throw_error(ec,
"wait");
1735 impl_.get_service().wait(impl_.get_implementation(), w, ec);
1736 ASIO_SYNC_OP_VOID_RETURN(ec);
1777 WaitHandler ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
1781 ASIO_MOVE_ARG(WaitHandler) handler
1782 ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
1784 return async_initiate<WaitHandler, void (asio::error_code)>(
1785 initiate_async_wait(
this), handler, w);
1798 #if defined(ASIO_WINDOWS_RUNTIME) 1800 detail::null_socket_service<Protocol>, Executor> impl_;
1801 #elif defined(ASIO_HAS_IOCP) 1803 detail::win_iocp_socket_service<Protocol>, Executor> impl_;
1814 class initiate_async_connect
1826 return self_->get_executor();
1829 template <
typename ConnectHandler>
1830 void operator()(ASIO_MOVE_ARG(ConnectHandler) handler,
1831 const endpoint_type& peer_endpoint,
1836 ASIO_CONNECT_HANDLER_CHECK(ConnectHandler, handler) type_check;
1840 asio::post(self_->impl_.get_executor(),
1841 asio::detail::bind_handler(
1842 ASIO_MOVE_CAST(ConnectHandler)(handler), open_ec));
1847 self_->impl_.get_service().async_connect(
1848 self_->impl_.get_implementation(), peer_endpoint,
1849 handler2.value, self_->impl_.get_executor());
1857 class initiate_async_wait
1869 return self_->get_executor();
1872 template <
typename WaitHandler>
1873 void operator()(ASIO_MOVE_ARG(WaitHandler) handler,
wait_type w)
const 1877 ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
1880 self_->impl_.get_service().async_wait(
1881 self_->impl_.get_implementation(), w,
1882 handler2.value, self_->impl_.get_executor());
1892 #include "asio/detail/pop_options.hpp" 1894 #endif // ASIO_BASIC_SOCKET_HPP Protocol protocol_type
The protocol type.
Definition: basic_socket.hpp:96
ASIO_SYNC_OP_VOID shutdown(shutdown_type what, asio::error_code &ec)
Disable sends or receives on the socket.
Definition: basic_socket.hpp:1684
bool at_mark() const
Determine whether the socket is at the out-of-band data mark.
Definition: basic_socket.hpp:708
The socket_base class is used as a base for the basic_stream_socket and basic_datagram_socket class t...
Definition: socket_base.hpp:30
ASIO_SYNC_OP_VOID close(asio::error_code &ec)
Close the socket.
Definition: basic_socket.hpp:534
endpoint_type local_endpoint() const
Get the local endpoint of the socket.
Definition: basic_socket.hpp:1553
Definition: blocking.hpp:208
wait_type
Wait types.
Definition: socket_base.hpp:82
ASIO_SYNC_OP_VOID set_option(const SettableSocketOption &option, asio::error_code &ec)
Set an option on the socket.
Definition: basic_socket.hpp:1044
Executor executor_type
The type of the executor associated with the object.
Definition: basic_socket.hpp:71
void non_blocking(bool mode)
Sets the non-blocking mode of the socket.
Definition: basic_socket.hpp:1236
basic_socket(const executor_type &ex, const protocol_type &protocol, const native_handle_type &native_socket)
Construct a basic_socket on an existing native socket.
Definition: basic_socket.hpp:248
executor_type get_executor() ASIO_NOEXCEPT
Get the executor associated with the object.
Definition: basic_socket.hpp:366
void open(const protocol_type &protocol=protocol_type())
Open the socket using the specified protocol.
Definition: basic_socket.hpp:415
ASIO_SYNC_OP_VOID connect(const endpoint_type &peer_endpoint, asio::error_code &ec)
Connect the socket to the specified endpoint.
Definition: basic_socket.hpp:884
basic_socket< Protocol, Executor > lowest_layer_type
A basic_socket is always the lowest layer.
Definition: basic_socket.hpp:103
lowest_layer_type & lowest_layer()
Get a reference to the lowest layer.
Definition: basic_socket.hpp:381
basic_socket(const executor_type &ex)
Construct a basic_socket without opening it.
Definition: basic_socket.hpp:113
ASIO_SYNC_OP_VOID io_control(IoControlCommand &command, asio::error_code &ec)
Perform an IO control command on the socket.
Definition: basic_socket.hpp:1200
basic_socket(ExecutionContext &context, const protocol_type &protocol, const native_handle_type &native_socket, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
Construct a basic_socket on an existing native socket.
Definition: basic_socket.hpp:273
basic_socket< Protocol, Executor1 > other
The socket type when rebound to the specified executor.
Definition: basic_socket.hpp:78
basic_socket(ExecutionContext &context, const protocol_type &protocol, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
Construct and open a basic_socket.
Definition: basic_socket.hpp:167
basic_socket(ExecutionContext &context, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
Construct a basic_socket without opening it.
Definition: basic_socket.hpp:127
void shutdown(shutdown_type what)
Disable sends or receives on the socket.
Definition: basic_socket.hpp:1655
ASIO_SYNC_OP_VOID open(const protocol_type &protocol, asio::error_code &ec)
Open the socket using the specified protocol.
Definition: basic_socket.hpp:441
bool at_mark(asio::error_code &ec) const
Determine whether the socket is at the out-of-band data mark.
Definition: basic_socket.hpp:726
void wait(wait_type w)
Wait for the socket to become ready to read, ready to write, or to have pending error conditions...
Definition: basic_socket.hpp:1707
endpoint_type remote_endpoint() const
Get the remote endpoint of the socket.
Definition: basic_socket.hpp:1603
const lowest_layer_type & lowest_layer() const
Get a const reference to the lowest layer.
Definition: basic_socket.hpp:395
bool is_open() const
Determine whether the socket is open.
Definition: basic_socket.hpp:486
void assign(const protocol_type &protocol, const native_handle_type &native_socket)
Assign an existing native socket to the socket.
Definition: basic_socket.hpp:458
Definition: type_traits.hpp:97
Definition: non_const_lvalue.hpp:27
native_handle_type release()
Release ownership of the underlying native socket.
Definition: basic_socket.hpp:559
basic_socket(ExecutionContext &context, const endpoint_type &endpoint, typename enable_if< is_convertible< ExecutionContext &, execution_context &>::value >::type *=0)
Construct a basic_socket, opening it and binding it to the given local endpoint.
Definition: basic_socket.hpp:221
detail::reactive_socket_service< Protocol >::native_handle_type native_handle_type
The native representation of a socket.
Definition: basic_socket.hpp:92
ASIO_SYNC_OP_VOID wait(wait_type w, asio::error_code &ec)
Wait for the socket to become ready to read, ready to write, or to have pending error conditions...
Definition: basic_socket.hpp:1733
void set_option(const SettableSocketOption &option)
Set an option on the socket.
Definition: basic_socket.hpp:997
ASIO_SYNC_OP_VOID bind(const endpoint_type &endpoint, asio::error_code &ec)
Bind the socket to the given local endpoint.
Definition: basic_socket.hpp:813
shutdown_type
Different ways a socket may be shutdown.
Definition: socket_base.hpp:34
~basic_socket()
Protected destructor to prevent deletion through this type.
Definition: basic_socket.hpp:1794
void native_non_blocking(bool mode)
Sets the non-blocking mode of the native socket implementation.
Definition: basic_socket.hpp:1437
void close()
Close the socket.
Definition: basic_socket.hpp:503
Class to represent an error code value.
Definition: error_code.hpp:80
Definition: io_object_impl.hpp:33
native_handle_type release(asio::error_code &ec)
Release ownership of the underlying native socket.
Definition: basic_socket.hpp:587
void bind(const endpoint_type &endpoint)
Bind the socket to the given local endpoint.
Definition: basic_socket.hpp:783
bool non_blocking() const
Gets the non-blocking mode of the socket.
Definition: basic_socket.hpp:1218
Definition: reactive_socket_service.hpp:47
void io_control(IoControlCommand &command)
Perform an IO control command on the socket.
Definition: basic_socket.hpp:1165
Protocol::endpoint endpoint_type
The endpoint type.
Definition: basic_socket.hpp:99
ASIO_SYNC_OP_VOID assign(const protocol_type &protocol, const native_handle_type &native_socket, asio::error_code &ec)
Assign an existing native socket to the socket.
Definition: basic_socket.hpp:477
basic_socket(const executor_type &ex, const protocol_type &protocol)
Construct and open a basic_socket.
Definition: basic_socket.hpp:146
Provides socket functionality.
Definition: basic_socket.hpp:52
std::size_t available() const
Determine the number of bytes available for reading.
Definition: basic_socket.hpp:741
endpoint_type local_endpoint(asio::error_code &ec) const
Get the local endpoint of the socket.
Definition: basic_socket.hpp:1583
ASIO_SYNC_OP_VOID get_option(GettableSocketOption &option, asio::error_code &ec) const
Get an option from the socket.
Definition: basic_socket.hpp:1135
basic_socket(const executor_type &ex, const endpoint_type &endpoint)
Construct a basic_socket, opening it and binding it to the given local endpoint.
Definition: basic_socket.hpp:193
endpoint_type remote_endpoint(asio::error_code &ec) const
Get the remote endpoint of the socket.
Definition: basic_socket.hpp:1633
void get_option(GettableSocketOption &option) const
Get an option from the socket.
Definition: basic_socket.hpp:1087
bool native_non_blocking() const
Gets the non-blocking mode of the native socket implementation.
Definition: basic_socket.hpp:1347
Rebinds the socket type to another executor.
Definition: basic_socket.hpp:75
ASIO_SYNC_OP_VOID cancel(asio::error_code &ec)
Cancel all asynchronous operations associated with the socket.
Definition: basic_socket.hpp:692
ASIO_SYNC_OP_VOID native_non_blocking(bool mode, asio::error_code &ec)
Sets the non-blocking mode of the native socket implementation.
Definition: basic_socket.hpp:1530
ASIO_SYNC_OP_VOID non_blocking(bool mode, asio::error_code &ec)
Sets the non-blocking mode of the socket.
Definition: basic_socket.hpp:1256
Definition: any_io_executor.hpp:28
std::size_t available(asio::error_code &ec) const
Determine the number of bytes available for reading.
Definition: basic_socket.hpp:760
native_handle_type native_handle()
Get the native socket representation.
Definition: basic_socket.hpp:598
void assign(int v, const error_category &c)
Assign a new error value.
Definition: error_code.hpp:112
ASIO_INITFN_AUTO_RESULT_TYPE(ConnectHandler, void(asio::error_code)) async_connect(const endpoint_type &peer_endpoint
Start an asynchronous connect.
void connect(const endpoint_type &peer_endpoint)
Connect the socket to the specified endpoint.
Definition: basic_socket.hpp:843
void cancel()
Cancel all asynchronous operations associated with the socket.
Definition: basic_socket.hpp:644