connect_pair.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // local/connect_pair.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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 BOOST_ASIO_SVC_TPARAM BOOST_ASIO_SVC_TPARAM1>
  29. void connect_pair(
  30. basic_socket<Protocol BOOST_ASIO_SVC_TARG>& socket1,
  31. basic_socket<Protocol BOOST_ASIO_SVC_TARG1>& socket2);
  32. /// Create a pair of connected sockets.
  33. template <typename Protocol BOOST_ASIO_SVC_TPARAM BOOST_ASIO_SVC_TPARAM1>
  34. BOOST_ASIO_SYNC_OP_VOID connect_pair(
  35. basic_socket<Protocol BOOST_ASIO_SVC_TARG>& socket1,
  36. basic_socket<Protocol BOOST_ASIO_SVC_TARG1>& socket2,
  37. boost::system::error_code& ec);
  38. template <typename Protocol BOOST_ASIO_SVC_TPARAM BOOST_ASIO_SVC_TPARAM1>
  39. inline void connect_pair(
  40. basic_socket<Protocol BOOST_ASIO_SVC_TARG>& socket1,
  41. basic_socket<Protocol BOOST_ASIO_SVC_TARG1>& socket2)
  42. {
  43. boost::system::error_code ec;
  44. connect_pair(socket1, socket2, ec);
  45. boost::asio::detail::throw_error(ec, "connect_pair");
  46. }
  47. template <typename Protocol BOOST_ASIO_SVC_TPARAM BOOST_ASIO_SVC_TPARAM1>
  48. inline BOOST_ASIO_SYNC_OP_VOID connect_pair(
  49. basic_socket<Protocol BOOST_ASIO_SVC_TARG>& socket1,
  50. basic_socket<Protocol BOOST_ASIO_SVC_TARG1>& socket2,
  51. boost::system::error_code& ec)
  52. {
  53. // Check that this function is only being used with a UNIX domain socket.
  54. boost::asio::local::basic_endpoint<Protocol>* tmp
  55. = static_cast<typename Protocol::endpoint*>(0);
  56. (void)tmp;
  57. Protocol protocol;
  58. boost::asio::detail::socket_type sv[2];
  59. if (boost::asio::detail::socket_ops::socketpair(protocol.family(),
  60. protocol.type(), protocol.protocol(), sv, ec)
  61. == boost::asio::detail::socket_error_retval)
  62. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  63. socket1.assign(protocol, sv[0], ec);
  64. if (ec)
  65. {
  66. boost::system::error_code temp_ec;
  67. boost::asio::detail::socket_ops::state_type state[2] = { 0, 0 };
  68. boost::asio::detail::socket_ops::close(sv[0], state[0], true, temp_ec);
  69. boost::asio::detail::socket_ops::close(sv[1], state[1], true, temp_ec);
  70. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  71. }
  72. socket2.assign(protocol, sv[1], ec);
  73. if (ec)
  74. {
  75. boost::system::error_code temp_ec;
  76. socket1.close(temp_ec);
  77. boost::asio::detail::socket_ops::state_type state = 0;
  78. boost::asio::detail::socket_ops::close(sv[1], state, true, temp_ec);
  79. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  80. }
  81. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  82. }
  83. } // namespace local
  84. } // namespace asio
  85. } // namespace boost
  86. #include <boost/asio/detail/pop_options.hpp>
  87. #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
  88. // || defined(GENERATING_DOCUMENTATION)
  89. #endif // BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP