1//
2// local/connect_pair.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6//
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)
9//
10
11#ifndef BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
12#define BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18#include <boost/asio/detail/config.hpp>
19
20#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) \
21 || defined(GENERATING_DOCUMENTATION)
22
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>
28
29#include <boost/asio/detail/push_options.hpp>
30
31namespace boost {
32namespace asio {
33namespace local {
34
35/// Create a pair of connected sockets.
36template <typename Protocol, typename SocketService1, typename SocketService2>
37void connect_pair(
38 basic_socket<Protocol, SocketService1>& socket1,
39 basic_socket<Protocol, SocketService2>& socket2);
40
41/// Create a pair of connected sockets.
42template <typename Protocol, typename SocketService1, typename SocketService2>
43boost::system::error_code connect_pair(
44 basic_socket<Protocol, SocketService1>& socket1,
45 basic_socket<Protocol, SocketService2>& socket2,
46 boost::system::error_code& ec);
47
48template <typename Protocol, typename SocketService1, typename SocketService2>
49inline void connect_pair(
50 basic_socket<Protocol, SocketService1>& socket1,
51 basic_socket<Protocol, SocketService2>& socket2)
52{
53 boost::system::error_code ec;
54 connect_pair(socket1, socket2, ec);
55 boost::asio::detail::throw_error(ec, "connect_pair");
56}
57
58template <typename Protocol, typename SocketService1, typename SocketService2>
59inline boost::system::error_code connect_pair(
60 basic_socket<Protocol, SocketService1>& socket1,
61 basic_socket<Protocol, SocketService2>& socket2,
62 boost::system::error_code& ec)
63{
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);
67 (void)tmp;
68
69 Protocol protocol;
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)
74 return ec;
75
76 if (socket1.assign(protocol, sv[0], ec))
77 {
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);
82 return ec;
83 }
84
85 if (socket2.assign(protocol, sv[1], ec))
86 {
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);
91 return ec;
92 }
93
94 return ec;
95}
96
97} // namespace local
98} // namespace asio
99} // namespace boost
100
101#include <boost/asio/detail/pop_options.hpp>
102
103#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
104 // || defined(GENERATING_DOCUMENTATION)
105
106#endif // BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
107