Chris@16
|
1 //
|
Chris@16
|
2 // detail/socket_holder.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_DETAIL_SOCKET_HOLDER_HPP
|
Chris@16
|
12 #define BOOST_ASIO_DETAIL_SOCKET_HOLDER_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 #include <boost/asio/detail/noncopyable.hpp>
|
Chris@16
|
20 #include <boost/asio/detail/socket_ops.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost {
|
Chris@16
|
25 namespace asio {
|
Chris@16
|
26 namespace detail {
|
Chris@16
|
27
|
Chris@16
|
28 // Implement the resource acquisition is initialisation idiom for sockets.
|
Chris@16
|
29 class socket_holder
|
Chris@16
|
30 : private noncopyable
|
Chris@16
|
31 {
|
Chris@16
|
32 public:
|
Chris@16
|
33 // Construct as an uninitialised socket.
|
Chris@16
|
34 socket_holder()
|
Chris@16
|
35 : socket_(invalid_socket)
|
Chris@16
|
36 {
|
Chris@16
|
37 }
|
Chris@16
|
38
|
Chris@16
|
39 // Construct to take ownership of the specified socket.
|
Chris@16
|
40 explicit socket_holder(socket_type s)
|
Chris@16
|
41 : socket_(s)
|
Chris@16
|
42 {
|
Chris@16
|
43 }
|
Chris@16
|
44
|
Chris@16
|
45 // Destructor.
|
Chris@16
|
46 ~socket_holder()
|
Chris@16
|
47 {
|
Chris@16
|
48 if (socket_ != invalid_socket)
|
Chris@16
|
49 {
|
Chris@16
|
50 boost::system::error_code ec;
|
Chris@16
|
51 socket_ops::state_type state = 0;
|
Chris@16
|
52 socket_ops::close(socket_, state, true, ec);
|
Chris@16
|
53 }
|
Chris@16
|
54 }
|
Chris@16
|
55
|
Chris@16
|
56 // Get the underlying socket.
|
Chris@16
|
57 socket_type get() const
|
Chris@16
|
58 {
|
Chris@16
|
59 return socket_;
|
Chris@16
|
60 }
|
Chris@16
|
61
|
Chris@16
|
62 // Reset to an uninitialised socket.
|
Chris@16
|
63 void reset()
|
Chris@16
|
64 {
|
Chris@16
|
65 if (socket_ != invalid_socket)
|
Chris@16
|
66 {
|
Chris@16
|
67 boost::system::error_code ec;
|
Chris@16
|
68 socket_ops::state_type state = 0;
|
Chris@16
|
69 socket_ops::close(socket_, state, true, ec);
|
Chris@16
|
70 socket_ = invalid_socket;
|
Chris@16
|
71 }
|
Chris@16
|
72 }
|
Chris@16
|
73
|
Chris@16
|
74 // Reset to take ownership of the specified socket.
|
Chris@16
|
75 void reset(socket_type s)
|
Chris@16
|
76 {
|
Chris@16
|
77 reset();
|
Chris@16
|
78 socket_ = s;
|
Chris@16
|
79 }
|
Chris@16
|
80
|
Chris@16
|
81 // Release ownership of the socket.
|
Chris@16
|
82 socket_type release()
|
Chris@16
|
83 {
|
Chris@16
|
84 socket_type tmp = socket_;
|
Chris@16
|
85 socket_ = invalid_socket;
|
Chris@16
|
86 return tmp;
|
Chris@16
|
87 }
|
Chris@16
|
88
|
Chris@16
|
89 private:
|
Chris@16
|
90 // The underlying socket.
|
Chris@16
|
91 socket_type socket_;
|
Chris@16
|
92 };
|
Chris@16
|
93
|
Chris@16
|
94 } // namespace detail
|
Chris@16
|
95 } // namespace asio
|
Chris@16
|
96 } // namespace boost
|
Chris@16
|
97
|
Chris@16
|
98 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
99
|
Chris@16
|
100 #endif // BOOST_ASIO_DETAIL_SOCKET_HOLDER_HPP
|