connect_pair.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #ifndef BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
  11. #define BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <boost/asio/basic_socket.hpp>
  19. #include <boost/asio/detail/socket_ops.hpp>
  20. #include <boost/asio/detail/throw_error.hpp>
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/local/basic_endpoint.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. namespace local {
  27. /// Create a pair of connected sockets.
  28. template <typename Protocol, typename Executor1, typename Executor2>
  29. void connect_pair(basic_socket<Protocol, Executor1>& socket1,
  30. basic_socket<Protocol, Executor2>& socket2);
  31. /// Create a pair of connected sockets.
  32. template <typename Protocol, typename Executor1, typename Executor2>
  33. BOOST_ASIO_SYNC_OP_VOID connect_pair(basic_socket<Protocol, Executor1>& socket1,
  34. basic_socket<Protocol, Executor2>& socket2, boost::system::error_code& ec);
  35. template <typename Protocol, typename Executor1, typename Executor2>
  36. inline void connect_pair(basic_socket<Protocol, Executor1>& socket1,
  37. basic_socket<Protocol, Executor2>& socket2)
  38. {
  39. boost::system::error_code ec;
  40. connect_pair(socket1, socket2, ec);
  41. boost::asio::detail::throw_error(ec, "connect_pair");
  42. }
  43. template <typename Protocol, typename Executor1, typename Executor2>
  44. inline BOOST_ASIO_SYNC_OP_VOID connect_pair(
  45. basic_socket<Protocol, Executor1>& socket1,
  46. basic_socket<Protocol, Executor2>& socket2, boost::system::error_code& ec)
  47. {
  48. // Check that this function is only being used with a UNIX domain socket.
  49. boost::asio::local::basic_endpoint<Protocol>* tmp
  50. = static_cast<typename Protocol::endpoint*>(0);
  51. (void)tmp;
  52. Protocol protocol;
  53. boost::asio::detail::socket_type sv[2];
  54. if (boost::asio::detail::socket_ops::socketpair(protocol.family(),
  55. protocol.type(), protocol.protocol(), sv, ec)
  56. == boost::asio::detail::socket_error_retval)
  57. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  58. socket1.assign(protocol, sv[0], ec);
  59. if (ec)
  60. {
  61. boost::system::error_code temp_ec;
  62. boost::asio::detail::socket_ops::state_type state[2] = { 0, 0 };
  63. boost::asio::detail::socket_ops::close(sv[0], state[0], true, temp_ec);
  64. boost::asio::detail::socket_ops::close(sv[1], state[1], true, temp_ec);
  65. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  66. }
  67. socket2.assign(protocol, sv[1], ec);
  68. if (ec)
  69. {
  70. boost::system::error_code temp_ec;
  71. socket1.close(temp_ec);
  72. boost::asio::detail::socket_ops::state_type state = 0;
  73. boost::asio::detail::socket_ops::close(sv[1], state, true, temp_ec);
  74. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  75. }
  76. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  77. }
  78. } // namespace local
  79. } // namespace asio
  80. } // namespace boost
  81. #include <boost/asio/detail/pop_options.hpp>
  82. #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  83. // || defined(GENERATING_DOCUMENTATION)
  84. #endif // BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP