change support python version
[platform/upstream/boost.git] / boost / asio / detail / impl / reactive_socket_service_base.ipp
1 //
2 // detail/reactive_socket_service_base.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 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
11 #ifndef BOOST_ASIO_DETAIL_IMPL_REACTIVE_SOCKET_SERVICE_BASE_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_SOCKET_SERVICE_BASE_IPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if !defined(BOOST_ASIO_HAS_IOCP) \
21   && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
22
23 #include <boost/asio/detail/reactive_socket_service_base.hpp>
24
25 #include <boost/asio/detail/push_options.hpp>
26
27 namespace boost {
28 namespace asio {
29 namespace detail {
30
31 reactive_socket_service_base::reactive_socket_service_base(
32     execution_context& context)
33   : reactor_(use_service<reactor>(context))
34 {
35   reactor_.init_task();
36 }
37
38 void reactive_socket_service_base::base_shutdown()
39 {
40 }
41
42 void reactive_socket_service_base::construct(
43     reactive_socket_service_base::base_implementation_type& impl)
44 {
45   impl.socket_ = invalid_socket;
46   impl.state_ = 0;
47 }
48
49 void reactive_socket_service_base::base_move_construct(
50     reactive_socket_service_base::base_implementation_type& impl,
51     reactive_socket_service_base::base_implementation_type& other_impl)
52 {
53   impl.socket_ = other_impl.socket_;
54   other_impl.socket_ = invalid_socket;
55
56   impl.state_ = other_impl.state_;
57   other_impl.state_ = 0;
58
59   reactor_.move_descriptor(impl.socket_,
60       impl.reactor_data_, other_impl.reactor_data_);
61 }
62
63 void reactive_socket_service_base::base_move_assign(
64     reactive_socket_service_base::base_implementation_type& impl,
65     reactive_socket_service_base& other_service,
66     reactive_socket_service_base::base_implementation_type& other_impl)
67 {
68   destroy(impl);
69
70   impl.socket_ = other_impl.socket_;
71   other_impl.socket_ = invalid_socket;
72
73   impl.state_ = other_impl.state_;
74   other_impl.state_ = 0;
75
76   other_service.reactor_.move_descriptor(impl.socket_,
77       impl.reactor_data_, other_impl.reactor_data_);
78 }
79
80 void reactive_socket_service_base::destroy(
81     reactive_socket_service_base::base_implementation_type& impl)
82 {
83   if (impl.socket_ != invalid_socket)
84   {
85     BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
86           "socket", &impl, impl.socket_, "close"));
87
88     reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_,
89         (impl.state_ & socket_ops::possible_dup) == 0);
90
91     boost::system::error_code ignored_ec;
92     socket_ops::close(impl.socket_, impl.state_, true, ignored_ec);
93
94     reactor_.cleanup_descriptor_data(impl.reactor_data_);
95   }
96 }
97
98 boost::system::error_code reactive_socket_service_base::close(
99     reactive_socket_service_base::base_implementation_type& impl,
100     boost::system::error_code& ec)
101 {
102   if (is_open(impl))
103   {
104     BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
105           "socket", &impl, impl.socket_, "close"));
106
107     reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_,
108         (impl.state_ & socket_ops::possible_dup) == 0);
109
110     socket_ops::close(impl.socket_, impl.state_, false, ec);
111
112     reactor_.cleanup_descriptor_data(impl.reactor_data_);
113   }
114   else
115   {
116     ec = boost::system::error_code();
117   }
118
119   // The descriptor is closed by the OS even if close() returns an error.
120   //
121   // (Actually, POSIX says the state of the descriptor is unspecified. On
122   // Linux the descriptor is apparently closed anyway; e.g. see
123   //   http://lkml.org/lkml/2005/9/10/129
124   // We'll just have to assume that other OSes follow the same behaviour. The
125   // known exception is when Windows's closesocket() function fails with
126   // WSAEWOULDBLOCK, but this case is handled inside socket_ops::close().
127   construct(impl);
128
129   return ec;
130 }
131
132 socket_type reactive_socket_service_base::release(
133     reactive_socket_service_base::base_implementation_type& impl,
134     boost::system::error_code& ec)
135 {
136   if (!is_open(impl))
137   {
138     ec = boost::asio::error::bad_descriptor;
139     return invalid_socket;
140   }
141
142   BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
143         "socket", &impl, impl.socket_, "release"));
144
145   reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_, false);
146   reactor_.cleanup_descriptor_data(impl.reactor_data_);
147   socket_type sock = impl.socket_;
148   construct(impl);
149   ec = boost::system::error_code();
150   return sock;
151 }
152
153 boost::system::error_code reactive_socket_service_base::cancel(
154     reactive_socket_service_base::base_implementation_type& impl,
155     boost::system::error_code& ec)
156 {
157   if (!is_open(impl))
158   {
159     ec = boost::asio::error::bad_descriptor;
160     return ec;
161   }
162
163   BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
164         "socket", &impl, impl.socket_, "cancel"));
165
166   reactor_.cancel_ops(impl.socket_, impl.reactor_data_);
167   ec = boost::system::error_code();
168   return ec;
169 }
170
171 boost::system::error_code reactive_socket_service_base::do_open(
172     reactive_socket_service_base::base_implementation_type& impl,
173     int af, int type, int protocol, boost::system::error_code& ec)
174 {
175   if (is_open(impl))
176   {
177     ec = boost::asio::error::already_open;
178     return ec;
179   }
180
181   socket_holder sock(socket_ops::socket(af, type, protocol, ec));
182   if (sock.get() == invalid_socket)
183     return ec;
184
185   if (int err = reactor_.register_descriptor(sock.get(), impl.reactor_data_))
186   {
187     ec = boost::system::error_code(err,
188         boost::asio::error::get_system_category());
189     return ec;
190   }
191
192   impl.socket_ = sock.release();
193   switch (type)
194   {
195   case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
196   case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
197   default: impl.state_ = 0; break;
198   }
199   ec = boost::system::error_code();
200   return ec;
201 }
202
203 boost::system::error_code reactive_socket_service_base::do_assign(
204     reactive_socket_service_base::base_implementation_type& impl, int type,
205     const reactive_socket_service_base::native_handle_type& native_socket,
206     boost::system::error_code& ec)
207 {
208   if (is_open(impl))
209   {
210     ec = boost::asio::error::already_open;
211     return ec;
212   }
213
214   if (int err = reactor_.register_descriptor(
215         native_socket, impl.reactor_data_))
216   {
217     ec = boost::system::error_code(err,
218         boost::asio::error::get_system_category());
219     return ec;
220   }
221
222   impl.socket_ = native_socket;
223   switch (type)
224   {
225   case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
226   case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
227   default: impl.state_ = 0; break;
228   }
229   impl.state_ |= socket_ops::possible_dup;
230   ec = boost::system::error_code();
231   return ec;
232 }
233
234 void reactive_socket_service_base::start_op(
235     reactive_socket_service_base::base_implementation_type& impl,
236     int op_type, reactor_op* op, bool is_continuation,
237     bool is_non_blocking, bool noop)
238 {
239   if (!noop)
240   {
241     if ((impl.state_ & socket_ops::non_blocking)
242         || socket_ops::set_internal_non_blocking(
243           impl.socket_, impl.state_, true, op->ec_))
244     {
245       reactor_.start_op(op_type, impl.socket_,
246           impl.reactor_data_, op, is_continuation, is_non_blocking);
247       return;
248     }
249   }
250
251   reactor_.post_immediate_completion(op, is_continuation);
252 }
253
254 void reactive_socket_service_base::start_accept_op(
255     reactive_socket_service_base::base_implementation_type& impl,
256     reactor_op* op, bool is_continuation, bool peer_is_open)
257 {
258   if (!peer_is_open)
259     start_op(impl, reactor::read_op, op, is_continuation, true, false);
260   else
261   {
262     op->ec_ = boost::asio::error::already_open;
263     reactor_.post_immediate_completion(op, is_continuation);
264   }
265 }
266
267 void reactive_socket_service_base::start_connect_op(
268     reactive_socket_service_base::base_implementation_type& impl,
269     reactor_op* op, bool is_continuation,
270     const socket_addr_type* addr, size_t addrlen)
271 {
272   if ((impl.state_ & socket_ops::non_blocking)
273       || socket_ops::set_internal_non_blocking(
274         impl.socket_, impl.state_, true, op->ec_))
275   {
276     if (socket_ops::connect(impl.socket_, addr, addrlen, op->ec_) != 0)
277     {
278       if (op->ec_ == boost::asio::error::in_progress
279           || op->ec_ == boost::asio::error::would_block)
280       {
281         op->ec_ = boost::system::error_code();
282         reactor_.start_op(reactor::connect_op, impl.socket_,
283             impl.reactor_data_, op, is_continuation, false);
284         return;
285       }
286     }
287   }
288
289   reactor_.post_immediate_completion(op, is_continuation);
290 }
291
292 } // namespace detail
293 } // namespace asio
294 } // namespace boost
295
296 #include <boost/asio/detail/pop_options.hpp>
297
298 #endif // !defined(BOOST_ASIO_HAS_IOCP)
299        //   && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
300
301 #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_SOCKET_SERVICE_BASE_IPP