Chris@16
|
1 //
|
Chris@16
|
2 // local/connect_pair.hpp
|
Chris@16
|
3 // ~~~~~~~~~~~~~~~~~~~~~~
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
Chris@16
|
6 //
|
Chris@16
|
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
|
Chris@16
|
12 #define BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
Chris@16
|
17
|
Chris@16
|
18 #include <boost/asio/detail/config.hpp>
|
Chris@16
|
19
|
Chris@16
|
20 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS) \
|
Chris@16
|
21 || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/asio/basic_socket.hpp>
|
Chris@16
|
24 #include <boost/asio/detail/socket_ops.hpp>
|
Chris@16
|
25 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
26 #include <boost/asio/error.hpp>
|
Chris@16
|
27 #include <boost/asio/local/basic_endpoint.hpp>
|
Chris@16
|
28
|
Chris@16
|
29 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
30
|
Chris@16
|
31 namespace boost {
|
Chris@16
|
32 namespace asio {
|
Chris@16
|
33 namespace local {
|
Chris@16
|
34
|
Chris@16
|
35 /// Create a pair of connected sockets.
|
Chris@16
|
36 template <typename Protocol, typename SocketService1, typename SocketService2>
|
Chris@16
|
37 void connect_pair(
|
Chris@16
|
38 basic_socket<Protocol, SocketService1>& socket1,
|
Chris@16
|
39 basic_socket<Protocol, SocketService2>& socket2);
|
Chris@16
|
40
|
Chris@16
|
41 /// Create a pair of connected sockets.
|
Chris@16
|
42 template <typename Protocol, typename SocketService1, typename SocketService2>
|
Chris@16
|
43 boost::system::error_code connect_pair(
|
Chris@16
|
44 basic_socket<Protocol, SocketService1>& socket1,
|
Chris@16
|
45 basic_socket<Protocol, SocketService2>& socket2,
|
Chris@16
|
46 boost::system::error_code& ec);
|
Chris@16
|
47
|
Chris@16
|
48 template <typename Protocol, typename SocketService1, typename SocketService2>
|
Chris@16
|
49 inline void connect_pair(
|
Chris@16
|
50 basic_socket<Protocol, SocketService1>& socket1,
|
Chris@16
|
51 basic_socket<Protocol, SocketService2>& socket2)
|
Chris@16
|
52 {
|
Chris@16
|
53 boost::system::error_code ec;
|
Chris@16
|
54 connect_pair(socket1, socket2, ec);
|
Chris@16
|
55 boost::asio::detail::throw_error(ec, "connect_pair");
|
Chris@16
|
56 }
|
Chris@16
|
57
|
Chris@16
|
58 template <typename Protocol, typename SocketService1, typename SocketService2>
|
Chris@16
|
59 inline boost::system::error_code connect_pair(
|
Chris@16
|
60 basic_socket<Protocol, SocketService1>& socket1,
|
Chris@16
|
61 basic_socket<Protocol, SocketService2>& socket2,
|
Chris@16
|
62 boost::system::error_code& ec)
|
Chris@16
|
63 {
|
Chris@16
|
64 // Check that this function is only being used with a UNIX domain socket.
|
Chris@16
|
65 boost::asio::local::basic_endpoint<Protocol>* tmp
|
Chris@16
|
66 = static_cast<typename Protocol::endpoint*>(0);
|
Chris@16
|
67 (void)tmp;
|
Chris@16
|
68
|
Chris@16
|
69 Protocol protocol;
|
Chris@16
|
70 boost::asio::detail::socket_type sv[2];
|
Chris@16
|
71 if (boost::asio::detail::socket_ops::socketpair(protocol.family(),
|
Chris@16
|
72 protocol.type(), protocol.protocol(), sv, ec)
|
Chris@16
|
73 == boost::asio::detail::socket_error_retval)
|
Chris@16
|
74 return ec;
|
Chris@16
|
75
|
Chris@16
|
76 if (socket1.assign(protocol, sv[0], ec))
|
Chris@16
|
77 {
|
Chris@16
|
78 boost::system::error_code temp_ec;
|
Chris@16
|
79 boost::asio::detail::socket_ops::state_type state[2] = { 0, 0 };
|
Chris@16
|
80 boost::asio::detail::socket_ops::close(sv[0], state[0], true, temp_ec);
|
Chris@16
|
81 boost::asio::detail::socket_ops::close(sv[1], state[1], true, temp_ec);
|
Chris@16
|
82 return ec;
|
Chris@16
|
83 }
|
Chris@16
|
84
|
Chris@16
|
85 if (socket2.assign(protocol, sv[1], ec))
|
Chris@16
|
86 {
|
Chris@16
|
87 boost::system::error_code temp_ec;
|
Chris@16
|
88 socket1.close(temp_ec);
|
Chris@16
|
89 boost::asio::detail::socket_ops::state_type state = 0;
|
Chris@16
|
90 boost::asio::detail::socket_ops::close(sv[1], state, true, temp_ec);
|
Chris@16
|
91 return ec;
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 return ec;
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 } // namespace local
|
Chris@16
|
98 } // namespace asio
|
Chris@16
|
99 } // namespace boost
|
Chris@16
|
100
|
Chris@16
|
101 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
102
|
Chris@16
|
103 #endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
|
Chris@16
|
104 // || defined(GENERATING_DOCUMENTATION)
|
Chris@16
|
105
|
Chris@16
|
106 #endif // BOOST_ASIO_LOCAL_CONNECT_PAIR_HPP
|