Statistics
| Revision:

root / tmp / org.txm.statsengine.r.core.win32 / res / win32 / library / BH / include / boost / asio / detail / impl / reactive_socket_service_base.ipp @ 2486

History | View | Annotate | Download (7.7 kB)

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