PageRenderTime 13ms CodeModel.GetById 7ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/detail/impl/reactive_descriptor_service.ipp

http://hadesmem.googlecode.com/
C++ Header | 205 lines | 149 code | 41 blank | 15 comment | 12 complexity | 310f71f117786e3695ff603510be46e0 MD5 | raw file
  1//
  2// detail/impl/reactive_descriptor_service.ipp
  3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4//
  5// Copyright (c) 2003-2011 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_DESCRIPTOR_SERVICE_IPP
 12#define BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_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_WINDOWS) && !defined(__CYGWIN__)
 21
 22#include <boost/asio/error.hpp>
 23#include <boost/asio/detail/reactive_descriptor_service.hpp>
 24
 25#include <boost/asio/detail/push_options.hpp>
 26
 27namespace boost {
 28namespace asio {
 29namespace detail {
 30
 31reactive_descriptor_service::reactive_descriptor_service(
 32    boost::asio::io_service& io_service)
 33  : reactor_(boost::asio::use_service<reactor>(io_service))
 34{
 35  reactor_.init_task();
 36}
 37
 38void reactive_descriptor_service::shutdown_service()
 39{
 40}
 41
 42void reactive_descriptor_service::construct(
 43    reactive_descriptor_service::implementation_type& impl)
 44{
 45  impl.descriptor_ = -1;
 46  impl.state_ = 0;
 47}
 48
 49void reactive_descriptor_service::move_construct(
 50    reactive_descriptor_service::implementation_type& impl,
 51    reactive_descriptor_service::implementation_type& other_impl)
 52{
 53  impl.descriptor_ = other_impl.descriptor_;
 54  other_impl.descriptor_ = -1;
 55
 56  impl.state_ = other_impl.state_;
 57  other_impl.state_ = 0;
 58
 59  reactor_.move_descriptor(impl.descriptor_,
 60      impl.reactor_data_, other_impl.reactor_data_);
 61}
 62
 63void reactive_descriptor_service::move_assign(
 64    reactive_descriptor_service::implementation_type& impl,
 65    reactive_descriptor_service& other_service,
 66    reactive_descriptor_service::implementation_type& other_impl)
 67{
 68  destroy(impl);
 69
 70  impl.descriptor_ = other_impl.descriptor_;
 71  other_impl.descriptor_ = -1;
 72
 73  impl.state_ = other_impl.state_;
 74  other_impl.state_ = 0;
 75
 76  other_service.reactor_.move_descriptor(impl.descriptor_,
 77      impl.reactor_data_, other_impl.reactor_data_);
 78}
 79
 80void reactive_descriptor_service::destroy(
 81    reactive_descriptor_service::implementation_type& impl)
 82{
 83  if (is_open(impl))
 84  {
 85    BOOST_ASIO_HANDLER_OPERATION(("descriptor", &impl, "close"));
 86
 87    reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
 88        (impl.state_ & descriptor_ops::possible_dup) == 0);
 89  }
 90
 91  boost::system::error_code ignored_ec;
 92  descriptor_ops::close(impl.descriptor_, impl.state_, ignored_ec);
 93}
 94
 95boost::system::error_code reactive_descriptor_service::assign(
 96    reactive_descriptor_service::implementation_type& impl,
 97    const native_handle_type& native_descriptor, boost::system::error_code& ec)
 98{
 99  if (is_open(impl))
100  {
101    ec = boost::asio::error::already_open;
102    return ec;
103  }
104
105  if (int err = reactor_.register_descriptor(
106        native_descriptor, impl.reactor_data_))
107  {
108    ec = boost::system::error_code(err,
109        boost::asio::error::get_system_category());
110    return ec;
111  }
112
113  impl.descriptor_ = native_descriptor;
114  impl.state_ = descriptor_ops::possible_dup;
115  ec = boost::system::error_code();
116  return ec;
117}
118
119boost::system::error_code reactive_descriptor_service::close(
120    reactive_descriptor_service::implementation_type& impl,
121    boost::system::error_code& ec)
122{
123  if (is_open(impl))
124  {
125    BOOST_ASIO_HANDLER_OPERATION(("descriptor", &impl, "close"));
126
127    reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
128        (impl.state_ & descriptor_ops::possible_dup) == 0);
129  }
130
131  descriptor_ops::close(impl.descriptor_, impl.state_, ec);
132
133  // The descriptor is closed by the OS even if close() returns an error.
134  //
135  // (Actually, POSIX says the state of the descriptor is unspecified. On
136  // Linux the descriptor is apparently closed anyway; e.g. see
137  //   http://lkml.org/lkml/2005/9/10/129
138  // We'll just have to assume that other OSes follow the same behaviour.)
139  construct(impl);
140
141  return ec;
142}
143
144reactive_descriptor_service::native_handle_type
145reactive_descriptor_service::release(
146    reactive_descriptor_service::implementation_type& impl)
147{
148  native_handle_type descriptor = impl.descriptor_;
149
150  if (is_open(impl))
151  {
152    BOOST_ASIO_HANDLER_OPERATION(("descriptor", &impl, "release"));
153
154    reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_, false);
155    construct(impl);
156  }
157
158  return descriptor;
159}
160
161boost::system::error_code reactive_descriptor_service::cancel(
162    reactive_descriptor_service::implementation_type& impl,
163    boost::system::error_code& ec)
164{
165  if (!is_open(impl))
166  {
167    ec = boost::asio::error::bad_descriptor;
168    return ec;
169  }
170
171  BOOST_ASIO_HANDLER_OPERATION(("descriptor", &impl, "cancel"));
172
173  reactor_.cancel_ops(impl.descriptor_, impl.reactor_data_);
174  ec = boost::system::error_code();
175  return ec;
176}
177
178void reactive_descriptor_service::start_op(
179    reactive_descriptor_service::implementation_type& impl,
180    int op_type, reactor_op* op, bool is_non_blocking, bool noop)
181{
182  if (!noop)
183  {
184    if ((impl.state_ & descriptor_ops::non_blocking) ||
185        descriptor_ops::set_internal_non_blocking(
186          impl.descriptor_, impl.state_, true, op->ec_))
187    {
188      reactor_.start_op(op_type, impl.descriptor_,
189          impl.reactor_data_, op, is_non_blocking);
190      return;
191    }
192  }
193
194  reactor_.post_immediate_completion(op);
195}
196
197} // namespace detail
198} // namespace asio
199} // namespace boost
200
201#include <boost/asio/detail/pop_options.hpp>
202
203#endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
204
205#endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP