diff options
Diffstat (limited to 'boost/asio/basic_stream_socket.hpp')
-rw-r--r-- | boost/asio/basic_stream_socket.hpp | 161 |
1 files changed, 115 insertions, 46 deletions
diff --git a/boost/asio/basic_stream_socket.hpp b/boost/asio/basic_stream_socket.hpp index 5b959d7db8..709e214a37 100644 --- a/boost/asio/basic_stream_socket.hpp +++ b/boost/asio/basic_stream_socket.hpp @@ -22,7 +22,10 @@ #include <boost/asio/detail/handler_type_requirements.hpp> #include <boost/asio/detail/throw_error.hpp> #include <boost/asio/error.hpp> -#include <boost/asio/stream_socket_service.hpp> + +#if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) +# include <boost/asio/stream_socket_service.hpp> +#endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) #include <boost/asio/detail/push_options.hpp> @@ -41,18 +44,19 @@ namespace asio { * @par Concepts: * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream. */ -template <typename Protocol, - typename StreamSocketService = stream_socket_service<Protocol> > +template <typename Protocol + BOOST_ASIO_SVC_TPARAM_DEF1(= stream_socket_service<Protocol>)> class basic_stream_socket - : public basic_socket<Protocol, StreamSocketService> + : public basic_socket<Protocol BOOST_ASIO_SVC_TARG> { public: - /// (Deprecated: Use native_handle_type.) The native representation of a - /// socket. - typedef typename StreamSocketService::native_handle_type native_type; - /// The native representation of a socket. - typedef typename StreamSocketService::native_handle_type native_handle_type; +#if defined(GENERATING_DOCUMENTATION) + typedef implementation_defined native_handle_type; +#else + typedef typename basic_socket< + Protocol BOOST_ASIO_SVC_TARG>::native_handle_type native_handle_type; +#endif /// The protocol type. typedef Protocol protocol_type; @@ -66,11 +70,11 @@ public: * needs to be opened and then connected or accepted before data can be sent * or received on it. * - * @param io_service The io_service object that the stream socket will use to + * @param io_context The io_context object that the stream socket will use to * dispatch handlers for any asynchronous operations performed on the socket. */ - explicit basic_stream_socket(boost::asio::io_service& io_service) - : basic_socket<Protocol, StreamSocketService>(io_service) + explicit basic_stream_socket(boost::asio::io_context& io_context) + : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context) { } @@ -79,16 +83,16 @@ public: * This constructor creates and opens a stream socket. The socket needs to be * connected or accepted before data can be sent or received on it. * - * @param io_service The io_service object that the stream socket will use to + * @param io_context The io_context object that the stream socket will use to * dispatch handlers for any asynchronous operations performed on the socket. * * @param protocol An object specifying protocol parameters to be used. * * @throws boost::system::system_error Thrown on failure. */ - basic_stream_socket(boost::asio::io_service& io_service, + basic_stream_socket(boost::asio::io_context& io_context, const protocol_type& protocol) - : basic_socket<Protocol, StreamSocketService>(io_service, protocol) + : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context, protocol) { } @@ -99,7 +103,7 @@ public: * to the specified endpoint on the local machine. The protocol used is the * protocol associated with the given endpoint. * - * @param io_service The io_service object that the stream socket will use to + * @param io_context The io_context object that the stream socket will use to * dispatch handlers for any asynchronous operations performed on the socket. * * @param endpoint An endpoint on the local machine to which the stream @@ -107,9 +111,9 @@ public: * * @throws boost::system::system_error Thrown on failure. */ - basic_stream_socket(boost::asio::io_service& io_service, + basic_stream_socket(boost::asio::io_context& io_context, const endpoint_type& endpoint) - : basic_socket<Protocol, StreamSocketService>(io_service, endpoint) + : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(io_context, endpoint) { } @@ -118,7 +122,7 @@ public: * This constructor creates a stream socket object to hold an existing native * socket. * - * @param io_service The io_service object that the stream socket will use to + * @param io_context The io_context object that the stream socket will use to * dispatch handlers for any asynchronous operations performed on the socket. * * @param protocol An object specifying protocol parameters to be used. @@ -127,10 +131,10 @@ public: * * @throws boost::system::system_error Thrown on failure. */ - basic_stream_socket(boost::asio::io_service& io_service, + basic_stream_socket(boost::asio::io_context& io_context, const protocol_type& protocol, const native_handle_type& native_socket) - : basic_socket<Protocol, StreamSocketService>( - io_service, protocol, native_socket) + : basic_socket<Protocol BOOST_ASIO_SVC_TARG>( + io_context, protocol, native_socket) { } @@ -143,11 +147,10 @@ public: * will occur. * * @note Following the move, the moved-from object is in the same state as if - * constructed using the @c basic_stream_socket(io_service&) constructor. + * constructed using the @c basic_stream_socket(io_context&) constructor. */ basic_stream_socket(basic_stream_socket&& other) - : basic_socket<Protocol, StreamSocketService>( - BOOST_ASIO_MOVE_CAST(basic_stream_socket)(other)) + : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(other)) { } @@ -159,12 +162,11 @@ public: * will occur. * * @note Following the move, the moved-from object is in the same state as if - * constructed using the @c basic_stream_socket(io_service&) constructor. + * constructed using the @c basic_stream_socket(io_context&) constructor. */ basic_stream_socket& operator=(basic_stream_socket&& other) { - basic_socket<Protocol, StreamSocketService>::operator=( - BOOST_ASIO_MOVE_CAST(basic_stream_socket)(other)); + basic_socket<Protocol BOOST_ASIO_SVC_TARG>::operator=(std::move(other)); return *this; } @@ -177,15 +179,13 @@ public: * will occur. * * @note Following the move, the moved-from object is in the same state as if - * constructed using the @c basic_stream_socket(io_service&) constructor. + * constructed using the @c basic_stream_socket(io_context&) constructor. */ - template <typename Protocol1, typename StreamSocketService1> + template <typename Protocol1 BOOST_ASIO_SVC_TPARAM1> basic_stream_socket( - basic_stream_socket<Protocol1, StreamSocketService1>&& other, + basic_stream_socket<Protocol1 BOOST_ASIO_SVC_TARG1>&& other, typename enable_if<is_convertible<Protocol1, Protocol>::value>::type* = 0) - : basic_socket<Protocol, StreamSocketService>( - BOOST_ASIO_MOVE_CAST2(basic_stream_socket< - Protocol1, StreamSocketService1>)(other)) + : basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(other)) { } @@ -197,20 +197,27 @@ public: * will occur. * * @note Following the move, the moved-from object is in the same state as if - * constructed using the @c basic_stream_socket(io_service&) constructor. + * constructed using the @c basic_stream_socket(io_context&) constructor. */ - template <typename Protocol1, typename StreamSocketService1> + template <typename Protocol1 BOOST_ASIO_SVC_TPARAM1> typename enable_if<is_convertible<Protocol1, Protocol>::value, basic_stream_socket>::type& operator=( - basic_stream_socket<Protocol1, StreamSocketService1>&& other) + basic_stream_socket<Protocol1 BOOST_ASIO_SVC_TARG1>&& other) { - basic_socket<Protocol, StreamSocketService>::operator=( - BOOST_ASIO_MOVE_CAST2(basic_stream_socket< - Protocol1, StreamSocketService1>)(other)); + basic_socket<Protocol BOOST_ASIO_SVC_TARG>::operator=(std::move(other)); return *this; } #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) + /// Destroys the socket. + /** + * This function destroys the socket, cancelling any outstanding asynchronous + * operations associated with the socket as if by calling @c cancel. + */ + ~basic_stream_socket() + { + } + /// Send some data on the socket. /** * This function is used to send data on the stream socket. The function @@ -330,7 +337,7 @@ public: * Regardless of whether the asynchronous operation completes immediately or * not, the handler will not be invoked from within this function. Invocation * of the handler will be performed in a manner equivalent to using - * boost::asio::io_service::post(). + * boost::asio::io_context::post(). * * @note The send operation may not transmit all of the data to the peer. * Consider using the @ref async_write function if you need to ensure that all @@ -355,9 +362,20 @@ public: // not meet the documented type requirements for a WriteHandler. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; +#if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) return this->get_service().async_send( this->get_implementation(), buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); +#else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) + async_completion<WriteHandler, + void (boost::system::error_code, std::size_t)> init(handler); + + this->get_service().async_send( + this->get_implementation(), buffers, 0, + init.completion_handler); + + return init.result.get(); +#endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) } /// Start an asynchronous send. @@ -382,7 +400,7 @@ public: * Regardless of whether the asynchronous operation completes immediately or * not, the handler will not be invoked from within this function. Invocation * of the handler will be performed in a manner equivalent to using - * boost::asio::io_service::post(). + * boost::asio::io_context::post(). * * @note The send operation may not transmit all of the data to the peer. * Consider using the @ref async_write function if you need to ensure that all @@ -408,9 +426,20 @@ public: // not meet the documented type requirements for a WriteHandler. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; +#if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) return this->get_service().async_send( this->get_implementation(), buffers, flags, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); +#else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) + async_completion<WriteHandler, + void (boost::system::error_code, std::size_t)> init(handler); + + this->get_service().async_send( + this->get_implementation(), buffers, flags, + init.completion_handler); + + return init.result.get(); +#endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) } /// Receive some data on the socket. @@ -538,7 +567,7 @@ public: * Regardless of whether the asynchronous operation completes immediately or * not, the handler will not be invoked from within this function. Invocation * of the handler will be performed in a manner equivalent to using - * boost::asio::io_service::post(). + * boost::asio::io_context::post(). * * @note The receive operation may not receive all of the requested number of * bytes. Consider using the @ref async_read function if you need to ensure @@ -565,8 +594,18 @@ public: // not meet the documented type requirements for a ReadHandler. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; +#if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) return this->get_service().async_receive(this->get_implementation(), buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); +#else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) + async_completion<ReadHandler, + void (boost::system::error_code, std::size_t)> init(handler); + + this->get_service().async_receive(this->get_implementation(), + buffers, 0, init.completion_handler); + + return init.result.get(); +#endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) } /// Start an asynchronous receive. @@ -591,7 +630,7 @@ public: * Regardless of whether the asynchronous operation completes immediately or * not, the handler will not be invoked from within this function. Invocation * of the handler will be performed in a manner equivalent to using - * boost::asio::io_service::post(). + * boost::asio::io_context::post(). * * @note The receive operation may not receive all of the requested number of * bytes. Consider using the @ref async_read function if you need to ensure @@ -619,8 +658,18 @@ public: // not meet the documented type requirements for a ReadHandler. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; +#if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) return this->get_service().async_receive(this->get_implementation(), buffers, flags, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); +#else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) + async_completion<ReadHandler, + void (boost::system::error_code, std::size_t)> init(handler); + + this->get_service().async_receive(this->get_implementation(), + buffers, flags, init.completion_handler); + + return init.result.get(); +#endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) } /// Write some data to the socket. @@ -703,7 +752,7 @@ public: * Regardless of whether the asynchronous operation completes immediately or * not, the handler will not be invoked from within this function. Invocation * of the handler will be performed in a manner equivalent to using - * boost::asio::io_service::post(). + * boost::asio::io_context::post(). * * @note The write operation may not transmit all of the data to the peer. * Consider using the @ref async_write function if you need to ensure that all @@ -728,8 +777,18 @@ public: // not meet the documented type requirements for a WriteHandler. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check; +#if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) return this->get_service().async_send(this->get_implementation(), buffers, 0, BOOST_ASIO_MOVE_CAST(WriteHandler)(handler)); +#else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) + async_completion<WriteHandler, + void (boost::system::error_code, std::size_t)> init(handler); + + this->get_service().async_send(this->get_implementation(), + buffers, 0, init.completion_handler); + + return init.result.get(); +#endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) } /// Read some data from the socket. @@ -815,7 +874,7 @@ public: * Regardless of whether the asynchronous operation completes immediately or * not, the handler will not be invoked from within this function. Invocation * of the handler will be performed in a manner equivalent to using - * boost::asio::io_service::post(). + * boost::asio::io_context::post(). * * @note The read operation may not read all of the requested number of bytes. * Consider using the @ref async_read function if you need to ensure that the @@ -841,8 +900,18 @@ public: // not meet the documented type requirements for a ReadHandler. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check; +#if defined(BOOST_ASIO_ENABLE_OLD_SERVICES) return this->get_service().async_receive(this->get_implementation(), buffers, 0, BOOST_ASIO_MOVE_CAST(ReadHandler)(handler)); +#else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) + async_completion<ReadHandler, + void (boost::system::error_code, std::size_t)> init(handler); + + this->get_service().async_receive(this->get_implementation(), + buffers, 0, init.completion_handler); + + return init.result.get(); +#endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES) } }; |