2 // local/connect_pair.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
12 #define BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) \
21 || defined(GENERATING_DOCUMENTATION)
23 #include <boost/asio/basic_socket.hpp>
24 #include <boost/asio/detail/socket_ops.hpp>
25 #include <boost/asio/detail/throw_error.hpp>
26 #include <boost/asio/error.hpp>
27 #include <boost/asio/local/basic_endpoint.hpp>
29 #include <boost/asio/detail/push_options.hpp>
35 /// Create a pair of connected sockets.
36 template <typename Protocol, typename Executor1, typename Executor2>
37 void connect_pair(basic_socket<Protocol, Executor1>& socket1,
38 basic_socket<Protocol, Executor2>& socket2);
40 /// Create a pair of connected sockets.
41 template <typename Protocol, typename Executor1, typename Executor2>
42 BOOST_ASIO_SYNC_OP_VOID connect_pair(basic_socket<Protocol, Executor1>& socket1,
43 basic_socket<Protocol, Executor2>& socket2, boost::system::error_code& ec);
45 template <typename Protocol, typename Executor1, typename Executor2>
46 inline void connect_pair(basic_socket<Protocol, Executor1>& socket1,
47 basic_socket<Protocol, Executor2>& socket2)
49 boost::system::error_code ec;
50 connect_pair(socket1, socket2, ec);
51 boost::asio::detail::throw_error(ec, "connect_pair");
54 template <typename Protocol, typename Executor1, typename Executor2>
55 inline BOOST_ASIO_SYNC_OP_VOID connect_pair(
56 basic_socket<Protocol, Executor1>& socket1,
57 basic_socket<Protocol, Executor2>& socket2, boost::system::error_code& ec)
59 // Check that this function is only being used with a UNIX domain socket.
60 boost::asio::local::basic_endpoint<Protocol>* tmp
61 = static_cast<typename Protocol::endpoint*>(0);
65 boost::asio::detail::socket_type sv[2];
66 if (boost::asio::detail::socket_ops::socketpair(protocol.family(),
67 protocol.type(), protocol.protocol(), sv, ec)
68 == boost::asio::detail::socket_error_retval)
69 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
71 socket1.assign(protocol, sv[0], ec);
74 boost::system::error_code temp_ec;
75 boost::asio::detail::socket_ops::state_type state[2] = { 0, 0 };
76 boost::asio::detail::socket_ops::close(sv[0], state[0], true, temp_ec);
77 boost::asio::detail::socket_ops::close(sv[1], state[1], true, temp_ec);
78 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
81 socket2.assign(protocol, sv[1], ec);
84 boost::system::error_code temp_ec;
85 socket1.close(temp_ec);
86 boost::asio::detail::socket_ops::state_type state = 0;
87 boost::asio::detail::socket_ops::close(sv[1], state, true, temp_ec);
88 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
91 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
98 #include <boost/asio/detail/pop_options.hpp>
100 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
101 // || defined(GENERATING_DOCUMENTATION)
103 #endif // BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP