1//
2// local/connect_pair.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2024 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 Executor1, typename Executor2>
37void connect_pair(basic_socket<Protocol, Executor1>& socket1,
38 basic_socket<Protocol, Executor2>& socket2);
39
40/// Create a pair of connected sockets.
41template <typename Protocol, typename Executor1, typename Executor2>
42BOOST_ASIO_SYNC_OP_VOID connect_pair(basic_socket<Protocol, Executor1>& socket1,
43 basic_socket<Protocol, Executor2>& socket2, boost::system::error_code& ec);
44
45template <typename Protocol, typename Executor1, typename Executor2>
46inline void connect_pair(basic_socket<Protocol, Executor1>& socket1,
47 basic_socket<Protocol, Executor2>& socket2)
48{
49 boost::system::error_code ec;
50 connect_pair(socket1, socket2, ec);
51 boost::asio::detail::throw_error(err: ec, location: "connect_pair");
52}
53
54template <typename Protocol, typename Executor1, typename Executor2>
55inline BOOST_ASIO_SYNC_OP_VOID connect_pair(
56 basic_socket<Protocol, Executor1>& socket1,
57 basic_socket<Protocol, Executor2>& socket2, boost::system::error_code& ec)
58{
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);
62 (void)tmp;
63
64 Protocol protocol;
65 boost::asio::detail::socket_type sv[2];
66 if (boost::asio::detail::socket_ops::socketpair(af: protocol.family(),
67 type: protocol.type(), protocol: protocol.protocol(), sv, ec)
68 == boost::asio::detail::socket_error_retval)
69 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
70
71 socket1.assign(protocol, sv[0], ec);
72 if (ec)
73 {
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(s: sv[0], state&: state[0], destruction: true, ec&: temp_ec);
77 boost::asio::detail::socket_ops::close(s: sv[1], state&: state[1], destruction: true, ec&: temp_ec);
78 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
79 }
80
81 socket2.assign(protocol, sv[1], ec);
82 if (ec)
83 {
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(s: sv[1], state, destruction: true, ec&: temp_ec);
88 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
89 }
90
91 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
92}
93
94} // namespace local
95} // namespace asio
96} // namespace boost
97
98#include <boost/asio/detail/pop_options.hpp>
99
100#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
101 // || defined(GENERATING_DOCUMENTATION)
102
103#endif // BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
104

source code of boost/libs/asio/include/boost/asio/local/connect_pair.hpp