connect_pair.hpp 2.9 KB

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