From efb4d691b9932230c2041b05f0ad8a1fee15d8af Mon Sep 17 00:00:00 2001 From: chris_kohlhoff Date: Fri, 13 Jan 2006 11:56:05 +0000 Subject: [PATCH] Use protocol-specific socket and acceptor types. The stream_socket, datagram_socket and socket_acceptor typedefs have been removed. --- asio/include/Makefile.am | 8 +- asio/include/asio.hpp | 3 - asio/include/asio/basic_datagram_socket.hpp | 221 +++++++++------ asio/include/asio/basic_socket_acceptor.hpp | 189 ++++++++----- asio/include/asio/basic_stream_socket.hpp | 257 ++++++++++++------ asio/include/asio/datagram_socket.hpp | 32 --- asio/include/asio/datagram_socket_service.hpp | 49 ++-- .../asio/detail/reactive_socket_service.hpp | 12 +- asio/include/asio/ipv4/tcp.hpp | 28 ++ asio/include/asio/ipv4/udp.hpp | 17 ++ asio/include/asio/socket_acceptor.hpp | 32 --- asio/include/asio/socket_acceptor_service.hpp | 53 ++-- asio/include/asio/stream_socket.hpp | 32 --- asio/include/asio/stream_socket_service.hpp | 35 ++- asio/src/Makefile.am | 9 - asio/src/doc/boost/reference_dox.txt | 6 +- asio/src/doc/reference_dox.txt | 6 +- asio/src/examples/chat/chat_client.cpp | 2 +- asio/src/examples/chat/chat_server.cpp | 6 +- .../examples/echo/async_tcp_echo_server.cpp | 6 +- .../examples/echo/async_udp_echo_server.cpp | 2 +- .../echo/blocking_tcp_echo_client.cpp | 2 +- .../echo/blocking_tcp_echo_server.cpp | 8 +- .../echo/blocking_udp_echo_client.cpp | 3 +- .../echo/blocking_udp_echo_server.cpp | 2 +- asio/src/examples/http/server/connection.cpp | 2 +- asio/src/examples/http/server/connection.hpp | 4 +- asio/src/examples/http/server/server.cpp | 2 +- asio/src/examples/http/server/server.hpp | 2 +- asio/src/examples/iostreams/socket_stream.hpp | 4 +- asio/src/examples/multicast/receiver.cpp | 4 +- asio/src/examples/multicast/sender.cpp | 2 +- .../src/examples/serialization/connection.hpp | 10 +- asio/src/examples/serialization/server.cpp | 2 +- asio/src/examples/services/basic_logger.hpp | 2 +- asio/src/examples/services/daytime_client.cpp | 2 +- .../services/stream_socket_service.hpp | 35 ++- asio/src/examples/ssl/client.cpp | 2 +- asio/src/examples/ssl/server.cpp | 6 +- asio/src/examples/timeouts/accept_timeout.cpp | 4 +- .../src/examples/timeouts/connect_timeout.cpp | 4 +- .../timeouts/datagram_receive_timeout.cpp | 2 +- .../timeouts/stream_receive_timeout.cpp | 6 +- .../src/examples/tutorial/daytime1/client.cpp | 2 +- .../src/examples/tutorial/daytime2/server.cpp | 4 +- .../src/examples/tutorial/daytime3/server.cpp | 14 +- .../src/examples/tutorial/daytime4/client.cpp | 2 +- .../src/examples/tutorial/daytime5/server.cpp | 2 +- .../src/examples/tutorial/daytime6/server.cpp | 4 +- .../src/examples/tutorial/daytime7/server.cpp | 18 +- asio/src/examples/tutorial/daytime_dox.txt | 42 +-- asio/src/tests/performance/client.cpp | 2 +- asio/src/tests/performance/server.cpp | 8 +- asio/src/tests/unit/Jamfile | 33 --- .../tests/unit/buffered_read_stream_test.cpp | 10 +- asio/src/tests/unit/buffered_stream_test.cpp | 11 +- .../tests/unit/buffered_write_stream_test.cpp | 10 +- asio/src/tests/unit/datagram_socket_test.cpp | 80 ------ asio/src/tests/unit/error_handler_test.cpp | 2 +- asio/src/tests/unit/ipv4/tcp_test.cpp | 71 +++++ asio/src/tests/unit/ipv4/udp_test.cpp | 60 ++++ asio/src/tests/unit/is_read_buffered_test.cpp | 10 +- .../src/tests/unit/is_write_buffered_test.cpp | 10 +- asio/src/tests/unit/socket_acceptor_test.cpp | 91 ------- asio/src/tests/unit/stream_socket_test.cpp | 20 -- 65 files changed, 852 insertions(+), 769 deletions(-) delete mode 100644 asio/include/asio/datagram_socket.hpp delete mode 100644 asio/include/asio/socket_acceptor.hpp delete mode 100644 asio/include/asio/stream_socket.hpp delete mode 100644 asio/src/tests/unit/datagram_socket_test.cpp delete mode 100644 asio/src/tests/unit/socket_acceptor_test.cpp delete mode 100644 asio/src/tests/unit/stream_socket_test.cpp diff --git a/asio/include/Makefile.am b/asio/include/Makefile.am index 3920be02..f12b923b 100644 --- a/asio/include/Makefile.am +++ b/asio/include/Makefile.am @@ -1,5 +1,4 @@ nobase_include_HEADERS = \ - asio.hpp \ asio/basic_datagram_socket.hpp \ asio/basic_deadline_timer.hpp \ asio/basic_io_service.hpp \ @@ -14,7 +13,6 @@ nobase_include_HEADERS = \ asio/buffered_write_stream.hpp \ asio/buffered_write_stream_fwd.hpp \ asio/completion_condition.hpp \ - asio/datagram_socket.hpp \ asio/datagram_socket_service.hpp \ asio/deadline_timer.hpp \ asio/deadline_timer_service.hpp \ @@ -97,7 +95,6 @@ nobase_include_HEADERS = \ asio/placeholders.hpp \ asio/read.hpp \ asio/service_factory.hpp \ - asio/socket_acceptor.hpp \ asio/socket_acceptor_service.hpp \ asio/socket_base.hpp \ asio/ssl/basic_context.hpp \ @@ -113,12 +110,13 @@ nobase_include_HEADERS = \ asio/ssl/stream_base.hpp \ asio/ssl/stream_service.hpp \ asio/ssl.hpp \ - asio/stream_socket.hpp \ asio/stream_socket_service.hpp \ asio/system_exception.hpp \ asio/thread.hpp \ asio/time_traits.hpp \ - asio/write.hpp + asio/write.hpp \ + asio.hpp MAINTAINERCLEANFILES = \ $(srcdir)/Makefile.in + diff --git a/asio/include/asio.hpp b/asio/include/asio.hpp index 8807db8b..63fc41ea 100644 --- a/asio/include/asio.hpp +++ b/asio/include/asio.hpp @@ -29,7 +29,6 @@ #include "asio/buffered_write_stream_fwd.hpp" #include "asio/buffered_write_stream.hpp" #include "asio/completion_condition.hpp" -#include "asio/datagram_socket.hpp" #include "asio/datagram_socket_service.hpp" #include "asio/deadline_timer_service.hpp" #include "asio/deadline_timer.hpp" @@ -51,10 +50,8 @@ #include "asio/placeholders.hpp" #include "asio/read.hpp" #include "asio/service_factory.hpp" -#include "asio/socket_acceptor.hpp" #include "asio/socket_acceptor_service.hpp" #include "asio/socket_base.hpp" -#include "asio/stream_socket.hpp" #include "asio/stream_socket_service.hpp" #include "asio/system_exception.hpp" #include "asio/thread.hpp" diff --git a/asio/include/asio/basic_datagram_socket.hpp b/asio/include/asio/basic_datagram_socket.hpp index 21b6cb8f..0df6ab5b 100644 --- a/asio/include/asio/basic_datagram_socket.hpp +++ b/asio/include/asio/basic_datagram_socket.hpp @@ -18,6 +18,7 @@ #include "asio/detail/push_options.hpp" #include "asio/detail/push_options.hpp" +#include #include #include #include "asio/detail/pop_options.hpp" @@ -59,6 +60,12 @@ public: /// The io_service type for this type. typedef typename service_type::io_service_type io_service_type; + /// The protocol type. + typedef typename service_type::protocol_type protocol_type; + + /// The endpoint type. + typedef typename service_type::endpoint_type endpoint_type; + /// The type used for reporting errors. typedef asio::error error_type; @@ -75,11 +82,31 @@ public: * socket. */ explicit basic_datagram_socket(io_service_type& io_service) - : service_(io_service.get_service(service_factory())), - impl_(service_.null()) + : service_(&io_service.get_service(service_factory())), + impl_(service_->null()) { } + /// Construct and open a basic_datagram_socket. + /** + * This constructor creates and opens a datagram socket. + * + * @param io_service The io_service object that the datagram 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 asio::error Thrown on failure. + */ + basic_datagram_socket(io_service_type& io_service, + const protocol_type& protocol) + : service_(&io_service.get_service(service_factory())), + impl_(service_->null()) + { + service_->open(impl_, protocol, throw_error()); + } + /// Construct a basic_datagram_socket, opening it and binding it to the given /// local endpoint. /** @@ -87,7 +114,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 datagram socket will usei + * @param io_service The io_service object that the datagram socket will use * to dispatch handlers for any asynchronous operations performed on the * socket. * @@ -96,21 +123,41 @@ public: * * @throws asio::error Thrown on failure. */ - template - basic_datagram_socket(io_service_type& io_service, const Endpoint& endpoint) - : service_(io_service.get_service(service_factory())), - impl_(service_.null()) + basic_datagram_socket(io_service_type& io_service, + const endpoint_type& endpoint) + : service_(&io_service.get_service(service_factory())), + impl_(service_->null()) { - service_.open(impl_, endpoint.protocol(), throw_error()); + service_->open(impl_, endpoint.protocol(), throw_error()); close_on_block_exit auto_close(service_, impl_); - service_.bind(impl_, endpoint, throw_error()); + service_->bind(impl_, endpoint, throw_error()); auto_close.cancel(); } + /// Construct a basic_datagram_socket on an existing implementation. + /** + * This constructor creates a datagram socket to hold an existing + * implementation. + * + * @param io_service The io_service object that the datagram socket will use + * to dispatch handlers for any asynchronous operations performed on the + * socket. + * + * @param impl The new underlying socket implementation. + * + * @throws asio::error Thrown on failure. + */ + basic_datagram_socket(io_service_type& io_service, impl_type impl) + : service_(&io_service.get_service(service_factory())), + impl_(impl) + { + service_->assign(impl_, impl); + } + /// Destructor. ~basic_datagram_socket() { - service_.close(impl_, ignore_error()); + service_->close(impl_, ignore_error()); } /// Get the io_service associated with the object. @@ -123,7 +170,7 @@ public: */ io_service_type& io_service() { - return service_.io_service(); + return service_->io_service(); } /// Open the socket using the specified protocol. @@ -141,10 +188,9 @@ public: * socket.open(asio::ipv4::udp()); * @endcode */ - template - void open(const Protocol& protocol) + void open(const protocol_type& protocol = protocol_type()) { - service_.open(impl_, protocol, throw_error()); + service_->open(impl_, protocol, throw_error()); } /// Open the socket using the specified protocol. @@ -172,10 +218,10 @@ public: * } * @endcode */ - template - void open(const Protocol& protocol, Error_Handler error_handler) + template + void open(const protocol_type& protocol, Error_Handler error_handler) { - service_.open(impl_, protocol, error_handler); + service_->open(impl_, protocol, error_handler); } /// Close the socket. @@ -190,7 +236,7 @@ public: */ void close() { - service_.close(impl_, throw_error()); + service_->close(impl_, throw_error()); } /// Close the socket. @@ -223,7 +269,7 @@ public: template void close(Error_Handler error_handler) { - service_.close(impl_, error_handler); + service_->close(impl_, error_handler); } /// Get a reference to the lowest layer. @@ -260,7 +306,7 @@ public: */ void set_impl(impl_type new_impl) { - service_.assign(impl_, new_impl); + service_->assign(impl_, new_impl); } /// Bind the socket to the given local endpoint. @@ -280,10 +326,9 @@ public: * socket.bind(asio::ipv4::udp::endpoint(12345)); * @endcode */ - template - void bind(const Endpoint& endpoint) + void bind(const endpoint_type& endpoint) { - service_.bind(impl_, endpoint, throw_error()); + service_->bind(impl_, endpoint, throw_error()); } /// Bind the socket to the given local endpoint. @@ -314,10 +359,10 @@ public: * } * @endcode */ - template - void bind(const Endpoint& endpoint, Error_Handler error_handler) + template + void bind(const endpoint_type& endpoint, Error_Handler error_handler) { - service_.bind(impl_, endpoint, error_handler); + service_->bind(impl_, endpoint, error_handler); } /// Connect a datagram socket to the specified endpoint. @@ -343,10 +388,9 @@ public: * socket.connect(endpoint); * @endcode */ - template - void connect(const Endpoint& peer_endpoint) + void connect(const endpoint_type& peer_endpoint) { - service_.connect(impl_, peer_endpoint, throw_error()); + service_->connect(impl_, peer_endpoint, throw_error()); } /// Connect a datagram socket to the specified endpoint. @@ -382,10 +426,10 @@ public: * } * @endcode */ - template - void connect(const Endpoint& peer_endpoint, Error_Handler error_handler) + template + void connect(const endpoint_type& peer_endpoint, Error_Handler error_handler) { - service_.connect(impl_, peer_endpoint, error_handler); + service_->connect(impl_, peer_endpoint, error_handler); } /// Start an asynchronous connect. @@ -429,10 +473,10 @@ public: * socket.async_connect(endpoint, connect_handler); * @endcode */ - template - void async_connect(const Endpoint& peer_endpoint, Handler handler) + template + void async_connect(const endpoint_type& peer_endpoint, Handler handler) { - service_.async_connect(impl_, peer_endpoint, handler); + service_->async_connect(impl_, peer_endpoint, handler); } /// Set an option on the socket. @@ -465,7 +509,7 @@ public: template void set_option(const Socket_Option& option) { - service_.set_option(impl_, option, throw_error()); + service_->set_option(impl_, option, throw_error()); } /// Set an option on the socket. @@ -508,7 +552,7 @@ public: template void set_option(const Socket_Option& option, Error_Handler error_handler) { - service_.set_option(impl_, option, error_handler); + service_->set_option(impl_, option, error_handler); } /// Get an option from the socket. @@ -542,7 +586,7 @@ public: template void get_option(Socket_Option& option) const { - service_.get_option(impl_, option, throw_error()); + service_->get_option(impl_, option, throw_error()); } /// Get an option from the socket. @@ -586,7 +630,7 @@ public: template void get_option(Socket_Option& option, Error_Handler error_handler) const { - service_.get_option(impl_, option, error_handler); + service_->get_option(impl_, option, error_handler); } /// Perform an IO control command on the socket. @@ -614,7 +658,7 @@ public: template void io_control(IO_Control_Command& command) { - service_.io_control(impl_, command, throw_error()); + service_->io_control(impl_, command, throw_error()); } /// Perform an IO control command on the socket. @@ -652,7 +696,7 @@ public: template void io_control(IO_Control_Command& command, Error_Handler error_handler) { - service_.io_control(impl_, command, error_handler); + service_->io_control(impl_, command, error_handler); } /// Get the local endpoint of the socket. @@ -672,10 +716,9 @@ public: * socket.get_local_endpoint(endpoint); * @endcode */ - template - void get_local_endpoint(Endpoint& endpoint) const + void get_local_endpoint(endpoint_type& endpoint) const { - service_.get_local_endpoint(impl_, endpoint, throw_error()); + service_->get_local_endpoint(impl_, endpoint, throw_error()); } /// Get the local endpoint of the socket. @@ -705,11 +748,11 @@ public: * } * @endcode */ - template - void get_local_endpoint(Endpoint& endpoint, + template + void get_local_endpoint(endpoint_type& endpoint, Error_Handler error_handler) const { - service_.get_local_endpoint(impl_, endpoint, error_handler); + service_->get_local_endpoint(impl_, endpoint, error_handler); } /// Get the remote endpoint of the socket. @@ -729,10 +772,9 @@ public: * socket.get_remote_endpoint(endpoint); * @endcode */ - template - void get_remote_endpoint(Endpoint& endpoint) const + void get_remote_endpoint(endpoint_type& endpoint) const { - service_.get_remote_endpoint(impl_, endpoint, throw_error()); + service_->get_remote_endpoint(impl_, endpoint, throw_error()); } /// Get the remote endpoint of the socket. @@ -762,11 +804,11 @@ public: * } * @endcode */ - template - void get_remote_endpoint(Endpoint& endpoint, + template + void get_remote_endpoint(endpoint_type& endpoint, Error_Handler error_handler) const { - service_.get_remote_endpoint(impl_, endpoint, error_handler); + service_->get_remote_endpoint(impl_, endpoint, error_handler); } /// Disable sends or receives on the socket. @@ -788,7 +830,7 @@ public: */ void shutdown(shutdown_type what) { - service_.shutdown(impl_, what, throw_error()); + service_->shutdown(impl_, what, throw_error()); } /// Disable sends or receives on the socket. @@ -822,7 +864,7 @@ public: template void shutdown(shutdown_type what, Error_Handler error_handler) { - service_.shutdown(impl_, what, error_handler); + service_->shutdown(impl_, what, error_handler); } /// Send some data on a connected socket. @@ -852,7 +894,7 @@ public: template std::size_t send(const Const_Buffers& buffers, message_flags flags) { - return service_.send(impl_, buffers, flags, throw_error()); + return service_->send(impl_, buffers, flags, throw_error()); } /// Send some data on a connected socket. @@ -881,7 +923,7 @@ public: std::size_t send(const Const_Buffers& buffers, message_flags flags, Error_Handler error_handler) { - return service_.send(impl_, buffers, flags, error_handler); + return service_->send(impl_, buffers, flags, error_handler); } /// Start an asynchronous send on a connected socket. @@ -926,7 +968,7 @@ public: void async_send(const Const_Buffers& buffers, message_flags flags, Handler handler) { - service_.async_send(impl_, buffers, flags, handler); + service_->async_send(impl_, buffers, flags, handler); } /// Send a datagram to the specified endpoint. @@ -955,11 +997,11 @@ public: * buffers in one go, and how to use it with arrays, boost::array or * std::vector. */ - template + template std::size_t send_to(const Const_Buffers& buffers, message_flags flags, - const Endpoint& destination) + const endpoint_type& destination) { - return service_.send_to(impl_, buffers, flags, destination, throw_error()); + return service_->send_to(impl_, buffers, flags, destination, throw_error()); } /// Send a datagram to the specified endpoint. @@ -983,11 +1025,11 @@ public: * * @returns The number of bytes sent. */ - template + template std::size_t send_to(const Const_Buffers& buffers, message_flags flags, - const Endpoint& destination, Error_Handler error_handler) + const endpoint_type& destination, Error_Handler error_handler) { - return service_.send_to(impl_, buffers, flags, destination, error_handler); + return service_->send_to(impl_, buffers, flags, destination, error_handler); } /// Start an asynchronous send. @@ -1028,11 +1070,11 @@ public: * buffers in one go, and how to use it with arrays, boost::array or * std::vector. */ - template + template void async_send_to(const Const_Buffers& buffers, message_flags flags, - const Endpoint& destination, Handler handler) + const endpoint_type& destination, Handler handler) { - service_.async_send_to(impl_, buffers, flags, destination, handler); + service_->async_send_to(impl_, buffers, flags, destination, handler); } /// Receive some data on a connected socket. @@ -1064,7 +1106,7 @@ public: template std::size_t receive(const Mutable_Buffers& buffers, message_flags flags) { - return service_.receive(impl_, buffers, flags, throw_error()); + return service_->receive(impl_, buffers, flags, throw_error()); } /// Receive some data on a connected socket. @@ -1094,7 +1136,7 @@ public: std::size_t receive(const Mutable_Buffers& buffers, message_flags flags, Error_Handler error_handler) { - return service_.receive(impl_, buffers, flags, error_handler); + return service_->receive(impl_, buffers, flags, error_handler); } /// Start an asynchronous receive on a connected socket. @@ -1139,7 +1181,7 @@ public: void async_receive(const Mutable_Buffers& buffers, message_flags flags, Handler handler) { - service_.async_receive(impl_, buffers, flags, handler); + service_->async_receive(impl_, buffers, flags, handler); } /// Receive a datagram with the endpoint of the sender. @@ -1170,11 +1212,11 @@ public: * multiple buffers in one go, and how to use it with arrays, boost::array or * std::vector. */ - template + template std::size_t receive_from(const Mutable_Buffers& buffers, message_flags flags, - Endpoint& sender_endpoint) + endpoint_type& sender_endpoint) { - return service_.receive_from(impl_, buffers, flags, sender_endpoint, + return service_->receive_from(impl_, buffers, flags, sender_endpoint, throw_error()); } @@ -1199,11 +1241,11 @@ public: * * @returns The number of bytes received. */ - template + template std::size_t receive_from(const Mutable_Buffers& buffers, message_flags flags, - Endpoint& sender_endpoint, Error_Handler error_handler) + endpoint_type& sender_endpoint, Error_Handler error_handler) { - return service_.receive_from(impl_, buffers, flags, sender_endpoint, + return service_->receive_from(impl_, buffers, flags, sender_endpoint, error_handler); } @@ -1245,17 +1287,24 @@ public: * multiple buffers in one go, and how to use it with arrays, boost::array or * std::vector. */ - template + template void async_receive_from(const Mutable_Buffers& buffers, message_flags flags, - Endpoint& sender_endpoint, Handler handler) + endpoint_type& sender_endpoint, Handler handler) { - service_.async_receive_from(impl_, buffers, flags, sender_endpoint, + service_->async_receive_from(impl_, buffers, flags, sender_endpoint, handler); } + /// Swap implementation of socket with another. + void swap(basic_datagram_socket& other) + { + std::swap(service_, other.service_); + std::swap(impl_, other.impl_); + } + private: /// The backend service implementation. - service_type& service_; + service_type* service_; /// The underlying native implementation. impl_type impl_; @@ -1264,8 +1313,8 @@ private: class close_on_block_exit { public: - close_on_block_exit(service_type& service, impl_type& impl) - : service_(&service), impl_(impl) + close_on_block_exit(service_type* service, impl_type& impl) + : service_(service), impl_(impl) { } @@ -1288,6 +1337,14 @@ private: }; }; +/// Swap implementation of socket with another. +template +inline void swap(basic_datagram_socket& a, + basic_datagram_socket& b) +{ + a.swap(b); +} + } // namespace asio #include "asio/detail/pop_options.hpp" diff --git a/asio/include/asio/basic_socket_acceptor.hpp b/asio/include/asio/basic_socket_acceptor.hpp index 8b324223..b84b6f50 100644 --- a/asio/include/asio/basic_socket_acceptor.hpp +++ b/asio/include/asio/basic_socket_acceptor.hpp @@ -17,6 +17,10 @@ #include "asio/detail/push_options.hpp" +#include "asio/detail/push_options.hpp" +#include +#include "asio/detail/pop_options.hpp" + #include "asio/error.hpp" #include "asio/error_handler.hpp" #include "asio/service_factory.hpp" @@ -65,6 +69,15 @@ public: /// The io_service type for this type. typedef typename service_type::io_service_type io_service_type; + /// The protocol type. + typedef typename service_type::protocol_type protocol_type; + + /// The endpoint type. + typedef typename service_type::endpoint_type endpoint_type; + + /// The socket type to be accepted. + typedef typename service_type::socket_type socket_type; + /// The type used for reporting errors. typedef asio::error error_type; @@ -79,11 +92,31 @@ public: * acceptor. */ explicit basic_socket_acceptor(io_service_type& io_service) - : service_(io_service.get_service(service_factory())), - impl_(service_.null()) + : service_(&io_service.get_service(service_factory())), + impl_(service_->null()) { } + /// Construct an open acceptor. + /** + * This constructor creates an acceptor and automatically opens it. + * + * @param io_service The io_service object that the acceptor will use to + * dispatch handlers for any asynchronous operations performed on the + * acceptor. + * + * @param protocol An object specifying protocol parameters to be used. + * + * @throws asio::error Thrown on failure. + */ + basic_socket_acceptor(io_service_type& io_service, + const protocol_type& protocol) + : service_(&io_service.get_service(service_factory())), + impl_(service_->null()) + { + service_->open(impl_, protocol, throw_error()); + } + /// Construct an acceptor opened on the given endpoint. /** * This constructor creates an acceptor and automatically opens it to listen @@ -109,23 +142,40 @@ public: * acceptor.listen(listen_backlog); * @endcode */ - template - basic_socket_acceptor(io_service_type& io_service, const Endpoint& endpoint, - int listen_backlog = 0) - : service_(io_service.get_service(service_factory())), - impl_(service_.null()) + basic_socket_acceptor(io_service_type& io_service, + const endpoint_type& endpoint, int listen_backlog = 0) + : service_(&io_service.get_service(service_factory())), + impl_(service_->null()) { - service_.open(impl_, endpoint.protocol(), throw_error()); + service_->open(impl_, endpoint.protocol(), throw_error()); close_on_block_exit auto_close(service_, impl_); - service_.bind(impl_, endpoint, throw_error()); - service_.listen(impl_, listen_backlog, throw_error()); + service_->bind(impl_, endpoint, throw_error()); + service_->listen(impl_, listen_backlog, throw_error()); auto_close.cancel(); } + /// Construct a basic_socket_acceptor on an existing implementation. + /** + * This constructor creates an acceptor to hold an existing implementation. + * + * @param io_service The io_service object that the acceptor will use to + * dispatch handlers for any asynchronous operations performed on the socket. + * + * @param impl The new underlying acceptor implementation. + * + * @throws asio::error Thrown on failure. + */ + basic_socket_acceptor(io_service_type& io_service, impl_type impl) + : service_(&io_service.get_service(service_factory())), + impl_(impl) + { + service_->assign(impl_, impl); + } + /// Destructor. ~basic_socket_acceptor() { - service_.close(impl_, ignore_error()); + service_->close(impl_, ignore_error()); } /// Get the io_service associated with the object. @@ -138,7 +188,7 @@ public: */ io_service_type& io_service() { - return service_.io_service(); + return service_->io_service(); } /// Open the acceptor using the specified protocol. @@ -154,10 +204,9 @@ public: * acceptor.open(asio::ipv4::tcp()); * @endcode */ - template - void open(const Protocol& protocol) + void open(const protocol_type& protocol = protocol_type()) { - service_.open(impl_, protocol, throw_error()); + service_->open(impl_, protocol, throw_error()); } /// Open the acceptor using the specified protocol. @@ -185,10 +234,10 @@ public: * } * @endcode */ - template - void open(const Protocol& protocol, Error_Handler error_handler) + template + void open(const protocol_type& protocol, Error_Handler error_handler) { - service_.open(impl_, protocol, error_handler); + service_->open(impl_, protocol, error_handler); } /// Bind the acceptor to the given local endpoint. @@ -208,10 +257,9 @@ public: * acceptor.bind(asio::ipv4::tcp::endpoint(12345)); * @endcode */ - template - void bind(const Endpoint& endpoint) + void bind(const endpoint_type& endpoint) { - service_.bind(impl_, endpoint, throw_error()); + service_->bind(impl_, endpoint, throw_error()); } /// Bind the acceptor to the given local endpoint. @@ -242,10 +290,10 @@ public: * } * @endcode */ - template - void bind(const Endpoint& endpoint, Error_Handler error_handler) + template + void bind(const endpoint_type& endpoint, Error_Handler error_handler) { - service_.bind(impl_, endpoint, error_handler); + service_->bind(impl_, endpoint, error_handler); } /// Place the acceptor into the state where it will listen for new @@ -259,7 +307,7 @@ public: */ void listen(int backlog = 0) { - service_.listen(impl_, backlog, throw_error()); + service_->listen(impl_, backlog, throw_error()); } /// Place the acceptor into the state where it will listen for new @@ -293,7 +341,7 @@ public: template void listen(int backlog, Error_Handler error_handler) { - service_.listen(impl_, backlog, error_handler); + service_->listen(impl_, backlog, error_handler); } /// Close the acceptor. @@ -308,7 +356,7 @@ public: */ void close() { - service_.close(impl_, throw_error()); + service_->close(impl_, throw_error()); } /// Close the acceptor. @@ -341,7 +389,7 @@ public: template void close(Error_Handler error_handler) { - service_.close(impl_, error_handler); + service_->close(impl_, error_handler); } /// Get the underlying implementation in the native type. @@ -378,7 +426,7 @@ public: template void set_option(const Option& option) { - service_.set_option(impl_, option, throw_error()); + service_->set_option(impl_, option, throw_error()); } /// Set an option on the acceptor. @@ -414,7 +462,7 @@ public: template void set_option(const Option& option, Error_Handler error_handler) { - service_.set_option(impl_, option, error_handler); + service_->set_option(impl_, option, error_handler); } /// Get an option from the acceptor. @@ -442,7 +490,7 @@ public: template void get_option(Option& option) { - service_.get_option(impl_, option, throw_error()); + service_->get_option(impl_, option, throw_error()); } /// Get an option from the acceptor. @@ -480,7 +528,7 @@ public: template void get_option(Option& option, Error_Handler error_handler) { - service_.get_option(impl_, option, error_handler); + service_->get_option(impl_, option, error_handler); } /// Get the local endpoint of the acceptor. @@ -501,10 +549,9 @@ public: * acceptor.get_local_endpoint(endpoint); * @endcode */ - template - void get_local_endpoint(Endpoint& endpoint) const + void get_local_endpoint(endpoint_type& endpoint) const { - service_.get_local_endpoint(impl_, endpoint, throw_error()); + service_->get_local_endpoint(impl_, endpoint, throw_error()); } /// Get the local endpoint of the acceptor. @@ -535,10 +582,11 @@ public: * } * @endcode */ - template - void get_local_endpoint(Endpoint& endpoint, Error_Handler error_handler) const + template + void get_local_endpoint(endpoint_type& endpoint, + Error_Handler error_handler) const { - service_.get_local_endpoint(impl_, endpoint, error_handler); + service_->get_local_endpoint(impl_, endpoint, error_handler); } /// Accept a new connection. @@ -559,10 +607,9 @@ public: * acceptor.accept(socket); * @endcode */ - template - void accept(Socket& peer) + void accept(socket_type& peer) { - service_.accept(impl_, to_socket(peer), throw_error()); + service_->accept(impl_, peer, throw_error()); } /// Accept a new connection. @@ -593,10 +640,10 @@ public: * } * @endcode */ - template - void accept(Socket& peer, Error_Handler error_handler) + template + void accept(socket_type& peer, Error_Handler error_handler) { - service_.accept(impl_, to_socket(peer), error_handler); + service_->accept(impl_, peer, error_handler); } /// Start an asynchronous accept. @@ -637,10 +684,10 @@ public: * acceptor.async_accept(socket, accept_handler); * @endcode */ - template - void async_accept(Socket& peer, Handler handler) + template + void async_accept(socket_type& peer, Handler handler) { - service_.async_accept(impl_, to_socket(peer), handler); + service_->async_accept(impl_, peer, handler); } /// Accept a new connection and obtain the endpoint of the peer @@ -666,11 +713,9 @@ public: * acceptor.accept_endpoint(socket, endpoint); * @endcode */ - template - void accept_endpoint(Socket& peer, Endpoint& peer_endpoint) + void accept_endpoint(socket_type& peer, endpoint_type& peer_endpoint) { - service_.accept_endpoint(impl_, to_socket(peer), peer_endpoint, - throw_error()); + service_->accept_endpoint(impl_, peer, peer_endpoint, throw_error()); } /// Accept a new connection and obtain the endpoint of the peer @@ -707,12 +752,11 @@ public: * } * @endcode */ - template - void accept_endpoint(Socket& peer, Endpoint& peer_endpoint, + template + void accept_endpoint(socket_type& peer, endpoint_type& peer_endpoint, Error_Handler error_handler) { - service_.accept_endpoint(impl_, to_socket(peer), peer_endpoint, - error_handler); + service_->accept_endpoint(impl_, peer, peer_endpoint, error_handler); } /// Start an asynchronous accept. @@ -741,34 +785,33 @@ public: * of the handler will be performed in a manner equivalent to using * asio::io_service::post(). */ - template - void async_accept_endpoint(Socket& peer, Endpoint& peer_endpoint, + template + void async_accept_endpoint(socket_type& peer, endpoint_type& peer_endpoint, Handler handler) { - service_.async_accept_endpoint(impl_, to_socket(peer), peer_endpoint, - handler); + service_->async_accept_endpoint(impl_, peer, peer_endpoint, handler); + } + + /// Swap implementation of socket with another. + void swap(basic_socket_acceptor& other) + { + std::swap(service_, other.service_); + std::swap(impl_, other.impl_); } private: /// The backend service implementation. - service_type& service_; + service_type* service_; /// The underlying native implementation. impl_type impl_; - // Helper function to convert a stack of layers into a socket. - template - typename Socket::lowest_layer_type& to_socket(Socket& peer) - { - return peer.lowest_layer(); - } - // Helper class to automatically close the implementation on block exit. class close_on_block_exit { public: - close_on_block_exit(service_type& service, impl_type& impl) - : service_(&service), impl_(impl) + close_on_block_exit(service_type* service, impl_type& impl) + : service_(service), impl_(impl) { } @@ -791,6 +834,14 @@ private: }; }; +/// Swap implementation of socket with another. +template +inline void swap(basic_socket_acceptor& a, + basic_socket_acceptor& b) +{ + a.swap(b); +} + } // namespace asio #include "asio/detail/pop_options.hpp" diff --git a/asio/include/asio/basic_stream_socket.hpp b/asio/include/asio/basic_stream_socket.hpp index 45db9d8f..acf499a9 100644 --- a/asio/include/asio/basic_stream_socket.hpp +++ b/asio/include/asio/basic_stream_socket.hpp @@ -18,6 +18,7 @@ #include "asio/detail/push_options.hpp" #include "asio/detail/push_options.hpp" +#include #include #include #include "asio/detail/pop_options.hpp" @@ -35,8 +36,6 @@ namespace asio { * The basic_stream_socket class template provides asynchronous and blocking * stream-oriented socket functionality. * - * Most applications will use the asio::stream_socket typedef. - * * @par Thread Safety: * @e Distinct @e objects: Safe.@n * @e Shared @e objects: Unsafe. @@ -60,6 +59,12 @@ public: /// The io_service type for this type. typedef typename service_type::io_service_type io_service_type; + /// The protocol type. + typedef typename service_type::protocol_type protocol_type; + + /// The endpoint type. + typedef typename service_type::endpoint_type endpoint_type; + /// The type used for reporting errors. typedef asio::error error_type; @@ -68,23 +73,88 @@ public: /// Construct a basic_stream_socket without opening it. /** - * This constructor creates a stream socket without connecting it to a remote - * peer. The socket needs to be connected or accepted before data can be sent + * This constructor creates a stream socket without opening it. The socket + * 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 * dispatch handlers for any asynchronous operations performed on the socket. */ explicit basic_stream_socket(io_service_type& io_service) - : service_(io_service.get_service(service_factory())), - impl_(service_.null()) + : service_(&io_service.get_service(service_factory())), + impl_(service_->null()) { } + /// Construct and open a basic_stream_socket. + /** + * 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 + * dispatch handlers for any asynchronous operations performed on the socket. + * + * @param protocol An object specifying protocol parameters to be used. + * + * @throws asio::error Thrown on failure. + */ + basic_stream_socket(io_service_type& io_service, + const protocol_type& protocol) + : service_(&io_service.get_service(service_factory())), + impl_(service_->null()) + { + service_->open(impl_, protocol, throw_error()); + } + + /// Construct a basic_stream_socket, opening it and binding it to the given + /// local endpoint. + /** + * This constructor creates a stream socket and automatically opens it bound + * 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 + * dispatch handlers for any asynchronous operations performed on the socket. + * + * @param endpoint An endpoint on the local machine to which the stream + * socket will be bound. + * + * @throws asio::error Thrown on failure. + */ + basic_stream_socket(io_service_type& io_service, + const endpoint_type& endpoint) + : service_(&io_service.get_service(service_factory())), + impl_(service_->null()) + { + service_->open(impl_, endpoint.protocol(), throw_error()); + close_on_block_exit auto_close(service_, impl_); + service_->bind(impl_, endpoint, throw_error()); + auto_close.cancel(); + } + + /// Construct a basic_stream_socket on an existing implementation. + /** + * This constructor creates a stream socket to hold an existing + * implementation. + * + * @param io_service The io_service object that the stream socket will use to + * dispatch handlers for any asynchronous operations performed on the socket. + * + * @param impl The new underlying socket implementation. + * + * @throws asio::error Thrown on failure. + */ + basic_stream_socket(io_service_type& io_service, impl_type impl) + : service_(&io_service.get_service(service_factory())), + impl_(impl) + { + service_->assign(impl_, impl); + } + /// Destructor. ~basic_stream_socket() { - service_.close(impl_, ignore_error()); + service_->close(impl_, ignore_error()); } /// Get the io_service associated with the object. @@ -97,7 +167,7 @@ public: */ io_service_type& io_service() { - return service_.io_service(); + return service_->io_service(); } /// Open the socket using the specified protocol. @@ -105,7 +175,7 @@ public: * This function opens the stream socket so that it will use the specified * protocol. * - * @param protocol An object specifying which protocol is to be used. + * @param protocol An object specifying protocol parameters to be used. * * @throws asio::error Thrown on failure. * @@ -115,10 +185,9 @@ public: * socket.open(asio::ipv4::tcp()); * @endcode */ - template - void open(const Protocol& protocol) + void open(const protocol_type& protocol = protocol_type()) { - service_.open(impl_, protocol, throw_error()); + service_->open(impl_, protocol, throw_error()); } /// Open the socket using the specified protocol. @@ -146,10 +215,10 @@ public: * } * @endcode */ - template - void open(const Protocol& protocol, Error_Handler error_handler) + template + void open(const protocol_type& protocol, Error_Handler error_handler) { - service_.open(impl_, protocol, error_handler); + service_->open(impl_, protocol, error_handler); } /// Close the socket. @@ -161,7 +230,7 @@ public: */ void close() { - service_.close(impl_, throw_error()); + service_->close(impl_, throw_error()); } /// Close the socket. @@ -191,7 +260,7 @@ public: template void close(Error_Handler error_handler) { - service_.close(impl_, error_handler); + service_->close(impl_, error_handler); } /// Get a reference to the lowest layer. @@ -219,18 +288,6 @@ public: return impl_; } - /// Set the underlying implementation in the native type. - /** - * This function is used by the acceptor implementation to set the underlying - * implementation associated with the stream socket. - * - * @param new_impl The new underlying socket implementation. - */ - void set_impl(impl_type new_impl) - { - service_.assign(impl_, new_impl); - } - /// Bind the socket to the given local endpoint. /** * This function binds the stream socket to the specified endpoint on the @@ -248,10 +305,9 @@ public: * socket.bind(asio::ipv4::tcp::endpoint(12345)); * @endcode */ - template - void bind(const Endpoint& endpoint) + void bind(const endpoint_type& endpoint) { - service_.bind(impl_, endpoint, throw_error()); + service_->bind(impl_, endpoint, throw_error()); } /// Bind the socket to the given local endpoint. @@ -282,10 +338,10 @@ public: * } * @endcode */ - template - void bind(const Endpoint& endpoint, Error_Handler error_handler) + template + void bind(const endpoint_type& endpoint, Error_Handler error_handler) { - service_.bind(impl_, endpoint, error_handler); + service_->bind(impl_, endpoint, error_handler); } /// Connect a stream socket to the specified endpoint. @@ -310,10 +366,9 @@ public: * socket.connect(endpoint); * @endcode */ - template - void connect(const Endpoint& peer_endpoint) + void connect(const endpoint_type& peer_endpoint) { - service_.connect(impl_, peer_endpoint, throw_error()); + service_->connect(impl_, peer_endpoint, throw_error()); } /// Connect a stream socket to the specified endpoint. @@ -348,10 +403,10 @@ public: * } * @endcode */ - template - void connect(const Endpoint& peer_endpoint, Error_Handler error_handler) + template + void connect(const endpoint_type& peer_endpoint, Error_Handler error_handler) { - service_.connect(impl_, peer_endpoint, error_handler); + service_->connect(impl_, peer_endpoint, error_handler); } /// Start an asynchronous connect. @@ -394,10 +449,10 @@ public: * socket.async_connect(endpoint, connect_handler); * @endcode */ - template - void async_connect(const Endpoint& peer_endpoint, Handler handler) + template + void async_connect(const endpoint_type& peer_endpoint, Handler handler) { - service_.async_connect(impl_, peer_endpoint, handler); + service_->async_connect(impl_, peer_endpoint, handler); } /// Set an option on the socket. @@ -429,7 +484,7 @@ public: template void set_option(const Socket_Option& option) { - service_.set_option(impl_, option, throw_error()); + service_->set_option(impl_, option, throw_error()); } /// Set an option on the socket. @@ -471,7 +526,7 @@ public: template void set_option(const Socket_Option& option, Error_Handler error_handler) { - service_.set_option(impl_, option, error_handler); + service_->set_option(impl_, option, error_handler); } /// Get an option from the socket. @@ -504,7 +559,7 @@ public: template void get_option(Socket_Option& option) const { - service_.get_option(impl_, option, throw_error()); + service_->get_option(impl_, option, throw_error()); } /// Get an option from the socket. @@ -547,7 +602,7 @@ public: template void get_option(Socket_Option& option, Error_Handler error_handler) const { - service_.get_option(impl_, option, error_handler); + service_->get_option(impl_, option, error_handler); } /// Perform an IO control command on the socket. @@ -575,7 +630,7 @@ public: template void io_control(IO_Control_Command& command) { - service_.io_control(impl_, command, throw_error()); + service_->io_control(impl_, command, throw_error()); } /// Perform an IO control command on the socket. @@ -613,7 +668,7 @@ public: template void io_control(IO_Control_Command& command, Error_Handler error_handler) { - service_.io_control(impl_, command, error_handler); + service_->io_control(impl_, command, error_handler); } /// Get the local endpoint of the socket. @@ -633,10 +688,9 @@ public: * socket.get_local_endpoint(endpoint); * @endcode */ - template - void get_local_endpoint(Endpoint& endpoint) const + void get_local_endpoint(endpoint_type& endpoint) const { - service_.get_local_endpoint(impl_, endpoint, throw_error()); + service_->get_local_endpoint(impl_, endpoint, throw_error()); } /// Get the local endpoint of the socket. @@ -666,11 +720,11 @@ public: * } * @endcode */ - template - void get_local_endpoint(Endpoint& endpoint, + template + void get_local_endpoint(endpoint_type& endpoint, Error_Handler error_handler) const { - service_.get_local_endpoint(impl_, endpoint, error_handler); + service_->get_local_endpoint(impl_, endpoint, error_handler); } /// Get the remote endpoint of the socket. @@ -690,10 +744,9 @@ public: * socket.get_remote_endpoint(endpoint); * @endcode */ - template - void get_remote_endpoint(Endpoint& endpoint) const + void get_remote_endpoint(endpoint_type& endpoint) const { - service_.get_remote_endpoint(impl_, endpoint, throw_error()); + service_->get_remote_endpoint(impl_, endpoint, throw_error()); } /// Get the remote endpoint of the socket. @@ -723,11 +776,11 @@ public: * } * @endcode */ - template - void get_remote_endpoint(Endpoint& endpoint, + template + void get_remote_endpoint(endpoint_type& endpoint, Error_Handler error_handler) const { - service_.get_remote_endpoint(impl_, endpoint, error_handler); + service_->get_remote_endpoint(impl_, endpoint, error_handler); } /// Disable sends or receives on the socket. @@ -749,7 +802,7 @@ public: */ void shutdown(shutdown_type what) { - service_.shutdown(impl_, what, throw_error()); + service_->shutdown(impl_, what, throw_error()); } /// Disable sends or receives on the socket. @@ -783,7 +836,7 @@ public: template void shutdown(shutdown_type what, Error_Handler error_handler) { - service_.shutdown(impl_, what, error_handler); + service_->shutdown(impl_, what, error_handler); } /// Send some data on the socket. @@ -816,7 +869,7 @@ public: template std::size_t send(const Const_Buffers& buffers, message_flags flags) { - return service_.send(impl_, buffers, flags, throw_error()); + return service_->send(impl_, buffers, flags, throw_error()); } /// Send some data on the socket. @@ -847,7 +900,7 @@ public: std::size_t send(const Const_Buffers& buffers, message_flags flags, Error_Handler error_handler) { - return service_.send(impl_, buffers, flags, error_handler); + return service_->send(impl_, buffers, flags, error_handler); } /// Start an asynchronous send. @@ -891,7 +944,7 @@ public: void async_send(const Const_Buffers& buffers, message_flags flags, Handler handler) { - service_.async_send(impl_, buffers, flags, handler); + service_->async_send(impl_, buffers, flags, handler); } /// Receive some data on the socket. @@ -927,7 +980,7 @@ public: template std::size_t receive(const Mutable_Buffers& buffers, message_flags flags) { - return service_.receive(impl_, buffers, flags, throw_error()); + return service_->receive(impl_, buffers, flags, throw_error()); } /// Receive some data on a connected socket. @@ -958,7 +1011,7 @@ public: std::size_t receive(const Mutable_Buffers& buffers, message_flags flags, Error_Handler error_handler) { - return service_.receive(impl_, buffers, flags, error_handler); + return service_->receive(impl_, buffers, flags, error_handler); } /// Start an asynchronous receive. @@ -1004,7 +1057,7 @@ public: void async_receive(const Mutable_Buffers& buffers, message_flags flags, Handler handler) { - service_.async_receive(impl_, buffers, flags, handler); + service_->async_receive(impl_, buffers, flags, handler); } /// Write some data to the socket. @@ -1037,7 +1090,7 @@ public: template std::size_t write_some(const Const_Buffers& buffers) { - return service_.send(impl_, buffers, 0, throw_error()); + return service_->send(impl_, buffers, 0, throw_error()); } /// Write some data to the socket. @@ -1066,7 +1119,7 @@ public: std::size_t write_some(const Const_Buffers& buffers, Error_Handler error_handler) { - return service_.send(impl_, buffers, 0, error_handler); + return service_->send(impl_, buffers, 0, error_handler); } /// Start an asynchronous write. @@ -1107,7 +1160,7 @@ public: template void async_write_some(const Const_Buffers& buffers, Handler handler) { - service_.async_send(impl_, buffers, 0, handler); + service_->async_send(impl_, buffers, 0, handler); } /// Read some data from the socket. @@ -1141,7 +1194,7 @@ public: template std::size_t read_some(const Mutable_Buffers& buffers) { - return service_.receive(impl_, buffers, 0, throw_error()); + return service_->receive(impl_, buffers, 0, throw_error()); } /// Read some data from the socket. @@ -1171,7 +1224,7 @@ public: std::size_t read_some(const Mutable_Buffers& buffers, Error_Handler error_handler) { - return service_.receive(impl_, buffers, 0, error_handler); + return service_->receive(impl_, buffers, 0, error_handler); } /// Start an asynchronous read. @@ -1213,7 +1266,7 @@ public: template void async_read_some(const Mutable_Buffers& buffers, Handler handler) { - service_.async_receive(impl_, buffers, 0, handler); + service_->async_receive(impl_, buffers, 0, handler); } /// Peek at the incoming data on the stream socket. @@ -1239,7 +1292,7 @@ public: template std::size_t peek(const Mutable_Buffers& buffers) { - return service_.receive(impl_, buffers, message_peek, throw_error()); + return service_->receive(impl_, buffers, message_peek, throw_error()); } /// Peek at the incoming data on the stream socket. @@ -1263,7 +1316,7 @@ public: template std::size_t peek(const Mutable_Buffers& buffers, Error_Handler error_handler) { - return service_.receive(impl_, buffers, message_peek, error_handler); + return service_->receive(impl_, buffers, message_peek, error_handler); } /// Determine the amount of data that may be read without blocking. @@ -1278,7 +1331,7 @@ public: std::size_t in_avail() { bytes_readable command; - service_.io_control(impl_, command, throw_error()); + service_->io_control(impl_, command, throw_error()); return command.get(); } @@ -1300,18 +1353,60 @@ public: std::size_t in_avail(Error_Handler error_handler) { bytes_readable command; - service_.io_control(impl_, command, error_handler); + service_->io_control(impl_, command, error_handler); return command.get(); } + /// Swap implementation of socket with another. + void swap(basic_stream_socket& other) + { + std::swap(service_, other.service_); + std::swap(impl_, other.impl_); + } + private: /// The backend service implementation. - service_type& service_; + service_type* service_; /// The underlying native implementation. impl_type impl_; + + // Helper class to automatically close the implementation on block exit. + class close_on_block_exit + { + public: + close_on_block_exit(service_type* service, impl_type& impl) + : service_(service), impl_(impl) + { + } + + ~close_on_block_exit() + { + if (service_) + { + service_->close(impl_, ignore_error()); + } + } + + void cancel() + { + service_ = 0; + } + + private: + service_type* service_; + impl_type& impl_; + }; }; +/// Swap implementation of socket with another. +template +inline void swap(basic_stream_socket& a, + basic_stream_socket& b) +{ + a.swap(b); +} + } // namespace asio #include "asio/detail/pop_options.hpp" diff --git a/asio/include/asio/datagram_socket.hpp b/asio/include/asio/datagram_socket.hpp deleted file mode 100644 index f0a4fdea..00000000 --- a/asio/include/asio/datagram_socket.hpp +++ /dev/null @@ -1,32 +0,0 @@ -// -// datagram_socket.hpp -// ~~~~~~~~~~~~~~~~~~~ -// -// Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris at kohlhoff dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -#ifndef ASIO_DATAGRAM_SOCKET_HPP -#define ASIO_DATAGRAM_SOCKET_HPP - -#if defined(_MSC_VER) && (_MSC_VER >= 1200) -# pragma once -#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) - -#include "asio/detail/push_options.hpp" - -#include "asio/basic_datagram_socket.hpp" -#include "asio/datagram_socket_service.hpp" - -namespace asio { - -/// Typedef for the typical usage of datagram_socket. -typedef basic_datagram_socket > datagram_socket; - -} // namespace asio - -#include "asio/detail/pop_options.hpp" - -#endif // ASIO_DATAGRAM_SOCKET_HPP diff --git a/asio/include/asio/datagram_socket_service.hpp b/asio/include/asio/datagram_socket_service.hpp index 4e388f47..650d0bfa 100644 --- a/asio/include/asio/datagram_socket_service.hpp +++ b/asio/include/asio/datagram_socket_service.hpp @@ -19,7 +19,6 @@ #include "asio/detail/push_options.hpp" #include -#include #include #include "asio/detail/pop_options.hpp" @@ -34,7 +33,7 @@ namespace asio { /// Default service implementation for a datagram socket. -template > +template class datagram_socket_service : private noncopyable { @@ -42,6 +41,12 @@ public: /// The io_service type. typedef basic_io_service io_service_type; + /// The protocol type. + typedef Protocol protocol_type; + + /// The endpoint type. + typedef typename Protocol::endpoint endpoint_type; + private: // The type of the platform-specific implementation. #if defined(ASIO_HAS_IOCP) @@ -85,8 +90,8 @@ public: } // Open a new datagram socket implementation. - template - void open(impl_type& impl, const Protocol& protocol, + template + void open(impl_type& impl, const protocol_type& protocol, Error_Handler error_handler) { if (protocol.type() == SOCK_DGRAM) @@ -109,24 +114,24 @@ public: } // Bind the datagram socket to the specified local endpoint. - template - void bind(impl_type& impl, const Endpoint& endpoint, + template + void bind(impl_type& impl, const endpoint_type& endpoint, Error_Handler error_handler) { service_impl_.bind(impl, endpoint, error_handler); } /// Connect the datagram socket to the specified endpoint. - template - void connect(impl_type& impl, const Endpoint& peer_endpoint, + template + void connect(impl_type& impl, const endpoint_type& peer_endpoint, Error_Handler error_handler) { service_impl_.connect(impl, peer_endpoint, error_handler); } /// Start an asynchronous connect. - template - void async_connect(impl_type& impl, const Endpoint& peer_endpoint, + template + void async_connect(impl_type& impl, const endpoint_type& peer_endpoint, Handler handler) { service_impl_.async_connect(impl, peer_endpoint, handler); @@ -157,16 +162,16 @@ public: } /// Get the local endpoint. - template - void get_local_endpoint(const impl_type& impl, Endpoint& endpoint, + template + void get_local_endpoint(const impl_type& impl, endpoint_type& endpoint, Error_Handler error_handler) const { service_impl_.get_local_endpoint(impl, endpoint, error_handler); } // Get the remote endpoint. - template - void get_remote_endpoint(const impl_type& impl, Endpoint& endpoint, + template + void get_remote_endpoint(const impl_type& impl, endpoint_type& endpoint, Error_Handler error_handler) const { service_impl_.get_remote_endpoint(impl, endpoint, error_handler); @@ -197,9 +202,9 @@ public: } /// Send a datagram to the specified endpoint. - template + template std::size_t send_to(impl_type& impl, const Const_Buffers& buffers, - socket_base::message_flags flags, const Endpoint& destination, + socket_base::message_flags flags, const endpoint_type& destination, Error_Handler error_handler) { return service_impl_.send_to(impl, buffers, flags, destination, @@ -207,9 +212,9 @@ public: } /// Start an asynchronous send. - template + template void async_send_to(impl_type& impl, const Const_Buffers& buffers, - socket_base::message_flags flags, const Endpoint& destination, + socket_base::message_flags flags, const endpoint_type& destination, Handler handler) { service_impl_.async_send_to(impl, buffers, flags, destination, handler); @@ -232,9 +237,9 @@ public: } /// Receive a datagram with the endpoint of the sender. - template + template std::size_t receive_from(impl_type& impl, const Mutable_Buffers& buffers, - socket_base::message_flags flags, Endpoint& sender_endpoint, + socket_base::message_flags flags, endpoint_type& sender_endpoint, Error_Handler error_handler) { return service_impl_.receive_from(impl, buffers, flags, sender_endpoint, @@ -242,9 +247,9 @@ public: } /// Start an asynchronous receive that will get the endpoint of the sender. - template + template void async_receive_from(impl_type& impl, const Mutable_Buffers& buffers, - socket_base::message_flags flags, Endpoint& sender_endpoint, + socket_base::message_flags flags, endpoint_type& sender_endpoint, Handler handler) { service_impl_.async_receive_from(impl, buffers, flags, sender_endpoint, diff --git a/asio/include/asio/detail/reactive_socket_service.hpp b/asio/include/asio/detail/reactive_socket_service.hpp index 5eee758d..997adfb5 100644 --- a/asio/include/asio/detail/reactive_socket_service.hpp +++ b/asio/include/asio/detail/reactive_socket_service.hpp @@ -920,7 +920,8 @@ public: if (new_socket >= 0) { impl_type new_impl(new_socket); - peer.set_impl(new_impl); + Socket tmp(peer.io_service(), new_impl); + tmp.swap(peer); return; } @@ -975,7 +976,8 @@ public: { peer_endpoint.size(addr_len); impl_type new_impl(new_socket); - peer.set_impl(new_impl); + Socket tmp(peer.io_service(), new_impl); + tmp.swap(peer); return; } @@ -1033,7 +1035,8 @@ public: return false; impl_type new_impl(new_socket); - peer_.set_impl(new_impl); + Socket tmp(peer_.io_service(), new_impl); + tmp.swap(peer_); io_service_.post(bind_handler(handler_, error)); return true; } @@ -1120,7 +1123,8 @@ public: peer_endpoint_.size(addr_len); impl_type new_impl(new_socket); - peer_.set_impl(new_impl); + Socket tmp(peer_.io_service(), new_impl); + tmp.swap(peer_); io_service_.post(bind_handler(handler_, error)); return true; } diff --git a/asio/include/asio/ipv4/tcp.hpp b/asio/include/asio/ipv4/tcp.hpp index 16b1521f..97a5f531 100644 --- a/asio/include/asio/ipv4/tcp.hpp +++ b/asio/include/asio/ipv4/tcp.hpp @@ -23,9 +23,14 @@ #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) # include #endif // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) +#include #include "asio/detail/pop_options.hpp" +#include "asio/basic_socket_acceptor.hpp" +#include "asio/basic_stream_socket.hpp" #include "asio/error.hpp" +#include "asio/socket_acceptor_service.hpp" +#include "asio/stream_socket_service.hpp" #include "asio/ipv4/address.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/detail/socket_option.hpp" @@ -68,6 +73,29 @@ public: return PF_INET; } + /// Template typedefs for acceptor and socket types. + template + struct types + { + /// The service type for IPv4 TCP sockets. + typedef stream_socket_service socket_service; + + /// The IPv4 TCP socket type. + typedef basic_stream_socket socket; + + /// The service type for IPv4 TCP acceptors. + typedef socket_acceptor_service acceptor_service; + + /// The IPv4 TCP acceptor type. + typedef basic_socket_acceptor acceptor; + }; + + /// The IPv4 TCP socket type. + typedef types >::socket socket; + + /// The IPv4 TCP acceptor type. + typedef types >::acceptor acceptor; + /// Socket option for disabling the Nagle algorithm. /** * Implements the IPPROTO_TCP/TCP_NODELAY socket option. diff --git a/asio/include/asio/ipv4/udp.hpp b/asio/include/asio/ipv4/udp.hpp index 0ae8c992..009f3668 100644 --- a/asio/include/asio/ipv4/udp.hpp +++ b/asio/include/asio/ipv4/udp.hpp @@ -23,8 +23,11 @@ #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) # include #endif // BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) +#include #include "asio/detail/pop_options.hpp" +#include "asio/basic_datagram_socket.hpp" +#include "asio/datagram_socket_service.hpp" #include "asio/error.hpp" #include "asio/ipv4/address.hpp" #include "asio/detail/socket_types.hpp" @@ -66,6 +69,20 @@ public: { return PF_INET; } + + /// Template typedefs for socket types. + template + struct types + { + /// The service type for IPv4 UDP sockets. + typedef datagram_socket_service socket_service; + + /// The IPv4 UDP socket type. + typedef basic_datagram_socket socket; + }; + + /// The IPv4 UDP socket type. + typedef types >::socket socket; }; /// Describes an endpoint for a UDP socket. diff --git a/asio/include/asio/socket_acceptor.hpp b/asio/include/asio/socket_acceptor.hpp deleted file mode 100644 index 0cad3d32..00000000 --- a/asio/include/asio/socket_acceptor.hpp +++ /dev/null @@ -1,32 +0,0 @@ -// -// socket_acceptor.hpp -// ~~~~~~~~~~~~~~~~~~~ -// -// Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris at kohlhoff dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -#ifndef ASIO_SOCKET_ACCEPTOR_HPP -#define ASIO_SOCKET_ACCEPTOR_HPP - -#if defined(_MSC_VER) && (_MSC_VER >= 1200) -# pragma once -#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) - -#include "asio/detail/push_options.hpp" - -#include "asio/basic_socket_acceptor.hpp" -#include "asio/socket_acceptor_service.hpp" - -namespace asio { - -/// Typedef for the typical usage of socket_acceptor. -typedef basic_socket_acceptor > socket_acceptor; - -} // namespace asio - -#include "asio/detail/pop_options.hpp" - -#endif // ASIO_SOCKET_ACCEPTOR_HPP diff --git a/asio/include/asio/socket_acceptor_service.hpp b/asio/include/asio/socket_acceptor_service.hpp index 7d601f4a..34f2af96 100644 --- a/asio/include/asio/socket_acceptor_service.hpp +++ b/asio/include/asio/socket_acceptor_service.hpp @@ -17,10 +17,6 @@ #include "asio/detail/push_options.hpp" -#include "asio/detail/push_options.hpp" -#include -#include "asio/detail/pop_options.hpp" - #include "asio/basic_io_service.hpp" #include "asio/basic_stream_socket.hpp" #include "asio/detail/epoll_reactor.hpp" @@ -33,7 +29,7 @@ namespace asio { /// Default service implementation for a socket acceptor. -template > +template class socket_acceptor_service : private noncopyable { @@ -41,6 +37,15 @@ public: /// The io_service type. typedef basic_io_service io_service_type; + /// The protocol type. + typedef Protocol protocol_type; + + /// The endpoint type. + typedef typename protocol_type::endpoint endpoint_type; + + /// The socket type to be accepted. + typedef Socket socket_type; + private: // The type of the platform-specific implementation. #if defined(ASIO_HAS_IOCP) @@ -84,16 +89,22 @@ public: } /// Open a new socket acceptor implementation. - template - void open(impl_type& impl, const Protocol& protocol, + template + void open(impl_type& impl, const protocol_type& protocol, Error_Handler error_handler) { service_impl_.open(impl, protocol, error_handler); } + /// Assign a new datagram socket implementation. + void assign(impl_type& impl, impl_type new_impl) + { + service_impl_.assign(impl, new_impl); + } + /// Bind the socket acceptor to the specified local endpoint. - template - void bind(impl_type& impl, const Endpoint& endpoint, + template + void bind(impl_type& impl, const endpoint_type& endpoint, Error_Handler error_handler) { service_impl_.bind(impl, endpoint, error_handler); @@ -130,39 +141,39 @@ public: } /// Get the local endpoint. - template - void get_local_endpoint(const impl_type& impl, Endpoint& endpoint, + template + void get_local_endpoint(const impl_type& impl, endpoint_type& endpoint, Error_Handler error_handler) const { service_impl_.get_local_endpoint(impl, endpoint, error_handler); } /// Accept a new connection. - template - void accept(impl_type& impl, Socket& peer, Error_Handler error_handler) + template + void accept(impl_type& impl, socket_type& peer, Error_Handler error_handler) { service_impl_.accept(impl, peer, error_handler); } /// Accept a new connection. - template - void accept_endpoint(impl_type& impl, Socket& peer, Endpoint& peer_endpoint, - Error_Handler error_handler) + template + void accept_endpoint(impl_type& impl, socket_type& peer, + endpoint_type& peer_endpoint, Error_Handler error_handler) { service_impl_.accept_endpoint(impl, peer, peer_endpoint, error_handler); } /// Start an asynchronous accept. - template - void async_accept(impl_type& impl, Socket& peer, Handler handler) + template + void async_accept(impl_type& impl, socket_type& peer, Handler handler) { service_impl_.async_accept(impl, peer, handler); } /// Start an asynchronous accept. - template - void async_accept_endpoint(impl_type& impl, Socket& peer, - Endpoint& peer_endpoint, Handler handler) + template + void async_accept_endpoint(impl_type& impl, socket_type& peer, + endpoint_type& peer_endpoint, Handler handler) { service_impl_.async_accept_endpoint(impl, peer, peer_endpoint, handler); } diff --git a/asio/include/asio/stream_socket.hpp b/asio/include/asio/stream_socket.hpp deleted file mode 100644 index e83210de..00000000 --- a/asio/include/asio/stream_socket.hpp +++ /dev/null @@ -1,32 +0,0 @@ -// -// stream_socket.hpp -// ~~~~~~~~~~~~~~~~~ -// -// Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris at kohlhoff dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -#ifndef ASIO_STREAM_SOCKET_HPP -#define ASIO_STREAM_SOCKET_HPP - -#if defined(_MSC_VER) && (_MSC_VER >= 1200) -# pragma once -#endif // defined(_MSC_VER) && (_MSC_VER >= 1200) - -#include "asio/detail/push_options.hpp" - -#include "asio/basic_stream_socket.hpp" -#include "asio/stream_socket_service.hpp" - -namespace asio { - -/// Typedef for the typical usage of stream_socket. -typedef basic_stream_socket > stream_socket; - -} // namespace asio - -#include "asio/detail/pop_options.hpp" - -#endif // ASIO_STREAM_SOCKET_HPP diff --git a/asio/include/asio/stream_socket_service.hpp b/asio/include/asio/stream_socket_service.hpp index e7d983ec..fec9be0d 100644 --- a/asio/include/asio/stream_socket_service.hpp +++ b/asio/include/asio/stream_socket_service.hpp @@ -19,7 +19,6 @@ #include "asio/detail/push_options.hpp" #include -#include #include #include "asio/detail/pop_options.hpp" @@ -34,7 +33,7 @@ namespace asio { /// Default service implementation for a stream socket. -template > +template class stream_socket_service : private noncopyable { @@ -42,6 +41,12 @@ public: /// The io_service type. typedef basic_io_service io_service_type; + /// The protocol type. + typedef Protocol protocol_type; + + /// The endpoint type. + typedef typename Protocol::endpoint endpoint_type; + private: // The type of the platform-specific implementation. #if defined(ASIO_HAS_IOCP) @@ -81,12 +86,12 @@ public: /// Return a null stream socket implementation. impl_type null() const { - return service_impl_.null(); + return service_impl_type::null(); } /// Open a new stream socket implementation. - template - void open(impl_type& impl, const Protocol& protocol, + template + void open(impl_type& impl, const protocol_type& protocol, Error_Handler error_handler) { if (protocol.type() == SOCK_STREAM) @@ -109,24 +114,24 @@ public: } /// Bind the stream socket to the specified local endpoint. - template - void bind(impl_type& impl, const Endpoint& endpoint, + template + void bind(impl_type& impl, const endpoint_type& endpoint, Error_Handler error_handler) { service_impl_.bind(impl, endpoint, error_handler); } /// Connect the stream socket to the specified endpoint. - template - void connect(impl_type& impl, const Endpoint& peer_endpoint, + template + void connect(impl_type& impl, const endpoint_type& peer_endpoint, Error_Handler error_handler) { service_impl_.connect(impl, peer_endpoint, error_handler); } /// Start an asynchronous connect. - template - void async_connect(impl_type& impl, const Endpoint& peer_endpoint, + template + void async_connect(impl_type& impl, const endpoint_type& peer_endpoint, Handler handler) { service_impl_.async_connect(impl, peer_endpoint, handler); @@ -157,16 +162,16 @@ public: } /// Get the local endpoint. - template - void get_local_endpoint(const impl_type& impl, Endpoint& endpoint, + template + void get_local_endpoint(const impl_type& impl, endpoint_type& endpoint, Error_Handler error_handler) const { service_impl_.get_local_endpoint(impl, endpoint, error_handler); } /// Get the remote endpoint. - template - void get_remote_endpoint(const impl_type& impl, Endpoint& endpoint, + template + void get_remote_endpoint(const impl_type& impl, endpoint_type& endpoint, Error_Handler error_handler) const { service_impl_.get_remote_endpoint(impl, endpoint, error_handler); diff --git a/asio/src/Makefile.am b/asio/src/Makefile.am index f4bb0454..6f658b04 100644 --- a/asio/src/Makefile.am +++ b/asio/src/Makefile.am @@ -17,7 +17,6 @@ noinst_PROGRAMS = \ tests/unit/buffer_test \ tests/unit/completion_condition_test \ tests/unit/datagram_socket_service_test \ - tests/unit/datagram_socket_test \ tests/unit/deadline_timer_service_test \ tests/unit/deadline_timer_test \ tests/unit/error_handler_test \ @@ -38,10 +37,8 @@ noinst_PROGRAMS = \ tests/unit/read_test \ tests/unit/service_factory_test \ tests/unit/socket_acceptor_service_test \ - tests/unit/socket_acceptor_test \ tests/unit/socket_base_test \ tests/unit/stream_socket_service_test \ - tests/unit/stream_socket_test \ tests/unit/thread_test \ tests/unit/time_traits_test \ tests/unit/write_test \ @@ -103,7 +100,6 @@ TESTS = \ tests/unit/buffer_test \ tests/unit/completion_condition_test \ tests/unit/datagram_socket_service_test \ - tests/unit/datagram_socket_test \ tests/unit/deadline_timer_service_test \ tests/unit/deadline_timer_test \ tests/unit/error_handler_test \ @@ -124,10 +120,8 @@ TESTS = \ tests/unit/read_test \ tests/unit/service_factory_test \ tests/unit/socket_acceptor_service_test \ - tests/unit/socket_acceptor_test \ tests/unit/socket_base_test \ tests/unit/stream_socket_service_test \ - tests/unit/stream_socket_test \ tests/unit/thread_test \ tests/unit/time_traits_test \ tests/unit/write_test @@ -169,8 +163,6 @@ tests_unit_ipv4_tcp_test_SOURCES = tests/unit/ipv4/tcp_test.cpp tests_unit_ipv4_udp_test_SOURCES = tests/unit/ipv4/udp_test.cpp tests_unit_locking_dispatcher_test_SOURCES = tests/unit/locking_dispatcher_test.cpp tests_unit_is_read_buffered_test_SOURCES = tests/unit/is_read_buffered_test.cpp -tests_unit_socket_acceptor_test_SOURCES = tests/unit/socket_acceptor_test.cpp -tests_unit_datagram_socket_test_SOURCES = tests/unit/datagram_socket_test.cpp tests_unit_read_test_SOURCES = tests/unit/read_test.cpp tests_unit_write_test_SOURCES = tests/unit/write_test.cpp tests_unit_is_write_buffered_test_SOURCES = tests/unit/is_write_buffered_test.cpp @@ -195,7 +187,6 @@ tests_unit_service_factory_test_SOURCES = tests/unit/service_factory_test.cpp tests_unit_socket_acceptor_service_test_SOURCES = tests/unit/socket_acceptor_service_test.cpp tests_unit_socket_base_test_SOURCES = tests/unit/socket_base_test.cpp tests_unit_stream_socket_service_test_SOURCES = tests/unit/stream_socket_service_test.cpp -tests_unit_stream_socket_test_SOURCES = tests/unit/stream_socket_test.cpp tests_unit_thread_test_SOURCES = tests/unit/thread_test.cpp tests_unit_time_traits_test_SOURCES = tests/unit/time_traits_test.cpp examples_chat_chat_client_SOURCES = examples/chat/chat_client.cpp diff --git a/asio/src/doc/boost/reference_dox.txt b/asio/src/doc/boost/reference_dox.txt index 1d0ce6c4..14f9a267 100644 --- a/asio/src/doc/boost/reference_dox.txt +++ b/asio/src/doc/boost/reference_dox.txt @@ -59,7 +59,6 @@ Standard exception class.

Classes

\li boost::asio::const_buffer -\li boost::asio::datagram_socket \li boost::asio::deadline_timer \li boost::asio::error \li boost::asio::io_service @@ -67,14 +66,15 @@ Standard exception class. \li boost::asio::ipv4::host \li boost::asio::ipv4::host_resolver \li boost::asio::ipv4::tcp +\li boost::asio::ipv4::tcp::acceptor \li boost::asio::ipv4::tcp::endpoint +\li boost::asio::ipv4::tcp::socket \li boost::asio::ipv4::udp \li boost::asio::ipv4::udp::endpoint +\li boost::asio::ipv4::udp::socket \li boost::asio::locking_dispatcher \li boost::asio::mutable_buffer -\li boost::asio::socket_acceptor \li boost::asio::socket_base -\li boost::asio::stream_socket \li boost::asio::ssl::context \li boost::asio::ssl::context_base \li boost::asio::ssl::stream_base diff --git a/asio/src/doc/reference_dox.txt b/asio/src/doc/reference_dox.txt index 6e81d717..33c0572b 100644 --- a/asio/src/doc/reference_dox.txt +++ b/asio/src/doc/reference_dox.txt @@ -59,7 +59,6 @@ Standard exception class.

Classes

\li asio::const_buffer -\li asio::datagram_socket \li asio::deadline_timer \li asio::error \li asio::io_service @@ -67,14 +66,15 @@ Standard exception class. \li asio::ipv4::host \li asio::ipv4::host_resolver \li asio::ipv4::tcp +\li asio::ipv4::tcp::acceptor \li asio::ipv4::tcp::endpoint +\li asio::ipv4::tcp::socket \li asio::ipv4::udp \li asio::ipv4::udp::endpoint +\li asio::ipv4::udp::socket \li asio::locking_dispatcher \li asio::mutable_buffer -\li asio::socket_acceptor \li asio::socket_base -\li asio::stream_socket \li asio::ssl::context \li asio::ssl::context_base \li asio::ssl::stream_base diff --git a/asio/src/examples/chat/chat_client.cpp b/asio/src/examples/chat/chat_client.cpp index 8f5cd893..12228fbe 100644 --- a/asio/src/examples/chat/chat_client.cpp +++ b/asio/src/examples/chat/chat_client.cpp @@ -116,7 +116,7 @@ private: private: asio::io_service& io_service_; - asio::stream_socket socket_; + asio::ipv4::tcp::socket socket_; chat_message read_msg_; chat_message_queue write_msgs_; }; diff --git a/asio/src/examples/chat/chat_server.cpp b/asio/src/examples/chat/chat_server.cpp index cd55aee7..62bbf7a0 100644 --- a/asio/src/examples/chat/chat_server.cpp +++ b/asio/src/examples/chat/chat_server.cpp @@ -71,7 +71,7 @@ public: { } - asio::stream_socket& socket() + asio::ipv4::tcp::socket& socket() { return socket_; } @@ -152,7 +152,7 @@ public: } private: - asio::stream_socket socket_; + asio::ipv4::tcp::socket socket_; chat_room& room_; chat_message read_msg_; chat_message_queue write_msgs_; @@ -196,7 +196,7 @@ public: private: asio::io_service& io_service_; - asio::socket_acceptor acceptor_; + asio::ipv4::tcp::acceptor acceptor_; chat_room room_; }; diff --git a/asio/src/examples/echo/async_tcp_echo_server.cpp b/asio/src/examples/echo/async_tcp_echo_server.cpp index 207a0082..e6f9db8d 100644 --- a/asio/src/examples/echo/async_tcp_echo_server.cpp +++ b/asio/src/examples/echo/async_tcp_echo_server.cpp @@ -11,7 +11,7 @@ public: { } - asio::stream_socket& socket() + asio::ipv4::tcp::socket& socket() { return socket_; } @@ -55,7 +55,7 @@ public: } private: - asio::stream_socket socket_; + asio::ipv4::tcp::socket socket_; enum { max_length = 1024 }; char data_[max_length]; }; @@ -97,7 +97,7 @@ public: private: asio::io_service& io_service_; - asio::socket_acceptor acceptor_; + asio::ipv4::tcp::acceptor acceptor_; }; int main(int argc, char* argv[]) diff --git a/asio/src/examples/echo/async_udp_echo_server.cpp b/asio/src/examples/echo/async_udp_echo_server.cpp index 1a841c70..214feaff 100644 --- a/asio/src/examples/echo/async_udp_echo_server.cpp +++ b/asio/src/examples/echo/async_udp_echo_server.cpp @@ -48,7 +48,7 @@ public: private: asio::io_service& io_service_; - asio::datagram_socket socket_; + asio::ipv4::udp::socket socket_; asio::ipv4::udp::endpoint sender_endpoint_; enum { max_length = 1024 }; char data_[max_length]; diff --git a/asio/src/examples/echo/blocking_tcp_echo_client.cpp b/asio/src/examples/echo/blocking_tcp_echo_client.cpp index 1858aa7d..2e155785 100644 --- a/asio/src/examples/echo/blocking_tcp_echo_client.cpp +++ b/asio/src/examples/echo/blocking_tcp_echo_client.cpp @@ -23,7 +23,7 @@ int main(int argc, char* argv[]) hr.get_host_by_name(h, argv[1]); asio::ipv4::tcp::endpoint ep(atoi(argv[2]), h.address(0)); - asio::stream_socket s(io_service); + asio::ipv4::tcp::socket s(io_service); s.connect(ep); std::cout << "Enter message: "; diff --git a/asio/src/examples/echo/blocking_tcp_echo_server.cpp b/asio/src/examples/echo/blocking_tcp_echo_server.cpp index 3ec9da25..e7affddc 100644 --- a/asio/src/examples/echo/blocking_tcp_echo_server.cpp +++ b/asio/src/examples/echo/blocking_tcp_echo_server.cpp @@ -6,9 +6,9 @@ const int max_length = 1024; -typedef boost::shared_ptr stream_socket_ptr; +typedef boost::shared_ptr socket_ptr; -void session(stream_socket_ptr sock) +void session(socket_ptr sock) { try { @@ -39,11 +39,11 @@ void session(stream_socket_ptr sock) void server(asio::io_service& io_service, short port) { - asio::socket_acceptor a(io_service, + asio::ipv4::tcp::acceptor a(io_service, asio::ipv4::tcp::endpoint(port)); for (;;) { - stream_socket_ptr sock(new asio::stream_socket(io_service)); + socket_ptr sock(new asio::ipv4::tcp::socket(io_service)); asio::error error; a.accept(*sock, asio::assign_error(error)); if (!error) diff --git a/asio/src/examples/echo/blocking_udp_echo_client.cpp b/asio/src/examples/echo/blocking_udp_echo_client.cpp index 462e9bad..779b9426 100644 --- a/asio/src/examples/echo/blocking_udp_echo_client.cpp +++ b/asio/src/examples/echo/blocking_udp_echo_client.cpp @@ -17,7 +17,8 @@ int main(int argc, char* argv[]) asio::io_service io_service; - asio::datagram_socket s(io_service, asio::ipv4::udp::endpoint(0)); + asio::ipv4::udp::socket s(io_service, + asio::ipv4::udp::endpoint(0)); asio::ipv4::host_resolver hr(io_service); asio::ipv4::host h; diff --git a/asio/src/examples/echo/blocking_udp_echo_server.cpp b/asio/src/examples/echo/blocking_udp_echo_server.cpp index 10394962..1db79031 100644 --- a/asio/src/examples/echo/blocking_udp_echo_server.cpp +++ b/asio/src/examples/echo/blocking_udp_echo_server.cpp @@ -6,7 +6,7 @@ enum { max_length = 1024 }; void server(asio::io_service& io_service, short port) { - asio::datagram_socket sock(io_service, + asio::ipv4::udp::socket sock(io_service, asio::ipv4::udp::endpoint(port)); for (;;) { diff --git a/asio/src/examples/http/server/connection.cpp b/asio/src/examples/http/server/connection.cpp index 77449c02..37108525 100644 --- a/asio/src/examples/http/server/connection.cpp +++ b/asio/src/examples/http/server/connection.cpp @@ -15,7 +15,7 @@ connection::connection(asio::io_service& io_service, { } -asio::stream_socket& connection::socket() +asio::ipv4::tcp::socket& connection::socket() { return socket_; } diff --git a/asio/src/examples/http/server/connection.hpp b/asio/src/examples/http/server/connection.hpp index 70002eb2..9fbf2f54 100644 --- a/asio/src/examples/http/server/connection.hpp +++ b/asio/src/examples/http/server/connection.hpp @@ -27,7 +27,7 @@ public: connection_manager& manager, request_handler& handler); /// Get the socket associated with the connection. - asio::stream_socket& socket(); + asio::ipv4::tcp::socket& socket(); /// Start the first asynchronous operation for the connection. void start(); @@ -43,7 +43,7 @@ private: void handle_write(const asio::error& e); /// Socket for the connection. - asio::stream_socket socket_; + asio::ipv4::tcp::socket socket_; /// The manager for this connection. connection_manager& connection_manager_; diff --git a/asio/src/examples/http/server/server.cpp b/asio/src/examples/http/server/server.cpp index b396dd8d..f1ad86e6 100644 --- a/asio/src/examples/http/server/server.cpp +++ b/asio/src/examples/http/server/server.cpp @@ -15,7 +15,7 @@ server::server(short port, const std::string& doc_root) // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). asio::ipv4::tcp::endpoint endpoint(port); acceptor_.open(endpoint.protocol()); - acceptor_.set_option(asio::socket_acceptor::reuse_address(true)); + acceptor_.set_option(asio::ipv4::tcp::acceptor::reuse_address(true)); acceptor_.bind(endpoint); acceptor_.listen(); acceptor_.async_accept(new_connection_->socket(), diff --git a/asio/src/examples/http/server/server.hpp b/asio/src/examples/http/server/server.hpp index 1c3f4dff..580b39bb 100644 --- a/asio/src/examples/http/server/server.hpp +++ b/asio/src/examples/http/server/server.hpp @@ -37,7 +37,7 @@ private: asio::io_service io_service_; /// Acceptor used to listen for incoming connections. - asio::socket_acceptor acceptor_; + asio::ipv4::tcp::acceptor acceptor_; /// The connection manager which owns all live connections. connection_manager connection_manager_; diff --git a/asio/src/examples/iostreams/socket_stream.hpp b/asio/src/examples/iostreams/socket_stream.hpp index 45e9bb4e..5e87da63 100644 --- a/asio/src/examples/iostreams/socket_stream.hpp +++ b/asio/src/examples/iostreams/socket_stream.hpp @@ -15,7 +15,7 @@ public: // Constructor. socket_device(short port, const char* hostname) : io_service_(new asio::io_service), - socket_(new asio::stream_socket(*io_service_)) + socket_(new asio::ipv4::tcp::socket(*io_service_)) { asio::ipv4::host_resolver host_resolver(*io_service_); asio::ipv4::host host; @@ -45,7 +45,7 @@ public: private: boost::shared_ptr io_service_; - boost::shared_ptr socket_; + boost::shared_ptr socket_; }; // Typedefs for iostreams types. diff --git a/asio/src/examples/multicast/receiver.cpp b/asio/src/examples/multicast/receiver.cpp index 7ce2129a..1ea58fad 100644 --- a/asio/src/examples/multicast/receiver.cpp +++ b/asio/src/examples/multicast/receiver.cpp @@ -14,7 +14,7 @@ public: { // Create the socket so that multiple may be bound to the same address. socket_.open(asio::ipv4::udp()); - socket_.set_option(asio::datagram_socket::reuse_address(true)); + socket_.set_option(asio::ipv4::udp::socket::reuse_address(true)); socket_.bind(asio::ipv4::udp::endpoint(multicast_port)); // Join the multicast group. @@ -44,7 +44,7 @@ public: } private: - asio::datagram_socket socket_; + asio::ipv4::udp::socket socket_; asio::ipv4::udp::endpoint sender_endpoint_; enum { max_length = 1024 }; char data_[max_length]; diff --git a/asio/src/examples/multicast/sender.cpp b/asio/src/examples/multicast/sender.cpp index ddb7db65..46f27604 100644 --- a/asio/src/examples/multicast/sender.cpp +++ b/asio/src/examples/multicast/sender.cpp @@ -56,7 +56,7 @@ public: } private: - asio::datagram_socket socket_; + asio::ipv4::udp::socket socket_; asio::deadline_timer timer_; int message_count_; std::string message_; diff --git a/asio/src/examples/serialization/connection.hpp b/asio/src/examples/serialization/connection.hpp index 0bd20711..60dc0898 100644 --- a/asio/src/examples/serialization/connection.hpp +++ b/asio/src/examples/serialization/connection.hpp @@ -25,14 +25,14 @@ class connection { public: /// Constructor. - connection(asio::demuxer& demuxer) - : socket_(demuxer) + connection(asio::io_service& io_service) + : socket_(io_service) { } /// Get the underlying socket. Used for making a connection or for accepting /// an incoming connection. - asio::stream_socket& socket() + asio::ipv4::tcp::socket& socket() { return socket_; } @@ -55,7 +55,7 @@ public: { // Something went wrong, inform the caller. asio::error error(asio::error::invalid_argument); - socket_.demuxer().post(boost::bind(handler, error)); + socket_.io_service().post(boost::bind(handler, error)); return; } outbound_header_ = header_stream.str(); @@ -145,7 +145,7 @@ public: private: /// The underlying socket. - asio::stream_socket socket_; + asio::ipv4::tcp::socket socket_; /// The size of a fixed length header. enum { header_length = 8 }; diff --git a/asio/src/examples/serialization/server.cpp b/asio/src/examples/serialization/server.cpp index b2a4f104..4e6d8cbf 100644 --- a/asio/src/examples/serialization/server.cpp +++ b/asio/src/examples/serialization/server.cpp @@ -95,7 +95,7 @@ public: private: /// The acceptor object used to accept incoming socket connections. - asio::socket_acceptor acceptor_; + asio::ipv4::tcp::acceptor acceptor_; /// The data to be sent to each client. std::vector stocks_; diff --git a/asio/src/examples/services/basic_logger.hpp b/asio/src/examples/services/basic_logger.hpp index 92d2a554..bc52b54f 100644 --- a/asio/src/examples/services/basic_logger.hpp +++ b/asio/src/examples/services/basic_logger.hpp @@ -27,7 +27,7 @@ public: /** * This constructor creates a logger. * - * @param d The io_service object used to locate the logger service. + * @param io_service The io_service object used to locate the logger service. * * @param identifier An identifier for this logger. */ diff --git a/asio/src/examples/services/daytime_client.cpp b/asio/src/examples/services/daytime_client.cpp index 43228095..f9d51b6b 100644 --- a/asio/src/examples/services/daytime_client.cpp +++ b/asio/src/examples/services/daytime_client.cpp @@ -5,7 +5,7 @@ #include "stream_socket_service.hpp" typedef asio::basic_stream_socket< - services::stream_socket_service<> > debug_stream_socket; + services::stream_socket_service > debug_stream_socket; char read_buffer[1024]; diff --git a/asio/src/examples/services/stream_socket_service.hpp b/asio/src/examples/services/stream_socket_service.hpp index 144bffc7..21386103 100644 --- a/asio/src/examples/services/stream_socket_service.hpp +++ b/asio/src/examples/services/stream_socket_service.hpp @@ -8,18 +8,25 @@ namespace services { /// Debugging stream socket service that wraps the normal stream socket service. -template > +template > class stream_socket_service : private boost::noncopyable { private: /// The type of the wrapped stream socket service. - typedef asio::stream_socket_service service_impl_type; + typedef asio::stream_socket_service< + Protocol, Allocator> service_impl_type; public: /// The io_service type for this service. typedef asio::basic_io_service io_service_type; + /// The protocol type. + typedef Protocol protocol_type; + + /// The endpoint type. + typedef typename Protocol::endpoint endpoint_type; + /// The type of a stream socket. typedef typename service_impl_type::impl_type impl_type; @@ -44,8 +51,8 @@ public: } /// Open a new stream socket implementation. - template - void open(impl_type& impl, const Protocol& protocol, + template + void open(impl_type& impl, const protocol_type& protocol, Error_Handler error_handler) { logger_.log("Opening new socket"); @@ -68,8 +75,8 @@ public: } /// Bind the stream socket to the specified local endpoint. - template - void bind(impl_type& impl, const Endpoint& endpoint, + template + void bind(impl_type& impl, const endpoint_type& endpoint, Error_Handler error_handler) { logger_.log("Binding socket"); @@ -77,8 +84,8 @@ public: } /// Connect the stream socket to the specified endpoint. - template - void connect(impl_type& impl, const Endpoint& peer_endpoint, + template + void connect(impl_type& impl, const endpoint_type& peer_endpoint, Error_Handler error_handler) { logger_.log("Connecting socket"); @@ -118,8 +125,8 @@ public: }; /// Start an asynchronous connect. - template - void async_connect(impl_type& impl, const Endpoint& peer_endpoint, + template + void async_connect(impl_type& impl, const endpoint_type& peer_endpoint, Handler handler) { logger_.log("Starting asynchronous connect"); @@ -155,8 +162,8 @@ public: } /// Get the local endpoint. - template - void get_local_endpoint(const impl_type& impl, Endpoint& endpoint, + template + void get_local_endpoint(const impl_type& impl, endpoint_type& endpoint, Error_Handler error_handler) const { logger_.log("Getting socket's local endpoint"); @@ -164,8 +171,8 @@ public: } /// Get the remote endpoint. - template - void get_remote_endpoint(const impl_type& impl, Endpoint& endpoint, + template + void get_remote_endpoint(const impl_type& impl, endpoint_type& endpoint, Error_Handler error_handler) const { logger_.log("Getting socket's remote endpoint"); diff --git a/asio/src/examples/ssl/client.cpp b/asio/src/examples/ssl/client.cpp index 09c54840..3d3a7067 100644 --- a/asio/src/examples/ssl/client.cpp +++ b/asio/src/examples/ssl/client.cpp @@ -83,7 +83,7 @@ public: } private: - asio::ssl::stream socket_; + asio::ssl::stream socket_; char request_[max_length]; char reply_[max_length]; }; diff --git a/asio/src/examples/ssl/server.cpp b/asio/src/examples/ssl/server.cpp index 7edfee38..db24573a 100644 --- a/asio/src/examples/ssl/server.cpp +++ b/asio/src/examples/ssl/server.cpp @@ -12,7 +12,7 @@ public: { } - asio::stream_socket& socket() + asio::ipv4::tcp::socket& socket() { return socket_.lowest_layer(); } @@ -70,7 +70,7 @@ public: } private: - asio::ssl::stream socket_; + asio::ssl::stream socket_; enum { max_length = 1024 }; char data_[max_length]; }; @@ -121,7 +121,7 @@ public: private: asio::io_service& io_service_; - asio::socket_acceptor acceptor_; + asio::ipv4::tcp::acceptor acceptor_; asio::ssl::context context_; }; diff --git a/asio/src/examples/timeouts/accept_timeout.cpp b/asio/src/examples/timeouts/accept_timeout.cpp index 65c1301a..ba89220b 100644 --- a/asio/src/examples/timeouts/accept_timeout.cpp +++ b/asio/src/examples/timeouts/accept_timeout.cpp @@ -42,8 +42,8 @@ public: private: io_service& io_service_; deadline_timer timer_; - socket_acceptor acceptor_; - stream_socket socket_; + ipv4::tcp::acceptor acceptor_; + ipv4::tcp::socket socket_; }; int main() diff --git a/asio/src/examples/timeouts/connect_timeout.cpp b/asio/src/examples/timeouts/connect_timeout.cpp index 92e75f68..dffa0dee 100644 --- a/asio/src/examples/timeouts/connect_timeout.cpp +++ b/asio/src/examples/timeouts/connect_timeout.cpp @@ -41,7 +41,7 @@ public: private: io_service& io_service_; deadline_timer timer_; - stream_socket socket_; + ipv4::tcp::socket socket_; }; int main() @@ -49,7 +49,7 @@ int main() try { io_service ios; - socket_acceptor a(ios, ipv4::tcp::endpoint(32123), 1); + ipv4::tcp::acceptor a(ios, ipv4::tcp::endpoint(32123), 1); // Make lots of connections so that at least some of them will block. connect_handler ch1(ios); diff --git a/asio/src/examples/timeouts/datagram_receive_timeout.cpp b/asio/src/examples/timeouts/datagram_receive_timeout.cpp index 700981df..daaf30e0 100644 --- a/asio/src/examples/timeouts/datagram_receive_timeout.cpp +++ b/asio/src/examples/timeouts/datagram_receive_timeout.cpp @@ -43,7 +43,7 @@ public: private: io_service& io_service_; deadline_timer timer_; - datagram_socket socket_; + ipv4::udp::socket socket_; ipv4::udp::endpoint sender_endpoint_; enum { max_length = 512 }; char data_[max_length]; diff --git a/asio/src/examples/timeouts/stream_receive_timeout.cpp b/asio/src/examples/timeouts/stream_receive_timeout.cpp index 2d432b66..9d729fad 100644 --- a/asio/src/examples/timeouts/stream_receive_timeout.cpp +++ b/asio/src/examples/timeouts/stream_receive_timeout.cpp @@ -57,8 +57,8 @@ public: private: io_service& io_service_; deadline_timer timer_; - socket_acceptor acceptor_; - stream_socket socket_; + ipv4::tcp::acceptor acceptor_; + ipv4::tcp::socket socket_; char buf_[1024]; }; @@ -75,7 +75,7 @@ int main() stream_handler sh(ios); - stream_socket s(ios); + ipv4::tcp::socket s(ios); s.async_connect(ipv4::tcp::endpoint(32123, ipv4::address::loopback()), boost::bind(connect_handler)); diff --git a/asio/src/examples/tutorial/daytime1/client.cpp b/asio/src/examples/tutorial/daytime1/client.cpp index f57bd738..d06131da 100644 --- a/asio/src/examples/tutorial/daytime1/client.cpp +++ b/asio/src/examples/tutorial/daytime1/client.cpp @@ -18,7 +18,7 @@ int main(int argc, char* argv[]) host_resolver.get_host_by_name(host, argv[1]); asio::ipv4::tcp::endpoint remote_endpoint(13, host.address(0)); - asio::stream_socket socket(io_service); + asio::ipv4::tcp::socket socket(io_service); socket.connect(remote_endpoint); for (;;) diff --git a/asio/src/examples/tutorial/daytime2/server.cpp b/asio/src/examples/tutorial/daytime2/server.cpp index 4f40635a..9a2bf85c 100644 --- a/asio/src/examples/tutorial/daytime2/server.cpp +++ b/asio/src/examples/tutorial/daytime2/server.cpp @@ -9,12 +9,12 @@ int main() { asio::io_service io_service; - asio::socket_acceptor acceptor(io_service, + asio::ipv4::tcp::acceptor acceptor(io_service, asio::ipv4::tcp::endpoint(13)); for (;;) { - asio::stream_socket socket(io_service); + asio::ipv4::tcp::socket socket(io_service); acceptor.accept(socket); using namespace std; // For time_t, time and ctime. diff --git a/asio/src/examples/tutorial/daytime3/server.cpp b/asio/src/examples/tutorial/daytime3/server.cpp index 698de7fe..9128e350 100644 --- a/asio/src/examples/tutorial/daytime3/server.cpp +++ b/asio/src/examples/tutorial/daytime3/server.cpp @@ -3,7 +3,7 @@ #include #include -void handle_write(asio::stream_socket* socket, char* write_buf, +void handle_write(asio::ipv4::tcp::socket* socket, char* write_buf, const asio::error& /*error*/, size_t /*bytes_transferred*/) { using namespace std; // For free. @@ -11,8 +11,8 @@ void handle_write(asio::stream_socket* socket, char* write_buf, delete socket; } -void handle_accept(asio::socket_acceptor* acceptor, - asio::stream_socket* socket, const asio::error& error) +void handle_accept(asio::ipv4::tcp::acceptor* acceptor, + asio::ipv4::tcp::socket* socket, const asio::error& error) { if (!error) { @@ -27,7 +27,7 @@ void handle_accept(asio::socket_acceptor* acceptor, asio::placeholders::error, asio::placeholders::bytes_transferred)); - socket = new asio::stream_socket(acceptor->io_service()); + socket = new asio::ipv4::tcp::socket(acceptor->io_service()); acceptor->async_accept(*socket, boost::bind(handle_accept, acceptor, socket, @@ -45,11 +45,11 @@ int main() { asio::io_service io_service; - asio::socket_acceptor acceptor(io_service, + asio::ipv4::tcp::acceptor acceptor(io_service, asio::ipv4::tcp::endpoint(13)); - asio::stream_socket* socket - = new asio::stream_socket(io_service); + asio::ipv4::tcp::socket* socket + = new asio::ipv4::tcp::socket(io_service); acceptor.async_accept(*socket, boost::bind(handle_accept, &acceptor, socket, diff --git a/asio/src/examples/tutorial/daytime4/client.cpp b/asio/src/examples/tutorial/daytime4/client.cpp index cd35303c..5169b8c8 100644 --- a/asio/src/examples/tutorial/daytime4/client.cpp +++ b/asio/src/examples/tutorial/daytime4/client.cpp @@ -19,7 +19,7 @@ int main(int argc, char* argv[]) asio::ipv4::udp::endpoint receiver_endpoint(13, host.address(0)); char send_buf[1] = { 0 }; - asio::datagram_socket socket(io_service, + asio::ipv4::udp::socket socket(io_service, asio::ipv4::udp::endpoint(0)); socket.send_to( asio::buffer(send_buf, sizeof(send_buf)), diff --git a/asio/src/examples/tutorial/daytime5/server.cpp b/asio/src/examples/tutorial/daytime5/server.cpp index 2cd46d59..06140c7a 100644 --- a/asio/src/examples/tutorial/daytime5/server.cpp +++ b/asio/src/examples/tutorial/daytime5/server.cpp @@ -9,7 +9,7 @@ int main() { asio::io_service io_service; - asio::datagram_socket socket(io_service, + asio::ipv4::udp::socket socket(io_service, asio::ipv4::udp::endpoint(13)); for (;;) diff --git a/asio/src/examples/tutorial/daytime6/server.cpp b/asio/src/examples/tutorial/daytime6/server.cpp index d66eab77..78fd50ae 100644 --- a/asio/src/examples/tutorial/daytime6/server.cpp +++ b/asio/src/examples/tutorial/daytime6/server.cpp @@ -10,7 +10,7 @@ void handle_send_to(char* send_buf, const asio::error& /*error*/, free(send_buf); } -void handle_receive_from(asio::datagram_socket* socket, char* recv_buf, +void handle_receive_from(asio::ipv4::udp::socket* socket, char* recv_buf, size_t recv_length, asio::ipv4::udp::endpoint* remote_endpoint, const asio::error& error, size_t /*bytes_transferred*/) { @@ -44,7 +44,7 @@ int main() size_t recv_length = sizeof(recv_buf); asio::ipv4::udp::endpoint remote_endpoint; - asio::datagram_socket socket(io_service, + asio::ipv4::udp::socket socket(io_service, asio::ipv4::udp::endpoint(13)); socket.async_receive_from( asio::buffer(recv_buf, recv_length), 0, remote_endpoint, diff --git a/asio/src/examples/tutorial/daytime7/server.cpp b/asio/src/examples/tutorial/daytime7/server.cpp index d40f4921..45ca9de5 100644 --- a/asio/src/examples/tutorial/daytime7/server.cpp +++ b/asio/src/examples/tutorial/daytime7/server.cpp @@ -3,15 +3,15 @@ #include #include -void handle_tcp_write(asio::stream_socket* socket, char* write_buf) +void handle_tcp_write(asio::ipv4::tcp::socket* socket, char* write_buf) { using namespace std; // For free. free(write_buf); delete socket; } -void handle_tcp_accept(asio::socket_acceptor* acceptor, - asio::stream_socket* socket, const asio::error& error) +void handle_tcp_accept(asio::ipv4::tcp::acceptor* acceptor, + asio::ipv4::tcp::socket* socket, const asio::error& error) { if (!error) { @@ -24,7 +24,7 @@ void handle_tcp_accept(asio::socket_acceptor* acceptor, asio::buffer(write_buf, write_length), boost::bind(handle_tcp_write, socket, write_buf)); - socket = new asio::stream_socket(acceptor->io_service()); + socket = new asio::ipv4::tcp::socket(acceptor->io_service()); acceptor->async_accept(*socket, boost::bind(handle_tcp_accept, acceptor, socket, @@ -42,7 +42,7 @@ void handle_udp_send_to(char* send_buf) free(send_buf); } -void handle_udp_receive_from(asio::datagram_socket* socket, +void handle_udp_receive_from(asio::ipv4::udp::socket* socket, char* recv_buf, size_t recv_length, asio::ipv4::udp::endpoint* remote_endpoint, const asio::error& error) @@ -71,17 +71,17 @@ int main() { asio::io_service io_service; - asio::socket_acceptor tcp_acceptor(io_service, + asio::ipv4::tcp::acceptor tcp_acceptor(io_service, asio::ipv4::tcp::endpoint(13)); - asio::stream_socket* tcp_socket - = new asio::stream_socket(io_service); + asio::ipv4::tcp::socket* tcp_socket + = new asio::ipv4::tcp::socket(io_service); tcp_acceptor.async_accept(*tcp_socket, boost::bind(handle_tcp_accept, &tcp_acceptor, tcp_socket, asio::placeholders::error)); - asio::datagram_socket udp_socket(io_service, + asio::ipv4::udp::socket udp_socket(io_service, asio::ipv4::udp::endpoint(13)); char recv_buf[1]; diff --git a/asio/src/examples/tutorial/daytime_dox.txt b/asio/src/examples/tutorial/daytime_dox.txt index 8d4b367f..f87c663e 100644 --- a/asio/src/examples/tutorial/daytime_dox.txt +++ b/asio/src/examples/tutorial/daytime_dox.txt @@ -43,8 +43,8 @@ Now create and connect the socket. The connection is open. All we need to do now is read the response from the daytime service. When the server closes the connection, the -asio::stream_socket::read_some() function will exit with the asio::error::eof -error, which is how we know to exit the loop. +asio::ipv4::tcp::socket::read_some() function will exit with the +asio::error::eof error, which is how we know to exit the loop. \until } @@ -75,10 +75,10 @@ with TCP. \until asio::io_service -An asio::socket_acceptor object needs to be created to listen +An asio::ipv4::tcp::acceptor object needs to be created to listen for new connections. It is initialised to listen on TCP port 13. -\until asio::socket_acceptor +\until asio::ipv4::tcp::acceptor \until endpoint This is an iterative server, which means that it will handle one @@ -118,15 +118,15 @@ Return to \ref tutdaytime2 \dontinclude daytime3/server.cpp \skip int main() -\until asio::socket_acceptor acceptor +\until asio::ipv4::tcp::acceptor acceptor \until endpoint We need to set up the initial socket to hold an accepted client connection. -\until asio::stream_socket* -\until stream_socket +\until asio::ipv4::tcp::socket* +\until ipv4::tcp::socket -The function asio::socket_acceptor::async_accept() will cause the +The function asio::ipv4::tcp::acceptor::async_accept() will cause the application to listen in the background for a new connection. When such a connection is received, the io_service will invoke the handle_accept() function with an asio::error argument. @@ -155,7 +155,7 @@ Determine what we are going to send. \until size_t write_length We now call \ref async_write to serve the data to the client. Note that we -are using \ref async_write, rather than asio::stream_socket::write_some, to +are using \ref async_write, rather than asio::ipv4::tcp::socket::write_some, to ensure that the entire block of data is sent. \until asio::placeholders::bytes_transferred @@ -215,14 +215,14 @@ Create an object for the remote endpoint, UDP port 13 on the server. \until asio::ipv4::udp::endpoint Since UDP is datagram-oriented, we will not be using a stream socket. Create an -asio::datagram_socket and initiate contact with the remote endpoint. +asio::ipv4::udp::socket and initiate contact with the remote endpoint. \until asio::buffer(send_buf, \until receiver_endpoint Now we need to be ready to accept whatever the server sends back to us. The endpoint on our side that receives the server's response will be initialised by -asio::datagram_socket::receive_from(). +asio::ipv4::udp::socket::receive_from(). \until } @@ -253,13 +253,13 @@ with UDP. \until asio::io_service -Create an asio::datagram_socket object to receive requests on UDP port 13. +Create an asio::ipv4::udp::socket object to receive requests on UDP port 13. -\until asio::datagram_socket +\until asio::ipv4::udp::socket \until endpoint Wait for a client to initiate contact with us. The remote_endpoint object will -be populated by asio::datagram_socket::receive_from(). +be populated by asio::ipv4::udp::socket::receive_from(). \until throw @@ -299,12 +299,12 @@ Return to \ref tutdaytime5 \skip int main() \until asio::ipv4::udp::endpoint -Create an asio::datagram_socket object to receive requests on UDP port 13. +Create an asio::ipv4::udp::socket object to receive requests on UDP port 13. -\until asio::datagram_socket +\until asio::ipv4::udp::socket \until endpoint -The function asio::datagram_socket::async_receive_from() will cause the +The function asio::ipv4::udp::socket::async_receive_from() will cause the application to listen in the background for a new request. When such a request is received, the io_service will invoke the handle_receive_from() function with two arguments: asio::error, and bytes_transferred. @@ -335,7 +335,7 @@ Determine what we are going to send. \until size_t send_length -We now call asio::datagram_socket::async_send_to() to serve the data to the +We now call asio::ipv4::udp::socket::async_send_to() to serve the data to the client. \until asio::placeholders::bytes_transferred @@ -387,12 +387,12 @@ have just written, into a single server application. \skip int main() \until asio::io_service -We will begin by creating an asio::stream_socket object to hold an accepted TCP -client connection. +We will begin by creating an asio::ipv4::tcp::socket object to hold an accepted +TCP client connection. \until asio::placeholders::error -Now we need an asio::datagram_socket object for UDP client requests. +Now we need an asio::ipv4::udp::socket object for UDP client requests. \until &remote_endpoint, asio::placeholders::error diff --git a/asio/src/tests/performance/client.cpp b/asio/src/tests/performance/client.cpp index 42399ede..64207d3f 100644 --- a/asio/src/tests/performance/client.cpp +++ b/asio/src/tests/performance/client.cpp @@ -153,7 +153,7 @@ private: private: locking_dispatcher dispatcher_; - stream_socket socket_; + ipv4::tcp::socket socket_; size_t block_size_; char* read_data_; size_t read_data_length_; diff --git a/asio/src/tests/performance/server.cpp b/asio/src/tests/performance/server.cpp index 1618ec75..57973a8b 100644 --- a/asio/src/tests/performance/server.cpp +++ b/asio/src/tests/performance/server.cpp @@ -38,7 +38,7 @@ public: delete[] write_data_; } - stream_socket& socket() + ipv4::tcp::socket& socket() { return socket_; } @@ -113,7 +113,7 @@ public: private: io_service& io_service_; locking_dispatcher dispatcher_; - stream_socket socket_; + ipv4::tcp::socket socket_; size_t block_size_; char* read_data_; size_t read_data_length_; @@ -132,7 +132,7 @@ public: block_size_(block_size) { acceptor_.open(ipv4::tcp()); - acceptor_.set_option(stream_socket::reuse_address(1)); + acceptor_.set_option(ipv4::tcp::acceptor::reuse_address(1)); acceptor_.bind(endpoint); acceptor_.listen(); @@ -166,7 +166,7 @@ public: private: io_service& io_service_; - socket_acceptor acceptor_; + ipv4::tcp::acceptor acceptor_; size_t block_size_; }; diff --git a/asio/src/tests/unit/Jamfile b/asio/src/tests/unit/Jamfile index 2a5eaafd..76250bbc 100644 --- a/asio/src/tests/unit/Jamfile +++ b/asio/src/tests/unit/Jamfile @@ -148,17 +148,6 @@ run datagram_socket_service_test.cpp $(SOCKET_LIBS) ; -run datagram_socket_test.cpp - @boost/libs/date_time/build/boost_date_time - : - : - : ../../.. - multi - <*>ws2_32 - <*>mswsock - $(SOCKET_LIBS) - ; - run deadline_timer_service_test.cpp @boost/libs/date_time/build/boost_date_time : @@ -392,17 +381,6 @@ run socket_acceptor_service_test.cpp $(SOCKET_LIBS) ; -run socket_acceptor_test.cpp - @boost/libs/date_time/build/boost_date_time - : - : - : ../../.. - multi - <*>ws2_32 - <*>mswsock - $(SOCKET_LIBS) - ; - run socket_base_test.cpp @boost/libs/date_time/build/boost_date_time : @@ -425,17 +403,6 @@ run stream_socket_service_test.cpp $(SOCKET_LIBS) ; -run stream_socket_test.cpp - @boost/libs/date_time/build/boost_date_time - : - : - : ../../.. - multi - <*>ws2_32 - <*>mswsock - $(SOCKET_LIBS) - ; - run time_traits_test.cpp @boost/libs/date_time/build/boost_date_time : diff --git a/asio/src/tests/unit/buffered_read_stream_test.cpp b/asio/src/tests/unit/buffered_read_stream_test.cpp index 178c1012..397e6a6b 100644 --- a/asio/src/tests/unit/buffered_read_stream_test.cpp +++ b/asio/src/tests/unit/buffered_read_stream_test.cpp @@ -17,7 +17,7 @@ #include "unit_test.hpp" typedef asio::buffered_read_stream< - asio::stream_socket> stream_type; + asio::ipv4::tcp::socket> stream_type; void test_sync_operations() { @@ -25,7 +25,7 @@ void test_sync_operations() asio::io_service io_service; - asio::socket_acceptor acceptor(io_service, + asio::ipv4::tcp::acceptor acceptor(io_service, asio::ipv4::tcp::endpoint(0)); asio::ipv4::tcp::endpoint server_endpoint; acceptor.get_local_endpoint(server_endpoint); @@ -35,7 +35,7 @@ void test_sync_operations() client_socket.lowest_layer().connect(server_endpoint); stream_type server_socket(io_service); - acceptor.accept(server_socket); + acceptor.accept(server_socket.lowest_layer()); const char write_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -132,7 +132,7 @@ void test_async_operations() asio::io_service io_service; - asio::socket_acceptor acceptor(io_service, + asio::ipv4::tcp::acceptor acceptor(io_service, asio::ipv4::tcp::endpoint(0)); asio::ipv4::tcp::endpoint server_endpoint; acceptor.get_local_endpoint(server_endpoint); @@ -142,7 +142,7 @@ void test_async_operations() client_socket.lowest_layer().connect(server_endpoint); stream_type server_socket(io_service); - acceptor.async_accept(server_socket, handle_accept); + acceptor.async_accept(server_socket.lowest_layer(), handle_accept); io_service.run(); io_service.reset(); diff --git a/asio/src/tests/unit/buffered_stream_test.cpp b/asio/src/tests/unit/buffered_stream_test.cpp index 6049010f..d5967758 100644 --- a/asio/src/tests/unit/buffered_stream_test.cpp +++ b/asio/src/tests/unit/buffered_stream_test.cpp @@ -16,7 +16,8 @@ #include "asio.hpp" #include "unit_test.hpp" -typedef asio::buffered_stream stream_type; +typedef asio::buffered_stream< + asio::ipv4::tcp::socket> stream_type; void test_sync_operations() { @@ -24,7 +25,7 @@ void test_sync_operations() asio::io_service io_service; - asio::socket_acceptor acceptor(io_service, + asio::ipv4::tcp::acceptor acceptor(io_service, asio::ipv4::tcp::endpoint(0)); asio::ipv4::tcp::endpoint server_endpoint; acceptor.get_local_endpoint(server_endpoint); @@ -34,7 +35,7 @@ void test_sync_operations() client_socket.lowest_layer().connect(server_endpoint); stream_type server_socket(io_service); - acceptor.accept(server_socket); + acceptor.accept(server_socket.lowest_layer()); const char write_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -138,7 +139,7 @@ void test_async_operations() asio::io_service io_service; - asio::socket_acceptor acceptor(io_service, + asio::ipv4::tcp::acceptor acceptor(io_service, asio::ipv4::tcp::endpoint(0)); asio::ipv4::tcp::endpoint server_endpoint; acceptor.get_local_endpoint(server_endpoint); @@ -148,7 +149,7 @@ void test_async_operations() client_socket.lowest_layer().connect(server_endpoint); stream_type server_socket(io_service); - acceptor.async_accept(server_socket, handle_accept); + acceptor.async_accept(server_socket.lowest_layer(), handle_accept); io_service.run(); io_service.reset(); diff --git a/asio/src/tests/unit/buffered_write_stream_test.cpp b/asio/src/tests/unit/buffered_write_stream_test.cpp index 68febbbe..82047532 100644 --- a/asio/src/tests/unit/buffered_write_stream_test.cpp +++ b/asio/src/tests/unit/buffered_write_stream_test.cpp @@ -17,7 +17,7 @@ #include "unit_test.hpp" typedef asio::buffered_write_stream< - asio::stream_socket> stream_type; + asio::ipv4::tcp::socket> stream_type; void test_sync_operations() { @@ -25,7 +25,7 @@ void test_sync_operations() asio::io_service io_service; - asio::socket_acceptor acceptor(io_service, + asio::ipv4::tcp::acceptor acceptor(io_service, asio::ipv4::tcp::endpoint(0)); asio::ipv4::tcp::endpoint server_endpoint; acceptor.get_local_endpoint(server_endpoint); @@ -35,7 +35,7 @@ void test_sync_operations() client_socket.lowest_layer().connect(server_endpoint); stream_type server_socket(io_service); - acceptor.accept(server_socket); + acceptor.accept(server_socket.lowest_layer()); const char write_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -139,7 +139,7 @@ void test_async_operations() asio::io_service io_service; - asio::socket_acceptor acceptor(io_service, + asio::ipv4::tcp::acceptor acceptor(io_service, asio::ipv4::tcp::endpoint(0)); asio::ipv4::tcp::endpoint server_endpoint; acceptor.get_local_endpoint(server_endpoint); @@ -149,7 +149,7 @@ void test_async_operations() client_socket.lowest_layer().connect(server_endpoint); stream_type server_socket(io_service); - acceptor.async_accept(server_socket, handle_accept); + acceptor.async_accept(server_socket.lowest_layer(), handle_accept); io_service.run(); io_service.reset(); diff --git a/asio/src/tests/unit/datagram_socket_test.cpp b/asio/src/tests/unit/datagram_socket_test.cpp deleted file mode 100644 index eda6707e..00000000 --- a/asio/src/tests/unit/datagram_socket_test.cpp +++ /dev/null @@ -1,80 +0,0 @@ -// -// datagram_socket_test.cpp -// ~~~~~~~~~~~~~~~~~~~~~~~~ -// -// Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris at kohlhoff dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -// Test that header file is self-contained. -#include "asio/datagram_socket.hpp" - -#include -#include -#include "asio.hpp" -#include "unit_test.hpp" - -using namespace asio; - -void handle_send(size_t expected_bytes_sent, const error& err, - size_t bytes_sent) -{ - BOOST_CHECK(!err); - BOOST_CHECK(expected_bytes_sent == bytes_sent); -} - -void handle_recv(size_t expected_bytes_recvd, const error& err, - size_t bytes_recvd) -{ - BOOST_CHECK(!err); - BOOST_CHECK(expected_bytes_recvd == bytes_recvd); -} - -void datagram_socket_test() -{ - using namespace std; // For memcmp and memset. - - io_service ios; - - datagram_socket s1(ios, ipv4::udp::endpoint(0)); - ipv4::udp::endpoint target_endpoint; - s1.get_local_endpoint(target_endpoint); - target_endpoint.address(ipv4::address::loopback()); - - datagram_socket s2(ios); - s2.open(ipv4::udp()); - s2.bind(ipv4::udp::endpoint(0)); - char send_msg[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - s2.send_to(buffer(send_msg, sizeof(send_msg)), 0, target_endpoint); - - char recv_msg[sizeof(send_msg)]; - ipv4::udp::endpoint sender_endpoint; - size_t bytes_recvd = s1.receive_from(buffer(recv_msg, sizeof(recv_msg)), 0, - sender_endpoint); - - BOOST_CHECK(bytes_recvd == sizeof(send_msg)); - BOOST_CHECK(memcmp(send_msg, recv_msg, sizeof(send_msg)) == 0); - - memset(recv_msg, 0, sizeof(recv_msg)); - - target_endpoint = sender_endpoint; - s1.async_send_to(buffer(send_msg, sizeof(send_msg)), 0, target_endpoint, - boost::bind(handle_send, sizeof(send_msg), - placeholders::error, placeholders::bytes_transferred)); - s2.async_receive_from(buffer(recv_msg, sizeof(recv_msg)), 0, sender_endpoint, - boost::bind(handle_recv, sizeof(recv_msg), - placeholders::error, placeholders::bytes_transferred)); - - ios.run(); - - BOOST_CHECK(memcmp(send_msg, recv_msg, sizeof(send_msg)) == 0); -} - -test_suite* init_unit_test_suite(int argc, char* argv[]) -{ - test_suite* test = BOOST_TEST_SUITE("datagram_socket"); - test->add(BOOST_TEST_CASE(&datagram_socket_test)); - return test; -} diff --git a/asio/src/tests/unit/error_handler_test.cpp b/asio/src/tests/unit/error_handler_test.cpp index 32f44d32..7d8778df 100644 --- a/asio/src/tests/unit/error_handler_test.cpp +++ b/asio/src/tests/unit/error_handler_test.cpp @@ -21,7 +21,7 @@ void error_handler_test() { io_service ios; - stream_socket s(ios); + ipv4::tcp::socket s(ios); ipv4::tcp::endpoint endpoint(321, ipv4::address::any()); error expected_err; diff --git a/asio/src/tests/unit/ipv4/tcp_test.cpp b/asio/src/tests/unit/ipv4/tcp_test.cpp index 2e02c16b..02be73d3 100644 --- a/asio/src/tests/unit/ipv4/tcp_test.cpp +++ b/asio/src/tests/unit/ipv4/tcp_test.cpp @@ -11,10 +11,81 @@ // Test that header file is self-contained. #include "asio/ipv4/tcp.hpp" +#include +#include +#include "asio.hpp" #include "../unit_test.hpp" +using namespace asio; + +void handle_accept(const error& err) +{ + BOOST_CHECK(!err); +} + +void handle_connect(const error& err) +{ + BOOST_CHECK(!err); +} + +void ipv4_tcp_acceptor_test() +{ + io_service ios; + + ipv4::tcp::acceptor acceptor(ios, ipv4::tcp::endpoint(0)); + ipv4::tcp::endpoint server_endpoint; + acceptor.get_local_endpoint(server_endpoint); + server_endpoint.address(ipv4::address::loopback()); + + ipv4::tcp::socket client_side_socket(ios); + ipv4::tcp::socket server_side_socket(ios); + + client_side_socket.connect(server_endpoint); + acceptor.accept(server_side_socket); + + client_side_socket.close(); + server_side_socket.close(); + + client_side_socket.connect(server_endpoint); + ipv4::tcp::endpoint client_endpoint; + acceptor.accept_endpoint(server_side_socket, client_endpoint); + + ipv4::tcp::endpoint client_side_local_endpoint; + client_side_socket.get_local_endpoint(client_side_local_endpoint); + BOOST_CHECK(client_side_local_endpoint.port() == client_endpoint.port()); + + ipv4::tcp::endpoint server_side_remote_endpoint; + server_side_socket.get_remote_endpoint(server_side_remote_endpoint); + BOOST_CHECK(server_side_remote_endpoint.port() == client_endpoint.port()); + + client_side_socket.close(); + server_side_socket.close(); + + acceptor.async_accept(server_side_socket, handle_accept); + client_side_socket.async_connect(server_endpoint, handle_connect); + + ios.run(); + + client_side_socket.close(); + server_side_socket.close(); + + acceptor.async_accept_endpoint(server_side_socket, client_endpoint, + handle_accept); + client_side_socket.async_connect(server_endpoint, handle_connect); + + ios.reset(); + ios.run(); + + client_side_socket.get_local_endpoint(client_side_local_endpoint); + BOOST_CHECK(client_side_local_endpoint.port() == client_endpoint.port()); + + server_side_socket.get_remote_endpoint(server_side_remote_endpoint); + BOOST_CHECK(server_side_remote_endpoint.port() == client_endpoint.port()); +} + test_suite* init_unit_test_suite(int argc, char* argv[]) { test_suite* test = BOOST_TEST_SUITE("ipv4/tcp"); + test->add(BOOST_TEST_CASE(&ipv4_tcp_acceptor_test)); return test; } diff --git a/asio/src/tests/unit/ipv4/udp_test.cpp b/asio/src/tests/unit/ipv4/udp_test.cpp index c6dc4a78..789b23b4 100644 --- a/asio/src/tests/unit/ipv4/udp_test.cpp +++ b/asio/src/tests/unit/ipv4/udp_test.cpp @@ -11,10 +11,70 @@ // Test that header file is self-contained. #include "asio/ipv4/udp.hpp" +#include +#include +#include "asio.hpp" #include "../unit_test.hpp" +using namespace asio; + +void handle_send(size_t expected_bytes_sent, const error& err, + size_t bytes_sent) +{ + BOOST_CHECK(!err); + BOOST_CHECK(expected_bytes_sent == bytes_sent); +} + +void handle_recv(size_t expected_bytes_recvd, const error& err, + size_t bytes_recvd) +{ + BOOST_CHECK(!err); + BOOST_CHECK(expected_bytes_recvd == bytes_recvd); +} + +void ipv4_udp_test() +{ + using namespace std; // For memcmp and memset. + + io_service ios; + + ipv4::udp::socket s1(ios, ipv4::udp::endpoint(0)); + ipv4::udp::endpoint target_endpoint; + s1.get_local_endpoint(target_endpoint); + target_endpoint.address(ipv4::address::loopback()); + + ipv4::udp::socket s2(ios); + s2.open(ipv4::udp()); + s2.bind(ipv4::udp::endpoint(0)); + char send_msg[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + s2.send_to(buffer(send_msg, sizeof(send_msg)), 0, target_endpoint); + + char recv_msg[sizeof(send_msg)]; + ipv4::udp::endpoint sender_endpoint; + size_t bytes_recvd = s1.receive_from(buffer(recv_msg, sizeof(recv_msg)), 0, + sender_endpoint); + + BOOST_CHECK(bytes_recvd == sizeof(send_msg)); + BOOST_CHECK(memcmp(send_msg, recv_msg, sizeof(send_msg)) == 0); + + memset(recv_msg, 0, sizeof(recv_msg)); + + target_endpoint = sender_endpoint; + s1.async_send_to(buffer(send_msg, sizeof(send_msg)), 0, target_endpoint, + boost::bind(handle_send, sizeof(send_msg), + placeholders::error, placeholders::bytes_transferred)); + s2.async_receive_from(buffer(recv_msg, sizeof(recv_msg)), 0, sender_endpoint, + boost::bind(handle_recv, sizeof(recv_msg), + placeholders::error, placeholders::bytes_transferred)); + + ios.run(); + + BOOST_CHECK(memcmp(send_msg, recv_msg, sizeof(send_msg)) == 0); +} + test_suite* init_unit_test_suite(int argc, char* argv[]) { test_suite* test = BOOST_TEST_SUITE("ipv4/udp"); + test->add(BOOST_TEST_CASE(&ipv4_udp_test)); return test; } diff --git a/asio/src/tests/unit/is_read_buffered_test.cpp b/asio/src/tests/unit/is_read_buffered_test.cpp index bd3f51bb..94d6ecdf 100644 --- a/asio/src/tests/unit/is_read_buffered_test.cpp +++ b/asio/src/tests/unit/is_read_buffered_test.cpp @@ -86,16 +86,18 @@ private: void is_read_buffered_test() { BOOST_CHECK(!asio::is_read_buffered< - asio::stream_socket>::value); + asio::ipv4::tcp::socket>::value); BOOST_CHECK(!!asio::is_read_buffered< - asio::buffered_read_stream >::value); + asio::buffered_read_stream< + asio::ipv4::tcp::socket> >::value); BOOST_CHECK(!asio::is_read_buffered< - asio::buffered_write_stream >::value); + asio::buffered_write_stream< + asio::ipv4::tcp::socket> >::value); BOOST_CHECK(!!asio::is_read_buffered< - asio::buffered_stream >::value); + asio::buffered_stream >::value); BOOST_CHECK(!asio::is_read_buffered::value); diff --git a/asio/src/tests/unit/is_write_buffered_test.cpp b/asio/src/tests/unit/is_write_buffered_test.cpp index 27b10ed0..d079f233 100644 --- a/asio/src/tests/unit/is_write_buffered_test.cpp +++ b/asio/src/tests/unit/is_write_buffered_test.cpp @@ -86,16 +86,18 @@ private: void is_write_buffered_test() { BOOST_CHECK(!asio::is_write_buffered< - asio::stream_socket>::value); + asio::ipv4::tcp::socket>::value); BOOST_CHECK(!asio::is_write_buffered< - asio::buffered_read_stream >::value); + asio::buffered_read_stream< + asio::ipv4::tcp::socket> >::value); BOOST_CHECK(!!asio::is_write_buffered< - asio::buffered_write_stream >::value); + asio::buffered_write_stream< + asio::ipv4::tcp::socket> >::value); BOOST_CHECK(!!asio::is_write_buffered< - asio::buffered_stream >::value); + asio::buffered_stream >::value); BOOST_CHECK(!asio::is_write_buffered::value); diff --git a/asio/src/tests/unit/socket_acceptor_test.cpp b/asio/src/tests/unit/socket_acceptor_test.cpp deleted file mode 100644 index 8506e4ca..00000000 --- a/asio/src/tests/unit/socket_acceptor_test.cpp +++ /dev/null @@ -1,91 +0,0 @@ -// -// socket_acceptor_test.cpp -// ~~~~~~~~~~~~~~~~~~~~~~~~ -// -// Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris at kohlhoff dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -// Test that header file is self-contained. -#include "asio/socket_acceptor.hpp" - -#include -#include -#include "asio.hpp" -#include "unit_test.hpp" - -using namespace asio; - -void handle_accept(const error& err) -{ - BOOST_CHECK(!err); -} - -void handle_connect(const error& err) -{ - BOOST_CHECK(!err); -} - -void socket_acceptor_test() -{ - io_service ios; - - socket_acceptor acceptor(ios, ipv4::tcp::endpoint(0)); - ipv4::tcp::endpoint server_endpoint; - acceptor.get_local_endpoint(server_endpoint); - server_endpoint.address(ipv4::address::loopback()); - - stream_socket client_side_socket(ios); - stream_socket server_side_socket(ios); - - client_side_socket.connect(server_endpoint); - acceptor.accept(server_side_socket); - - client_side_socket.close(); - server_side_socket.close(); - - client_side_socket.connect(server_endpoint); - ipv4::tcp::endpoint client_endpoint; - acceptor.accept_endpoint(server_side_socket, client_endpoint); - - ipv4::tcp::endpoint client_side_local_endpoint; - client_side_socket.get_local_endpoint(client_side_local_endpoint); - BOOST_CHECK(client_side_local_endpoint.port() == client_endpoint.port()); - - ipv4::tcp::endpoint server_side_remote_endpoint; - server_side_socket.get_remote_endpoint(server_side_remote_endpoint); - BOOST_CHECK(server_side_remote_endpoint.port() == client_endpoint.port()); - - client_side_socket.close(); - server_side_socket.close(); - - acceptor.async_accept(server_side_socket, handle_accept); - client_side_socket.async_connect(server_endpoint, handle_connect); - - ios.run(); - - client_side_socket.close(); - server_side_socket.close(); - - acceptor.async_accept_endpoint(server_side_socket, client_endpoint, - handle_accept); - client_side_socket.async_connect(server_endpoint, handle_connect); - - ios.reset(); - ios.run(); - - client_side_socket.get_local_endpoint(client_side_local_endpoint); - BOOST_CHECK(client_side_local_endpoint.port() == client_endpoint.port()); - - server_side_socket.get_remote_endpoint(server_side_remote_endpoint); - BOOST_CHECK(server_side_remote_endpoint.port() == client_endpoint.port()); -} - -test_suite* init_unit_test_suite(int argc, char* argv[]) -{ - test_suite* test = BOOST_TEST_SUITE("socket_acceptor"); - test->add(BOOST_TEST_CASE(&socket_acceptor_test)); - return test; -} diff --git a/asio/src/tests/unit/stream_socket_test.cpp b/asio/src/tests/unit/stream_socket_test.cpp deleted file mode 100644 index f74234b4..00000000 --- a/asio/src/tests/unit/stream_socket_test.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// -// stream_socket_test.cpp -// ~~~~~~~~~~~~~~~~~~~~~~ -// -// Copyright (c) 2003-2005 Christopher M. Kohlhoff (chris at kohlhoff dot com) -// -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -// - -// Test that header file is self-contained. -#include "asio/stream_socket.hpp" - -#include "unit_test.hpp" - -test_suite* init_unit_test_suite(int argc, char* argv[]) -{ - test_suite* test = BOOST_TEST_SUITE("stream_socket"); - return test; -}