2 // local/connect_pair.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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 SocketService1, typename SocketService2>
38 basic_socket<Protocol, SocketService1>& socket1,
39 basic_socket<Protocol, SocketService2>& socket2);
41 /// Create a pair of connected sockets.
42 template <typename Protocol, typename SocketService1, typename SocketService2>
43 boost::system::error_code connect_pair(
44 basic_socket<Protocol, SocketService1>& socket1,
45 basic_socket<Protocol, SocketService2>& socket2,
46 boost::system::error_code& ec);
48 template <typename Protocol, typename SocketService1, typename SocketService2>
49 inline void connect_pair(
50 basic_socket<Protocol, SocketService1>& socket1,
51 basic_socket<Protocol, SocketService2>& socket2)
53 boost::system::error_code ec;
54 connect_pair(socket1, socket2, ec);
55 boost::asio::detail::throw_error(ec, "connect_pair");
58 template <typename Protocol, typename SocketService1, typename SocketService2>
59 inline boost::system::error_code connect_pair(
60 basic_socket<Protocol, SocketService1>& socket1,
61 basic_socket<Protocol, SocketService2>& socket2,
62 boost::system::error_code& ec)
64 // Check that this function is only being used with a UNIX domain socket.
65 boost::asio::local::basic_endpoint<Protocol>* tmp
66 = static_cast<typename Protocol::endpoint*>(0);
70 boost::asio::detail::socket_type sv[2];
71 if (boost::asio::detail::socket_ops::socketpair(protocol.family(),
72 protocol.type(), protocol.protocol(), sv, ec)
73 == boost::asio::detail::socket_error_retval)
76 if (socket1.assign(protocol, sv[0], ec))
78 boost::system::error_code temp_ec;
79 boost::asio::detail::socket_ops::state_type state[2] = { 0, 0 };
80 boost::asio::detail::socket_ops::close(sv[0], state[0], true, temp_ec);
81 boost::asio::detail::socket_ops::close(sv[1], state[1], true, temp_ec);
85 if (socket2.assign(protocol, sv[1], ec))
87 boost::system::error_code temp_ec;
88 socket1.close(temp_ec);
89 boost::asio::detail::socket_ops::state_type state = 0;
90 boost::asio::detail::socket_ops::close(sv[1], state, true, temp_ec);
101 #include <boost/asio/detail/pop_options.hpp>
103 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
104 // || defined(GENERATING_DOCUMENTATION)
106 #endif // BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP