Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:00

0001 //
0002 // local/connect_pair.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
0012 #define BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) \
0021   || defined(GENERATING_DOCUMENTATION)
0022 
0023 #include <boost/asio/basic_socket.hpp>
0024 #include <boost/asio/detail/socket_ops.hpp>
0025 #include <boost/asio/detail/throw_error.hpp>
0026 #include <boost/asio/error.hpp>
0027 #include <boost/asio/local/basic_endpoint.hpp>
0028 
0029 #include <boost/asio/detail/push_options.hpp>
0030 
0031 namespace boost {
0032 namespace asio {
0033 namespace local {
0034 
0035 /// Create a pair of connected sockets.
0036 template <typename Protocol, typename Executor1, typename Executor2>
0037 void connect_pair(basic_socket<Protocol, Executor1>& socket1,
0038     basic_socket<Protocol, Executor2>& socket2);
0039 
0040 /// Create a pair of connected sockets.
0041 template <typename Protocol, typename Executor1, typename Executor2>
0042 BOOST_ASIO_SYNC_OP_VOID connect_pair(basic_socket<Protocol, Executor1>& socket1,
0043     basic_socket<Protocol, Executor2>& socket2, boost::system::error_code& ec);
0044 
0045 template <typename Protocol, typename Executor1, typename Executor2>
0046 inline void connect_pair(basic_socket<Protocol, Executor1>& socket1,
0047     basic_socket<Protocol, Executor2>& socket2)
0048 {
0049   boost::system::error_code ec;
0050   connect_pair(socket1, socket2, ec);
0051   boost::asio::detail::throw_error(ec, "connect_pair");
0052 }
0053 
0054 template <typename Protocol, typename Executor1, typename Executor2>
0055 inline BOOST_ASIO_SYNC_OP_VOID connect_pair(
0056     basic_socket<Protocol, Executor1>& socket1,
0057     basic_socket<Protocol, Executor2>& socket2, boost::system::error_code& ec)
0058 {
0059   // Check that this function is only being used with a UNIX domain socket.
0060   boost::asio::local::basic_endpoint<Protocol>* tmp
0061     = static_cast<typename Protocol::endpoint*>(0);
0062   (void)tmp;
0063 
0064   Protocol protocol;
0065   boost::asio::detail::socket_type sv[2];
0066   if (boost::asio::detail::socket_ops::socketpair(protocol.family(),
0067         protocol.type(), protocol.protocol(), sv, ec)
0068       == boost::asio::detail::socket_error_retval)
0069     BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
0070 
0071   socket1.assign(protocol, sv[0], ec);
0072   if (ec)
0073   {
0074     boost::system::error_code temp_ec;
0075     boost::asio::detail::socket_ops::state_type state[2] = { 0, 0 };
0076     boost::asio::detail::socket_ops::close(sv[0], state[0], true, temp_ec);
0077     boost::asio::detail::socket_ops::close(sv[1], state[1], true, temp_ec);
0078     BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
0079   }
0080 
0081   socket2.assign(protocol, sv[1], ec);
0082   if (ec)
0083   {
0084     boost::system::error_code temp_ec;
0085     socket1.close(temp_ec);
0086     boost::asio::detail::socket_ops::state_type state = 0;
0087     boost::asio::detail::socket_ops::close(sv[1], state, true, temp_ec);
0088     BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
0089   }
0090 
0091   BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
0092 }
0093 
0094 } // namespace local
0095 } // namespace asio
0096 } // namespace boost
0097 
0098 #include <boost/asio/detail/pop_options.hpp>
0099 
0100 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
0101        //   || defined(GENERATING_DOCUMENTATION)
0102 
0103 #endif // BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP