replace boost.asio with standalone asio.
This commit is contained in:
+422
@@ -0,0 +1,422 @@
|
||||
//
|
||||
// impl/awaitable.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_AWAITABLE_HPP
|
||||
#define ASIO_IMPL_AWAITABLE_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include <exception>
|
||||
#include <new>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include "asio/detail/thread_context.hpp"
|
||||
#include "asio/detail/thread_info_base.hpp"
|
||||
#include "asio/detail/type_traits.hpp"
|
||||
#include "asio/post.hpp"
|
||||
#include "asio/system_error.hpp"
|
||||
#include "asio/this_coro.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
// An awaitable_thread represents a thread-of-execution that is composed of one
|
||||
// or more "stack frames", with each frame represented by an awaitable_frame.
|
||||
// All execution occurs in the context of the awaitable_thread's executor. An
|
||||
// awaitable_thread continues to "pump" the stack frames by repeatedly resuming
|
||||
// the top stack frame until the stack is empty, or until ownership of the
|
||||
// stack is transferred to another awaitable_thread object.
|
||||
//
|
||||
// +------------------------------------+
|
||||
// | top_of_stack_ |
|
||||
// | V
|
||||
// +--------------+---+ +-----------------+
|
||||
// | | | |
|
||||
// | awaitable_thread |<---------------------------+ awaitable_frame |
|
||||
// | | attached_thread_ | |
|
||||
// +--------------+---+ (Set only when +---+-------------+
|
||||
// | frames are being |
|
||||
// | actively pumped | caller_
|
||||
// | by a thread, and |
|
||||
// | then only for V
|
||||
// | the top frame.) +-----------------+
|
||||
// | | |
|
||||
// | | awaitable_frame |
|
||||
// | | |
|
||||
// | +---+-------------+
|
||||
// | |
|
||||
// | | caller_
|
||||
// | :
|
||||
// | :
|
||||
// | |
|
||||
// | V
|
||||
// | +-----------------+
|
||||
// | bottom_of_stack_ | |
|
||||
// +------------------------------->| awaitable_frame |
|
||||
// | |
|
||||
// +-----------------+
|
||||
|
||||
template <typename Executor>
|
||||
class awaitable_frame_base
|
||||
{
|
||||
public:
|
||||
#if !defined(ASIO_DISABLE_AWAITABLE_FRAME_RECYCLING)
|
||||
void* operator new(std::size_t size)
|
||||
{
|
||||
return asio::detail::thread_info_base::allocate(
|
||||
asio::detail::thread_info_base::awaitable_frame_tag(),
|
||||
asio::detail::thread_context::thread_call_stack::top(),
|
||||
size);
|
||||
}
|
||||
|
||||
void operator delete(void* pointer, std::size_t size)
|
||||
{
|
||||
asio::detail::thread_info_base::deallocate(
|
||||
asio::detail::thread_info_base::awaitable_frame_tag(),
|
||||
asio::detail::thread_context::thread_call_stack::top(),
|
||||
pointer, size);
|
||||
}
|
||||
#endif // !defined(ASIO_DISABLE_AWAITABLE_FRAME_RECYCLING)
|
||||
|
||||
// The frame starts in a suspended state until the awaitable_thread object
|
||||
// pumps the stack.
|
||||
auto initial_suspend() noexcept
|
||||
{
|
||||
return suspend_always();
|
||||
}
|
||||
|
||||
// On final suspension the frame is popped from the top of the stack.
|
||||
auto final_suspend() noexcept
|
||||
{
|
||||
struct result
|
||||
{
|
||||
awaitable_frame_base* this_;
|
||||
|
||||
bool await_ready() const noexcept
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void await_suspend(coroutine_handle<void>) noexcept
|
||||
{
|
||||
this_->pop_frame();
|
||||
}
|
||||
|
||||
void await_resume() const noexcept
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
return result{this};
|
||||
}
|
||||
|
||||
void set_except(std::exception_ptr e) noexcept
|
||||
{
|
||||
pending_exception_ = e;
|
||||
}
|
||||
|
||||
void set_error(const asio::error_code& ec)
|
||||
{
|
||||
this->set_except(std::make_exception_ptr(asio::system_error(ec)));
|
||||
}
|
||||
|
||||
void unhandled_exception()
|
||||
{
|
||||
set_except(std::current_exception());
|
||||
}
|
||||
|
||||
void rethrow_exception()
|
||||
{
|
||||
if (pending_exception_)
|
||||
{
|
||||
std::exception_ptr ex = std::exchange(pending_exception_, nullptr);
|
||||
std::rethrow_exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
auto await_transform(awaitable<T, Executor> a) const
|
||||
{
|
||||
return a;
|
||||
}
|
||||
|
||||
// This await transformation obtains the associated executor of the thread of
|
||||
// execution.
|
||||
auto await_transform(this_coro::executor_t) noexcept
|
||||
{
|
||||
struct result
|
||||
{
|
||||
awaitable_frame_base* this_;
|
||||
|
||||
bool await_ready() const noexcept
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
void await_suspend(coroutine_handle<void>) noexcept
|
||||
{
|
||||
}
|
||||
|
||||
auto await_resume() const noexcept
|
||||
{
|
||||
return this_->attached_thread_->get_executor();
|
||||
}
|
||||
};
|
||||
|
||||
return result{this};
|
||||
}
|
||||
|
||||
// This await transformation is used to run an async operation's initiation
|
||||
// function object after the coroutine has been suspended. This ensures that
|
||||
// immediate resumption of the coroutine in another thread does not cause a
|
||||
// race condition.
|
||||
template <typename Function>
|
||||
auto await_transform(Function f,
|
||||
typename enable_if<
|
||||
is_convertible<
|
||||
typename result_of<Function(awaitable_frame_base*)>::type,
|
||||
awaitable_thread<Executor>*
|
||||
>::value
|
||||
>::type* = 0)
|
||||
{
|
||||
struct result
|
||||
{
|
||||
Function function_;
|
||||
awaitable_frame_base* this_;
|
||||
|
||||
bool await_ready() const noexcept
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void await_suspend(coroutine_handle<void>) noexcept
|
||||
{
|
||||
function_(this_);
|
||||
}
|
||||
|
||||
void await_resume() const noexcept
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
return result{std::move(f), this};
|
||||
}
|
||||
|
||||
void attach_thread(awaitable_thread<Executor>* handler) noexcept
|
||||
{
|
||||
attached_thread_ = handler;
|
||||
}
|
||||
|
||||
awaitable_thread<Executor>* detach_thread() noexcept
|
||||
{
|
||||
return std::exchange(attached_thread_, nullptr);
|
||||
}
|
||||
|
||||
void push_frame(awaitable_frame_base<Executor>* caller) noexcept
|
||||
{
|
||||
caller_ = caller;
|
||||
attached_thread_ = caller_->attached_thread_;
|
||||
attached_thread_->top_of_stack_ = this;
|
||||
caller_->attached_thread_ = nullptr;
|
||||
}
|
||||
|
||||
void pop_frame() noexcept
|
||||
{
|
||||
if (caller_)
|
||||
caller_->attached_thread_ = attached_thread_;
|
||||
attached_thread_->top_of_stack_ = caller_;
|
||||
attached_thread_ = nullptr;
|
||||
caller_ = nullptr;
|
||||
}
|
||||
|
||||
void resume()
|
||||
{
|
||||
coro_.resume();
|
||||
}
|
||||
|
||||
void destroy()
|
||||
{
|
||||
coro_.destroy();
|
||||
}
|
||||
|
||||
protected:
|
||||
coroutine_handle<void> coro_ = nullptr;
|
||||
awaitable_thread<Executor>* attached_thread_ = nullptr;
|
||||
awaitable_frame_base<Executor>* caller_ = nullptr;
|
||||
std::exception_ptr pending_exception_ = nullptr;
|
||||
};
|
||||
|
||||
template <typename T, typename Executor>
|
||||
class awaitable_frame
|
||||
: public awaitable_frame_base<Executor>
|
||||
{
|
||||
public:
|
||||
awaitable_frame() noexcept
|
||||
{
|
||||
}
|
||||
|
||||
awaitable_frame(awaitable_frame&& other) noexcept
|
||||
: awaitable_frame_base<Executor>(std::move(other))
|
||||
{
|
||||
}
|
||||
|
||||
~awaitable_frame()
|
||||
{
|
||||
if (has_result_)
|
||||
static_cast<T*>(static_cast<void*>(result_))->~T();
|
||||
}
|
||||
|
||||
awaitable<T, Executor> get_return_object() noexcept
|
||||
{
|
||||
this->coro_ = coroutine_handle<awaitable_frame>::from_promise(*this);
|
||||
return awaitable<T, Executor>(this);
|
||||
};
|
||||
|
||||
template <typename U>
|
||||
void return_value(U&& u)
|
||||
{
|
||||
new (&result_) T(std::forward<U>(u));
|
||||
has_result_ = true;
|
||||
}
|
||||
|
||||
template <typename... Us>
|
||||
void return_values(Us&&... us)
|
||||
{
|
||||
this->return_value(std::forward_as_tuple(std::forward<Us>(us)...));
|
||||
}
|
||||
|
||||
T get()
|
||||
{
|
||||
this->caller_ = nullptr;
|
||||
this->rethrow_exception();
|
||||
return std::move(*static_cast<T*>(static_cast<void*>(result_)));
|
||||
}
|
||||
|
||||
private:
|
||||
alignas(T) unsigned char result_[sizeof(T)];
|
||||
bool has_result_ = false;
|
||||
};
|
||||
|
||||
template <typename Executor>
|
||||
class awaitable_frame<void, Executor>
|
||||
: public awaitable_frame_base<Executor>
|
||||
{
|
||||
public:
|
||||
awaitable<void, Executor> get_return_object()
|
||||
{
|
||||
this->coro_ = coroutine_handle<awaitable_frame>::from_promise(*this);
|
||||
return awaitable<void, Executor>(this);
|
||||
};
|
||||
|
||||
void return_void()
|
||||
{
|
||||
}
|
||||
|
||||
void get()
|
||||
{
|
||||
this->caller_ = nullptr;
|
||||
this->rethrow_exception();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor>
|
||||
class awaitable_thread
|
||||
{
|
||||
public:
|
||||
typedef Executor executor_type;
|
||||
|
||||
// Construct from the entry point of a new thread of execution.
|
||||
awaitable_thread(awaitable<void, Executor> p, const Executor& ex)
|
||||
: bottom_of_stack_(std::move(p)),
|
||||
top_of_stack_(bottom_of_stack_.frame_),
|
||||
executor_(ex)
|
||||
{
|
||||
}
|
||||
|
||||
// Transfer ownership from another awaitable_thread.
|
||||
awaitable_thread(awaitable_thread&& other) noexcept
|
||||
: bottom_of_stack_(std::move(other.bottom_of_stack_)),
|
||||
top_of_stack_(std::exchange(other.top_of_stack_, nullptr)),
|
||||
executor_(std::move(other.executor_))
|
||||
{
|
||||
}
|
||||
|
||||
// Clean up with a last ditch effort to ensure the thread is unwound within
|
||||
// the context of the executor.
|
||||
~awaitable_thread()
|
||||
{
|
||||
if (bottom_of_stack_.valid())
|
||||
{
|
||||
// Coroutine "stack unwinding" must be performed through the executor.
|
||||
(post)(executor_,
|
||||
[a = std::move(bottom_of_stack_)]() mutable
|
||||
{
|
||||
awaitable<void, Executor>(std::move(a));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
executor_type get_executor() const noexcept
|
||||
{
|
||||
return executor_;
|
||||
}
|
||||
|
||||
// Launch a new thread of execution.
|
||||
void launch()
|
||||
{
|
||||
top_of_stack_->attach_thread(this);
|
||||
pump();
|
||||
}
|
||||
|
||||
protected:
|
||||
template <typename> friend class awaitable_frame_base;
|
||||
|
||||
// Repeatedly resume the top stack frame until the stack is empty or until it
|
||||
// has been transferred to another resumable_thread object.
|
||||
void pump()
|
||||
{
|
||||
do top_of_stack_->resume(); while (top_of_stack_);
|
||||
if (bottom_of_stack_.valid())
|
||||
{
|
||||
awaitable<void, Executor> a(std::move(bottom_of_stack_));
|
||||
a.frame_->rethrow_exception();
|
||||
}
|
||||
}
|
||||
|
||||
awaitable<void, Executor> bottom_of_stack_;
|
||||
awaitable_frame_base<Executor>* top_of_stack_;
|
||||
executor_type executor_;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
} // namespace asio
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
namespace std { namespace experimental {
|
||||
|
||||
template <typename T, typename Executor, typename... Args>
|
||||
struct coroutine_traits<asio::awaitable<T, Executor>, Args...>
|
||||
{
|
||||
typedef asio::detail::awaitable_frame<T, Executor> promise_type;
|
||||
};
|
||||
|
||||
}} // namespace std::experimental
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_AWAITABLE_HPP
|
||||
@@ -0,0 +1,448 @@
|
||||
//
|
||||
// impl/buffered_read_stream.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_BUFFERED_READ_STREAM_HPP
|
||||
#define ASIO_IMPL_BUFFERED_READ_STREAM_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/detail/handler_alloc_helpers.hpp"
|
||||
#include "asio/detail/handler_cont_helpers.hpp"
|
||||
#include "asio/detail/handler_invoke_helpers.hpp"
|
||||
#include "asio/detail/handler_type_requirements.hpp"
|
||||
#include "asio/detail/non_const_lvalue.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
template <typename Stream>
|
||||
std::size_t buffered_read_stream<Stream>::fill()
|
||||
{
|
||||
detail::buffer_resize_guard<detail::buffered_stream_storage>
|
||||
resize_guard(storage_);
|
||||
std::size_t previous_size = storage_.size();
|
||||
storage_.resize(storage_.capacity());
|
||||
storage_.resize(previous_size + next_layer_.read_some(buffer(
|
||||
storage_.data() + previous_size,
|
||||
storage_.size() - previous_size)));
|
||||
resize_guard.commit();
|
||||
return storage_.size() - previous_size;
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
std::size_t buffered_read_stream<Stream>::fill(asio::error_code& ec)
|
||||
{
|
||||
detail::buffer_resize_guard<detail::buffered_stream_storage>
|
||||
resize_guard(storage_);
|
||||
std::size_t previous_size = storage_.size();
|
||||
storage_.resize(storage_.capacity());
|
||||
storage_.resize(previous_size + next_layer_.read_some(buffer(
|
||||
storage_.data() + previous_size,
|
||||
storage_.size() - previous_size),
|
||||
ec));
|
||||
resize_guard.commit();
|
||||
return storage_.size() - previous_size;
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename ReadHandler>
|
||||
class buffered_fill_handler
|
||||
{
|
||||
public:
|
||||
buffered_fill_handler(detail::buffered_stream_storage& storage,
|
||||
std::size_t previous_size, ReadHandler& handler)
|
||||
: storage_(storage),
|
||||
previous_size_(previous_size),
|
||||
handler_(ASIO_MOVE_CAST(ReadHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
buffered_fill_handler(const buffered_fill_handler& other)
|
||||
: storage_(other.storage_),
|
||||
previous_size_(other.previous_size_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
buffered_fill_handler(buffered_fill_handler&& other)
|
||||
: storage_(other.storage_),
|
||||
previous_size_(other.previous_size_),
|
||||
handler_(ASIO_MOVE_CAST(ReadHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec,
|
||||
const std::size_t bytes_transferred)
|
||||
{
|
||||
storage_.resize(previous_size_ + bytes_transferred);
|
||||
handler_(ec, bytes_transferred);
|
||||
}
|
||||
|
||||
//private:
|
||||
detail::buffered_stream_storage& storage_;
|
||||
std::size_t previous_size_;
|
||||
ReadHandler handler_;
|
||||
};
|
||||
|
||||
template <typename ReadHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
buffered_fill_handler<ReadHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename ReadHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
buffered_fill_handler<ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename ReadHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
buffered_fill_handler<ReadHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename ReadHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
buffered_fill_handler<ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename ReadHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
buffered_fill_handler<ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_buffered_fill
|
||||
{
|
||||
template <typename ReadHandler, typename Stream>
|
||||
void operator()(ASIO_MOVE_ARG(ReadHandler) handler,
|
||||
buffered_stream_storage* storage, Stream* next_layer) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a ReadHandler.
|
||||
ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
||||
|
||||
non_const_lvalue<ReadHandler> handler2(handler);
|
||||
std::size_t previous_size = storage->size();
|
||||
storage->resize(storage->capacity());
|
||||
next_layer->async_read_some(
|
||||
buffer(
|
||||
storage->data() + previous_size,
|
||||
storage->size() - previous_size),
|
||||
buffered_fill_handler<typename decay<ReadHandler>::type>(
|
||||
*storage, previous_size, handler2.value));
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename ReadHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::buffered_fill_handler<ReadHandler>, Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<ReadHandler, Allocator>::type type;
|
||||
|
||||
static type get(const detail::buffered_fill_handler<ReadHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ReadHandler, typename Executor>
|
||||
struct associated_executor<
|
||||
detail::buffered_fill_handler<ReadHandler>, Executor>
|
||||
{
|
||||
typedef typename associated_executor<ReadHandler, Executor>::type type;
|
||||
|
||||
static type get(const detail::buffered_fill_handler<ReadHandler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Stream>
|
||||
template <typename ReadHandler>
|
||||
ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
buffered_read_stream<Stream>::async_fill(
|
||||
ASIO_MOVE_ARG(ReadHandler) handler)
|
||||
{
|
||||
return async_initiate<ReadHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_buffered_fill(), handler, &storage_, &next_layer_);
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
template <typename MutableBufferSequence>
|
||||
std::size_t buffered_read_stream<Stream>::read_some(
|
||||
const MutableBufferSequence& buffers)
|
||||
{
|
||||
using asio::buffer_size;
|
||||
if (buffer_size(buffers) == 0)
|
||||
return 0;
|
||||
|
||||
if (storage_.empty())
|
||||
this->fill();
|
||||
|
||||
return this->copy(buffers);
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
template <typename MutableBufferSequence>
|
||||
std::size_t buffered_read_stream<Stream>::read_some(
|
||||
const MutableBufferSequence& buffers, asio::error_code& ec)
|
||||
{
|
||||
ec = asio::error_code();
|
||||
|
||||
using asio::buffer_size;
|
||||
if (buffer_size(buffers) == 0)
|
||||
return 0;
|
||||
|
||||
if (storage_.empty() && !this->fill(ec))
|
||||
return 0;
|
||||
|
||||
return this->copy(buffers);
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename MutableBufferSequence, typename ReadHandler>
|
||||
class buffered_read_some_handler
|
||||
{
|
||||
public:
|
||||
buffered_read_some_handler(detail::buffered_stream_storage& storage,
|
||||
const MutableBufferSequence& buffers, ReadHandler& handler)
|
||||
: storage_(storage),
|
||||
buffers_(buffers),
|
||||
handler_(ASIO_MOVE_CAST(ReadHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
buffered_read_some_handler(const buffered_read_some_handler& other)
|
||||
: storage_(other.storage_),
|
||||
buffers_(other.buffers_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
buffered_read_some_handler(buffered_read_some_handler&& other)
|
||||
: storage_(other.storage_),
|
||||
buffers_(other.buffers_),
|
||||
handler_(ASIO_MOVE_CAST(ReadHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec, std::size_t)
|
||||
{
|
||||
if (ec || storage_.empty())
|
||||
{
|
||||
const std::size_t length = 0;
|
||||
handler_(ec, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
const std::size_t bytes_copied = asio::buffer_copy(
|
||||
buffers_, storage_.data(), storage_.size());
|
||||
storage_.consume(bytes_copied);
|
||||
handler_(ec, bytes_copied);
|
||||
}
|
||||
}
|
||||
|
||||
//private:
|
||||
detail::buffered_stream_storage& storage_;
|
||||
MutableBufferSequence buffers_;
|
||||
ReadHandler handler_;
|
||||
};
|
||||
|
||||
template <typename MutableBufferSequence, typename ReadHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
buffered_read_some_handler<
|
||||
MutableBufferSequence, ReadHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename MutableBufferSequence, typename ReadHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
buffered_read_some_handler<
|
||||
MutableBufferSequence, ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename MutableBufferSequence, typename ReadHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
buffered_read_some_handler<
|
||||
MutableBufferSequence, ReadHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename MutableBufferSequence,
|
||||
typename ReadHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
buffered_read_some_handler<
|
||||
MutableBufferSequence, ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename MutableBufferSequence,
|
||||
typename ReadHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
buffered_read_some_handler<
|
||||
MutableBufferSequence, ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_buffered_read_some
|
||||
{
|
||||
template <typename ReadHandler, typename Stream,
|
||||
typename MutableBufferSequence>
|
||||
void operator()(ASIO_MOVE_ARG(ReadHandler) handler,
|
||||
buffered_stream_storage* storage, Stream* next_layer,
|
||||
const MutableBufferSequence& buffers) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a ReadHandler.
|
||||
ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
||||
|
||||
using asio::buffer_size;
|
||||
non_const_lvalue<ReadHandler> handler2(handler);
|
||||
if (buffer_size(buffers) == 0 || !storage->empty())
|
||||
{
|
||||
next_layer->async_read_some(ASIO_MUTABLE_BUFFER(0, 0),
|
||||
buffered_read_some_handler<MutableBufferSequence,
|
||||
typename decay<ReadHandler>::type>(
|
||||
*storage, buffers, handler2.value));
|
||||
}
|
||||
else
|
||||
{
|
||||
initiate_async_buffered_fill()(
|
||||
buffered_read_some_handler<MutableBufferSequence,
|
||||
typename decay<ReadHandler>::type>(
|
||||
*storage, buffers, handler2.value),
|
||||
storage, next_layer);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename MutableBufferSequence,
|
||||
typename ReadHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::buffered_read_some_handler<MutableBufferSequence, ReadHandler>,
|
||||
Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<ReadHandler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::buffered_read_some_handler<
|
||||
MutableBufferSequence, ReadHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename MutableBufferSequence,
|
||||
typename ReadHandler, typename Executor>
|
||||
struct associated_executor<
|
||||
detail::buffered_read_some_handler<MutableBufferSequence, ReadHandler>,
|
||||
Executor>
|
||||
{
|
||||
typedef typename associated_executor<ReadHandler, Executor>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::buffered_read_some_handler<
|
||||
MutableBufferSequence, ReadHandler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Stream>
|
||||
template <typename MutableBufferSequence, typename ReadHandler>
|
||||
ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
buffered_read_stream<Stream>::async_read_some(
|
||||
const MutableBufferSequence& buffers,
|
||||
ASIO_MOVE_ARG(ReadHandler) handler)
|
||||
{
|
||||
return async_initiate<ReadHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_buffered_read_some(),
|
||||
handler, &storage_, &next_layer_, buffers);
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
template <typename MutableBufferSequence>
|
||||
std::size_t buffered_read_stream<Stream>::peek(
|
||||
const MutableBufferSequence& buffers)
|
||||
{
|
||||
if (storage_.empty())
|
||||
this->fill();
|
||||
return this->peek_copy(buffers);
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
template <typename MutableBufferSequence>
|
||||
std::size_t buffered_read_stream<Stream>::peek(
|
||||
const MutableBufferSequence& buffers, asio::error_code& ec)
|
||||
{
|
||||
ec = asio::error_code();
|
||||
if (storage_.empty() && !this->fill(ec))
|
||||
return 0;
|
||||
return this->peek_copy(buffers);
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_BUFFERED_READ_STREAM_HPP
|
||||
@@ -0,0 +1,430 @@
|
||||
//
|
||||
// impl/buffered_write_stream.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_BUFFERED_WRITE_STREAM_HPP
|
||||
#define ASIO_IMPL_BUFFERED_WRITE_STREAM_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/detail/handler_alloc_helpers.hpp"
|
||||
#include "asio/detail/handler_cont_helpers.hpp"
|
||||
#include "asio/detail/handler_invoke_helpers.hpp"
|
||||
#include "asio/detail/handler_type_requirements.hpp"
|
||||
#include "asio/detail/non_const_lvalue.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
template <typename Stream>
|
||||
std::size_t buffered_write_stream<Stream>::flush()
|
||||
{
|
||||
std::size_t bytes_written = write(next_layer_,
|
||||
buffer(storage_.data(), storage_.size()));
|
||||
storage_.consume(bytes_written);
|
||||
return bytes_written;
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
std::size_t buffered_write_stream<Stream>::flush(asio::error_code& ec)
|
||||
{
|
||||
std::size_t bytes_written = write(next_layer_,
|
||||
buffer(storage_.data(), storage_.size()),
|
||||
transfer_all(), ec);
|
||||
storage_.consume(bytes_written);
|
||||
return bytes_written;
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename WriteHandler>
|
||||
class buffered_flush_handler
|
||||
{
|
||||
public:
|
||||
buffered_flush_handler(detail::buffered_stream_storage& storage,
|
||||
WriteHandler& handler)
|
||||
: storage_(storage),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
buffered_flush_handler(const buffered_flush_handler& other)
|
||||
: storage_(other.storage_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
buffered_flush_handler(buffered_flush_handler&& other)
|
||||
: storage_(other.storage_),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec,
|
||||
const std::size_t bytes_written)
|
||||
{
|
||||
storage_.consume(bytes_written);
|
||||
handler_(ec, bytes_written);
|
||||
}
|
||||
|
||||
//private:
|
||||
detail::buffered_stream_storage& storage_;
|
||||
WriteHandler handler_;
|
||||
};
|
||||
|
||||
template <typename WriteHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
buffered_flush_handler<WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename WriteHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
buffered_flush_handler<WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename WriteHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
buffered_flush_handler<WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename WriteHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
buffered_flush_handler<WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename WriteHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
buffered_flush_handler<WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_buffered_flush
|
||||
{
|
||||
template <typename WriteHandler, typename Stream>
|
||||
void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
buffered_stream_storage* storage, Stream* next_layer) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a WriteHandler.
|
||||
ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
||||
|
||||
non_const_lvalue<WriteHandler> handler2(handler);
|
||||
async_write(*next_layer, buffer(storage->data(), storage->size()),
|
||||
buffered_flush_handler<typename decay<WriteHandler>::type>(
|
||||
*storage, handler2.value));
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename WriteHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::buffered_flush_handler<WriteHandler>, Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<WriteHandler, Allocator>::type type;
|
||||
|
||||
static type get(const detail::buffered_flush_handler<WriteHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<WriteHandler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename WriteHandler, typename Executor>
|
||||
struct associated_executor<
|
||||
detail::buffered_flush_handler<WriteHandler>, Executor>
|
||||
{
|
||||
typedef typename associated_executor<WriteHandler, Executor>::type type;
|
||||
|
||||
static type get(const detail::buffered_flush_handler<WriteHandler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<WriteHandler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Stream>
|
||||
template <typename WriteHandler>
|
||||
ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
buffered_write_stream<Stream>::async_flush(
|
||||
ASIO_MOVE_ARG(WriteHandler) handler)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_buffered_flush(),
|
||||
handler, &storage_, &next_layer_);
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
template <typename ConstBufferSequence>
|
||||
std::size_t buffered_write_stream<Stream>::write_some(
|
||||
const ConstBufferSequence& buffers)
|
||||
{
|
||||
using asio::buffer_size;
|
||||
if (buffer_size(buffers) == 0)
|
||||
return 0;
|
||||
|
||||
if (storage_.size() == storage_.capacity())
|
||||
this->flush();
|
||||
|
||||
return this->copy(buffers);
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
template <typename ConstBufferSequence>
|
||||
std::size_t buffered_write_stream<Stream>::write_some(
|
||||
const ConstBufferSequence& buffers, asio::error_code& ec)
|
||||
{
|
||||
ec = asio::error_code();
|
||||
|
||||
using asio::buffer_size;
|
||||
if (buffer_size(buffers) == 0)
|
||||
return 0;
|
||||
|
||||
if (storage_.size() == storage_.capacity() && !flush(ec))
|
||||
return 0;
|
||||
|
||||
return this->copy(buffers);
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename ConstBufferSequence, typename WriteHandler>
|
||||
class buffered_write_some_handler
|
||||
{
|
||||
public:
|
||||
buffered_write_some_handler(detail::buffered_stream_storage& storage,
|
||||
const ConstBufferSequence& buffers, WriteHandler& handler)
|
||||
: storage_(storage),
|
||||
buffers_(buffers),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
buffered_write_some_handler(const buffered_write_some_handler& other)
|
||||
: storage_(other.storage_),
|
||||
buffers_(other.buffers_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
buffered_write_some_handler(buffered_write_some_handler&& other)
|
||||
: storage_(other.storage_),
|
||||
buffers_(other.buffers_),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec, std::size_t)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
const std::size_t length = 0;
|
||||
handler_(ec, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
using asio::buffer_size;
|
||||
std::size_t orig_size = storage_.size();
|
||||
std::size_t space_avail = storage_.capacity() - orig_size;
|
||||
std::size_t bytes_avail = buffer_size(buffers_);
|
||||
std::size_t length = bytes_avail < space_avail
|
||||
? bytes_avail : space_avail;
|
||||
storage_.resize(orig_size + length);
|
||||
const std::size_t bytes_copied = asio::buffer_copy(
|
||||
storage_.data() + orig_size, buffers_, length);
|
||||
handler_(ec, bytes_copied);
|
||||
}
|
||||
}
|
||||
|
||||
//private:
|
||||
detail::buffered_stream_storage& storage_;
|
||||
ConstBufferSequence buffers_;
|
||||
WriteHandler handler_;
|
||||
};
|
||||
|
||||
template <typename ConstBufferSequence, typename WriteHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
buffered_write_some_handler<
|
||||
ConstBufferSequence, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename ConstBufferSequence, typename WriteHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
buffered_write_some_handler<
|
||||
ConstBufferSequence, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename ConstBufferSequence, typename WriteHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
buffered_write_some_handler<
|
||||
ConstBufferSequence, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename ConstBufferSequence,
|
||||
typename WriteHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
buffered_write_some_handler<
|
||||
ConstBufferSequence, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename ConstBufferSequence,
|
||||
typename WriteHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
buffered_write_some_handler<
|
||||
ConstBufferSequence, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_buffered_write_some
|
||||
{
|
||||
template <typename WriteHandler, typename Stream,
|
||||
typename ConstBufferSequence>
|
||||
void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
buffered_stream_storage* storage, Stream* next_layer,
|
||||
const ConstBufferSequence& buffers) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a WriteHandler.
|
||||
ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
||||
|
||||
using asio::buffer_size;
|
||||
non_const_lvalue<WriteHandler> handler2(handler);
|
||||
if (buffer_size(buffers) == 0 || storage->size() < storage->capacity())
|
||||
{
|
||||
next_layer->async_write_some(ASIO_CONST_BUFFER(0, 0),
|
||||
buffered_write_some_handler<ConstBufferSequence,
|
||||
typename decay<WriteHandler>::type>(
|
||||
*storage, buffers, handler2.value));
|
||||
}
|
||||
else
|
||||
{
|
||||
initiate_async_buffered_flush()(
|
||||
buffered_write_some_handler<ConstBufferSequence,
|
||||
typename decay<WriteHandler>::type>(
|
||||
*storage, buffers, handler2.value),
|
||||
storage, next_layer);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename ConstBufferSequence,
|
||||
typename WriteHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::buffered_write_some_handler<ConstBufferSequence, WriteHandler>,
|
||||
Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<WriteHandler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::buffered_write_some_handler<
|
||||
ConstBufferSequence, WriteHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<WriteHandler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ConstBufferSequence,
|
||||
typename WriteHandler, typename Executor>
|
||||
struct associated_executor<
|
||||
detail::buffered_write_some_handler<ConstBufferSequence, WriteHandler>,
|
||||
Executor>
|
||||
{
|
||||
typedef typename associated_executor<WriteHandler, Executor>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::buffered_write_some_handler<
|
||||
ConstBufferSequence, WriteHandler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<WriteHandler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Stream>
|
||||
template <typename ConstBufferSequence, typename WriteHandler>
|
||||
ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
buffered_write_stream<Stream>::async_write_some(
|
||||
const ConstBufferSequence& buffers,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_buffered_write_some(),
|
||||
handler, &storage_, &next_layer_, buffers);
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
template <typename ConstBufferSequence>
|
||||
std::size_t buffered_write_stream<Stream>::copy(
|
||||
const ConstBufferSequence& buffers)
|
||||
{
|
||||
using asio::buffer_size;
|
||||
std::size_t orig_size = storage_.size();
|
||||
std::size_t space_avail = storage_.capacity() - orig_size;
|
||||
std::size_t bytes_avail = buffer_size(buffers);
|
||||
std::size_t length = bytes_avail < space_avail ? bytes_avail : space_avail;
|
||||
storage_.resize(orig_size + length);
|
||||
return asio::buffer_copy(
|
||||
storage_.data() + orig_size, buffers, length);
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_BUFFERED_WRITE_STREAM_HPP
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
//
|
||||
// impl/co_spawn.hpp
|
||||
// ~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_CO_SPAWN_HPP
|
||||
#define ASIO_IMPL_CO_SPAWN_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/awaitable.hpp"
|
||||
#include "asio/dispatch.hpp"
|
||||
#include "asio/post.hpp"
|
||||
#include "asio/use_awaitable.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
template <typename T, typename Executor, typename F, typename Handler>
|
||||
awaitable<void, Executor> co_spawn_entry_point(
|
||||
awaitable<T, Executor>*, Executor ex, F f, Handler handler)
|
||||
{
|
||||
auto spawn_work = make_work_guard(ex);
|
||||
auto handler_work = make_work_guard(handler, ex);
|
||||
|
||||
(void) co_await (post)(spawn_work.get_executor(),
|
||||
use_awaitable_t<Executor>{});
|
||||
|
||||
bool done = false;
|
||||
try
|
||||
{
|
||||
T t = co_await f();
|
||||
|
||||
done = true;
|
||||
|
||||
(dispatch)(handler_work.get_executor(),
|
||||
[handler = std::move(handler), t = std::move(t)]() mutable
|
||||
{
|
||||
handler(std::exception_ptr(), std::move(t));
|
||||
});
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
if (done)
|
||||
throw;
|
||||
|
||||
(dispatch)(handler_work.get_executor(),
|
||||
[handler = std::move(handler), e = std::current_exception()]() mutable
|
||||
{
|
||||
handler(e, T());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Executor, typename F, typename Handler>
|
||||
awaitable<void, Executor> co_spawn_entry_point(
|
||||
awaitable<void, Executor>*, Executor ex, F f, Handler handler)
|
||||
{
|
||||
auto spawn_work = make_work_guard(ex);
|
||||
auto handler_work = make_work_guard(handler, ex);
|
||||
|
||||
(void) co_await (post)(spawn_work.get_executor(),
|
||||
use_awaitable_t<Executor>{});
|
||||
|
||||
std::exception_ptr e = nullptr;
|
||||
try
|
||||
{
|
||||
co_await f();
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
e = std::current_exception();
|
||||
}
|
||||
|
||||
(dispatch)(handler_work.get_executor(),
|
||||
[handler = std::move(handler), e]() mutable
|
||||
{
|
||||
handler(e);
|
||||
});
|
||||
}
|
||||
|
||||
struct initiate_co_spawn
|
||||
{
|
||||
template <typename Handler, typename Executor, typename F>
|
||||
void operator()(Handler&& handler, const Executor& ex, F&& f) const
|
||||
{
|
||||
typedef typename result_of<F()>::type awaitable_type;
|
||||
typedef typename awaitable_type::executor_type executor_type;
|
||||
|
||||
executor_type ex2(ex);
|
||||
auto a = (co_spawn_entry_point)(static_cast<awaitable_type*>(nullptr),
|
||||
ex2, std::forward<F>(f), std::forward<Handler>(handler));
|
||||
awaitable_handler<executor_type, void>(std::move(a), ex2).launch();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename Executor, typename F, typename CompletionToken>
|
||||
inline ASIO_INITFN_RESULT_TYPE(CompletionToken,
|
||||
typename detail::awaitable_signature<typename result_of<F()>::type>::type)
|
||||
co_spawn(const Executor& ex, F&& f, CompletionToken&& token,
|
||||
typename enable_if<
|
||||
is_executor<Executor>::value
|
||||
>::type*)
|
||||
{
|
||||
return async_initiate<CompletionToken,
|
||||
typename detail::awaitable_signature<typename result_of<F()>::type>>(
|
||||
detail::initiate_co_spawn(), token, ex, std::forward<F>(f));
|
||||
}
|
||||
|
||||
template <typename ExecutionContext, typename F, typename CompletionToken>
|
||||
inline ASIO_INITFN_RESULT_TYPE(CompletionToken,
|
||||
typename detail::awaitable_signature<typename result_of<F()>::type>::type)
|
||||
co_spawn(ExecutionContext& ctx, F&& f, CompletionToken&& token,
|
||||
typename enable_if<
|
||||
is_convertible<ExecutionContext&, execution_context&>::value
|
||||
>::type*)
|
||||
{
|
||||
return (co_spawn)(ctx.get_executor(), std::forward<F>(f),
|
||||
std::forward<CompletionToken>(token));
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_CO_SPAWN_HPP
|
||||
+419
@@ -0,0 +1,419 @@
|
||||
//
|
||||
// impl/compose.hpp
|
||||
// ~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_COMPOSE_HPP
|
||||
#define ASIO_IMPL_COMPOSE_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/detail/handler_alloc_helpers.hpp"
|
||||
#include "asio/detail/handler_cont_helpers.hpp"
|
||||
#include "asio/detail/handler_invoke_helpers.hpp"
|
||||
#include "asio/detail/type_traits.hpp"
|
||||
#include "asio/detail/variadic_templates.hpp"
|
||||
#include "asio/executor_work_guard.hpp"
|
||||
#include "asio/is_executor.hpp"
|
||||
#include "asio/system_executor.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename>
|
||||
struct composed_work;
|
||||
|
||||
template <>
|
||||
struct composed_work<void()>
|
||||
{
|
||||
composed_work() ASIO_NOEXCEPT
|
||||
: head_(system_executor())
|
||||
{
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
head_.reset();
|
||||
}
|
||||
|
||||
typedef system_executor head_type;
|
||||
executor_work_guard<system_executor> head_;
|
||||
};
|
||||
|
||||
inline composed_work<void()> make_composed_work()
|
||||
{
|
||||
return composed_work<void()>();
|
||||
}
|
||||
|
||||
template <typename Head>
|
||||
struct composed_work<void(Head)>
|
||||
{
|
||||
explicit composed_work(const Head& ex) ASIO_NOEXCEPT
|
||||
: head_(ex)
|
||||
{
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
head_.reset();
|
||||
}
|
||||
|
||||
typedef Head head_type;
|
||||
executor_work_guard<Head> head_;
|
||||
};
|
||||
|
||||
template <typename Head>
|
||||
inline composed_work<void(Head)> make_composed_work(const Head& head)
|
||||
{
|
||||
return composed_work<void(Head)>(head);
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Head, typename... Tail>
|
||||
struct composed_work<void(Head, Tail...)>
|
||||
{
|
||||
explicit composed_work(const Head& head,
|
||||
const Tail&... tail) ASIO_NOEXCEPT
|
||||
: head_(head),
|
||||
tail_(tail...)
|
||||
{
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
head_.reset();
|
||||
tail_.reset();
|
||||
}
|
||||
|
||||
typedef Head head_type;
|
||||
executor_work_guard<Head> head_;
|
||||
composed_work<void(Tail...)> tail_;
|
||||
};
|
||||
|
||||
template <typename Head, typename... Tail>
|
||||
inline composed_work<void(Head, Tail...)>
|
||||
make_composed_work(const Head& head, const Tail&... tail)
|
||||
{
|
||||
return composed_work<void(Head, Tail...)>(head, tail...);
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
#define ASIO_PRIVATE_COMPOSED_WORK_DEF(n) \
|
||||
template <typename Head, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
struct composed_work<void(Head, ASIO_VARIADIC_TARGS(n))> \
|
||||
{ \
|
||||
explicit composed_work(const Head& head, \
|
||||
ASIO_VARIADIC_CONSTREF_PARAMS(n)) ASIO_NOEXCEPT \
|
||||
: head_(head), \
|
||||
tail_(ASIO_VARIADIC_BYVAL_ARGS(n)) \
|
||||
{ \
|
||||
} \
|
||||
\
|
||||
void reset() \
|
||||
{ \
|
||||
head_.reset(); \
|
||||
tail_.reset(); \
|
||||
} \
|
||||
\
|
||||
typedef Head head_type; \
|
||||
executor_work_guard<Head> head_; \
|
||||
composed_work<void(ASIO_VARIADIC_TARGS(n))> tail_; \
|
||||
}; \
|
||||
\
|
||||
template <typename Head, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
inline composed_work<void(Head, ASIO_VARIADIC_TARGS(n))> \
|
||||
make_composed_work(const Head& head, ASIO_VARIADIC_CONSTREF_PARAMS(n)) \
|
||||
{ \
|
||||
return composed_work< \
|
||||
void(Head, ASIO_VARIADIC_TARGS(n))>( \
|
||||
head, ASIO_VARIADIC_BYVAL_ARGS(n)); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_COMPOSED_WORK_DEF)
|
||||
#undef ASIO_PRIVATE_COMPOSED_WORK_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
template <typename Impl, typename Work, typename Handler, typename Signature>
|
||||
class composed_op;
|
||||
|
||||
template <typename Impl, typename Work, typename Handler,
|
||||
typename R, typename... Args>
|
||||
class composed_op<Impl, Work, Handler, R(Args...)>
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
template <typename Impl, typename Work, typename Handler, typename Signature>
|
||||
class composed_op
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
{
|
||||
public:
|
||||
composed_op(ASIO_MOVE_ARG(Impl) impl,
|
||||
ASIO_MOVE_ARG(Work) work,
|
||||
ASIO_MOVE_ARG(Handler) handler)
|
||||
: impl_(ASIO_MOVE_CAST(Impl)(impl)),
|
||||
work_(ASIO_MOVE_CAST(Work)(work)),
|
||||
handler_(ASIO_MOVE_CAST(Handler)(handler)),
|
||||
invocations_(0)
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
composed_op(composed_op&& other)
|
||||
: impl_(ASIO_MOVE_CAST(Impl)(other.impl_)),
|
||||
work_(ASIO_MOVE_CAST(Work)(other.work_)),
|
||||
handler_(ASIO_MOVE_CAST(Handler)(other.handler_)),
|
||||
invocations_(other.invocations_)
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
typedef typename associated_executor<Handler,
|
||||
typename Work::head_type>::type executor_type;
|
||||
|
||||
executor_type get_executor() const ASIO_NOEXCEPT
|
||||
{
|
||||
return (get_associated_executor)(handler_, work_.head_.get_executor());
|
||||
}
|
||||
|
||||
typedef typename associated_allocator<Handler,
|
||||
std::allocator<void> >::type allocator_type;
|
||||
|
||||
allocator_type get_allocator() const ASIO_NOEXCEPT
|
||||
{
|
||||
return (get_associated_allocator)(handler_, std::allocator<void>());
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template<typename... T>
|
||||
void operator()(ASIO_MOVE_ARG(T)... t)
|
||||
{
|
||||
if (invocations_ < ~unsigned(0))
|
||||
++invocations_;
|
||||
impl_(*this, ASIO_MOVE_CAST(T)(t)...);
|
||||
}
|
||||
|
||||
void complete(Args... args)
|
||||
{
|
||||
this->work_.reset();
|
||||
this->handler_(ASIO_MOVE_CAST(Args)(args)...);
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
void operator()()
|
||||
{
|
||||
if (invocations_ < ~unsigned(0))
|
||||
++invocations_;
|
||||
impl_(*this);
|
||||
}
|
||||
|
||||
void complete()
|
||||
{
|
||||
this->work_.reset();
|
||||
this->handler_();
|
||||
}
|
||||
|
||||
#define ASIO_PRIVATE_COMPOSED_OP_DEF(n) \
|
||||
template<ASIO_VARIADIC_TPARAMS(n)> \
|
||||
void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
if (invocations_ < ~unsigned(0)) \
|
||||
++invocations_; \
|
||||
impl_(*this, ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
} \
|
||||
\
|
||||
template<ASIO_VARIADIC_TPARAMS(n)> \
|
||||
void complete(ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
this->work_.reset(); \
|
||||
this->handler_(ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_COMPOSED_OP_DEF)
|
||||
#undef ASIO_PRIVATE_COMPOSED_OP_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
//private:
|
||||
Impl impl_;
|
||||
Work work_;
|
||||
Handler handler_;
|
||||
unsigned invocations_;
|
||||
};
|
||||
|
||||
template <typename Impl, typename Work, typename Handler, typename Signature>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
composed_op<Impl, Work, Handler, Signature>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Impl, typename Work, typename Handler, typename Signature>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
composed_op<Impl, Work, Handler, Signature>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Impl, typename Work, typename Handler, typename Signature>
|
||||
inline bool asio_handler_is_continuation(
|
||||
composed_op<Impl, Work, Handler, Signature>* this_handler)
|
||||
{
|
||||
return this_handler->invocations_ > 1 ? true
|
||||
: asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Impl,
|
||||
typename Work, typename Handler, typename Signature>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
composed_op<Impl, Work, Handler, Signature>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Impl,
|
||||
typename Work, typename Handler, typename Signature>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
composed_op<Impl, Work, Handler, Signature>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Signature>
|
||||
struct initiate_composed_op
|
||||
{
|
||||
template <typename Handler, typename Impl, typename Work>
|
||||
void operator()(ASIO_MOVE_ARG(Handler) handler,
|
||||
ASIO_MOVE_ARG(Impl) impl,
|
||||
ASIO_MOVE_ARG(Work) work) const
|
||||
{
|
||||
composed_op<typename decay<Impl>::type, typename decay<Work>::type,
|
||||
typename decay<Handler>::type, Signature>(
|
||||
ASIO_MOVE_CAST(Impl)(impl), ASIO_MOVE_CAST(Work)(work),
|
||||
ASIO_MOVE_CAST(Handler)(handler))();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename IoObject>
|
||||
inline typename IoObject::executor_type
|
||||
get_composed_io_executor(IoObject& io_object)
|
||||
{
|
||||
return io_object.get_executor();
|
||||
}
|
||||
|
||||
template <typename Executor>
|
||||
inline const Executor& get_composed_io_executor(const Executor& ex,
|
||||
typename enable_if<is_executor<Executor>::value>::type* = 0)
|
||||
{
|
||||
return ex;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename CompletionToken, typename Signature,
|
||||
typename Implementation, typename... IoObjectsOrExecutors>
|
||||
ASIO_INITFN_RESULT_TYPE(CompletionToken, Signature)
|
||||
async_compose(ASIO_MOVE_ARG(Implementation) implementation,
|
||||
ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token,
|
||||
ASIO_MOVE_ARG(IoObjectsOrExecutors)... io_objects_or_executors)
|
||||
{
|
||||
return async_initiate<CompletionToken, Signature>(
|
||||
detail::initiate_composed_op<Signature>(), token,
|
||||
ASIO_MOVE_CAST(Implementation)(implementation),
|
||||
detail::make_composed_work(
|
||||
detail::get_composed_io_executor(
|
||||
ASIO_MOVE_CAST(IoObjectsOrExecutors)(
|
||||
io_objects_or_executors))...));
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename CompletionToken, typename Signature, typename Implementation>
|
||||
ASIO_INITFN_RESULT_TYPE(CompletionToken, Signature)
|
||||
async_compose(ASIO_MOVE_ARG(Implementation) implementation,
|
||||
ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token)
|
||||
{
|
||||
return async_initiate<CompletionToken, Signature>(
|
||||
detail::initiate_composed_op<Signature>(), token,
|
||||
ASIO_MOVE_CAST(Implementation)(implementation),
|
||||
detail::make_composed_work());
|
||||
}
|
||||
|
||||
# define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n) \
|
||||
ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_##n
|
||||
|
||||
# define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1 \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1))
|
||||
# define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2 \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2))
|
||||
# define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3 \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3))
|
||||
# define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4 \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T4)(x4))
|
||||
# define ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5 \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T1)(x1)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T2)(x2)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T3)(x3)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T4)(x4)), \
|
||||
detail::get_composed_io_executor(ASIO_MOVE_CAST(T5)(x5))
|
||||
|
||||
#define ASIO_PRIVATE_ASYNC_COMPOSE_DEF(n) \
|
||||
template <typename CompletionToken, typename Signature, \
|
||||
typename Implementation, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
ASIO_INITFN_RESULT_TYPE(CompletionToken, Signature) \
|
||||
async_compose(ASIO_MOVE_ARG(Implementation) implementation, \
|
||||
ASIO_NONDEDUCED_MOVE_ARG(CompletionToken) token, \
|
||||
ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
return async_initiate<CompletionToken, Signature>( \
|
||||
detail::initiate_composed_op<Signature>(), token, \
|
||||
ASIO_MOVE_CAST(Implementation)(implementation), \
|
||||
detail::make_composed_work( \
|
||||
ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR(n))); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ASYNC_COMPOSE_DEF)
|
||||
#undef ASIO_PRIVATE_ASYNC_COMPOSE_DEF
|
||||
|
||||
#undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR
|
||||
#undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_1
|
||||
#undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_2
|
||||
#undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_3
|
||||
#undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_4
|
||||
#undef ASIO_PRIVATE_GET_COMPOSED_IO_EXECUTOR_5
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_COMPOSE_HPP
|
||||
+828
@@ -0,0 +1,828 @@
|
||||
//
|
||||
// impl/connect.hpp
|
||||
// ~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_CONNECT_HPP
|
||||
#define ASIO_IMPL_CONNECT_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include <algorithm>
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/detail/bind_handler.hpp"
|
||||
#include "asio/detail/handler_alloc_helpers.hpp"
|
||||
#include "asio/detail/handler_cont_helpers.hpp"
|
||||
#include "asio/detail/handler_invoke_helpers.hpp"
|
||||
#include "asio/detail/handler_type_requirements.hpp"
|
||||
#include "asio/detail/non_const_lvalue.hpp"
|
||||
#include "asio/detail/throw_error.hpp"
|
||||
#include "asio/error.hpp"
|
||||
#include "asio/post.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
struct default_connect_condition
|
||||
{
|
||||
template <typename Endpoint>
|
||||
bool operator()(const asio::error_code&, const Endpoint&)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Protocol, typename Iterator>
|
||||
inline typename Protocol::endpoint deref_connect_result(
|
||||
Iterator iter, asio::error_code& ec)
|
||||
{
|
||||
return ec ? typename Protocol::endpoint() : *iter;
|
||||
}
|
||||
|
||||
template <typename T, typename Iterator>
|
||||
struct legacy_connect_condition_helper : T
|
||||
{
|
||||
typedef char (*fallback_func_type)(...);
|
||||
operator fallback_func_type() const;
|
||||
};
|
||||
|
||||
template <typename R, typename Arg1, typename Arg2, typename Iterator>
|
||||
struct legacy_connect_condition_helper<R (*)(Arg1, Arg2), Iterator>
|
||||
{
|
||||
R operator()(Arg1, Arg2) const;
|
||||
char operator()(...) const;
|
||||
};
|
||||
|
||||
template <typename T, typename Iterator>
|
||||
struct is_legacy_connect_condition
|
||||
{
|
||||
static char asio_connect_condition_check(char);
|
||||
static char (&asio_connect_condition_check(Iterator))[2];
|
||||
|
||||
static const bool value =
|
||||
sizeof(asio_connect_condition_check(
|
||||
(*static_cast<legacy_connect_condition_helper<T, Iterator>*>(0))(
|
||||
*static_cast<const asio::error_code*>(0),
|
||||
*static_cast<const Iterator*>(0)))) != 1;
|
||||
};
|
||||
|
||||
template <typename ConnectCondition, typename Iterator>
|
||||
inline Iterator call_connect_condition(ConnectCondition& connect_condition,
|
||||
const asio::error_code& ec, Iterator next, Iterator end,
|
||||
typename enable_if<is_legacy_connect_condition<
|
||||
ConnectCondition, Iterator>::value>::type* = 0)
|
||||
{
|
||||
if (next != end)
|
||||
return connect_condition(ec, next);
|
||||
return end;
|
||||
}
|
||||
|
||||
template <typename ConnectCondition, typename Iterator>
|
||||
inline Iterator call_connect_condition(ConnectCondition& connect_condition,
|
||||
const asio::error_code& ec, Iterator next, Iterator end,
|
||||
typename enable_if<!is_legacy_connect_condition<
|
||||
ConnectCondition, Iterator>::value>::type* = 0)
|
||||
{
|
||||
for (;next != end; ++next)
|
||||
if (connect_condition(ec, *next))
|
||||
return next;
|
||||
return end;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor, typename EndpointSequence>
|
||||
typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
|
||||
const EndpointSequence& endpoints,
|
||||
typename enable_if<is_endpoint_sequence<
|
||||
EndpointSequence>::value>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
typename Protocol::endpoint result = connect(s, endpoints, ec);
|
||||
asio::detail::throw_error(ec, "connect");
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor, typename EndpointSequence>
|
||||
typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
|
||||
const EndpointSequence& endpoints, asio::error_code& ec,
|
||||
typename enable_if<is_endpoint_sequence<
|
||||
EndpointSequence>::value>::type*)
|
||||
{
|
||||
return detail::deref_connect_result<Protocol>(
|
||||
connect(s, endpoints.begin(), endpoints.end(),
|
||||
detail::default_connect_condition(), ec), ec);
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
template <typename Protocol, typename Executor, typename Iterator>
|
||||
Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
|
||||
typename enable_if<!is_endpoint_sequence<Iterator>::value>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
Iterator result = connect(s, begin, ec);
|
||||
asio::detail::throw_error(ec, "connect");
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator>
|
||||
inline Iterator connect(basic_socket<Protocol, Executor>& s,
|
||||
Iterator begin, asio::error_code& ec,
|
||||
typename enable_if<!is_endpoint_sequence<Iterator>::value>::type*)
|
||||
{
|
||||
return connect(s, begin, Iterator(), detail::default_connect_condition(), ec);
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator>
|
||||
Iterator connect(basic_socket<Protocol, Executor>& s,
|
||||
Iterator begin, Iterator end)
|
||||
{
|
||||
asio::error_code ec;
|
||||
Iterator result = connect(s, begin, end, ec);
|
||||
asio::detail::throw_error(ec, "connect");
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator>
|
||||
inline Iterator connect(basic_socket<Protocol, Executor>& s,
|
||||
Iterator begin, Iterator end, asio::error_code& ec)
|
||||
{
|
||||
return connect(s, begin, end, detail::default_connect_condition(), ec);
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor,
|
||||
typename EndpointSequence, typename ConnectCondition>
|
||||
typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
|
||||
const EndpointSequence& endpoints, ConnectCondition connect_condition,
|
||||
typename enable_if<is_endpoint_sequence<
|
||||
EndpointSequence>::value>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
typename Protocol::endpoint result = connect(
|
||||
s, endpoints, connect_condition, ec);
|
||||
asio::detail::throw_error(ec, "connect");
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor,
|
||||
typename EndpointSequence, typename ConnectCondition>
|
||||
typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
|
||||
const EndpointSequence& endpoints, ConnectCondition connect_condition,
|
||||
asio::error_code& ec,
|
||||
typename enable_if<is_endpoint_sequence<
|
||||
EndpointSequence>::value>::type*)
|
||||
{
|
||||
return detail::deref_connect_result<Protocol>(
|
||||
connect(s, endpoints.begin(), endpoints.end(),
|
||||
connect_condition, ec), ec);
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
template <typename Protocol, typename Executor,
|
||||
typename Iterator, typename ConnectCondition>
|
||||
Iterator connect(basic_socket<Protocol, Executor>& s,
|
||||
Iterator begin, ConnectCondition connect_condition,
|
||||
typename enable_if<!is_endpoint_sequence<Iterator>::value>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
Iterator result = connect(s, begin, connect_condition, ec);
|
||||
asio::detail::throw_error(ec, "connect");
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor,
|
||||
typename Iterator, typename ConnectCondition>
|
||||
inline Iterator connect(basic_socket<Protocol, Executor>& s,
|
||||
Iterator begin, ConnectCondition connect_condition,
|
||||
asio::error_code& ec,
|
||||
typename enable_if<!is_endpoint_sequence<Iterator>::value>::type*)
|
||||
{
|
||||
return connect(s, begin, Iterator(), connect_condition, ec);
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
template <typename Protocol, typename Executor,
|
||||
typename Iterator, typename ConnectCondition>
|
||||
Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
|
||||
Iterator end, ConnectCondition connect_condition)
|
||||
{
|
||||
asio::error_code ec;
|
||||
Iterator result = connect(s, begin, end, connect_condition, ec);
|
||||
asio::detail::throw_error(ec, "connect");
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor,
|
||||
typename Iterator, typename ConnectCondition>
|
||||
Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
|
||||
Iterator end, ConnectCondition connect_condition,
|
||||
asio::error_code& ec)
|
||||
{
|
||||
ec = asio::error_code();
|
||||
|
||||
for (Iterator iter = begin; iter != end; ++iter)
|
||||
{
|
||||
iter = (detail::call_connect_condition(connect_condition, ec, iter, end));
|
||||
if (iter != end)
|
||||
{
|
||||
s.close(ec);
|
||||
s.connect(*iter, ec);
|
||||
if (!ec)
|
||||
return iter;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
if (!ec)
|
||||
ec = asio::error::not_found;
|
||||
|
||||
return end;
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// Enable the empty base class optimisation for the connect condition.
|
||||
template <typename ConnectCondition>
|
||||
class base_from_connect_condition
|
||||
{
|
||||
protected:
|
||||
explicit base_from_connect_condition(
|
||||
const ConnectCondition& connect_condition)
|
||||
: connect_condition_(connect_condition)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename Iterator>
|
||||
void check_condition(const asio::error_code& ec,
|
||||
Iterator& iter, Iterator& end)
|
||||
{
|
||||
iter = detail::call_connect_condition(connect_condition_, ec, iter, end);
|
||||
}
|
||||
|
||||
private:
|
||||
ConnectCondition connect_condition_;
|
||||
};
|
||||
|
||||
// The default_connect_condition implementation is essentially a no-op. This
|
||||
// template specialisation lets us eliminate all costs associated with it.
|
||||
template <>
|
||||
class base_from_connect_condition<default_connect_condition>
|
||||
{
|
||||
protected:
|
||||
explicit base_from_connect_condition(const default_connect_condition&)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename Iterator>
|
||||
void check_condition(const asio::error_code&, Iterator&, Iterator&)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Protocol, typename Executor, typename EndpointSequence,
|
||||
typename ConnectCondition, typename RangeConnectHandler>
|
||||
class range_connect_op : base_from_connect_condition<ConnectCondition>
|
||||
{
|
||||
public:
|
||||
range_connect_op(basic_socket<Protocol, Executor>& sock,
|
||||
const EndpointSequence& endpoints,
|
||||
const ConnectCondition& connect_condition,
|
||||
RangeConnectHandler& handler)
|
||||
: base_from_connect_condition<ConnectCondition>(connect_condition),
|
||||
socket_(sock),
|
||||
endpoints_(endpoints),
|
||||
index_(0),
|
||||
start_(0),
|
||||
handler_(ASIO_MOVE_CAST(RangeConnectHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
range_connect_op(const range_connect_op& other)
|
||||
: base_from_connect_condition<ConnectCondition>(other),
|
||||
socket_(other.socket_),
|
||||
endpoints_(other.endpoints_),
|
||||
index_(other.index_),
|
||||
start_(other.start_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
range_connect_op(range_connect_op&& other)
|
||||
: base_from_connect_condition<ConnectCondition>(other),
|
||||
socket_(other.socket_),
|
||||
endpoints_(other.endpoints_),
|
||||
index_(other.index_),
|
||||
start_(other.start_),
|
||||
handler_(ASIO_MOVE_CAST(RangeConnectHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(asio::error_code ec, int start = 0)
|
||||
{
|
||||
this->process(ec, start,
|
||||
const_cast<const EndpointSequence&>(endpoints_).begin(),
|
||||
const_cast<const EndpointSequence&>(endpoints_).end());
|
||||
}
|
||||
|
||||
//private:
|
||||
template <typename Iterator>
|
||||
void process(asio::error_code ec,
|
||||
int start, Iterator begin, Iterator end)
|
||||
{
|
||||
Iterator iter = begin;
|
||||
std::advance(iter, index_);
|
||||
|
||||
switch (start_ = start)
|
||||
{
|
||||
case 1:
|
||||
for (;;)
|
||||
{
|
||||
this->check_condition(ec, iter, end);
|
||||
index_ = std::distance(begin, iter);
|
||||
|
||||
if (iter != end)
|
||||
{
|
||||
socket_.close(ec);
|
||||
socket_.async_connect(*iter,
|
||||
ASIO_MOVE_CAST(range_connect_op)(*this));
|
||||
return;
|
||||
}
|
||||
|
||||
if (start)
|
||||
{
|
||||
ec = asio::error::not_found;
|
||||
asio::post(socket_.get_executor(),
|
||||
detail::bind_handler(
|
||||
ASIO_MOVE_CAST(range_connect_op)(*this), ec));
|
||||
return;
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
if (iter == end)
|
||||
break;
|
||||
|
||||
if (!socket_.is_open())
|
||||
{
|
||||
ec = asio::error::operation_aborted;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!ec)
|
||||
break;
|
||||
|
||||
++iter;
|
||||
++index_;
|
||||
}
|
||||
|
||||
handler_(static_cast<const asio::error_code&>(ec),
|
||||
static_cast<const typename Protocol::endpoint&>(
|
||||
ec || iter == end ? typename Protocol::endpoint() : *iter));
|
||||
}
|
||||
}
|
||||
|
||||
basic_socket<Protocol, Executor>& socket_;
|
||||
EndpointSequence endpoints_;
|
||||
std::size_t index_;
|
||||
int start_;
|
||||
RangeConnectHandler handler_;
|
||||
};
|
||||
|
||||
template <typename Protocol, typename Executor, typename EndpointSequence,
|
||||
typename ConnectCondition, typename RangeConnectHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
range_connect_op<Protocol, Executor, EndpointSequence,
|
||||
ConnectCondition, RangeConnectHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor, typename EndpointSequence,
|
||||
typename ConnectCondition, typename RangeConnectHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
range_connect_op<Protocol, Executor, EndpointSequence,
|
||||
ConnectCondition, RangeConnectHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor, typename EndpointSequence,
|
||||
typename ConnectCondition, typename RangeConnectHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
range_connect_op<Protocol, Executor, EndpointSequence,
|
||||
ConnectCondition, RangeConnectHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Executor, typename Protocol,
|
||||
typename EndpointSequence, typename ConnectCondition,
|
||||
typename RangeConnectHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
range_connect_op<Protocol, Executor, EndpointSequence,
|
||||
ConnectCondition, RangeConnectHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Executor, typename Protocol,
|
||||
typename EndpointSequence, typename ConnectCondition,
|
||||
typename RangeConnectHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
range_connect_op<Protocol, Executor, EndpointSequence,
|
||||
ConnectCondition, RangeConnectHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_range_connect
|
||||
{
|
||||
template <typename RangeConnectHandler, typename Protocol,
|
||||
typename Executor, typename EndpointSequence, typename ConnectCondition>
|
||||
void operator()(ASIO_MOVE_ARG(RangeConnectHandler) handler,
|
||||
basic_socket<Protocol, Executor>* s, const EndpointSequence& endpoints,
|
||||
const ConnectCondition& connect_condition) const
|
||||
{
|
||||
// If you get an error on the following line it means that your
|
||||
// handler does not meet the documented type requirements for an
|
||||
// RangeConnectHandler.
|
||||
ASIO_RANGE_CONNECT_HANDLER_CHECK(RangeConnectHandler,
|
||||
handler, typename Protocol::endpoint) type_check;
|
||||
|
||||
non_const_lvalue<RangeConnectHandler> handler2(handler);
|
||||
range_connect_op<Protocol, Executor, EndpointSequence, ConnectCondition,
|
||||
typename decay<RangeConnectHandler>::type>(*s, endpoints,
|
||||
connect_condition, handler2.value)(asio::error_code(), 1);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator,
|
||||
typename ConnectCondition, typename IteratorConnectHandler>
|
||||
class iterator_connect_op : base_from_connect_condition<ConnectCondition>
|
||||
{
|
||||
public:
|
||||
iterator_connect_op(basic_socket<Protocol, Executor>& sock,
|
||||
const Iterator& begin, const Iterator& end,
|
||||
const ConnectCondition& connect_condition,
|
||||
IteratorConnectHandler& handler)
|
||||
: base_from_connect_condition<ConnectCondition>(connect_condition),
|
||||
socket_(sock),
|
||||
iter_(begin),
|
||||
end_(end),
|
||||
start_(0),
|
||||
handler_(ASIO_MOVE_CAST(IteratorConnectHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
iterator_connect_op(const iterator_connect_op& other)
|
||||
: base_from_connect_condition<ConnectCondition>(other),
|
||||
socket_(other.socket_),
|
||||
iter_(other.iter_),
|
||||
end_(other.end_),
|
||||
start_(other.start_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
iterator_connect_op(iterator_connect_op&& other)
|
||||
: base_from_connect_condition<ConnectCondition>(other),
|
||||
socket_(other.socket_),
|
||||
iter_(other.iter_),
|
||||
end_(other.end_),
|
||||
start_(other.start_),
|
||||
handler_(ASIO_MOVE_CAST(IteratorConnectHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(asio::error_code ec, int start = 0)
|
||||
{
|
||||
switch (start_ = start)
|
||||
{
|
||||
case 1:
|
||||
for (;;)
|
||||
{
|
||||
this->check_condition(ec, iter_, end_);
|
||||
|
||||
if (iter_ != end_)
|
||||
{
|
||||
socket_.close(ec);
|
||||
socket_.async_connect(*iter_,
|
||||
ASIO_MOVE_CAST(iterator_connect_op)(*this));
|
||||
return;
|
||||
}
|
||||
|
||||
if (start)
|
||||
{
|
||||
ec = asio::error::not_found;
|
||||
asio::post(socket_.get_executor(),
|
||||
detail::bind_handler(
|
||||
ASIO_MOVE_CAST(iterator_connect_op)(*this), ec));
|
||||
return;
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
if (iter_ == end_)
|
||||
break;
|
||||
|
||||
if (!socket_.is_open())
|
||||
{
|
||||
ec = asio::error::operation_aborted;
|
||||
break;
|
||||
}
|
||||
|
||||
if (!ec)
|
||||
break;
|
||||
|
||||
++iter_;
|
||||
}
|
||||
|
||||
handler_(static_cast<const asio::error_code&>(ec),
|
||||
static_cast<const Iterator&>(iter_));
|
||||
}
|
||||
}
|
||||
|
||||
//private:
|
||||
basic_socket<Protocol, Executor>& socket_;
|
||||
Iterator iter_;
|
||||
Iterator end_;
|
||||
int start_;
|
||||
IteratorConnectHandler handler_;
|
||||
};
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator,
|
||||
typename ConnectCondition, typename IteratorConnectHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
iterator_connect_op<Protocol, Executor, Iterator,
|
||||
ConnectCondition, IteratorConnectHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator,
|
||||
typename ConnectCondition, typename IteratorConnectHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
iterator_connect_op<Protocol, Executor, Iterator,
|
||||
ConnectCondition, IteratorConnectHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator,
|
||||
typename ConnectCondition, typename IteratorConnectHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
iterator_connect_op<Protocol, Executor, Iterator,
|
||||
ConnectCondition, IteratorConnectHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Executor, typename Protocol,
|
||||
typename Iterator, typename ConnectCondition,
|
||||
typename IteratorConnectHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
iterator_connect_op<Protocol, Executor, Iterator,
|
||||
ConnectCondition, IteratorConnectHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Executor, typename Protocol,
|
||||
typename Iterator, typename ConnectCondition,
|
||||
typename IteratorConnectHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
iterator_connect_op<Protocol, Executor, Iterator,
|
||||
ConnectCondition, IteratorConnectHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_iterator_connect
|
||||
{
|
||||
template <typename IteratorConnectHandler, typename Protocol,
|
||||
typename Executor, typename Iterator, typename ConnectCondition>
|
||||
void operator()(ASIO_MOVE_ARG(IteratorConnectHandler) handler,
|
||||
basic_socket<Protocol, Executor>* s, Iterator begin,
|
||||
Iterator end, const ConnectCondition& connect_condition) const
|
||||
{
|
||||
// If you get an error on the following line it means that your
|
||||
// handler does not meet the documented type requirements for an
|
||||
// IteratorConnectHandler.
|
||||
ASIO_ITERATOR_CONNECT_HANDLER_CHECK(
|
||||
IteratorConnectHandler, handler, Iterator) type_check;
|
||||
|
||||
non_const_lvalue<IteratorConnectHandler> handler2(handler);
|
||||
iterator_connect_op<Protocol, Executor, Iterator, ConnectCondition,
|
||||
typename decay<IteratorConnectHandler>::type>(*s, begin, end,
|
||||
connect_condition, handler2.value)(asio::error_code(), 1);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Protocol, typename Executor, typename EndpointSequence,
|
||||
typename ConnectCondition, typename RangeConnectHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::range_connect_op<Protocol, Executor, EndpointSequence,
|
||||
ConnectCondition, RangeConnectHandler>, Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<
|
||||
RangeConnectHandler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::range_connect_op<Protocol, Executor, EndpointSequence,
|
||||
ConnectCondition, RangeConnectHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<RangeConnectHandler,
|
||||
Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Protocol, typename Executor, typename EndpointSequence,
|
||||
typename ConnectCondition, typename RangeConnectHandler, typename Executor1>
|
||||
struct associated_executor<
|
||||
detail::range_connect_op<Protocol, Executor, EndpointSequence,
|
||||
ConnectCondition, RangeConnectHandler>, Executor1>
|
||||
{
|
||||
typedef typename associated_executor<
|
||||
RangeConnectHandler, Executor1>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::range_connect_op<Protocol, Executor, EndpointSequence,
|
||||
ConnectCondition, RangeConnectHandler>& h,
|
||||
const Executor1& ex = Executor1()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<RangeConnectHandler,
|
||||
Executor1>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator,
|
||||
typename ConnectCondition, typename IteratorConnectHandler,
|
||||
typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::iterator_connect_op<Protocol, Executor,
|
||||
Iterator, ConnectCondition, IteratorConnectHandler>,
|
||||
Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<
|
||||
IteratorConnectHandler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::iterator_connect_op<Protocol, Executor,
|
||||
Iterator, ConnectCondition, IteratorConnectHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<IteratorConnectHandler,
|
||||
Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator,
|
||||
typename ConnectCondition, typename IteratorConnectHandler,
|
||||
typename Executor1>
|
||||
struct associated_executor<
|
||||
detail::iterator_connect_op<Protocol, Executor,
|
||||
Iterator, ConnectCondition, IteratorConnectHandler>,
|
||||
Executor1>
|
||||
{
|
||||
typedef typename associated_executor<
|
||||
IteratorConnectHandler, Executor1>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::iterator_connect_op<Protocol, Executor,
|
||||
Iterator, ConnectCondition, IteratorConnectHandler>& h,
|
||||
const Executor1& ex = Executor1()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<IteratorConnectHandler,
|
||||
Executor1>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Protocol, typename Executor,
|
||||
typename EndpointSequence, typename RangeConnectHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(RangeConnectHandler,
|
||||
void (asio::error_code, typename Protocol::endpoint))
|
||||
async_connect(basic_socket<Protocol, Executor>& s,
|
||||
const EndpointSequence& endpoints,
|
||||
ASIO_MOVE_ARG(RangeConnectHandler) handler,
|
||||
typename enable_if<is_endpoint_sequence<
|
||||
EndpointSequence>::value>::type*)
|
||||
{
|
||||
return async_initiate<RangeConnectHandler,
|
||||
void (asio::error_code, typename Protocol::endpoint)>(
|
||||
detail::initiate_async_range_connect(), handler,
|
||||
&s, endpoints, detail::default_connect_condition());
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
template <typename Protocol, typename Executor,
|
||||
typename Iterator, typename IteratorConnectHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(IteratorConnectHandler,
|
||||
void (asio::error_code, Iterator))
|
||||
async_connect(basic_socket<Protocol, Executor>& s, Iterator begin,
|
||||
ASIO_MOVE_ARG(IteratorConnectHandler) handler,
|
||||
typename enable_if<!is_endpoint_sequence<Iterator>::value>::type*)
|
||||
{
|
||||
return async_initiate<IteratorConnectHandler,
|
||||
void (asio::error_code, Iterator)>(
|
||||
detail::initiate_async_iterator_connect(), handler,
|
||||
&s, begin, Iterator(), detail::default_connect_condition());
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
template <typename Protocol, typename Executor,
|
||||
typename Iterator, typename IteratorConnectHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(IteratorConnectHandler,
|
||||
void (asio::error_code, Iterator))
|
||||
async_connect(basic_socket<Protocol, Executor>& s, Iterator begin, Iterator end,
|
||||
ASIO_MOVE_ARG(IteratorConnectHandler) handler)
|
||||
{
|
||||
return async_initiate<IteratorConnectHandler,
|
||||
void (asio::error_code, Iterator)>(
|
||||
detail::initiate_async_iterator_connect(), handler,
|
||||
&s, begin, end, detail::default_connect_condition());
|
||||
}
|
||||
|
||||
template <typename Protocol, typename Executor, typename EndpointSequence,
|
||||
typename ConnectCondition, typename RangeConnectHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(RangeConnectHandler,
|
||||
void (asio::error_code, typename Protocol::endpoint))
|
||||
async_connect(basic_socket<Protocol, Executor>& s,
|
||||
const EndpointSequence& endpoints, ConnectCondition connect_condition,
|
||||
ASIO_MOVE_ARG(RangeConnectHandler) handler,
|
||||
typename enable_if<is_endpoint_sequence<
|
||||
EndpointSequence>::value>::type*)
|
||||
{
|
||||
return async_initiate<RangeConnectHandler,
|
||||
void (asio::error_code, typename Protocol::endpoint)>(
|
||||
detail::initiate_async_range_connect(),
|
||||
handler, &s, endpoints, connect_condition);
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
template <typename Protocol, typename Executor, typename Iterator,
|
||||
typename ConnectCondition, typename IteratorConnectHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(IteratorConnectHandler,
|
||||
void (asio::error_code, Iterator))
|
||||
async_connect(basic_socket<Protocol, Executor>& s, Iterator begin,
|
||||
ConnectCondition connect_condition,
|
||||
ASIO_MOVE_ARG(IteratorConnectHandler) handler,
|
||||
typename enable_if<!is_endpoint_sequence<Iterator>::value>::type*)
|
||||
{
|
||||
return async_initiate<IteratorConnectHandler,
|
||||
void (asio::error_code, Iterator)>(
|
||||
detail::initiate_async_iterator_connect(),
|
||||
handler, &s, begin, Iterator(), connect_condition);
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
template <typename Protocol, typename Executor, typename Iterator,
|
||||
typename ConnectCondition, typename IteratorConnectHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(IteratorConnectHandler,
|
||||
void (asio::error_code, Iterator))
|
||||
async_connect(basic_socket<Protocol, Executor>& s, Iterator begin,
|
||||
Iterator end, ConnectCondition connect_condition,
|
||||
ASIO_MOVE_ARG(IteratorConnectHandler) handler)
|
||||
{
|
||||
return async_initiate<IteratorConnectHandler,
|
||||
void (asio::error_code, Iterator)>(
|
||||
detail::initiate_async_iterator_connect(),
|
||||
handler, &s, begin, end, connect_condition);
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_CONNECT_HPP
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
//
|
||||
// impl/defer.hpp
|
||||
// ~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_DEFER_HPP
|
||||
#define ASIO_IMPL_DEFER_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/detail/work_dispatcher.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
struct initiate_defer
|
||||
{
|
||||
template <typename CompletionHandler>
|
||||
void operator()(ASIO_MOVE_ARG(CompletionHandler) handler) const
|
||||
{
|
||||
typedef typename decay<CompletionHandler>::type DecayedHandler;
|
||||
|
||||
typename associated_executor<DecayedHandler>::type ex(
|
||||
(get_associated_executor)(handler));
|
||||
|
||||
typename associated_allocator<DecayedHandler>::type alloc(
|
||||
(get_associated_allocator)(handler));
|
||||
|
||||
ex.defer(ASIO_MOVE_CAST(CompletionHandler)(handler), alloc);
|
||||
}
|
||||
|
||||
template <typename CompletionHandler, typename Executor>
|
||||
void operator()(ASIO_MOVE_ARG(CompletionHandler) handler,
|
||||
ASIO_MOVE_ARG(Executor) ex) const
|
||||
{
|
||||
typedef typename decay<CompletionHandler>::type DecayedHandler;
|
||||
|
||||
typename associated_allocator<DecayedHandler>::type alloc(
|
||||
(get_associated_allocator)(handler));
|
||||
|
||||
ex.defer(detail::work_dispatcher<DecayedHandler>(
|
||||
ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename CompletionToken>
|
||||
ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) defer(
|
||||
ASIO_MOVE_ARG(CompletionToken) token)
|
||||
{
|
||||
return async_initiate<CompletionToken, void()>(
|
||||
detail::initiate_defer(), token);
|
||||
}
|
||||
|
||||
template <typename Executor, typename CompletionToken>
|
||||
ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) defer(
|
||||
const Executor& ex, ASIO_MOVE_ARG(CompletionToken) token,
|
||||
typename enable_if<is_executor<Executor>::value>::type*)
|
||||
{
|
||||
return async_initiate<CompletionToken, void()>(
|
||||
detail::initiate_defer(), token, ex);
|
||||
}
|
||||
|
||||
template <typename ExecutionContext, typename CompletionToken>
|
||||
inline ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) defer(
|
||||
ExecutionContext& ctx, ASIO_MOVE_ARG(CompletionToken) token,
|
||||
typename enable_if<is_convertible<
|
||||
ExecutionContext&, execution_context&>::value>::type*)
|
||||
{
|
||||
return (defer)(ctx.get_executor(),
|
||||
ASIO_MOVE_CAST(CompletionToken)(token));
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_DEFER_HPP
|
||||
+130
@@ -0,0 +1,130 @@
|
||||
//
|
||||
// impl/detached.hpp
|
||||
// ~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_DETACHED_HPP
|
||||
#define ASIO_IMPL_DETACHED_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/async_result.hpp"
|
||||
#include "asio/detail/variadic_templates.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
// Class to adapt a detached_t as a completion handler.
|
||||
class detached_handler
|
||||
{
|
||||
public:
|
||||
typedef void result_type;
|
||||
|
||||
detached_handler(detached_t)
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename... Args>
|
||||
void operator()(Args...)
|
||||
{
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
void operator()()
|
||||
{
|
||||
}
|
||||
|
||||
#define ASIO_PRIVATE_DETACHED_DEF(n) \
|
||||
template <ASIO_VARIADIC_TPARAMS(n)> \
|
||||
void operator()(ASIO_VARIADIC_TARGS(n)) \
|
||||
{ \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_DETACHED_DEF)
|
||||
#undef ASIO_PRIVATE_DETACHED_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Signature>
|
||||
struct async_result<detached_t, Signature>
|
||||
{
|
||||
typedef asio::detail::detached_handler completion_handler_type;
|
||||
|
||||
typedef void return_type;
|
||||
|
||||
explicit async_result(completion_handler_type&)
|
||||
{
|
||||
}
|
||||
|
||||
void get()
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Initiation, typename RawCompletionToken, typename... Args>
|
||||
static return_type initiate(
|
||||
ASIO_MOVE_ARG(Initiation) initiation,
|
||||
ASIO_MOVE_ARG(RawCompletionToken),
|
||||
ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
ASIO_MOVE_CAST(Initiation)(initiation)(
|
||||
detail::detached_handler(detached_t()),
|
||||
ASIO_MOVE_CAST(Args)(args)...);
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Initiation, typename RawCompletionToken>
|
||||
static return_type initiate(
|
||||
ASIO_MOVE_ARG(Initiation) initiation,
|
||||
ASIO_MOVE_ARG(RawCompletionToken))
|
||||
{
|
||||
ASIO_MOVE_CAST(Initiation)(initiation)(
|
||||
detail::detached_handler(detached_t()));
|
||||
}
|
||||
|
||||
#define ASIO_PRIVATE_INITIATE_DEF(n) \
|
||||
template <typename Initiation, typename RawCompletionToken, \
|
||||
ASIO_VARIADIC_TPARAMS(n)> \
|
||||
static return_type initiate( \
|
||||
ASIO_MOVE_ARG(Initiation) initiation, \
|
||||
ASIO_MOVE_ARG(RawCompletionToken), \
|
||||
ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
ASIO_MOVE_CAST(Initiation)(initiation)( \
|
||||
detail::detached_handler(detached_t()), \
|
||||
ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_INITIATE_DEF)
|
||||
#undef ASIO_PRIVATE_INITIATE_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_DETACHED_HPP
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
//
|
||||
// impl/dispatch.hpp
|
||||
// ~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_DISPATCH_HPP
|
||||
#define ASIO_IMPL_DISPATCH_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/detail/work_dispatcher.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
struct initiate_dispatch
|
||||
{
|
||||
template <typename CompletionHandler>
|
||||
void operator()(ASIO_MOVE_ARG(CompletionHandler) handler) const
|
||||
{
|
||||
typedef typename decay<CompletionHandler>::type DecayedHandler;
|
||||
|
||||
typename associated_executor<DecayedHandler>::type ex(
|
||||
(get_associated_executor)(handler));
|
||||
|
||||
typename associated_allocator<DecayedHandler>::type alloc(
|
||||
(get_associated_allocator)(handler));
|
||||
|
||||
ex.dispatch(ASIO_MOVE_CAST(CompletionHandler)(handler), alloc);
|
||||
}
|
||||
|
||||
template <typename CompletionHandler, typename Executor>
|
||||
void operator()(ASIO_MOVE_ARG(CompletionHandler) handler,
|
||||
ASIO_MOVE_ARG(Executor) ex) const
|
||||
{
|
||||
typedef typename decay<CompletionHandler>::type DecayedHandler;
|
||||
|
||||
typename associated_allocator<DecayedHandler>::type alloc(
|
||||
(get_associated_allocator)(handler));
|
||||
|
||||
ex.dispatch(detail::work_dispatcher<DecayedHandler>(
|
||||
ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename CompletionToken>
|
||||
ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) dispatch(
|
||||
ASIO_MOVE_ARG(CompletionToken) token)
|
||||
{
|
||||
return async_initiate<CompletionToken, void()>(
|
||||
detail::initiate_dispatch(), token);
|
||||
}
|
||||
|
||||
template <typename Executor, typename CompletionToken>
|
||||
ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) dispatch(
|
||||
const Executor& ex, ASIO_MOVE_ARG(CompletionToken) token,
|
||||
typename enable_if<is_executor<Executor>::value>::type*)
|
||||
{
|
||||
return async_initiate<CompletionToken, void()>(
|
||||
detail::initiate_dispatch(), token, ex);
|
||||
}
|
||||
|
||||
template <typename ExecutionContext, typename CompletionToken>
|
||||
inline ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) dispatch(
|
||||
ExecutionContext& ctx, ASIO_MOVE_ARG(CompletionToken) token,
|
||||
typename enable_if<is_convertible<
|
||||
ExecutionContext&, execution_context&>::value>::type*)
|
||||
{
|
||||
return (dispatch)(ctx.get_executor(),
|
||||
ASIO_MOVE_CAST(CompletionToken)(token));
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_DISPATCH_HPP
|
||||
+128
@@ -0,0 +1,128 @@
|
||||
//
|
||||
// impl/error.ipp
|
||||
// ~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_ERROR_IPP
|
||||
#define ASIO_IMPL_ERROR_IPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include <string>
|
||||
#include "asio/error.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace error {
|
||||
|
||||
#if !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
|
||||
|
||||
namespace detail {
|
||||
|
||||
class netdb_category : public asio::error_category
|
||||
{
|
||||
public:
|
||||
const char* name() const ASIO_ERROR_CATEGORY_NOEXCEPT
|
||||
{
|
||||
return "asio.netdb";
|
||||
}
|
||||
|
||||
std::string message(int value) const
|
||||
{
|
||||
if (value == error::host_not_found)
|
||||
return "Host not found (authoritative)";
|
||||
if (value == error::host_not_found_try_again)
|
||||
return "Host not found (non-authoritative), try again later";
|
||||
if (value == error::no_data)
|
||||
return "The query is valid, but it does not have associated data";
|
||||
if (value == error::no_recovery)
|
||||
return "A non-recoverable error occurred during database lookup";
|
||||
return "asio.netdb error";
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
const asio::error_category& get_netdb_category()
|
||||
{
|
||||
static detail::netdb_category instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
class addrinfo_category : public asio::error_category
|
||||
{
|
||||
public:
|
||||
const char* name() const ASIO_ERROR_CATEGORY_NOEXCEPT
|
||||
{
|
||||
return "asio.addrinfo";
|
||||
}
|
||||
|
||||
std::string message(int value) const
|
||||
{
|
||||
if (value == error::service_not_found)
|
||||
return "Service not found";
|
||||
if (value == error::socket_type_not_supported)
|
||||
return "Socket type not supported";
|
||||
return "asio.addrinfo error";
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
const asio::error_category& get_addrinfo_category()
|
||||
{
|
||||
static detail::addrinfo_category instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
#endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__)
|
||||
|
||||
namespace detail {
|
||||
|
||||
class misc_category : public asio::error_category
|
||||
{
|
||||
public:
|
||||
const char* name() const ASIO_ERROR_CATEGORY_NOEXCEPT
|
||||
{
|
||||
return "asio.misc";
|
||||
}
|
||||
|
||||
std::string message(int value) const
|
||||
{
|
||||
if (value == error::already_open)
|
||||
return "Already open";
|
||||
if (value == error::eof)
|
||||
return "End of file";
|
||||
if (value == error::not_found)
|
||||
return "Element not found";
|
||||
if (value == error::fd_set_failure)
|
||||
return "The descriptor does not fit into the select call's fd_set";
|
||||
return "asio.misc error";
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
const asio::error_category& get_misc_category()
|
||||
{
|
||||
static detail::misc_category instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
} // namespace error
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_ERROR_IPP
|
||||
+206
@@ -0,0 +1,206 @@
|
||||
//
|
||||
// impl/error_code.ipp
|
||||
// ~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_ERROR_CODE_IPP
|
||||
#define ASIO_IMPL_ERROR_CODE_IPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
# include <winerror.h>
|
||||
#elif defined(ASIO_WINDOWS_RUNTIME)
|
||||
# include <windows.h>
|
||||
#else
|
||||
# include <cerrno>
|
||||
# include <cstring>
|
||||
# include <string>
|
||||
#endif
|
||||
#include "asio/detail/local_free_on_block_exit.hpp"
|
||||
#include "asio/detail/socket_types.hpp"
|
||||
#include "asio/error_code.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
class system_category : public error_category
|
||||
{
|
||||
public:
|
||||
const char* name() const ASIO_ERROR_CATEGORY_NOEXCEPT
|
||||
{
|
||||
return "asio.system";
|
||||
}
|
||||
|
||||
std::string message(int value) const
|
||||
{
|
||||
#if defined(ASIO_WINDOWS_RUNTIME) || defined(ASIO_WINDOWS_APP)
|
||||
std::wstring wmsg(128, wchar_t());
|
||||
for (;;)
|
||||
{
|
||||
DWORD wlength = ::FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
|
||||
| FORMAT_MESSAGE_IGNORE_INSERTS, 0, value,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||
&wmsg[0], static_cast<DWORD>(wmsg.size()), 0);
|
||||
if (wlength == 0 && ::GetLastError() == ERROR_INSUFFICIENT_BUFFER)
|
||||
{
|
||||
wmsg.resize(wmsg.size() + wmsg.size() / 2);
|
||||
continue;
|
||||
}
|
||||
if (wlength && wmsg[wlength - 1] == '\n')
|
||||
--wlength;
|
||||
if (wlength && wmsg[wlength - 1] == '\r')
|
||||
--wlength;
|
||||
if (wlength)
|
||||
{
|
||||
std::string msg(wlength * 2, char());
|
||||
int length = ::WideCharToMultiByte(CP_ACP, 0,
|
||||
wmsg.c_str(), static_cast<int>(wlength),
|
||||
&msg[0], static_cast<int>(wlength * 2), 0, 0);
|
||||
if (length <= 0)
|
||||
return "asio.system error";
|
||||
msg.resize(static_cast<std::size_t>(length));
|
||||
return msg;
|
||||
}
|
||||
else
|
||||
return "asio.system error";
|
||||
}
|
||||
#elif defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
char* msg = 0;
|
||||
DWORD length = ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER
|
||||
| FORMAT_MESSAGE_FROM_SYSTEM
|
||||
| FORMAT_MESSAGE_IGNORE_INSERTS, 0, value,
|
||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (char*)&msg, 0, 0);
|
||||
detail::local_free_on_block_exit local_free_obj(msg);
|
||||
if (length && msg[length - 1] == '\n')
|
||||
msg[--length] = '\0';
|
||||
if (length && msg[length - 1] == '\r')
|
||||
msg[--length] = '\0';
|
||||
if (length)
|
||||
return msg;
|
||||
else
|
||||
return "asio.system error";
|
||||
#else // defined(ASIO_WINDOWS_DESKTOP) || defined(__CYGWIN__)
|
||||
#if !defined(__sun)
|
||||
if (value == ECANCELED)
|
||||
return "Operation aborted.";
|
||||
#endif // !defined(__sun)
|
||||
#if defined(__sun) || defined(__QNX__) || defined(__SYMBIAN32__)
|
||||
using namespace std;
|
||||
return strerror(value);
|
||||
#else
|
||||
char buf[256] = "";
|
||||
using namespace std;
|
||||
return strerror_result(strerror_r(value, buf, sizeof(buf)), buf);
|
||||
#endif
|
||||
#endif // defined(ASIO_WINDOWS_DESKTOP) || defined(__CYGWIN__)
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_STD_ERROR_CODE)
|
||||
std::error_condition default_error_condition(
|
||||
int ev) const ASIO_ERROR_CATEGORY_NOEXCEPT
|
||||
{
|
||||
switch (ev)
|
||||
{
|
||||
case access_denied:
|
||||
return std::errc::permission_denied;
|
||||
case address_family_not_supported:
|
||||
return std::errc::address_family_not_supported;
|
||||
case address_in_use:
|
||||
return std::errc::address_in_use;
|
||||
case already_connected:
|
||||
return std::errc::already_connected;
|
||||
case already_started:
|
||||
return std::errc::connection_already_in_progress;
|
||||
case broken_pipe:
|
||||
return std::errc::broken_pipe;
|
||||
case connection_aborted:
|
||||
return std::errc::connection_aborted;
|
||||
case connection_refused:
|
||||
return std::errc::connection_refused;
|
||||
case connection_reset:
|
||||
return std::errc::connection_reset;
|
||||
case bad_descriptor:
|
||||
return std::errc::bad_file_descriptor;
|
||||
case fault:
|
||||
return std::errc::bad_address;
|
||||
case host_unreachable:
|
||||
return std::errc::host_unreachable;
|
||||
case in_progress:
|
||||
return std::errc::operation_in_progress;
|
||||
case interrupted:
|
||||
return std::errc::interrupted;
|
||||
case invalid_argument:
|
||||
return std::errc::invalid_argument;
|
||||
case message_size:
|
||||
return std::errc::message_size;
|
||||
case name_too_long:
|
||||
return std::errc::filename_too_long;
|
||||
case network_down:
|
||||
return std::errc::network_down;
|
||||
case network_reset:
|
||||
return std::errc::network_reset;
|
||||
case network_unreachable:
|
||||
return std::errc::network_unreachable;
|
||||
case no_descriptors:
|
||||
return std::errc::too_many_files_open;
|
||||
case no_buffer_space:
|
||||
return std::errc::no_buffer_space;
|
||||
case no_memory:
|
||||
return std::errc::not_enough_memory;
|
||||
case no_permission:
|
||||
return std::errc::operation_not_permitted;
|
||||
case no_protocol_option:
|
||||
return std::errc::no_protocol_option;
|
||||
case no_such_device:
|
||||
return std::errc::no_such_device;
|
||||
case not_connected:
|
||||
return std::errc::not_connected;
|
||||
case not_socket:
|
||||
return std::errc::not_a_socket;
|
||||
case operation_aborted:
|
||||
return std::errc::operation_canceled;
|
||||
case operation_not_supported:
|
||||
return std::errc::operation_not_supported;
|
||||
case shut_down:
|
||||
return std::make_error_condition(ev, *this);
|
||||
case timed_out:
|
||||
return std::errc::timed_out;
|
||||
case try_again:
|
||||
return std::errc::resource_unavailable_try_again;
|
||||
case would_block:
|
||||
return std::errc::operation_would_block;
|
||||
default:
|
||||
return std::make_error_condition(ev, *this);
|
||||
}
|
||||
#endif // defined(ASIO_HAS_STD_ERROR_CODE)
|
||||
|
||||
private:
|
||||
// Helper function to adapt the result from glibc's variant of strerror_r.
|
||||
static const char* strerror_result(int, const char* s) { return s; }
|
||||
static const char* strerror_result(const char* s, const char*) { return s; }
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
const error_category& system_category()
|
||||
{
|
||||
static detail::system_category instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_ERROR_CODE_IPP
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
//
|
||||
// impl/execution_context.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_EXECUTION_CONTEXT_HPP
|
||||
#define ASIO_IMPL_EXECUTION_CONTEXT_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/handler_type_requirements.hpp"
|
||||
#include "asio/detail/scoped_ptr.hpp"
|
||||
#include "asio/detail/service_registry.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Service>
|
||||
inline Service& use_service(execution_context& e)
|
||||
{
|
||||
// Check that Service meets the necessary type requirements.
|
||||
(void)static_cast<execution_context::service*>(static_cast<Service*>(0));
|
||||
|
||||
return e.service_registry_->template use_service<Service>();
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Service, typename... Args>
|
||||
Service& make_service(execution_context& e, ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
detail::scoped_ptr<Service> svc(
|
||||
new Service(e, ASIO_MOVE_CAST(Args)(args)...));
|
||||
e.service_registry_->template add_service<Service>(svc.get());
|
||||
Service& result = *svc;
|
||||
svc.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Service>
|
||||
Service& make_service(execution_context& e)
|
||||
{
|
||||
detail::scoped_ptr<Service> svc(new Service(e));
|
||||
e.service_registry_->template add_service<Service>(svc.get());
|
||||
Service& result = *svc;
|
||||
svc.release();
|
||||
return result;
|
||||
}
|
||||
|
||||
#define ASIO_PRIVATE_MAKE_SERVICE_DEF(n) \
|
||||
template <typename Service, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
Service& make_service(execution_context& e, \
|
||||
ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
detail::scoped_ptr<Service> svc( \
|
||||
new Service(e, ASIO_VARIADIC_MOVE_ARGS(n))); \
|
||||
e.service_registry_->template add_service<Service>(svc.get()); \
|
||||
Service& result = *svc; \
|
||||
svc.release(); \
|
||||
return result; \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_MAKE_SERVICE_DEF)
|
||||
#undef ASIO_PRIVATE_MAKE_SERVICE_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Service>
|
||||
inline void add_service(execution_context& e, Service* svc)
|
||||
{
|
||||
// Check that Service meets the necessary type requirements.
|
||||
(void)static_cast<execution_context::service*>(static_cast<Service*>(0));
|
||||
|
||||
e.service_registry_->template add_service<Service>(svc);
|
||||
}
|
||||
|
||||
template <typename Service>
|
||||
inline bool has_service(execution_context& e)
|
||||
{
|
||||
// Check that Service meets the necessary type requirements.
|
||||
(void)static_cast<execution_context::service*>(static_cast<Service*>(0));
|
||||
|
||||
return e.service_registry_->template has_service<Service>();
|
||||
}
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
inline execution_context& execution_context::service::context()
|
||||
{
|
||||
return owner_;
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_EXECUTION_CONTEXT_HPP
|
||||
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// impl/execution_context.ipp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_EXECUTION_CONTEXT_IPP
|
||||
#define ASIO_IMPL_EXECUTION_CONTEXT_IPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/execution_context.hpp"
|
||||
#include "asio/detail/service_registry.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
execution_context::execution_context()
|
||||
: service_registry_(new asio::detail::service_registry(*this))
|
||||
{
|
||||
}
|
||||
|
||||
execution_context::~execution_context()
|
||||
{
|
||||
shutdown();
|
||||
destroy();
|
||||
delete service_registry_;
|
||||
}
|
||||
|
||||
void execution_context::shutdown()
|
||||
{
|
||||
service_registry_->shutdown_services();
|
||||
}
|
||||
|
||||
void execution_context::destroy()
|
||||
{
|
||||
service_registry_->destroy_services();
|
||||
}
|
||||
|
||||
void execution_context::notify_fork(
|
||||
asio::execution_context::fork_event event)
|
||||
{
|
||||
service_registry_->notify_fork(event);
|
||||
}
|
||||
|
||||
execution_context::service::service(execution_context& owner)
|
||||
: owner_(owner),
|
||||
next_(0)
|
||||
{
|
||||
}
|
||||
|
||||
execution_context::service::~service()
|
||||
{
|
||||
}
|
||||
|
||||
void execution_context::service::notify_fork(execution_context::fork_event)
|
||||
{
|
||||
}
|
||||
|
||||
service_already_exists::service_already_exists()
|
||||
: std::logic_error("Service already exists.")
|
||||
{
|
||||
}
|
||||
|
||||
invalid_service_owner::invalid_service_owner()
|
||||
: std::logic_error("Invalid service owner.")
|
||||
{
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_EXECUTION_CONTEXT_IPP
|
||||
+387
@@ -0,0 +1,387 @@
|
||||
//
|
||||
// impl/executor.hpp
|
||||
// ~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_EXECUTOR_HPP
|
||||
#define ASIO_IMPL_EXECUTOR_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/detail/atomic_count.hpp"
|
||||
#include "asio/detail/executor_function.hpp"
|
||||
#include "asio/detail/global.hpp"
|
||||
#include "asio/detail/memory.hpp"
|
||||
#include "asio/detail/recycling_allocator.hpp"
|
||||
#include "asio/executor.hpp"
|
||||
#include "asio/system_executor.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
|
||||
// Lightweight, move-only function object wrapper.
|
||||
class executor::function
|
||||
{
|
||||
public:
|
||||
template <typename F, typename Alloc>
|
||||
explicit function(F f, const Alloc& a)
|
||||
{
|
||||
// Allocate and construct an operation to wrap the function.
|
||||
typedef detail::executor_function<F, Alloc> func_type;
|
||||
typename func_type::ptr p = {
|
||||
detail::addressof(a), func_type::ptr::allocate(a), 0 };
|
||||
func_ = new (p.v) func_type(ASIO_MOVE_CAST(F)(f), a);
|
||||
p.v = 0;
|
||||
}
|
||||
|
||||
function(function&& other) ASIO_NOEXCEPT
|
||||
: func_(other.func_)
|
||||
{
|
||||
other.func_ = 0;
|
||||
}
|
||||
|
||||
~function()
|
||||
{
|
||||
if (func_)
|
||||
func_->destroy();
|
||||
}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
if (func_)
|
||||
{
|
||||
detail::executor_function_base* func = func_;
|
||||
func_ = 0;
|
||||
func->complete();
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
detail::executor_function_base* func_;
|
||||
};
|
||||
|
||||
#else // defined(ASIO_HAS_MOVE)
|
||||
|
||||
// Not so lightweight, copyable function object wrapper.
|
||||
class executor::function
|
||||
{
|
||||
public:
|
||||
template <typename F, typename Alloc>
|
||||
explicit function(const F& f, const Alloc&)
|
||||
: impl_(new impl<F>(f))
|
||||
{
|
||||
}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
impl_->invoke_(impl_.get());
|
||||
}
|
||||
|
||||
private:
|
||||
// Base class for polymorphic function implementations.
|
||||
struct impl_base
|
||||
{
|
||||
void (*invoke_)(impl_base*);
|
||||
};
|
||||
|
||||
// Polymorphic function implementation.
|
||||
template <typename F>
|
||||
struct impl : impl_base
|
||||
{
|
||||
impl(const F& f)
|
||||
: function_(f)
|
||||
{
|
||||
invoke_ = &function::invoke<F>;
|
||||
}
|
||||
|
||||
F function_;
|
||||
};
|
||||
|
||||
// Helper to invoke a function.
|
||||
template <typename F>
|
||||
static void invoke(impl_base* i)
|
||||
{
|
||||
static_cast<impl<F>*>(i)->function_();
|
||||
}
|
||||
|
||||
detail::shared_ptr<impl_base> impl_;
|
||||
};
|
||||
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
// Default polymorphic allocator implementation.
|
||||
template <typename Executor, typename Allocator>
|
||||
class executor::impl
|
||||
: public executor::impl_base
|
||||
{
|
||||
public:
|
||||
typedef ASIO_REBIND_ALLOC(Allocator, impl) allocator_type;
|
||||
|
||||
static impl_base* create(const Executor& e, Allocator a = Allocator())
|
||||
{
|
||||
raw_mem mem(a);
|
||||
impl* p = new (mem.ptr_) impl(e, a);
|
||||
mem.ptr_ = 0;
|
||||
return p;
|
||||
}
|
||||
|
||||
impl(const Executor& e, const Allocator& a) ASIO_NOEXCEPT
|
||||
: impl_base(false),
|
||||
ref_count_(1),
|
||||
executor_(e),
|
||||
allocator_(a)
|
||||
{
|
||||
}
|
||||
|
||||
impl_base* clone() const ASIO_NOEXCEPT
|
||||
{
|
||||
++ref_count_;
|
||||
return const_cast<impl_base*>(static_cast<const impl_base*>(this));
|
||||
}
|
||||
|
||||
void destroy() ASIO_NOEXCEPT
|
||||
{
|
||||
if (--ref_count_ == 0)
|
||||
{
|
||||
allocator_type alloc(allocator_);
|
||||
impl* p = this;
|
||||
p->~impl();
|
||||
alloc.deallocate(p, 1);
|
||||
}
|
||||
}
|
||||
|
||||
void on_work_started() ASIO_NOEXCEPT
|
||||
{
|
||||
executor_.on_work_started();
|
||||
}
|
||||
|
||||
void on_work_finished() ASIO_NOEXCEPT
|
||||
{
|
||||
executor_.on_work_finished();
|
||||
}
|
||||
|
||||
execution_context& context() ASIO_NOEXCEPT
|
||||
{
|
||||
return executor_.context();
|
||||
}
|
||||
|
||||
void dispatch(ASIO_MOVE_ARG(function) f)
|
||||
{
|
||||
executor_.dispatch(ASIO_MOVE_CAST(function)(f), allocator_);
|
||||
}
|
||||
|
||||
void post(ASIO_MOVE_ARG(function) f)
|
||||
{
|
||||
executor_.post(ASIO_MOVE_CAST(function)(f), allocator_);
|
||||
}
|
||||
|
||||
void defer(ASIO_MOVE_ARG(function) f)
|
||||
{
|
||||
executor_.defer(ASIO_MOVE_CAST(function)(f), allocator_);
|
||||
}
|
||||
|
||||
type_id_result_type target_type() const ASIO_NOEXCEPT
|
||||
{
|
||||
return type_id<Executor>();
|
||||
}
|
||||
|
||||
void* target() ASIO_NOEXCEPT
|
||||
{
|
||||
return &executor_;
|
||||
}
|
||||
|
||||
const void* target() const ASIO_NOEXCEPT
|
||||
{
|
||||
return &executor_;
|
||||
}
|
||||
|
||||
bool equals(const impl_base* e) const ASIO_NOEXCEPT
|
||||
{
|
||||
if (this == e)
|
||||
return true;
|
||||
if (target_type() != e->target_type())
|
||||
return false;
|
||||
return executor_ == *static_cast<const Executor*>(e->target());
|
||||
}
|
||||
|
||||
private:
|
||||
mutable detail::atomic_count ref_count_;
|
||||
Executor executor_;
|
||||
Allocator allocator_;
|
||||
|
||||
struct raw_mem
|
||||
{
|
||||
allocator_type allocator_;
|
||||
impl* ptr_;
|
||||
|
||||
explicit raw_mem(const Allocator& a)
|
||||
: allocator_(a),
|
||||
ptr_(allocator_.allocate(1))
|
||||
{
|
||||
}
|
||||
|
||||
~raw_mem()
|
||||
{
|
||||
if (ptr_)
|
||||
allocator_.deallocate(ptr_, 1);
|
||||
}
|
||||
|
||||
private:
|
||||
// Disallow copying and assignment.
|
||||
raw_mem(const raw_mem&);
|
||||
raw_mem operator=(const raw_mem&);
|
||||
};
|
||||
};
|
||||
|
||||
// Polymorphic allocator specialisation for system_executor.
|
||||
template <typename Allocator>
|
||||
class executor::impl<system_executor, Allocator>
|
||||
: public executor::impl_base
|
||||
{
|
||||
public:
|
||||
static impl_base* create(const system_executor&,
|
||||
const Allocator& = Allocator())
|
||||
{
|
||||
return &detail::global<impl<system_executor, std::allocator<void> > >();
|
||||
}
|
||||
|
||||
impl()
|
||||
: impl_base(true)
|
||||
{
|
||||
}
|
||||
|
||||
impl_base* clone() const ASIO_NOEXCEPT
|
||||
{
|
||||
return const_cast<impl_base*>(static_cast<const impl_base*>(this));
|
||||
}
|
||||
|
||||
void destroy() ASIO_NOEXCEPT
|
||||
{
|
||||
}
|
||||
|
||||
void on_work_started() ASIO_NOEXCEPT
|
||||
{
|
||||
executor_.on_work_started();
|
||||
}
|
||||
|
||||
void on_work_finished() ASIO_NOEXCEPT
|
||||
{
|
||||
executor_.on_work_finished();
|
||||
}
|
||||
|
||||
execution_context& context() ASIO_NOEXCEPT
|
||||
{
|
||||
return executor_.context();
|
||||
}
|
||||
|
||||
void dispatch(ASIO_MOVE_ARG(function) f)
|
||||
{
|
||||
executor_.dispatch(ASIO_MOVE_CAST(function)(f), allocator_);
|
||||
}
|
||||
|
||||
void post(ASIO_MOVE_ARG(function) f)
|
||||
{
|
||||
executor_.post(ASIO_MOVE_CAST(function)(f), allocator_);
|
||||
}
|
||||
|
||||
void defer(ASIO_MOVE_ARG(function) f)
|
||||
{
|
||||
executor_.defer(ASIO_MOVE_CAST(function)(f), allocator_);
|
||||
}
|
||||
|
||||
type_id_result_type target_type() const ASIO_NOEXCEPT
|
||||
{
|
||||
return type_id<system_executor>();
|
||||
}
|
||||
|
||||
void* target() ASIO_NOEXCEPT
|
||||
{
|
||||
return &executor_;
|
||||
}
|
||||
|
||||
const void* target() const ASIO_NOEXCEPT
|
||||
{
|
||||
return &executor_;
|
||||
}
|
||||
|
||||
bool equals(const impl_base* e) const ASIO_NOEXCEPT
|
||||
{
|
||||
return this == e;
|
||||
}
|
||||
|
||||
private:
|
||||
system_executor executor_;
|
||||
Allocator allocator_;
|
||||
};
|
||||
|
||||
template <typename Executor>
|
||||
executor::executor(Executor e)
|
||||
: impl_(impl<Executor, std::allocator<void> >::create(e))
|
||||
{
|
||||
}
|
||||
|
||||
template <typename Executor, typename Allocator>
|
||||
executor::executor(allocator_arg_t, const Allocator& a, Executor e)
|
||||
: impl_(impl<Executor, Allocator>::create(e, a))
|
||||
{
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void executor::dispatch(ASIO_MOVE_ARG(Function) f,
|
||||
const Allocator& a) const
|
||||
{
|
||||
impl_base* i = get_impl();
|
||||
if (i->fast_dispatch_)
|
||||
system_executor().dispatch(ASIO_MOVE_CAST(Function)(f), a);
|
||||
else
|
||||
i->dispatch(function(ASIO_MOVE_CAST(Function)(f), a));
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void executor::post(ASIO_MOVE_ARG(Function) f,
|
||||
const Allocator& a) const
|
||||
{
|
||||
get_impl()->post(function(ASIO_MOVE_CAST(Function)(f), a));
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void executor::defer(ASIO_MOVE_ARG(Function) f,
|
||||
const Allocator& a) const
|
||||
{
|
||||
get_impl()->defer(function(ASIO_MOVE_CAST(Function)(f), a));
|
||||
}
|
||||
|
||||
template <typename Executor>
|
||||
Executor* executor::target() ASIO_NOEXCEPT
|
||||
{
|
||||
return impl_ && impl_->target_type() == type_id<Executor>()
|
||||
? static_cast<Executor*>(impl_->target()) : 0;
|
||||
}
|
||||
|
||||
template <typename Executor>
|
||||
const Executor* executor::target() const ASIO_NOEXCEPT
|
||||
{
|
||||
return impl_ && impl_->target_type() == type_id<Executor>()
|
||||
? static_cast<Executor*>(impl_->target()) : 0;
|
||||
}
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_EXECUTOR_HPP
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// impl/executor.ipp
|
||||
// ~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_EXECUTOR_IPP
|
||||
#define ASIO_IMPL_EXECUTOR_IPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/executor.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
bad_executor::bad_executor() ASIO_NOEXCEPT
|
||||
{
|
||||
}
|
||||
|
||||
const char* bad_executor::what() const ASIO_NOEXCEPT_OR_NOTHROW
|
||||
{
|
||||
return "bad executor";
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_EXECUTOR_IPP
|
||||
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// impl/handler_alloc_hook.ipp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_HANDLER_ALLOC_HOOK_IPP
|
||||
#define ASIO_IMPL_HANDLER_ALLOC_HOOK_IPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/detail/thread_context.hpp"
|
||||
#include "asio/detail/thread_info_base.hpp"
|
||||
#include "asio/handler_alloc_hook.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
void* asio_handler_allocate(std::size_t size, ...)
|
||||
{
|
||||
#if !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
|
||||
return detail::thread_info_base::allocate(
|
||||
detail::thread_context::thread_call_stack::top(), size);
|
||||
#else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
|
||||
return ::operator new(size);
|
||||
#endif // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
|
||||
}
|
||||
|
||||
void asio_handler_deallocate(void* pointer, std::size_t size, ...)
|
||||
{
|
||||
#if !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
|
||||
detail::thread_info_base::deallocate(
|
||||
detail::thread_context::thread_call_stack::top(), pointer, size);
|
||||
#else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
|
||||
(void)size;
|
||||
::operator delete(pointer);
|
||||
#endif // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_HANDLER_ALLOC_HOOK_IPP
|
||||
+353
@@ -0,0 +1,353 @@
|
||||
//
|
||||
// impl/io_context.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_IO_CONTEXT_HPP
|
||||
#define ASIO_IMPL_IO_CONTEXT_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/completion_handler.hpp"
|
||||
#include "asio/detail/executor_op.hpp"
|
||||
#include "asio/detail/fenced_block.hpp"
|
||||
#include "asio/detail/handler_type_requirements.hpp"
|
||||
#include "asio/detail/non_const_lvalue.hpp"
|
||||
#include "asio/detail/recycling_allocator.hpp"
|
||||
#include "asio/detail/service_registry.hpp"
|
||||
#include "asio/detail/throw_error.hpp"
|
||||
#include "asio/detail/type_traits.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
namespace asio {
|
||||
|
||||
template <typename Service>
|
||||
inline Service& use_service(io_context& ioc)
|
||||
{
|
||||
// Check that Service meets the necessary type requirements.
|
||||
(void)static_cast<execution_context::service*>(static_cast<Service*>(0));
|
||||
(void)static_cast<const execution_context::id*>(&Service::id);
|
||||
|
||||
return ioc.service_registry_->template use_service<Service>(ioc);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline detail::io_context_impl& use_service<detail::io_context_impl>(
|
||||
io_context& ioc)
|
||||
{
|
||||
return ioc.impl_;
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#if defined(ASIO_HAS_IOCP)
|
||||
# include "asio/detail/win_iocp_io_context.hpp"
|
||||
#else
|
||||
# include "asio/detail/scheduler.hpp"
|
||||
#endif
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
inline io_context::executor_type
|
||||
io_context::get_executor() ASIO_NOEXCEPT
|
||||
{
|
||||
return executor_type(*this);
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_CHRONO)
|
||||
|
||||
template <typename Rep, typename Period>
|
||||
std::size_t io_context::run_for(
|
||||
const chrono::duration<Rep, Period>& rel_time)
|
||||
{
|
||||
return this->run_until(chrono::steady_clock::now() + rel_time);
|
||||
}
|
||||
|
||||
template <typename Clock, typename Duration>
|
||||
std::size_t io_context::run_until(
|
||||
const chrono::time_point<Clock, Duration>& abs_time)
|
||||
{
|
||||
std::size_t n = 0;
|
||||
while (this->run_one_until(abs_time))
|
||||
if (n != (std::numeric_limits<std::size_t>::max)())
|
||||
++n;
|
||||
return n;
|
||||
}
|
||||
|
||||
template <typename Rep, typename Period>
|
||||
std::size_t io_context::run_one_for(
|
||||
const chrono::duration<Rep, Period>& rel_time)
|
||||
{
|
||||
return this->run_one_until(chrono::steady_clock::now() + rel_time);
|
||||
}
|
||||
|
||||
template <typename Clock, typename Duration>
|
||||
std::size_t io_context::run_one_until(
|
||||
const chrono::time_point<Clock, Duration>& abs_time)
|
||||
{
|
||||
typename Clock::time_point now = Clock::now();
|
||||
while (now < abs_time)
|
||||
{
|
||||
typename Clock::duration rel_time = abs_time - now;
|
||||
if (rel_time > chrono::seconds(1))
|
||||
rel_time = chrono::seconds(1);
|
||||
|
||||
asio::error_code ec;
|
||||
std::size_t s = impl_.wait_one(
|
||||
static_cast<long>(chrono::duration_cast<
|
||||
chrono::microseconds>(rel_time).count()), ec);
|
||||
asio::detail::throw_error(ec);
|
||||
|
||||
if (s || impl_.stopped())
|
||||
return s;
|
||||
|
||||
now = Clock::now();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif // defined(ASIO_HAS_CHRONO)
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
inline void io_context::reset()
|
||||
{
|
||||
restart();
|
||||
}
|
||||
|
||||
struct io_context::initiate_dispatch
|
||||
{
|
||||
template <typename LegacyCompletionHandler>
|
||||
void operator()(ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
|
||||
io_context* self) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler does
|
||||
// not meet the documented type requirements for a LegacyCompletionHandler.
|
||||
ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
|
||||
LegacyCompletionHandler, handler) type_check;
|
||||
|
||||
detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
|
||||
if (self->impl_.can_dispatch())
|
||||
{
|
||||
detail::fenced_block b(detail::fenced_block::full);
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
handler2.value, handler2.value);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Allocate and construct an operation to wrap the handler.
|
||||
typedef detail::completion_handler<
|
||||
typename decay<LegacyCompletionHandler>::type> op;
|
||||
typename op::ptr p = { detail::addressof(handler2.value),
|
||||
op::ptr::allocate(handler2.value), 0 };
|
||||
p.p = new (p.v) op(handler2.value);
|
||||
|
||||
ASIO_HANDLER_CREATION((*self, *p.p,
|
||||
"io_context", self, 0, "dispatch"));
|
||||
|
||||
self->impl_.do_dispatch(p.p);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename LegacyCompletionHandler>
|
||||
ASIO_INITFN_RESULT_TYPE(LegacyCompletionHandler, void ())
|
||||
io_context::dispatch(ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
|
||||
{
|
||||
return async_initiate<LegacyCompletionHandler, void ()>(
|
||||
initiate_dispatch(), handler, this);
|
||||
}
|
||||
|
||||
struct io_context::initiate_post
|
||||
{
|
||||
template <typename LegacyCompletionHandler>
|
||||
void operator()(ASIO_MOVE_ARG(LegacyCompletionHandler) handler,
|
||||
io_context* self) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler does
|
||||
// not meet the documented type requirements for a LegacyCompletionHandler.
|
||||
ASIO_LEGACY_COMPLETION_HANDLER_CHECK(
|
||||
LegacyCompletionHandler, handler) type_check;
|
||||
|
||||
detail::non_const_lvalue<LegacyCompletionHandler> handler2(handler);
|
||||
|
||||
bool is_continuation =
|
||||
asio_handler_cont_helpers::is_continuation(handler2.value);
|
||||
|
||||
// Allocate and construct an operation to wrap the handler.
|
||||
typedef detail::completion_handler<
|
||||
typename decay<LegacyCompletionHandler>::type> op;
|
||||
typename op::ptr p = { detail::addressof(handler2.value),
|
||||
op::ptr::allocate(handler2.value), 0 };
|
||||
p.p = new (p.v) op(handler2.value);
|
||||
|
||||
ASIO_HANDLER_CREATION((*self, *p.p,
|
||||
"io_context", self, 0, "post"));
|
||||
|
||||
self->impl_.post_immediate_completion(p.p, is_continuation);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename LegacyCompletionHandler>
|
||||
ASIO_INITFN_RESULT_TYPE(LegacyCompletionHandler, void ())
|
||||
io_context::post(ASIO_MOVE_ARG(LegacyCompletionHandler) handler)
|
||||
{
|
||||
return async_initiate<LegacyCompletionHandler, void ()>(
|
||||
initiate_post(), handler, this);
|
||||
}
|
||||
|
||||
template <typename Handler>
|
||||
#if defined(GENERATING_DOCUMENTATION)
|
||||
unspecified
|
||||
#else
|
||||
inline detail::wrapped_handler<io_context&, Handler>
|
||||
#endif
|
||||
io_context::wrap(Handler handler)
|
||||
{
|
||||
return detail::wrapped_handler<io_context&, Handler>(*this, handler);
|
||||
}
|
||||
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
inline io_context&
|
||||
io_context::executor_type::context() const ASIO_NOEXCEPT
|
||||
{
|
||||
return io_context_;
|
||||
}
|
||||
|
||||
inline void
|
||||
io_context::executor_type::on_work_started() const ASIO_NOEXCEPT
|
||||
{
|
||||
io_context_.impl_.work_started();
|
||||
}
|
||||
|
||||
inline void
|
||||
io_context::executor_type::on_work_finished() const ASIO_NOEXCEPT
|
||||
{
|
||||
io_context_.impl_.work_finished();
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void io_context::executor_type::dispatch(
|
||||
ASIO_MOVE_ARG(Function) f, const Allocator& a) const
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
// Invoke immediately if we are already inside the thread pool.
|
||||
if (io_context_.impl_.can_dispatch())
|
||||
{
|
||||
// Make a local, non-const copy of the function.
|
||||
function_type tmp(ASIO_MOVE_CAST(Function)(f));
|
||||
|
||||
detail::fenced_block b(detail::fenced_block::full);
|
||||
asio_handler_invoke_helpers::invoke(tmp, tmp);
|
||||
return;
|
||||
}
|
||||
|
||||
// Allocate and construct an operation to wrap the function.
|
||||
typedef detail::executor_op<function_type, Allocator, detail::operation> op;
|
||||
typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
|
||||
p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
|
||||
|
||||
ASIO_HANDLER_CREATION((this->context(), *p.p,
|
||||
"io_context", &this->context(), 0, "dispatch"));
|
||||
|
||||
io_context_.impl_.post_immediate_completion(p.p, false);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void io_context::executor_type::post(
|
||||
ASIO_MOVE_ARG(Function) f, const Allocator& a) const
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
// Allocate and construct an operation to wrap the function.
|
||||
typedef detail::executor_op<function_type, Allocator, detail::operation> op;
|
||||
typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
|
||||
p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
|
||||
|
||||
ASIO_HANDLER_CREATION((this->context(), *p.p,
|
||||
"io_context", &this->context(), 0, "post"));
|
||||
|
||||
io_context_.impl_.post_immediate_completion(p.p, false);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void io_context::executor_type::defer(
|
||||
ASIO_MOVE_ARG(Function) f, const Allocator& a) const
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
// Allocate and construct an operation to wrap the function.
|
||||
typedef detail::executor_op<function_type, Allocator, detail::operation> op;
|
||||
typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
|
||||
p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
|
||||
|
||||
ASIO_HANDLER_CREATION((this->context(), *p.p,
|
||||
"io_context", &this->context(), 0, "defer"));
|
||||
|
||||
io_context_.impl_.post_immediate_completion(p.p, true);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
|
||||
inline bool
|
||||
io_context::executor_type::running_in_this_thread() const ASIO_NOEXCEPT
|
||||
{
|
||||
return io_context_.impl_.can_dispatch();
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
inline io_context::work::work(asio::io_context& io_context)
|
||||
: io_context_impl_(io_context.impl_)
|
||||
{
|
||||
io_context_impl_.work_started();
|
||||
}
|
||||
|
||||
inline io_context::work::work(const work& other)
|
||||
: io_context_impl_(other.io_context_impl_)
|
||||
{
|
||||
io_context_impl_.work_started();
|
||||
}
|
||||
|
||||
inline io_context::work::~work()
|
||||
{
|
||||
io_context_impl_.work_finished();
|
||||
}
|
||||
|
||||
inline asio::io_context& io_context::work::get_io_context()
|
||||
{
|
||||
return static_cast<asio::io_context&>(io_context_impl_.context());
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
inline asio::io_context& io_context::service::get_io_context()
|
||||
{
|
||||
return static_cast<asio::io_context&>(context());
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_IO_CONTEXT_HPP
|
||||
+175
@@ -0,0 +1,175 @@
|
||||
//
|
||||
// impl/io_context.ipp
|
||||
// ~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_IO_CONTEXT_IPP
|
||||
#define ASIO_IMPL_IO_CONTEXT_IPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/io_context.hpp"
|
||||
#include "asio/detail/concurrency_hint.hpp"
|
||||
#include "asio/detail/limits.hpp"
|
||||
#include "asio/detail/scoped_ptr.hpp"
|
||||
#include "asio/detail/service_registry.hpp"
|
||||
#include "asio/detail/throw_error.hpp"
|
||||
|
||||
#if defined(ASIO_HAS_IOCP)
|
||||
# include "asio/detail/win_iocp_io_context.hpp"
|
||||
#else
|
||||
# include "asio/detail/scheduler.hpp"
|
||||
#endif
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
io_context::io_context()
|
||||
: impl_(add_impl(new impl_type(*this,
|
||||
ASIO_CONCURRENCY_HINT_DEFAULT, false)))
|
||||
{
|
||||
}
|
||||
|
||||
io_context::io_context(int concurrency_hint)
|
||||
: impl_(add_impl(new impl_type(*this, concurrency_hint == 1
|
||||
? ASIO_CONCURRENCY_HINT_1 : concurrency_hint, false)))
|
||||
{
|
||||
}
|
||||
|
||||
io_context::impl_type& io_context::add_impl(io_context::impl_type* impl)
|
||||
{
|
||||
asio::detail::scoped_ptr<impl_type> scoped_impl(impl);
|
||||
asio::add_service<impl_type>(*this, scoped_impl.get());
|
||||
return *scoped_impl.release();
|
||||
}
|
||||
|
||||
io_context::~io_context()
|
||||
{
|
||||
}
|
||||
|
||||
io_context::count_type io_context::run()
|
||||
{
|
||||
asio::error_code ec;
|
||||
count_type s = impl_.run(ec);
|
||||
asio::detail::throw_error(ec);
|
||||
return s;
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
io_context::count_type io_context::run(asio::error_code& ec)
|
||||
{
|
||||
return impl_.run(ec);
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
io_context::count_type io_context::run_one()
|
||||
{
|
||||
asio::error_code ec;
|
||||
count_type s = impl_.run_one(ec);
|
||||
asio::detail::throw_error(ec);
|
||||
return s;
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
io_context::count_type io_context::run_one(asio::error_code& ec)
|
||||
{
|
||||
return impl_.run_one(ec);
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
io_context::count_type io_context::poll()
|
||||
{
|
||||
asio::error_code ec;
|
||||
count_type s = impl_.poll(ec);
|
||||
asio::detail::throw_error(ec);
|
||||
return s;
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
io_context::count_type io_context::poll(asio::error_code& ec)
|
||||
{
|
||||
return impl_.poll(ec);
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
io_context::count_type io_context::poll_one()
|
||||
{
|
||||
asio::error_code ec;
|
||||
count_type s = impl_.poll_one(ec);
|
||||
asio::detail::throw_error(ec);
|
||||
return s;
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
io_context::count_type io_context::poll_one(asio::error_code& ec)
|
||||
{
|
||||
return impl_.poll_one(ec);
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
void io_context::stop()
|
||||
{
|
||||
impl_.stop();
|
||||
}
|
||||
|
||||
bool io_context::stopped() const
|
||||
{
|
||||
return impl_.stopped();
|
||||
}
|
||||
|
||||
void io_context::restart()
|
||||
{
|
||||
impl_.restart();
|
||||
}
|
||||
|
||||
io_context::service::service(asio::io_context& owner)
|
||||
: execution_context::service(owner)
|
||||
{
|
||||
}
|
||||
|
||||
io_context::service::~service()
|
||||
{
|
||||
}
|
||||
|
||||
void io_context::service::shutdown()
|
||||
{
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
shutdown_service();
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
void io_context::service::shutdown_service()
|
||||
{
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
void io_context::service::notify_fork(io_context::fork_event ev)
|
||||
{
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
fork_service(ev);
|
||||
#else // !defined(ASIO_NO_DEPRECATED)
|
||||
(void)ev;
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DEPRECATED)
|
||||
void io_context::service::fork_service(io_context::fork_event)
|
||||
{
|
||||
}
|
||||
#endif // !defined(ASIO_NO_DEPRECATED)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_IO_CONTEXT_IPP
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
//
|
||||
// impl/post.hpp
|
||||
// ~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_POST_HPP
|
||||
#define ASIO_IMPL_POST_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/detail/work_dispatcher.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
struct initiate_post
|
||||
{
|
||||
template <typename CompletionHandler>
|
||||
void operator()(ASIO_MOVE_ARG(CompletionHandler) handler) const
|
||||
{
|
||||
typedef typename decay<CompletionHandler>::type DecayedHandler;
|
||||
|
||||
typename associated_executor<DecayedHandler>::type ex(
|
||||
(get_associated_executor)(handler));
|
||||
|
||||
typename associated_allocator<DecayedHandler>::type alloc(
|
||||
(get_associated_allocator)(handler));
|
||||
|
||||
ex.post(ASIO_MOVE_CAST(CompletionHandler)(handler), alloc);
|
||||
}
|
||||
|
||||
template <typename CompletionHandler, typename Executor>
|
||||
void operator()(ASIO_MOVE_ARG(CompletionHandler) handler,
|
||||
ASIO_MOVE_ARG(Executor) ex) const
|
||||
{
|
||||
typedef typename decay<CompletionHandler>::type DecayedHandler;
|
||||
|
||||
typename associated_allocator<DecayedHandler>::type alloc(
|
||||
(get_associated_allocator)(handler));
|
||||
|
||||
ex.post(detail::work_dispatcher<DecayedHandler>(
|
||||
ASIO_MOVE_CAST(CompletionHandler)(handler)), alloc);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename CompletionToken>
|
||||
ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) post(
|
||||
ASIO_MOVE_ARG(CompletionToken) token)
|
||||
{
|
||||
return async_initiate<CompletionToken, void()>(
|
||||
detail::initiate_post(), token);
|
||||
}
|
||||
|
||||
template <typename Executor, typename CompletionToken>
|
||||
ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) post(
|
||||
const Executor& ex, ASIO_MOVE_ARG(CompletionToken) token,
|
||||
typename enable_if<is_executor<Executor>::value>::type*)
|
||||
{
|
||||
return async_initiate<CompletionToken, void()>(
|
||||
detail::initiate_post(), token, ex);
|
||||
}
|
||||
|
||||
template <typename ExecutionContext, typename CompletionToken>
|
||||
inline ASIO_INITFN_RESULT_TYPE(CompletionToken, void()) post(
|
||||
ExecutionContext& ctx, ASIO_MOVE_ARG(CompletionToken) token,
|
||||
typename enable_if<is_convertible<
|
||||
ExecutionContext&, execution_context&>::value>::type*)
|
||||
{
|
||||
return (post)(ctx.get_executor(),
|
||||
ASIO_MOVE_CAST(CompletionToken)(token));
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_POST_HPP
|
||||
+1079
File diff suppressed because it is too large
Load Diff
+655
@@ -0,0 +1,655 @@
|
||||
//
|
||||
// impl/read_at.hpp
|
||||
// ~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_READ_AT_HPP
|
||||
#define ASIO_IMPL_READ_AT_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include <algorithm>
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/buffer.hpp"
|
||||
#include "asio/completion_condition.hpp"
|
||||
#include "asio/detail/array_fwd.hpp"
|
||||
#include "asio/detail/base_from_completion_cond.hpp"
|
||||
#include "asio/detail/bind_handler.hpp"
|
||||
#include "asio/detail/consuming_buffers.hpp"
|
||||
#include "asio/detail/dependent_type.hpp"
|
||||
#include "asio/detail/handler_alloc_helpers.hpp"
|
||||
#include "asio/detail/handler_cont_helpers.hpp"
|
||||
#include "asio/detail/handler_invoke_helpers.hpp"
|
||||
#include "asio/detail/handler_type_requirements.hpp"
|
||||
#include "asio/detail/non_const_lvalue.hpp"
|
||||
#include "asio/detail/throw_error.hpp"
|
||||
#include "asio/error.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
|
||||
typename MutableBufferIterator, typename CompletionCondition>
|
||||
std::size_t read_at_buffer_sequence(SyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, const MutableBufferSequence& buffers,
|
||||
const MutableBufferIterator&, CompletionCondition completion_condition,
|
||||
asio::error_code& ec)
|
||||
{
|
||||
ec = asio::error_code();
|
||||
asio::detail::consuming_buffers<mutable_buffer,
|
||||
MutableBufferSequence, MutableBufferIterator> tmp(buffers);
|
||||
while (!tmp.empty())
|
||||
{
|
||||
if (std::size_t max_size = detail::adapt_completion_condition_result(
|
||||
completion_condition(ec, tmp.total_consumed())))
|
||||
{
|
||||
tmp.consume(d.read_some_at(offset + tmp.total_consumed(),
|
||||
tmp.prepare(max_size), ec));
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
return tmp.total_consumed();;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
|
||||
typename CompletionCondition>
|
||||
std::size_t read_at(SyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, const MutableBufferSequence& buffers,
|
||||
CompletionCondition completion_condition, asio::error_code& ec)
|
||||
{
|
||||
return detail::read_at_buffer_sequence(d, offset, buffers,
|
||||
asio::buffer_sequence_begin(buffers),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
|
||||
inline std::size_t read_at(SyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, const MutableBufferSequence& buffers)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = read_at(
|
||||
d, offset, buffers, transfer_all(), ec);
|
||||
asio::detail::throw_error(ec, "read_at");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
|
||||
inline std::size_t read_at(SyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, const MutableBufferSequence& buffers,
|
||||
asio::error_code& ec)
|
||||
{
|
||||
return read_at(d, offset, buffers, transfer_all(), ec);
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t read_at(SyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, const MutableBufferSequence& buffers,
|
||||
CompletionCondition completion_condition)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = read_at(d, offset, buffers,
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
asio::detail::throw_error(ec, "read_at");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_EXTENSIONS)
|
||||
#if !defined(ASIO_NO_IOSTREAM)
|
||||
|
||||
template <typename SyncRandomAccessReadDevice, typename Allocator,
|
||||
typename CompletionCondition>
|
||||
std::size_t read_at(SyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
CompletionCondition completion_condition, asio::error_code& ec)
|
||||
{
|
||||
ec = asio::error_code();
|
||||
std::size_t total_transferred = 0;
|
||||
std::size_t max_size = detail::adapt_completion_condition_result(
|
||||
completion_condition(ec, total_transferred));
|
||||
std::size_t bytes_available = read_size_helper(b, max_size);
|
||||
while (bytes_available > 0)
|
||||
{
|
||||
std::size_t bytes_transferred = d.read_some_at(
|
||||
offset + total_transferred, b.prepare(bytes_available), ec);
|
||||
b.commit(bytes_transferred);
|
||||
total_transferred += bytes_transferred;
|
||||
max_size = detail::adapt_completion_condition_result(
|
||||
completion_condition(ec, total_transferred));
|
||||
bytes_available = read_size_helper(b, max_size);
|
||||
}
|
||||
return total_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessReadDevice, typename Allocator>
|
||||
inline std::size_t read_at(SyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = read_at(
|
||||
d, offset, b, transfer_all(), ec);
|
||||
asio::detail::throw_error(ec, "read_at");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessReadDevice, typename Allocator>
|
||||
inline std::size_t read_at(SyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
asio::error_code& ec)
|
||||
{
|
||||
return read_at(d, offset, b, transfer_all(), ec);
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessReadDevice, typename Allocator,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t read_at(SyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
CompletionCondition completion_condition)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = read_at(d, offset, b,
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
asio::detail::throw_error(ec, "read_at");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
#endif // !defined(ASIO_NO_IOSTREAM)
|
||||
#endif // !defined(ASIO_NO_EXTENSIONS)
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename MutableBufferIterator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
class read_at_op
|
||||
: detail::base_from_completion_cond<CompletionCondition>
|
||||
{
|
||||
public:
|
||||
read_at_op(AsyncRandomAccessReadDevice& device,
|
||||
uint64_t offset, const MutableBufferSequence& buffers,
|
||||
CompletionCondition& completion_condition, ReadHandler& handler)
|
||||
: detail::base_from_completion_cond<
|
||||
CompletionCondition>(completion_condition),
|
||||
device_(device),
|
||||
offset_(offset),
|
||||
buffers_(buffers),
|
||||
start_(0),
|
||||
handler_(ASIO_MOVE_CAST(ReadHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
read_at_op(const read_at_op& other)
|
||||
: detail::base_from_completion_cond<CompletionCondition>(other),
|
||||
device_(other.device_),
|
||||
offset_(other.offset_),
|
||||
buffers_(other.buffers_),
|
||||
start_(other.start_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
read_at_op(read_at_op&& other)
|
||||
: detail::base_from_completion_cond<CompletionCondition>(
|
||||
ASIO_MOVE_CAST(detail::base_from_completion_cond<
|
||||
CompletionCondition>)(other)),
|
||||
device_(other.device_),
|
||||
offset_(other.offset_),
|
||||
buffers_(ASIO_MOVE_CAST(buffers_type)(other.buffers_)),
|
||||
start_(other.start_),
|
||||
handler_(ASIO_MOVE_CAST(ReadHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec,
|
||||
std::size_t bytes_transferred, int start = 0)
|
||||
{
|
||||
std::size_t max_size;
|
||||
switch (start_ = start)
|
||||
{
|
||||
case 1:
|
||||
max_size = this->check_for_completion(ec, buffers_.total_consumed());
|
||||
do
|
||||
{
|
||||
device_.async_read_some_at(
|
||||
offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
|
||||
ASIO_MOVE_CAST(read_at_op)(*this));
|
||||
return; default:
|
||||
buffers_.consume(bytes_transferred);
|
||||
if ((!ec && bytes_transferred == 0) || buffers_.empty())
|
||||
break;
|
||||
max_size = this->check_for_completion(ec, buffers_.total_consumed());
|
||||
} while (max_size > 0);
|
||||
|
||||
handler_(ec, buffers_.total_consumed());
|
||||
}
|
||||
}
|
||||
|
||||
//private:
|
||||
typedef asio::detail::consuming_buffers<mutable_buffer,
|
||||
MutableBufferSequence, MutableBufferIterator> buffers_type;
|
||||
|
||||
AsyncRandomAccessReadDevice& device_;
|
||||
uint64_t offset_;
|
||||
buffers_type buffers_;
|
||||
int start_;
|
||||
ReadHandler handler_;
|
||||
};
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename MutableBufferIterator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
|
||||
MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename MutableBufferIterator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
|
||||
MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename MutableBufferIterator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
|
||||
MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
return this_handler->start_ == 0 ? true
|
||||
: asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename MutableBufferIterator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
|
||||
MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename MutableBufferIterator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
|
||||
MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename MutableBufferIterator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline void start_read_at_buffer_sequence_op(AsyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, const MutableBufferSequence& buffers,
|
||||
const MutableBufferIterator&, CompletionCondition& completion_condition,
|
||||
ReadHandler& handler)
|
||||
{
|
||||
detail::read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
|
||||
MutableBufferIterator, CompletionCondition, ReadHandler>(
|
||||
d, offset, buffers, completion_condition, handler)(
|
||||
asio::error_code(), 0, 1);
|
||||
}
|
||||
|
||||
struct initiate_async_read_at_buffer_sequence
|
||||
{
|
||||
template <typename ReadHandler, typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename CompletionCondition>
|
||||
void operator()(ASIO_MOVE_ARG(ReadHandler) handler,
|
||||
AsyncRandomAccessReadDevice* d, uint64_t offset,
|
||||
const MutableBufferSequence& buffers,
|
||||
ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a ReadHandler.
|
||||
ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
||||
|
||||
non_const_lvalue<ReadHandler> handler2(handler);
|
||||
non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
|
||||
start_read_at_buffer_sequence_op(*d, offset, buffers,
|
||||
asio::buffer_sequence_begin(buffers),
|
||||
completion_cond2.value, handler2.value);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename MutableBufferIterator,
|
||||
typename CompletionCondition, typename ReadHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
|
||||
MutableBufferIterator, CompletionCondition, ReadHandler>,
|
||||
Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<ReadHandler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::read_at_op<AsyncRandomAccessReadDevice,
|
||||
MutableBufferSequence, MutableBufferIterator,
|
||||
CompletionCondition, ReadHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice,
|
||||
typename MutableBufferSequence, typename MutableBufferIterator,
|
||||
typename CompletionCondition, typename ReadHandler, typename Executor>
|
||||
struct associated_executor<
|
||||
detail::read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
|
||||
MutableBufferIterator, CompletionCondition, ReadHandler>,
|
||||
Executor>
|
||||
{
|
||||
typedef typename associated_executor<ReadHandler, Executor>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::read_at_op<AsyncRandomAccessReadDevice,
|
||||
MutableBufferSequence, MutableBufferIterator,
|
||||
CompletionCondition, ReadHandler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_read_at(AsyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, const MutableBufferSequence& buffers,
|
||||
CompletionCondition completion_condition,
|
||||
ASIO_MOVE_ARG(ReadHandler) handler)
|
||||
{
|
||||
return async_initiate<ReadHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_read_at_buffer_sequence(), handler, &d, offset,
|
||||
buffers, ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
|
||||
typename ReadHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_read_at(AsyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, const MutableBufferSequence& buffers,
|
||||
ASIO_MOVE_ARG(ReadHandler) handler)
|
||||
{
|
||||
return async_initiate<ReadHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_read_at_buffer_sequence(),
|
||||
handler, &d, offset, buffers, transfer_all());
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_EXTENSIONS)
|
||||
#if !defined(ASIO_NO_IOSTREAM)
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename AsyncRandomAccessReadDevice, typename Allocator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
class read_at_streambuf_op
|
||||
: detail::base_from_completion_cond<CompletionCondition>
|
||||
{
|
||||
public:
|
||||
read_at_streambuf_op(AsyncRandomAccessReadDevice& device,
|
||||
uint64_t offset, basic_streambuf<Allocator>& streambuf,
|
||||
CompletionCondition& completion_condition, ReadHandler& handler)
|
||||
: detail::base_from_completion_cond<
|
||||
CompletionCondition>(completion_condition),
|
||||
device_(device),
|
||||
offset_(offset),
|
||||
streambuf_(streambuf),
|
||||
start_(0),
|
||||
total_transferred_(0),
|
||||
handler_(ASIO_MOVE_CAST(ReadHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
read_at_streambuf_op(const read_at_streambuf_op& other)
|
||||
: detail::base_from_completion_cond<CompletionCondition>(other),
|
||||
device_(other.device_),
|
||||
offset_(other.offset_),
|
||||
streambuf_(other.streambuf_),
|
||||
start_(other.start_),
|
||||
total_transferred_(other.total_transferred_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
read_at_streambuf_op(read_at_streambuf_op&& other)
|
||||
: detail::base_from_completion_cond<CompletionCondition>(
|
||||
ASIO_MOVE_CAST(detail::base_from_completion_cond<
|
||||
CompletionCondition>)(other)),
|
||||
device_(other.device_),
|
||||
offset_(other.offset_),
|
||||
streambuf_(other.streambuf_),
|
||||
start_(other.start_),
|
||||
total_transferred_(other.total_transferred_),
|
||||
handler_(ASIO_MOVE_CAST(ReadHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec,
|
||||
std::size_t bytes_transferred, int start = 0)
|
||||
{
|
||||
std::size_t max_size, bytes_available;
|
||||
switch (start_ = start)
|
||||
{
|
||||
case 1:
|
||||
max_size = this->check_for_completion(ec, total_transferred_);
|
||||
bytes_available = read_size_helper(streambuf_, max_size);
|
||||
for (;;)
|
||||
{
|
||||
device_.async_read_some_at(offset_ + total_transferred_,
|
||||
streambuf_.prepare(bytes_available),
|
||||
ASIO_MOVE_CAST(read_at_streambuf_op)(*this));
|
||||
return; default:
|
||||
total_transferred_ += bytes_transferred;
|
||||
streambuf_.commit(bytes_transferred);
|
||||
max_size = this->check_for_completion(ec, total_transferred_);
|
||||
bytes_available = read_size_helper(streambuf_, max_size);
|
||||
if ((!ec && bytes_transferred == 0) || bytes_available == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
handler_(ec, static_cast<const std::size_t&>(total_transferred_));
|
||||
}
|
||||
}
|
||||
|
||||
//private:
|
||||
AsyncRandomAccessReadDevice& device_;
|
||||
uint64_t offset_;
|
||||
asio::basic_streambuf<Allocator>& streambuf_;
|
||||
int start_;
|
||||
std::size_t total_transferred_;
|
||||
ReadHandler handler_;
|
||||
};
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice, typename Allocator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
|
||||
CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice, typename Allocator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
|
||||
CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice, typename Allocator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
|
||||
CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
return this_handler->start_ == 0 ? true
|
||||
: asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncRandomAccessReadDevice,
|
||||
typename Allocator, typename CompletionCondition, typename ReadHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
|
||||
CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncRandomAccessReadDevice,
|
||||
typename Allocator, typename CompletionCondition, typename ReadHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
|
||||
CompletionCondition, ReadHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_read_at_streambuf
|
||||
{
|
||||
template <typename ReadHandler, typename AsyncRandomAccessReadDevice,
|
||||
typename Allocator, typename CompletionCondition>
|
||||
void operator()(ASIO_MOVE_ARG(ReadHandler) handler,
|
||||
AsyncRandomAccessReadDevice* d, uint64_t offset,
|
||||
basic_streambuf<Allocator>* b,
|
||||
ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a ReadHandler.
|
||||
ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
|
||||
|
||||
non_const_lvalue<ReadHandler> handler2(handler);
|
||||
non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
|
||||
read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
|
||||
CompletionCondition, typename decay<ReadHandler>::type>(
|
||||
*d, offset, *b, completion_cond2.value, handler2.value)(
|
||||
asio::error_code(), 0, 1);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice, typename Allocator,
|
||||
typename CompletionCondition, typename ReadHandler, typename Allocator1>
|
||||
struct associated_allocator<
|
||||
detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
|
||||
Allocator, CompletionCondition, ReadHandler>,
|
||||
Allocator1>
|
||||
{
|
||||
typedef typename associated_allocator<ReadHandler, Allocator1>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
|
||||
Allocator, CompletionCondition, ReadHandler>& h,
|
||||
const Allocator1& a = Allocator1()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<ReadHandler, Allocator1>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice, typename Executor,
|
||||
typename CompletionCondition, typename ReadHandler, typename Executor1>
|
||||
struct associated_executor<
|
||||
detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
|
||||
Executor, CompletionCondition, ReadHandler>,
|
||||
Executor1>
|
||||
{
|
||||
typedef typename associated_executor<ReadHandler, Executor1>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
|
||||
Executor, CompletionCondition, ReadHandler>& h,
|
||||
const Executor1& ex = Executor1()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<ReadHandler, Executor1>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice, typename Allocator,
|
||||
typename CompletionCondition, typename ReadHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_read_at(AsyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
CompletionCondition completion_condition,
|
||||
ASIO_MOVE_ARG(ReadHandler) handler)
|
||||
{
|
||||
return async_initiate<ReadHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_read_at_streambuf(), handler, &d, offset,
|
||||
&b, ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessReadDevice, typename Allocator,
|
||||
typename ReadHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(ReadHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_read_at(AsyncRandomAccessReadDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
ASIO_MOVE_ARG(ReadHandler) handler)
|
||||
{
|
||||
return async_initiate<ReadHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_read_at_streambuf(),
|
||||
handler, &d, offset, &b, transfer_all());
|
||||
}
|
||||
|
||||
#endif // !defined(ASIO_NO_IOSTREAM)
|
||||
#endif // !defined(ASIO_NO_EXTENSIONS)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_READ_AT_HPP
|
||||
+3004
File diff suppressed because it is too large
Load Diff
+372
@@ -0,0 +1,372 @@
|
||||
|
||||
// impl/redirect_error.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_REDIRECT_ERROR_HPP
|
||||
#define ASIO_IMPL_REDIRECT_ERROR_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/async_result.hpp"
|
||||
#include "asio/detail/handler_alloc_helpers.hpp"
|
||||
#include "asio/detail/handler_cont_helpers.hpp"
|
||||
#include "asio/detail/handler_invoke_helpers.hpp"
|
||||
#include "asio/detail/type_traits.hpp"
|
||||
#include "asio/detail/variadic_templates.hpp"
|
||||
#include "asio/system_error.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
// Class to adapt a redirect_error_t as a completion handler.
|
||||
template <typename Handler>
|
||||
class redirect_error_handler
|
||||
{
|
||||
public:
|
||||
typedef void result_type;
|
||||
|
||||
template <typename CompletionToken>
|
||||
redirect_error_handler(redirect_error_t<CompletionToken> e)
|
||||
: ec_(e.ec_),
|
||||
handler_(ASIO_MOVE_CAST(CompletionToken)(e.token_))
|
||||
{
|
||||
}
|
||||
|
||||
template <typename RedirectedHandler>
|
||||
redirect_error_handler(asio::error_code& ec,
|
||||
ASIO_MOVE_ARG(RedirectedHandler) h)
|
||||
: ec_(ec),
|
||||
handler_(ASIO_MOVE_CAST(RedirectedHandler)(h))
|
||||
{
|
||||
}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
handler_();
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Arg, typename... Args>
|
||||
typename enable_if<
|
||||
!is_same<typename decay<Arg>::type, asio::error_code>::value
|
||||
>::type
|
||||
operator()(ASIO_MOVE_ARG(Arg) arg, ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
handler_(ASIO_MOVE_CAST(Arg)(arg),
|
||||
ASIO_MOVE_CAST(Args)(args)...);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void operator()(const asio::error_code& ec,
|
||||
ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
ec_ = ec;
|
||||
handler_(ASIO_MOVE_CAST(Args)(args)...);
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Arg>
|
||||
typename enable_if<
|
||||
!is_same<typename decay<Arg>::type, asio::error_code>::value
|
||||
>::type
|
||||
operator()(ASIO_MOVE_ARG(Arg) arg)
|
||||
{
|
||||
handler_(ASIO_MOVE_CAST(Arg)(arg));
|
||||
}
|
||||
|
||||
void operator()(const asio::error_code& ec)
|
||||
{
|
||||
ec_ = ec;
|
||||
handler_();
|
||||
}
|
||||
|
||||
#define ASIO_PRIVATE_REDIRECT_ERROR_DEF(n) \
|
||||
template <typename Arg, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
typename enable_if< \
|
||||
!is_same<typename decay<Arg>::type, asio::error_code>::value \
|
||||
>::type \
|
||||
operator()(ASIO_MOVE_ARG(Arg) arg, ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
handler_(ASIO_MOVE_CAST(Arg)(arg), \
|
||||
ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
} \
|
||||
\
|
||||
template <ASIO_VARIADIC_TPARAMS(n)> \
|
||||
void operator()(const asio::error_code& ec, \
|
||||
ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
ec_ = ec; \
|
||||
handler_(ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_REDIRECT_ERROR_DEF)
|
||||
#undef ASIO_PRIVATE_REDIRECT_ERROR_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
//private:
|
||||
asio::error_code& ec_;
|
||||
Handler handler_;
|
||||
};
|
||||
|
||||
template <typename Handler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
redirect_error_handler<Handler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Handler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
redirect_error_handler<Handler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Handler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
redirect_error_handler<Handler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Handler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
redirect_error_handler<Handler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Handler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
redirect_error_handler<Handler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Signature>
|
||||
struct redirect_error_signature
|
||||
{
|
||||
typedef Signature type;
|
||||
};
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename R, typename... Args>
|
||||
struct redirect_error_signature<R(asio::error_code, Args...)>
|
||||
{
|
||||
typedef R type(Args...);
|
||||
};
|
||||
|
||||
template <typename R, typename... Args>
|
||||
struct redirect_error_signature<R(const asio::error_code&, Args...)>
|
||||
{
|
||||
typedef R type(Args...);
|
||||
};
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename R>
|
||||
struct redirect_error_signature<R(asio::error_code)>
|
||||
{
|
||||
typedef R type();
|
||||
};
|
||||
|
||||
template <typename R>
|
||||
struct redirect_error_signature<R(const asio::error_code&)>
|
||||
{
|
||||
typedef R type();
|
||||
};
|
||||
|
||||
#define ASIO_PRIVATE_REDIRECT_ERROR_DEF(n) \
|
||||
template <typename R, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
struct redirect_error_signature< \
|
||||
R(asio::error_code, ASIO_VARIADIC_TARGS(n))> \
|
||||
{ \
|
||||
typedef R type(ASIO_VARIADIC_TARGS(n)); \
|
||||
}; \
|
||||
\
|
||||
template <typename R, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
struct redirect_error_signature< \
|
||||
R(const asio::error_code&, ASIO_VARIADIC_TARGS(n))> \
|
||||
{ \
|
||||
typedef R type(ASIO_VARIADIC_TARGS(n)); \
|
||||
}; \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_REDIRECT_ERROR_DEF)
|
||||
#undef ASIO_PRIVATE_REDIRECT_ERROR_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename CompletionToken, typename Signature>
|
||||
struct async_result<redirect_error_t<CompletionToken>, Signature>
|
||||
{
|
||||
typedef typename async_result<CompletionToken,
|
||||
typename detail::redirect_error_signature<Signature>::type>
|
||||
::return_type return_type;
|
||||
|
||||
template <typename Initiation>
|
||||
struct init_wrapper
|
||||
{
|
||||
template <typename Init>
|
||||
init_wrapper(asio::error_code& ec, ASIO_MOVE_ARG(Init) init)
|
||||
: ec_(ec),
|
||||
initiation_(ASIO_MOVE_CAST(Init)(init))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Handler, typename... Args>
|
||||
void operator()(
|
||||
ASIO_MOVE_ARG(Handler) handler,
|
||||
ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
ASIO_MOVE_CAST(Initiation)(initiation_)(
|
||||
detail::redirect_error_handler<
|
||||
typename decay<Handler>::type>(
|
||||
ec_, ASIO_MOVE_CAST(Handler)(handler)),
|
||||
ASIO_MOVE_CAST(Args)(args)...);
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Handler>
|
||||
void operator()(
|
||||
ASIO_MOVE_ARG(Handler) handler)
|
||||
{
|
||||
ASIO_MOVE_CAST(Initiation)(initiation_)(
|
||||
detail::redirect_error_handler<
|
||||
typename decay<Handler>::type>(
|
||||
ec_, ASIO_MOVE_CAST(Handler)(handler)));
|
||||
}
|
||||
|
||||
#define ASIO_PRIVATE_INIT_WRAPPER_DEF(n) \
|
||||
template <typename Handler, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
void operator()( \
|
||||
ASIO_MOVE_ARG(Handler) handler, \
|
||||
ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
ASIO_MOVE_CAST(Initiation)(initiation_)( \
|
||||
detail::redirect_error_handler< \
|
||||
typename decay<Handler>::type>( \
|
||||
ec_, ASIO_MOVE_CAST(Handler)(handler)), \
|
||||
ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_INIT_WRAPPER_DEF)
|
||||
#undef ASIO_PRIVATE_INIT_WRAPPER_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
asio::error_code& ec_;
|
||||
Initiation initiation_;
|
||||
};
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Initiation, typename RawCompletionToken, typename... Args>
|
||||
static return_type initiate(
|
||||
ASIO_MOVE_ARG(Initiation) initiation,
|
||||
ASIO_MOVE_ARG(RawCompletionToken) token,
|
||||
ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
return async_initiate<CompletionToken,
|
||||
typename detail::redirect_error_signature<Signature>::type>(
|
||||
init_wrapper<typename decay<Initiation>::type>(
|
||||
token.ec_, ASIO_MOVE_CAST(Initiation)(initiation)),
|
||||
token.token_, ASIO_MOVE_CAST(Args)(args)...);
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Initiation, typename RawCompletionToken>
|
||||
static return_type initiate(
|
||||
ASIO_MOVE_ARG(Initiation) initiation,
|
||||
ASIO_MOVE_ARG(RawCompletionToken) token)
|
||||
{
|
||||
return async_initiate<CompletionToken,
|
||||
typename detail::redirect_error_signature<Signature>::type>(
|
||||
init_wrapper<typename decay<Initiation>::type>(
|
||||
token.ec_, ASIO_MOVE_CAST(Initiation)(initiation)),
|
||||
token.token_);
|
||||
}
|
||||
|
||||
#define ASIO_PRIVATE_INITIATE_DEF(n) \
|
||||
template <typename Initiation, typename RawCompletionToken, \
|
||||
ASIO_VARIADIC_TPARAMS(n)> \
|
||||
static return_type initiate( \
|
||||
ASIO_MOVE_ARG(Initiation) initiation, \
|
||||
ASIO_MOVE_ARG(RawCompletionToken) token, \
|
||||
ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
return async_initiate<CompletionToken, \
|
||||
typename detail::redirect_error_signature<Signature>::type>( \
|
||||
init_wrapper<typename decay<Initiation>::type>( \
|
||||
token.ec_, ASIO_MOVE_CAST(Initiation)(initiation)), \
|
||||
token.token_, ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_INITIATE_DEF)
|
||||
#undef ASIO_PRIVATE_INITIATE_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
};
|
||||
|
||||
template <typename Handler, typename Executor>
|
||||
struct associated_executor<detail::redirect_error_handler<Handler>, Executor>
|
||||
{
|
||||
typedef typename associated_executor<Handler, Executor>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::redirect_error_handler<Handler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<Handler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Handler, typename Allocator>
|
||||
struct associated_allocator<detail::redirect_error_handler<Handler>, Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<Handler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::redirect_error_handler<Handler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<Handler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_REDIRECT_ERROR_HPP
|
||||
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// impl/serial_port_base.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
// Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_SERIAL_PORT_BASE_HPP
|
||||
#define ASIO_IMPL_SERIAL_PORT_BASE_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
inline serial_port_base::baud_rate::baud_rate(unsigned int rate)
|
||||
: value_(rate)
|
||||
{
|
||||
}
|
||||
|
||||
inline unsigned int serial_port_base::baud_rate::value() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
inline serial_port_base::flow_control::type
|
||||
serial_port_base::flow_control::value() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
inline serial_port_base::parity::type serial_port_base::parity::value() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
inline serial_port_base::stop_bits::type
|
||||
serial_port_base::stop_bits::value() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
inline unsigned int serial_port_base::character_size::value() const
|
||||
{
|
||||
return value_;
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_SERIAL_PORT_BASE_HPP
|
||||
+554
@@ -0,0 +1,554 @@
|
||||
//
|
||||
// impl/serial_port_base.ipp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
// Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_SERIAL_PORT_BASE_IPP
|
||||
#define ASIO_IMPL_SERIAL_PORT_BASE_IPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
|
||||
#if defined(ASIO_HAS_SERIAL_PORT)
|
||||
|
||||
#include <stdexcept>
|
||||
#include "asio/error.hpp"
|
||||
#include "asio/serial_port_base.hpp"
|
||||
#include "asio/detail/throw_exception.hpp"
|
||||
|
||||
#if defined(GENERATING_DOCUMENTATION)
|
||||
# define ASIO_OPTION_STORAGE implementation_defined
|
||||
#elif defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
# define ASIO_OPTION_STORAGE DCB
|
||||
#else
|
||||
# define ASIO_OPTION_STORAGE termios
|
||||
#endif
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::baud_rate::store(
|
||||
ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
storage.BaudRate = value_;
|
||||
#else
|
||||
speed_t baud;
|
||||
switch (value_)
|
||||
{
|
||||
// Do POSIX-specified rates first.
|
||||
case 0: baud = B0; break;
|
||||
case 50: baud = B50; break;
|
||||
case 75: baud = B75; break;
|
||||
case 110: baud = B110; break;
|
||||
case 134: baud = B134; break;
|
||||
case 150: baud = B150; break;
|
||||
case 200: baud = B200; break;
|
||||
case 300: baud = B300; break;
|
||||
case 600: baud = B600; break;
|
||||
case 1200: baud = B1200; break;
|
||||
case 1800: baud = B1800; break;
|
||||
case 2400: baud = B2400; break;
|
||||
case 4800: baud = B4800; break;
|
||||
case 9600: baud = B9600; break;
|
||||
case 19200: baud = B19200; break;
|
||||
case 38400: baud = B38400; break;
|
||||
// And now the extended ones conditionally.
|
||||
# ifdef B7200
|
||||
case 7200: baud = B7200; break;
|
||||
# endif
|
||||
# ifdef B14400
|
||||
case 14400: baud = B14400; break;
|
||||
# endif
|
||||
# ifdef B57600
|
||||
case 57600: baud = B57600; break;
|
||||
# endif
|
||||
# ifdef B115200
|
||||
case 115200: baud = B115200; break;
|
||||
# endif
|
||||
# ifdef B230400
|
||||
case 230400: baud = B230400; break;
|
||||
# endif
|
||||
# ifdef B460800
|
||||
case 460800: baud = B460800; break;
|
||||
# endif
|
||||
# ifdef B500000
|
||||
case 500000: baud = B500000; break;
|
||||
# endif
|
||||
# ifdef B576000
|
||||
case 576000: baud = B576000; break;
|
||||
# endif
|
||||
# ifdef B921600
|
||||
case 921600: baud = B921600; break;
|
||||
# endif
|
||||
# ifdef B1000000
|
||||
case 1000000: baud = B1000000; break;
|
||||
# endif
|
||||
# ifdef B1152000
|
||||
case 1152000: baud = B1152000; break;
|
||||
# endif
|
||||
# ifdef B2000000
|
||||
case 2000000: baud = B2000000; break;
|
||||
# endif
|
||||
# ifdef B3000000
|
||||
case 3000000: baud = B3000000; break;
|
||||
# endif
|
||||
# ifdef B3500000
|
||||
case 3500000: baud = B3500000; break;
|
||||
# endif
|
||||
# ifdef B4000000
|
||||
case 4000000: baud = B4000000; break;
|
||||
# endif
|
||||
default:
|
||||
ec = asio::error::invalid_argument;
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
|
||||
::cfsetspeed(&storage, baud);
|
||||
# else
|
||||
::cfsetispeed(&storage, baud);
|
||||
::cfsetospeed(&storage, baud);
|
||||
# endif
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::baud_rate::load(
|
||||
const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
value_ = storage.BaudRate;
|
||||
#else
|
||||
speed_t baud = ::cfgetospeed(&storage);
|
||||
switch (baud)
|
||||
{
|
||||
// First do those specified by POSIX.
|
||||
case B0: value_ = 0; break;
|
||||
case B50: value_ = 50; break;
|
||||
case B75: value_ = 75; break;
|
||||
case B110: value_ = 110; break;
|
||||
case B134: value_ = 134; break;
|
||||
case B150: value_ = 150; break;
|
||||
case B200: value_ = 200; break;
|
||||
case B300: value_ = 300; break;
|
||||
case B600: value_ = 600; break;
|
||||
case B1200: value_ = 1200; break;
|
||||
case B1800: value_ = 1800; break;
|
||||
case B2400: value_ = 2400; break;
|
||||
case B4800: value_ = 4800; break;
|
||||
case B9600: value_ = 9600; break;
|
||||
case B19200: value_ = 19200; break;
|
||||
case B38400: value_ = 38400; break;
|
||||
// Now conditionally handle a bunch of extended rates.
|
||||
# ifdef B7200
|
||||
case B7200: value_ = 7200; break;
|
||||
# endif
|
||||
# ifdef B14400
|
||||
case B14400: value_ = 14400; break;
|
||||
# endif
|
||||
# ifdef B57600
|
||||
case B57600: value_ = 57600; break;
|
||||
# endif
|
||||
# ifdef B115200
|
||||
case B115200: value_ = 115200; break;
|
||||
# endif
|
||||
# ifdef B230400
|
||||
case B230400: value_ = 230400; break;
|
||||
# endif
|
||||
# ifdef B460800
|
||||
case B460800: value_ = 460800; break;
|
||||
# endif
|
||||
# ifdef B500000
|
||||
case B500000: value_ = 500000; break;
|
||||
# endif
|
||||
# ifdef B576000
|
||||
case B576000: value_ = 576000; break;
|
||||
# endif
|
||||
# ifdef B921600
|
||||
case B921600: value_ = 921600; break;
|
||||
# endif
|
||||
# ifdef B1000000
|
||||
case B1000000: value_ = 1000000; break;
|
||||
# endif
|
||||
# ifdef B1152000
|
||||
case B1152000: value_ = 1152000; break;
|
||||
# endif
|
||||
# ifdef B2000000
|
||||
case B2000000: value_ = 2000000; break;
|
||||
# endif
|
||||
# ifdef B3000000
|
||||
case B3000000: value_ = 3000000; break;
|
||||
# endif
|
||||
# ifdef B3500000
|
||||
case B3500000: value_ = 3500000; break;
|
||||
# endif
|
||||
# ifdef B4000000
|
||||
case B4000000: value_ = 4000000; break;
|
||||
# endif
|
||||
default:
|
||||
value_ = 0;
|
||||
ec = asio::error::invalid_argument;
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
serial_port_base::flow_control::flow_control(
|
||||
serial_port_base::flow_control::type t)
|
||||
: value_(t)
|
||||
{
|
||||
if (t != none && t != software && t != hardware)
|
||||
{
|
||||
std::out_of_range ex("invalid flow_control value");
|
||||
asio::detail::throw_exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::flow_control::store(
|
||||
ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
storage.fOutxCtsFlow = FALSE;
|
||||
storage.fOutxDsrFlow = FALSE;
|
||||
storage.fTXContinueOnXoff = TRUE;
|
||||
storage.fDtrControl = DTR_CONTROL_ENABLE;
|
||||
storage.fDsrSensitivity = FALSE;
|
||||
storage.fOutX = FALSE;
|
||||
storage.fInX = FALSE;
|
||||
storage.fRtsControl = RTS_CONTROL_ENABLE;
|
||||
switch (value_)
|
||||
{
|
||||
case none:
|
||||
break;
|
||||
case software:
|
||||
storage.fOutX = TRUE;
|
||||
storage.fInX = TRUE;
|
||||
break;
|
||||
case hardware:
|
||||
storage.fOutxCtsFlow = TRUE;
|
||||
storage.fRtsControl = RTS_CONTROL_HANDSHAKE;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#else
|
||||
switch (value_)
|
||||
{
|
||||
case none:
|
||||
storage.c_iflag &= ~(IXOFF | IXON);
|
||||
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
|
||||
storage.c_cflag &= ~CRTSCTS;
|
||||
# elif defined(__QNXNTO__)
|
||||
storage.c_cflag &= ~(IHFLOW | OHFLOW);
|
||||
# endif
|
||||
break;
|
||||
case software:
|
||||
storage.c_iflag |= IXOFF | IXON;
|
||||
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
|
||||
storage.c_cflag &= ~CRTSCTS;
|
||||
# elif defined(__QNXNTO__)
|
||||
storage.c_cflag &= ~(IHFLOW | OHFLOW);
|
||||
# endif
|
||||
break;
|
||||
case hardware:
|
||||
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
|
||||
storage.c_iflag &= ~(IXOFF | IXON);
|
||||
storage.c_cflag |= CRTSCTS;
|
||||
break;
|
||||
# elif defined(__QNXNTO__)
|
||||
storage.c_iflag &= ~(IXOFF | IXON);
|
||||
storage.c_cflag |= (IHFLOW | OHFLOW);
|
||||
break;
|
||||
# else
|
||||
ec = asio::error::operation_not_supported;
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
# endif
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::flow_control::load(
|
||||
const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
if (storage.fOutX && storage.fInX)
|
||||
{
|
||||
value_ = software;
|
||||
}
|
||||
else if (storage.fOutxCtsFlow && storage.fRtsControl == RTS_CONTROL_HANDSHAKE)
|
||||
{
|
||||
value_ = hardware;
|
||||
}
|
||||
else
|
||||
{
|
||||
value_ = none;
|
||||
}
|
||||
#else
|
||||
if (storage.c_iflag & (IXOFF | IXON))
|
||||
{
|
||||
value_ = software;
|
||||
}
|
||||
# if defined(_BSD_SOURCE) || defined(_DEFAULT_SOURCE)
|
||||
else if (storage.c_cflag & CRTSCTS)
|
||||
{
|
||||
value_ = hardware;
|
||||
}
|
||||
# elif defined(__QNXNTO__)
|
||||
else if (storage.c_cflag & IHFLOW && storage.c_cflag & OHFLOW)
|
||||
{
|
||||
value_ = hardware;
|
||||
}
|
||||
# endif
|
||||
else
|
||||
{
|
||||
value_ = none;
|
||||
}
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
serial_port_base::parity::parity(serial_port_base::parity::type t)
|
||||
: value_(t)
|
||||
{
|
||||
if (t != none && t != odd && t != even)
|
||||
{
|
||||
std::out_of_range ex("invalid parity value");
|
||||
asio::detail::throw_exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::parity::store(
|
||||
ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
switch (value_)
|
||||
{
|
||||
case none:
|
||||
storage.fParity = FALSE;
|
||||
storage.Parity = NOPARITY;
|
||||
break;
|
||||
case odd:
|
||||
storage.fParity = TRUE;
|
||||
storage.Parity = ODDPARITY;
|
||||
break;
|
||||
case even:
|
||||
storage.fParity = TRUE;
|
||||
storage.Parity = EVENPARITY;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#else
|
||||
switch (value_)
|
||||
{
|
||||
case none:
|
||||
storage.c_iflag |= IGNPAR;
|
||||
storage.c_cflag &= ~(PARENB | PARODD);
|
||||
break;
|
||||
case even:
|
||||
storage.c_iflag &= ~(IGNPAR | PARMRK);
|
||||
storage.c_iflag |= INPCK;
|
||||
storage.c_cflag |= PARENB;
|
||||
storage.c_cflag &= ~PARODD;
|
||||
break;
|
||||
case odd:
|
||||
storage.c_iflag &= ~(IGNPAR | PARMRK);
|
||||
storage.c_iflag |= INPCK;
|
||||
storage.c_cflag |= (PARENB | PARODD);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::parity::load(
|
||||
const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
if (storage.Parity == EVENPARITY)
|
||||
{
|
||||
value_ = even;
|
||||
}
|
||||
else if (storage.Parity == ODDPARITY)
|
||||
{
|
||||
value_ = odd;
|
||||
}
|
||||
else
|
||||
{
|
||||
value_ = none;
|
||||
}
|
||||
#else
|
||||
if (storage.c_cflag & PARENB)
|
||||
{
|
||||
if (storage.c_cflag & PARODD)
|
||||
{
|
||||
value_ = odd;
|
||||
}
|
||||
else
|
||||
{
|
||||
value_ = even;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
value_ = none;
|
||||
}
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
serial_port_base::stop_bits::stop_bits(
|
||||
serial_port_base::stop_bits::type t)
|
||||
: value_(t)
|
||||
{
|
||||
if (t != one && t != onepointfive && t != two)
|
||||
{
|
||||
std::out_of_range ex("invalid stop_bits value");
|
||||
asio::detail::throw_exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::stop_bits::store(
|
||||
ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
switch (value_)
|
||||
{
|
||||
case one:
|
||||
storage.StopBits = ONESTOPBIT;
|
||||
break;
|
||||
case onepointfive:
|
||||
storage.StopBits = ONE5STOPBITS;
|
||||
break;
|
||||
case two:
|
||||
storage.StopBits = TWOSTOPBITS;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#else
|
||||
switch (value_)
|
||||
{
|
||||
case one:
|
||||
storage.c_cflag &= ~CSTOPB;
|
||||
break;
|
||||
case two:
|
||||
storage.c_cflag |= CSTOPB;
|
||||
break;
|
||||
default:
|
||||
ec = asio::error::operation_not_supported;
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::stop_bits::load(
|
||||
const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
if (storage.StopBits == ONESTOPBIT)
|
||||
{
|
||||
value_ = one;
|
||||
}
|
||||
else if (storage.StopBits == ONE5STOPBITS)
|
||||
{
|
||||
value_ = onepointfive;
|
||||
}
|
||||
else if (storage.StopBits == TWOSTOPBITS)
|
||||
{
|
||||
value_ = two;
|
||||
}
|
||||
else
|
||||
{
|
||||
value_ = one;
|
||||
}
|
||||
#else
|
||||
value_ = (storage.c_cflag & CSTOPB) ? two : one;
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
serial_port_base::character_size::character_size(unsigned int t)
|
||||
: value_(t)
|
||||
{
|
||||
if (t < 5 || t > 8)
|
||||
{
|
||||
std::out_of_range ex("invalid character_size value");
|
||||
asio::detail::throw_exception(ex);
|
||||
}
|
||||
}
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::character_size::store(
|
||||
ASIO_OPTION_STORAGE& storage, asio::error_code& ec) const
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
storage.ByteSize = value_;
|
||||
#else
|
||||
storage.c_cflag &= ~CSIZE;
|
||||
switch (value_)
|
||||
{
|
||||
case 5: storage.c_cflag |= CS5; break;
|
||||
case 6: storage.c_cflag |= CS6; break;
|
||||
case 7: storage.c_cflag |= CS7; break;
|
||||
case 8: storage.c_cflag |= CS8; break;
|
||||
default: break;
|
||||
}
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
ASIO_SYNC_OP_VOID serial_port_base::character_size::load(
|
||||
const ASIO_OPTION_STORAGE& storage, asio::error_code& ec)
|
||||
{
|
||||
#if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
|
||||
value_ = storage.ByteSize;
|
||||
#else
|
||||
if ((storage.c_cflag & CSIZE) == CS5) { value_ = 5; }
|
||||
else if ((storage.c_cflag & CSIZE) == CS6) { value_ = 6; }
|
||||
else if ((storage.c_cflag & CSIZE) == CS7) { value_ = 7; }
|
||||
else if ((storage.c_cflag & CSIZE) == CS8) { value_ = 8; }
|
||||
else
|
||||
{
|
||||
// Hmmm, use 8 for now.
|
||||
value_ = 8;
|
||||
}
|
||||
#endif
|
||||
ec = asio::error_code();
|
||||
ASIO_SYNC_OP_VOID_RETURN(ec);
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#undef ASIO_OPTION_STORAGE
|
||||
|
||||
#endif // defined(ASIO_HAS_SERIAL_PORT)
|
||||
|
||||
#endif // ASIO_IMPL_SERIAL_PORT_BASE_IPP
|
||||
+490
@@ -0,0 +1,490 @@
|
||||
//
|
||||
// impl/spawn.hpp
|
||||
// ~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_SPAWN_HPP
|
||||
#define ASIO_IMPL_SPAWN_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/async_result.hpp"
|
||||
#include "asio/bind_executor.hpp"
|
||||
#include "asio/detail/atomic_count.hpp"
|
||||
#include "asio/detail/handler_alloc_helpers.hpp"
|
||||
#include "asio/detail/handler_cont_helpers.hpp"
|
||||
#include "asio/detail/handler_invoke_helpers.hpp"
|
||||
#include "asio/detail/memory.hpp"
|
||||
#include "asio/detail/noncopyable.hpp"
|
||||
#include "asio/detail/type_traits.hpp"
|
||||
#include "asio/system_error.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
template <typename Handler, typename T>
|
||||
class coro_handler
|
||||
{
|
||||
public:
|
||||
coro_handler(basic_yield_context<Handler> ctx)
|
||||
: coro_(ctx.coro_.lock()),
|
||||
ca_(ctx.ca_),
|
||||
handler_(ctx.handler_),
|
||||
ready_(0),
|
||||
ec_(ctx.ec_),
|
||||
value_(0)
|
||||
{
|
||||
}
|
||||
|
||||
void operator()(T value)
|
||||
{
|
||||
*ec_ = asio::error_code();
|
||||
*value_ = ASIO_MOVE_CAST(T)(value);
|
||||
if (--*ready_ == 0)
|
||||
(*coro_)();
|
||||
}
|
||||
|
||||
void operator()(asio::error_code ec, T value)
|
||||
{
|
||||
*ec_ = ec;
|
||||
*value_ = ASIO_MOVE_CAST(T)(value);
|
||||
if (--*ready_ == 0)
|
||||
(*coro_)();
|
||||
}
|
||||
|
||||
//private:
|
||||
shared_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
|
||||
typename basic_yield_context<Handler>::caller_type& ca_;
|
||||
Handler handler_;
|
||||
atomic_count* ready_;
|
||||
asio::error_code* ec_;
|
||||
T* value_;
|
||||
};
|
||||
|
||||
template <typename Handler>
|
||||
class coro_handler<Handler, void>
|
||||
{
|
||||
public:
|
||||
coro_handler(basic_yield_context<Handler> ctx)
|
||||
: coro_(ctx.coro_.lock()),
|
||||
ca_(ctx.ca_),
|
||||
handler_(ctx.handler_),
|
||||
ready_(0),
|
||||
ec_(ctx.ec_)
|
||||
{
|
||||
}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
*ec_ = asio::error_code();
|
||||
if (--*ready_ == 0)
|
||||
(*coro_)();
|
||||
}
|
||||
|
||||
void operator()(asio::error_code ec)
|
||||
{
|
||||
*ec_ = ec;
|
||||
if (--*ready_ == 0)
|
||||
(*coro_)();
|
||||
}
|
||||
|
||||
//private:
|
||||
shared_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
|
||||
typename basic_yield_context<Handler>::caller_type& ca_;
|
||||
Handler handler_;
|
||||
atomic_count* ready_;
|
||||
asio::error_code* ec_;
|
||||
};
|
||||
|
||||
template <typename Handler, typename T>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
coro_handler<Handler, T>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Handler, typename T>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
coro_handler<Handler, T>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Handler, typename T>
|
||||
inline bool asio_handler_is_continuation(coro_handler<Handler, T>*)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Function, typename Handler, typename T>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
coro_handler<Handler, T>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Handler, typename T>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
coro_handler<Handler, T>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Handler, typename T>
|
||||
class coro_async_result
|
||||
{
|
||||
public:
|
||||
typedef coro_handler<Handler, T> completion_handler_type;
|
||||
typedef T return_type;
|
||||
|
||||
explicit coro_async_result(completion_handler_type& h)
|
||||
: handler_(h),
|
||||
ca_(h.ca_),
|
||||
ready_(2)
|
||||
{
|
||||
h.ready_ = &ready_;
|
||||
out_ec_ = h.ec_;
|
||||
if (!out_ec_) h.ec_ = &ec_;
|
||||
h.value_ = &value_;
|
||||
}
|
||||
|
||||
return_type get()
|
||||
{
|
||||
// Must not hold shared_ptr to coro while suspended.
|
||||
handler_.coro_.reset();
|
||||
|
||||
if (--ready_ != 0)
|
||||
ca_();
|
||||
if (!out_ec_ && ec_) throw asio::system_error(ec_);
|
||||
return ASIO_MOVE_CAST(return_type)(value_);
|
||||
}
|
||||
|
||||
private:
|
||||
completion_handler_type& handler_;
|
||||
typename basic_yield_context<Handler>::caller_type& ca_;
|
||||
atomic_count ready_;
|
||||
asio::error_code* out_ec_;
|
||||
asio::error_code ec_;
|
||||
return_type value_;
|
||||
};
|
||||
|
||||
template <typename Handler>
|
||||
class coro_async_result<Handler, void>
|
||||
{
|
||||
public:
|
||||
typedef coro_handler<Handler, void> completion_handler_type;
|
||||
typedef void return_type;
|
||||
|
||||
explicit coro_async_result(completion_handler_type& h)
|
||||
: handler_(h),
|
||||
ca_(h.ca_),
|
||||
ready_(2)
|
||||
{
|
||||
h.ready_ = &ready_;
|
||||
out_ec_ = h.ec_;
|
||||
if (!out_ec_) h.ec_ = &ec_;
|
||||
}
|
||||
|
||||
void get()
|
||||
{
|
||||
// Must not hold shared_ptr to coro while suspended.
|
||||
handler_.coro_.reset();
|
||||
|
||||
if (--ready_ != 0)
|
||||
ca_();
|
||||
if (!out_ec_ && ec_) throw asio::system_error(ec_);
|
||||
}
|
||||
|
||||
private:
|
||||
completion_handler_type& handler_;
|
||||
typename basic_yield_context<Handler>::caller_type& ca_;
|
||||
atomic_count ready_;
|
||||
asio::error_code* out_ec_;
|
||||
asio::error_code ec_;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Handler, typename ReturnType>
|
||||
class async_result<basic_yield_context<Handler>, ReturnType()>
|
||||
: public detail::coro_async_result<Handler, void>
|
||||
{
|
||||
public:
|
||||
explicit async_result(
|
||||
typename detail::coro_async_result<Handler,
|
||||
void>::completion_handler_type& h)
|
||||
: detail::coro_async_result<Handler, void>(h)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Handler, typename ReturnType, typename Arg1>
|
||||
class async_result<basic_yield_context<Handler>, ReturnType(Arg1)>
|
||||
: public detail::coro_async_result<Handler, typename decay<Arg1>::type>
|
||||
{
|
||||
public:
|
||||
explicit async_result(
|
||||
typename detail::coro_async_result<Handler,
|
||||
typename decay<Arg1>::type>::completion_handler_type& h)
|
||||
: detail::coro_async_result<Handler, typename decay<Arg1>::type>(h)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Handler, typename ReturnType>
|
||||
class async_result<basic_yield_context<Handler>,
|
||||
ReturnType(asio::error_code)>
|
||||
: public detail::coro_async_result<Handler, void>
|
||||
{
|
||||
public:
|
||||
explicit async_result(
|
||||
typename detail::coro_async_result<Handler,
|
||||
void>::completion_handler_type& h)
|
||||
: detail::coro_async_result<Handler, void>(h)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Handler, typename ReturnType, typename Arg2>
|
||||
class async_result<basic_yield_context<Handler>,
|
||||
ReturnType(asio::error_code, Arg2)>
|
||||
: public detail::coro_async_result<Handler, typename decay<Arg2>::type>
|
||||
{
|
||||
public:
|
||||
explicit async_result(
|
||||
typename detail::coro_async_result<Handler,
|
||||
typename decay<Arg2>::type>::completion_handler_type& h)
|
||||
: detail::coro_async_result<Handler, typename decay<Arg2>::type>(h)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Handler, typename T, typename Allocator>
|
||||
struct associated_allocator<detail::coro_handler<Handler, T>, Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<Handler, Allocator>::type type;
|
||||
|
||||
static type get(const detail::coro_handler<Handler, T>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<Handler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Handler, typename T, typename Executor>
|
||||
struct associated_executor<detail::coro_handler<Handler, T>, Executor>
|
||||
{
|
||||
typedef typename associated_executor<Handler, Executor>::type type;
|
||||
|
||||
static type get(const detail::coro_handler<Handler, T>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<Handler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
|
||||
template <typename Handler, typename Function>
|
||||
struct spawn_data : private noncopyable
|
||||
{
|
||||
template <typename Hand, typename Func>
|
||||
spawn_data(ASIO_MOVE_ARG(Hand) handler,
|
||||
bool call_handler, ASIO_MOVE_ARG(Func) function)
|
||||
: handler_(ASIO_MOVE_CAST(Hand)(handler)),
|
||||
call_handler_(call_handler),
|
||||
function_(ASIO_MOVE_CAST(Func)(function))
|
||||
{
|
||||
}
|
||||
|
||||
weak_ptr<typename basic_yield_context<Handler>::callee_type> coro_;
|
||||
Handler handler_;
|
||||
bool call_handler_;
|
||||
Function function_;
|
||||
};
|
||||
|
||||
template <typename Handler, typename Function>
|
||||
struct coro_entry_point
|
||||
{
|
||||
void operator()(typename basic_yield_context<Handler>::caller_type& ca)
|
||||
{
|
||||
shared_ptr<spawn_data<Handler, Function> > data(data_);
|
||||
#if !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
|
||||
ca(); // Yield until coroutine pointer has been initialised.
|
||||
#endif // !defined(BOOST_COROUTINES_UNIDIRECT) && !defined(BOOST_COROUTINES_V2)
|
||||
const basic_yield_context<Handler> yield(
|
||||
data->coro_, ca, data->handler_);
|
||||
|
||||
(data->function_)(yield);
|
||||
if (data->call_handler_)
|
||||
(data->handler_)();
|
||||
}
|
||||
|
||||
shared_ptr<spawn_data<Handler, Function> > data_;
|
||||
};
|
||||
|
||||
template <typename Handler, typename Function>
|
||||
struct spawn_helper
|
||||
{
|
||||
void operator()()
|
||||
{
|
||||
typedef typename basic_yield_context<Handler>::callee_type callee_type;
|
||||
coro_entry_point<Handler, Function> entry_point = { data_ };
|
||||
shared_ptr<callee_type> coro(new callee_type(entry_point, attributes_));
|
||||
data_->coro_ = coro;
|
||||
(*coro)();
|
||||
}
|
||||
|
||||
shared_ptr<spawn_data<Handler, Function> > data_;
|
||||
boost::coroutines::attributes attributes_;
|
||||
};
|
||||
|
||||
template <typename Function, typename Handler, typename Function1>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
spawn_helper<Handler, Function1>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->data_->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Handler, typename Function1>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
spawn_helper<Handler, Function1>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->data_->handler_);
|
||||
}
|
||||
|
||||
inline void default_spawn_handler() {}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename Function>
|
||||
inline void spawn(ASIO_MOVE_ARG(Function) function,
|
||||
const boost::coroutines::attributes& attributes)
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
typename associated_executor<function_type>::type ex(
|
||||
(get_associated_executor)(function));
|
||||
|
||||
asio::spawn(ex, ASIO_MOVE_CAST(Function)(function), attributes);
|
||||
}
|
||||
|
||||
template <typename Handler, typename Function>
|
||||
void spawn(ASIO_MOVE_ARG(Handler) handler,
|
||||
ASIO_MOVE_ARG(Function) function,
|
||||
const boost::coroutines::attributes& attributes,
|
||||
typename enable_if<!is_executor<typename decay<Handler>::type>::value &&
|
||||
!is_convertible<Handler&, execution_context&>::value>::type*)
|
||||
{
|
||||
typedef typename decay<Handler>::type handler_type;
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
typename associated_executor<handler_type>::type ex(
|
||||
(get_associated_executor)(handler));
|
||||
|
||||
typename associated_allocator<handler_type>::type a(
|
||||
(get_associated_allocator)(handler));
|
||||
|
||||
detail::spawn_helper<handler_type, function_type> helper;
|
||||
helper.data_.reset(
|
||||
new detail::spawn_data<handler_type, function_type>(
|
||||
ASIO_MOVE_CAST(Handler)(handler), true,
|
||||
ASIO_MOVE_CAST(Function)(function)));
|
||||
helper.attributes_ = attributes;
|
||||
|
||||
ex.dispatch(helper, a);
|
||||
}
|
||||
|
||||
template <typename Handler, typename Function>
|
||||
void spawn(basic_yield_context<Handler> ctx,
|
||||
ASIO_MOVE_ARG(Function) function,
|
||||
const boost::coroutines::attributes& attributes)
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
Handler handler(ctx.handler_); // Explicit copy that might be moved from.
|
||||
|
||||
typename associated_executor<Handler>::type ex(
|
||||
(get_associated_executor)(handler));
|
||||
|
||||
typename associated_allocator<Handler>::type a(
|
||||
(get_associated_allocator)(handler));
|
||||
|
||||
detail::spawn_helper<Handler, function_type> helper;
|
||||
helper.data_.reset(
|
||||
new detail::spawn_data<Handler, function_type>(
|
||||
ASIO_MOVE_CAST(Handler)(handler), false,
|
||||
ASIO_MOVE_CAST(Function)(function)));
|
||||
helper.attributes_ = attributes;
|
||||
|
||||
ex.dispatch(helper, a);
|
||||
}
|
||||
|
||||
template <typename Function, typename Executor>
|
||||
inline void spawn(const Executor& ex,
|
||||
ASIO_MOVE_ARG(Function) function,
|
||||
const boost::coroutines::attributes& attributes,
|
||||
typename enable_if<is_executor<Executor>::value>::type*)
|
||||
{
|
||||
asio::spawn(asio::strand<Executor>(ex),
|
||||
ASIO_MOVE_CAST(Function)(function), attributes);
|
||||
}
|
||||
|
||||
template <typename Function, typename Executor>
|
||||
inline void spawn(const strand<Executor>& ex,
|
||||
ASIO_MOVE_ARG(Function) function,
|
||||
const boost::coroutines::attributes& attributes)
|
||||
{
|
||||
asio::spawn(asio::bind_executor(
|
||||
ex, &detail::default_spawn_handler),
|
||||
ASIO_MOVE_CAST(Function)(function), attributes);
|
||||
}
|
||||
|
||||
template <typename Function>
|
||||
inline void spawn(const asio::io_context::strand& s,
|
||||
ASIO_MOVE_ARG(Function) function,
|
||||
const boost::coroutines::attributes& attributes)
|
||||
{
|
||||
asio::spawn(asio::bind_executor(
|
||||
s, &detail::default_spawn_handler),
|
||||
ASIO_MOVE_CAST(Function)(function), attributes);
|
||||
}
|
||||
|
||||
template <typename Function, typename ExecutionContext>
|
||||
inline void spawn(ExecutionContext& ctx,
|
||||
ASIO_MOVE_ARG(Function) function,
|
||||
const boost::coroutines::attributes& attributes,
|
||||
typename enable_if<is_convertible<
|
||||
ExecutionContext&, execution_context&>::value>::type*)
|
||||
{
|
||||
asio::spawn(ctx.get_executor(),
|
||||
ASIO_MOVE_CAST(Function)(function), attributes);
|
||||
}
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_SPAWN_HPP
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// impl/src.cpp
|
||||
// ~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#if defined(_MSC_VER) \
|
||||
|| defined(__BORLANDC__) \
|
||||
|| defined(__DMC__)
|
||||
# pragma message ( \
|
||||
"This file is deprecated. " \
|
||||
"Please #include <asio/impl/src.hpp> instead.")
|
||||
#elif defined(__GNUC__) \
|
||||
|| defined(__HP_aCC) \
|
||||
|| defined(__SUNPRO_CC) \
|
||||
|| defined(__IBMCPP__)
|
||||
# warning "This file is deprecated."
|
||||
# warning "Please #include <asio/impl/src.hpp> instead."
|
||||
#endif
|
||||
|
||||
#include "asio/impl/src.hpp"
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// impl/src.hpp
|
||||
// ~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_SRC_HPP
|
||||
#define ASIO_IMPL_SRC_HPP
|
||||
|
||||
#define ASIO_SOURCE
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
|
||||
#if defined(ASIO_HEADER_ONLY)
|
||||
# error Do not compile Asio library source with ASIO_HEADER_ONLY defined
|
||||
#endif
|
||||
|
||||
#include "asio/impl/error.ipp"
|
||||
#include "asio/impl/error_code.ipp"
|
||||
#include "asio/impl/execution_context.ipp"
|
||||
#include "asio/impl/executor.ipp"
|
||||
#include "asio/impl/handler_alloc_hook.ipp"
|
||||
#include "asio/impl/io_context.ipp"
|
||||
#include "asio/impl/serial_port_base.ipp"
|
||||
#include "asio/impl/system_context.ipp"
|
||||
#include "asio/impl/thread_pool.ipp"
|
||||
#include "asio/detail/impl/buffer_sequence_adapter.ipp"
|
||||
#include "asio/detail/impl/descriptor_ops.ipp"
|
||||
#include "asio/detail/impl/dev_poll_reactor.ipp"
|
||||
#include "asio/detail/impl/epoll_reactor.ipp"
|
||||
#include "asio/detail/impl/eventfd_select_interrupter.ipp"
|
||||
#include "asio/detail/impl/handler_tracking.ipp"
|
||||
#include "asio/detail/impl/kqueue_reactor.ipp"
|
||||
#include "asio/detail/impl/null_event.ipp"
|
||||
#include "asio/detail/impl/pipe_select_interrupter.ipp"
|
||||
#include "asio/detail/impl/posix_event.ipp"
|
||||
#include "asio/detail/impl/posix_mutex.ipp"
|
||||
#include "asio/detail/impl/posix_thread.ipp"
|
||||
#include "asio/detail/impl/posix_tss_ptr.ipp"
|
||||
#include "asio/detail/impl/reactive_descriptor_service.ipp"
|
||||
#include "asio/detail/impl/reactive_serial_port_service.ipp"
|
||||
#include "asio/detail/impl/reactive_socket_service_base.ipp"
|
||||
#include "asio/detail/impl/resolver_service_base.ipp"
|
||||
#include "asio/detail/impl/scheduler.ipp"
|
||||
#include "asio/detail/impl/select_reactor.ipp"
|
||||
#include "asio/detail/impl/service_registry.ipp"
|
||||
#include "asio/detail/impl/signal_set_service.ipp"
|
||||
#include "asio/detail/impl/socket_ops.ipp"
|
||||
#include "asio/detail/impl/socket_select_interrupter.ipp"
|
||||
#include "asio/detail/impl/strand_executor_service.ipp"
|
||||
#include "asio/detail/impl/strand_service.ipp"
|
||||
#include "asio/detail/impl/throw_error.ipp"
|
||||
#include "asio/detail/impl/timer_queue_ptime.ipp"
|
||||
#include "asio/detail/impl/timer_queue_set.ipp"
|
||||
#include "asio/detail/impl/win_iocp_handle_service.ipp"
|
||||
#include "asio/detail/impl/win_iocp_io_context.ipp"
|
||||
#include "asio/detail/impl/win_iocp_serial_port_service.ipp"
|
||||
#include "asio/detail/impl/win_iocp_socket_service_base.ipp"
|
||||
#include "asio/detail/impl/win_event.ipp"
|
||||
#include "asio/detail/impl/win_mutex.ipp"
|
||||
#include "asio/detail/impl/win_object_handle_service.ipp"
|
||||
#include "asio/detail/impl/win_static_mutex.ipp"
|
||||
#include "asio/detail/impl/win_thread.ipp"
|
||||
#include "asio/detail/impl/win_tss_ptr.ipp"
|
||||
#include "asio/detail/impl/winrt_ssocket_service_base.ipp"
|
||||
#include "asio/detail/impl/winrt_timer_scheduler.ipp"
|
||||
#include "asio/detail/impl/winsock_init.ipp"
|
||||
#include "asio/generic/detail/impl/endpoint.ipp"
|
||||
#include "asio/ip/impl/address.ipp"
|
||||
#include "asio/ip/impl/address_v4.ipp"
|
||||
#include "asio/ip/impl/address_v6.ipp"
|
||||
#include "asio/ip/impl/host_name.ipp"
|
||||
#include "asio/ip/impl/network_v4.ipp"
|
||||
#include "asio/ip/impl/network_v6.ipp"
|
||||
#include "asio/ip/detail/impl/endpoint.ipp"
|
||||
#include "asio/local/detail/impl/endpoint.ipp"
|
||||
|
||||
#endif // ASIO_IMPL_SRC_HPP
|
||||
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// impl/system_context.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_SYSTEM_CONTEXT_HPP
|
||||
#define ASIO_IMPL_SYSTEM_CONTEXT_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/system_executor.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
inline system_context::executor_type
|
||||
system_context::get_executor() ASIO_NOEXCEPT
|
||||
{
|
||||
return system_executor();
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_SYSTEM_CONTEXT_HPP
|
||||
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// impl/system_context.ipp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_SYSTEM_CONTEXT_IPP
|
||||
#define ASIO_IMPL_SYSTEM_CONTEXT_IPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/system_context.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
struct system_context::thread_function
|
||||
{
|
||||
detail::scheduler* scheduler_;
|
||||
|
||||
void operator()()
|
||||
{
|
||||
asio::error_code ec;
|
||||
scheduler_->run(ec);
|
||||
}
|
||||
};
|
||||
|
||||
system_context::system_context()
|
||||
: scheduler_(add_scheduler(new detail::scheduler(*this, 0, false)))
|
||||
{
|
||||
scheduler_.work_started();
|
||||
|
||||
thread_function f = { &scheduler_ };
|
||||
std::size_t num_threads = detail::thread::hardware_concurrency() * 2;
|
||||
threads_.create_threads(f, num_threads ? num_threads : 2);
|
||||
}
|
||||
|
||||
system_context::~system_context()
|
||||
{
|
||||
scheduler_.work_finished();
|
||||
scheduler_.stop();
|
||||
threads_.join();
|
||||
}
|
||||
|
||||
void system_context::stop()
|
||||
{
|
||||
scheduler_.stop();
|
||||
}
|
||||
|
||||
bool system_context::stopped() const ASIO_NOEXCEPT
|
||||
{
|
||||
return scheduler_.stopped();
|
||||
}
|
||||
|
||||
void system_context::join()
|
||||
{
|
||||
scheduler_.work_finished();
|
||||
threads_.join();
|
||||
}
|
||||
|
||||
detail::scheduler& system_context::add_scheduler(detail::scheduler* s)
|
||||
{
|
||||
detail::scoped_ptr<detail::scheduler> scoped_impl(s);
|
||||
asio::add_service<detail::scheduler>(*this, scoped_impl.get());
|
||||
return *scoped_impl.release();
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_SYSTEM_CONTEXT_IPP
|
||||
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// impl/system_executor.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_SYSTEM_EXECUTOR_HPP
|
||||
#define ASIO_IMPL_SYSTEM_EXECUTOR_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/executor_op.hpp"
|
||||
#include "asio/detail/global.hpp"
|
||||
#include "asio/detail/recycling_allocator.hpp"
|
||||
#include "asio/detail/type_traits.hpp"
|
||||
#include "asio/system_context.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
inline system_context& system_executor::context() const ASIO_NOEXCEPT
|
||||
{
|
||||
return detail::global<system_context>();
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void system_executor::dispatch(
|
||||
ASIO_MOVE_ARG(Function) f, const Allocator&) const
|
||||
{
|
||||
typename decay<Function>::type tmp(ASIO_MOVE_CAST(Function)(f));
|
||||
asio_handler_invoke_helpers::invoke(tmp, tmp);
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void system_executor::post(
|
||||
ASIO_MOVE_ARG(Function) f, const Allocator& a) const
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
system_context& ctx = detail::global<system_context>();
|
||||
|
||||
// Allocate and construct an operation to wrap the function.
|
||||
typedef detail::executor_op<function_type, Allocator> op;
|
||||
typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
|
||||
p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
|
||||
|
||||
ASIO_HANDLER_CREATION((ctx, *p.p,
|
||||
"system_executor", &this->context(), 0, "post"));
|
||||
|
||||
ctx.scheduler_.post_immediate_completion(p.p, false);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void system_executor::defer(
|
||||
ASIO_MOVE_ARG(Function) f, const Allocator& a) const
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
system_context& ctx = detail::global<system_context>();
|
||||
|
||||
// Allocate and construct an operation to wrap the function.
|
||||
typedef detail::executor_op<function_type, Allocator> op;
|
||||
typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
|
||||
p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
|
||||
|
||||
ASIO_HANDLER_CREATION((ctx, *p.p,
|
||||
"system_executor", &this->context(), 0, "defer"));
|
||||
|
||||
ctx.scheduler_.post_immediate_completion(p.p, true);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_SYSTEM_EXECUTOR_HPP
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
//
|
||||
// impl/thread_pool.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_THREAD_POOL_HPP
|
||||
#define ASIO_IMPL_THREAD_POOL_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/executor_op.hpp"
|
||||
#include "asio/detail/fenced_block.hpp"
|
||||
#include "asio/detail/recycling_allocator.hpp"
|
||||
#include "asio/detail/type_traits.hpp"
|
||||
#include "asio/execution_context.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
inline thread_pool::executor_type
|
||||
thread_pool::get_executor() ASIO_NOEXCEPT
|
||||
{
|
||||
return executor_type(*this);
|
||||
}
|
||||
|
||||
inline thread_pool&
|
||||
thread_pool::executor_type::context() const ASIO_NOEXCEPT
|
||||
{
|
||||
return pool_;
|
||||
}
|
||||
|
||||
inline void
|
||||
thread_pool::executor_type::on_work_started() const ASIO_NOEXCEPT
|
||||
{
|
||||
pool_.scheduler_.work_started();
|
||||
}
|
||||
|
||||
inline void thread_pool::executor_type::on_work_finished()
|
||||
const ASIO_NOEXCEPT
|
||||
{
|
||||
pool_.scheduler_.work_finished();
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void thread_pool::executor_type::dispatch(
|
||||
ASIO_MOVE_ARG(Function) f, const Allocator& a) const
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
// Invoke immediately if we are already inside the thread pool.
|
||||
if (pool_.scheduler_.can_dispatch())
|
||||
{
|
||||
// Make a local, non-const copy of the function.
|
||||
function_type tmp(ASIO_MOVE_CAST(Function)(f));
|
||||
|
||||
detail::fenced_block b(detail::fenced_block::full);
|
||||
asio_handler_invoke_helpers::invoke(tmp, tmp);
|
||||
return;
|
||||
}
|
||||
|
||||
// Allocate and construct an operation to wrap the function.
|
||||
typedef detail::executor_op<function_type, Allocator> op;
|
||||
typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
|
||||
p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
|
||||
|
||||
ASIO_HANDLER_CREATION((pool_, *p.p,
|
||||
"thread_pool", &this->context(), 0, "dispatch"));
|
||||
|
||||
pool_.scheduler_.post_immediate_completion(p.p, false);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void thread_pool::executor_type::post(
|
||||
ASIO_MOVE_ARG(Function) f, const Allocator& a) const
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
// Allocate and construct an operation to wrap the function.
|
||||
typedef detail::executor_op<function_type, Allocator> op;
|
||||
typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
|
||||
p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
|
||||
|
||||
ASIO_HANDLER_CREATION((pool_, *p.p,
|
||||
"thread_pool", &this->context(), 0, "post"));
|
||||
|
||||
pool_.scheduler_.post_immediate_completion(p.p, false);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator>
|
||||
void thread_pool::executor_type::defer(
|
||||
ASIO_MOVE_ARG(Function) f, const Allocator& a) const
|
||||
{
|
||||
typedef typename decay<Function>::type function_type;
|
||||
|
||||
// Allocate and construct an operation to wrap the function.
|
||||
typedef detail::executor_op<function_type, Allocator> op;
|
||||
typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
|
||||
p.p = new (p.v) op(ASIO_MOVE_CAST(Function)(f), a);
|
||||
|
||||
ASIO_HANDLER_CREATION((pool_, *p.p,
|
||||
"thread_pool", &this->context(), 0, "defer"));
|
||||
|
||||
pool_.scheduler_.post_immediate_completion(p.p, true);
|
||||
p.v = p.p = 0;
|
||||
}
|
||||
|
||||
inline bool
|
||||
thread_pool::executor_type::running_in_this_thread() const ASIO_NOEXCEPT
|
||||
{
|
||||
return pool_.scheduler_.can_dispatch();
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_THREAD_POOL_HPP
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
//
|
||||
// impl/thread_pool.ipp
|
||||
// ~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_THREAD_POOL_IPP
|
||||
#define ASIO_IMPL_THREAD_POOL_IPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/thread_pool.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
struct thread_pool::thread_function
|
||||
{
|
||||
detail::scheduler* scheduler_;
|
||||
|
||||
void operator()()
|
||||
{
|
||||
asio::error_code ec;
|
||||
scheduler_->run(ec);
|
||||
}
|
||||
};
|
||||
|
||||
thread_pool::thread_pool()
|
||||
: scheduler_(add_scheduler(new detail::scheduler(*this, 0, false)))
|
||||
{
|
||||
scheduler_.work_started();
|
||||
|
||||
thread_function f = { &scheduler_ };
|
||||
std::size_t num_threads = detail::thread::hardware_concurrency() * 2;
|
||||
threads_.create_threads(f, num_threads ? num_threads : 2);
|
||||
}
|
||||
|
||||
thread_pool::thread_pool(std::size_t num_threads)
|
||||
: scheduler_(add_scheduler(new detail::scheduler(
|
||||
*this, num_threads == 1 ? 1 : 0, false)))
|
||||
{
|
||||
scheduler_.work_started();
|
||||
|
||||
thread_function f = { &scheduler_ };
|
||||
threads_.create_threads(f, num_threads);
|
||||
}
|
||||
|
||||
thread_pool::~thread_pool()
|
||||
{
|
||||
stop();
|
||||
join();
|
||||
}
|
||||
|
||||
void thread_pool::stop()
|
||||
{
|
||||
scheduler_.stop();
|
||||
}
|
||||
|
||||
void thread_pool::join()
|
||||
{
|
||||
if (!threads_.empty())
|
||||
{
|
||||
scheduler_.work_finished();
|
||||
threads_.join();
|
||||
}
|
||||
}
|
||||
|
||||
detail::scheduler& thread_pool::add_scheduler(detail::scheduler* s)
|
||||
{
|
||||
detail::scoped_ptr<detail::scheduler> scoped_impl(s);
|
||||
asio::add_service<detail::scheduler>(*this, scoped_impl.get());
|
||||
return *scoped_impl.release();
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_THREAD_POOL_IPP
|
||||
+276
@@ -0,0 +1,276 @@
|
||||
//
|
||||
// impl/use_awaitable.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_USE_AWAITABLE_HPP
|
||||
#define ASIO_IMPL_USE_AWAITABLE_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include "asio/async_result.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
template <typename Executor, typename T>
|
||||
class awaitable_handler_base
|
||||
: public awaitable_thread<Executor>
|
||||
{
|
||||
public:
|
||||
typedef void result_type;
|
||||
typedef awaitable<T, Executor> awaitable_type;
|
||||
|
||||
// Construct from the entry point of a new thread of execution.
|
||||
awaitable_handler_base(awaitable<void, Executor> a, const Executor& ex)
|
||||
: awaitable_thread<Executor>(std::move(a), ex)
|
||||
{
|
||||
}
|
||||
|
||||
// Transfer ownership from another awaitable_thread.
|
||||
explicit awaitable_handler_base(awaitable_thread<Executor>* h)
|
||||
: awaitable_thread<Executor>(std::move(*h))
|
||||
{
|
||||
}
|
||||
|
||||
protected:
|
||||
awaitable_frame<T, Executor>* frame() noexcept
|
||||
{
|
||||
return static_cast<awaitable_frame<T, Executor>*>(this->top_of_stack_);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename, typename...>
|
||||
class awaitable_handler;
|
||||
|
||||
template <typename Executor>
|
||||
class awaitable_handler<Executor, void>
|
||||
: public awaitable_handler_base<Executor, void>
|
||||
{
|
||||
public:
|
||||
using awaitable_handler_base<Executor, void>::awaitable_handler_base;
|
||||
|
||||
void operator()()
|
||||
{
|
||||
this->frame()->attach_thread(this);
|
||||
this->frame()->return_void();
|
||||
this->frame()->pop_frame();
|
||||
this->pump();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor>
|
||||
class awaitable_handler<Executor, asio::error_code>
|
||||
: public awaitable_handler_base<Executor, void>
|
||||
{
|
||||
public:
|
||||
using awaitable_handler_base<Executor, void>::awaitable_handler_base;
|
||||
|
||||
void operator()(const asio::error_code& ec)
|
||||
{
|
||||
this->frame()->attach_thread(this);
|
||||
if (ec)
|
||||
this->frame()->set_error(ec);
|
||||
else
|
||||
this->frame()->return_void();
|
||||
this->frame()->pop_frame();
|
||||
this->pump();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor>
|
||||
class awaitable_handler<Executor, std::exception_ptr>
|
||||
: public awaitable_handler_base<Executor, void>
|
||||
{
|
||||
public:
|
||||
using awaitable_handler_base<Executor, void>::awaitable_handler_base;
|
||||
|
||||
void operator()(std::exception_ptr ex)
|
||||
{
|
||||
this->frame()->attach_thread(this);
|
||||
if (ex)
|
||||
this->frame()->set_except(ex);
|
||||
else
|
||||
this->frame()->return_void();
|
||||
this->frame()->pop_frame();
|
||||
this->pump();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor, typename T>
|
||||
class awaitable_handler<Executor, T>
|
||||
: public awaitable_handler_base<Executor, T>
|
||||
{
|
||||
public:
|
||||
using awaitable_handler_base<Executor, T>::awaitable_handler_base;
|
||||
|
||||
template <typename Arg>
|
||||
void operator()(Arg&& arg)
|
||||
{
|
||||
this->frame()->attach_thread(this);
|
||||
this->frame()->return_value(std::forward<Arg>(arg));
|
||||
this->frame()->pop_frame();
|
||||
this->pump();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor, typename T>
|
||||
class awaitable_handler<Executor, asio::error_code, T>
|
||||
: public awaitable_handler_base<Executor, T>
|
||||
{
|
||||
public:
|
||||
using awaitable_handler_base<Executor, T>::awaitable_handler_base;
|
||||
|
||||
template <typename Arg>
|
||||
void operator()(const asio::error_code& ec, Arg&& arg)
|
||||
{
|
||||
this->frame()->attach_thread(this);
|
||||
if (ec)
|
||||
this->frame()->set_error(ec);
|
||||
else
|
||||
this->frame()->return_value(std::forward<Arg>(arg));
|
||||
this->frame()->pop_frame();
|
||||
this->pump();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor, typename T>
|
||||
class awaitable_handler<Executor, std::exception_ptr, T>
|
||||
: public awaitable_handler_base<Executor, T>
|
||||
{
|
||||
public:
|
||||
using awaitable_handler_base<Executor, T>::awaitable_handler_base;
|
||||
|
||||
template <typename Arg>
|
||||
void operator()(std::exception_ptr ex, Arg&& arg)
|
||||
{
|
||||
this->frame()->attach_thread(this);
|
||||
if (ex)
|
||||
this->frame()->set_except(ex);
|
||||
else
|
||||
this->frame()->return_value(std::forward<Arg>(arg));
|
||||
this->frame()->pop_frame();
|
||||
this->pump();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor, typename... Ts>
|
||||
class awaitable_handler
|
||||
: public awaitable_handler_base<Executor, std::tuple<Ts...>>
|
||||
{
|
||||
public:
|
||||
using awaitable_handler_base<Executor,
|
||||
std::tuple<Ts...>>::awaitable_handler_base;
|
||||
|
||||
template <typename... Args>
|
||||
void operator()(Args&&... args)
|
||||
{
|
||||
this->frame()->attach_thread(this);
|
||||
this->frame()->return_values(std::forward<Args>(args)...);
|
||||
this->frame()->pop_frame();
|
||||
this->pump();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor, typename... Ts>
|
||||
class awaitable_handler<Executor, asio::error_code, Ts...>
|
||||
: public awaitable_handler_base<Executor, std::tuple<Ts...>>
|
||||
{
|
||||
public:
|
||||
using awaitable_handler_base<Executor,
|
||||
std::tuple<Ts...>>::awaitable_handler_base;
|
||||
|
||||
template <typename... Args>
|
||||
void operator()(const asio::error_code& ec, Args&&... args)
|
||||
{
|
||||
this->frame()->attach_thread(this);
|
||||
if (ec)
|
||||
this->frame()->set_error(ec);
|
||||
else
|
||||
this->frame()->return_values(std::forward<Args>(args)...);
|
||||
this->frame()->pop_frame();
|
||||
this->pump();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor, typename... Ts>
|
||||
class awaitable_handler<Executor, std::exception_ptr, Ts...>
|
||||
: public awaitable_handler_base<Executor, std::tuple<Ts...>>
|
||||
{
|
||||
public:
|
||||
using awaitable_handler_base<Executor,
|
||||
std::tuple<Ts...>>::awaitable_handler_base;
|
||||
|
||||
template <typename... Args>
|
||||
void operator()(std::exception_ptr ex, Args&&... args)
|
||||
{
|
||||
this->frame()->attach_thread(this);
|
||||
if (ex)
|
||||
this->frame()->set_except(ex);
|
||||
else
|
||||
this->frame()->return_values(std::forward<Args>(args)...);
|
||||
this->frame()->pop_frame();
|
||||
this->pump();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Executor, typename R, typename... Args>
|
||||
class async_result<use_awaitable_t<Executor>, R(Args...)>
|
||||
{
|
||||
public:
|
||||
typedef typename detail::awaitable_handler<
|
||||
Executor, typename decay<Args>::type...> handler_type;
|
||||
typedef typename handler_type::awaitable_type return_type;
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
template <typename T>
|
||||
static T dummy_return()
|
||||
{
|
||||
return std::move(*static_cast<T*>(nullptr));
|
||||
}
|
||||
|
||||
template <>
|
||||
static void dummy_return()
|
||||
{
|
||||
}
|
||||
#endif // defined(_MSC_VER)
|
||||
|
||||
template <typename Initiation, typename... InitArgs>
|
||||
static return_type initiate(Initiation initiation,
|
||||
use_awaitable_t<Executor>, InitArgs... args)
|
||||
{
|
||||
co_await [&](auto* frame)
|
||||
{
|
||||
handler_type handler(frame->detach_thread());
|
||||
std::move(initiation)(std::move(handler), std::move(args)...);
|
||||
return static_cast<handler_type*>(nullptr);
|
||||
};
|
||||
|
||||
for (;;) {} // Never reached.
|
||||
#if defined(_MSC_VER)
|
||||
co_return dummy_return<typename return_type::value_type>();
|
||||
#endif // defined(_MSC_VER)
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_USE_AWAITABLE_HPP
|
||||
+887
@@ -0,0 +1,887 @@
|
||||
//
|
||||
// impl/use_future.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_USE_FUTURE_HPP
|
||||
#define ASIO_IMPL_USE_FUTURE_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/detail/config.hpp"
|
||||
#include <tuple>
|
||||
#include "asio/async_result.hpp"
|
||||
#include "asio/detail/memory.hpp"
|
||||
#include "asio/error_code.hpp"
|
||||
#include "asio/packaged_task.hpp"
|
||||
#include "asio/system_error.hpp"
|
||||
#include "asio/system_executor.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
namespace detail {
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename T, typename F, typename... Args>
|
||||
inline void promise_invoke_and_set(std::promise<T>& p,
|
||||
F& f, ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
try
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
{
|
||||
p.set_value(f(ASIO_MOVE_CAST(Args)(args)...));
|
||||
}
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
catch (...)
|
||||
{
|
||||
p.set_exception(std::current_exception());
|
||||
}
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
}
|
||||
|
||||
template <typename F, typename... Args>
|
||||
inline void promise_invoke_and_set(std::promise<void>& p,
|
||||
F& f, ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
try
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
{
|
||||
f(ASIO_MOVE_CAST(Args)(args)...);
|
||||
p.set_value();
|
||||
}
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
catch (...)
|
||||
{
|
||||
p.set_exception(std::current_exception());
|
||||
}
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename T, typename F>
|
||||
inline void promise_invoke_and_set(std::promise<T>& p, F& f)
|
||||
{
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
try
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
{
|
||||
p.set_value(f());
|
||||
}
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
catch (...)
|
||||
{
|
||||
p.set_exception(std::current_exception());
|
||||
}
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
}
|
||||
|
||||
template <typename F, typename Args>
|
||||
inline void promise_invoke_and_set(std::promise<void>& p, F& f)
|
||||
{
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
try
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
{
|
||||
f();
|
||||
p.set_value();
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
p.set_exception(std::current_exception());
|
||||
}
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
}
|
||||
|
||||
#if defined(ASIO_NO_EXCEPTIONS)
|
||||
|
||||
#define ASIO_PRIVATE_PROMISE_INVOKE_DEF(n) \
|
||||
template <typename T, typename F, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
inline void promise_invoke_and_set(std::promise<T>& p, \
|
||||
F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
p.set_value(f(ASIO_VARIADIC_MOVE_ARGS(n))); \
|
||||
} \
|
||||
\
|
||||
template <typename F, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
inline void promise_invoke_and_set(std::promise<void>& p, \
|
||||
F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
f(ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
p.set_value(); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_PROMISE_INVOKE_DEF)
|
||||
#undef ASIO_PRIVATE_PROMISE_INVOKE_DEF
|
||||
|
||||
#else // defined(ASIO_NO_EXCEPTIONS)
|
||||
|
||||
#define ASIO_PRIVATE_PROMISE_INVOKE_DEF(n) \
|
||||
template <typename T, typename F, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
inline void promise_invoke_and_set(std::promise<T>& p, \
|
||||
F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
try \
|
||||
{ \
|
||||
p.set_value(f(ASIO_VARIADIC_MOVE_ARGS(n))); \
|
||||
} \
|
||||
catch (...) \
|
||||
{ \
|
||||
p.set_exception(std::current_exception()); \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
template <typename F, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
inline void promise_invoke_and_set(std::promise<void>& p, \
|
||||
F& f, ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{ \
|
||||
try \
|
||||
{ \
|
||||
f(ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
p.set_value(); \
|
||||
} \
|
||||
catch (...) \
|
||||
{ \
|
||||
p.set_exception(std::current_exception()); \
|
||||
} \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_PROMISE_INVOKE_DEF)
|
||||
#undef ASIO_PRIVATE_PROMISE_INVOKE_DEF
|
||||
|
||||
#endif // defined(ASIO_NO_EXCEPTIONS)
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
// A function object adapter to invoke a nullary function object and capture
|
||||
// any exception thrown into a promise.
|
||||
template <typename T, typename F>
|
||||
class promise_invoker
|
||||
{
|
||||
public:
|
||||
promise_invoker(const shared_ptr<std::promise<T> >& p,
|
||||
ASIO_MOVE_ARG(F) f)
|
||||
: p_(p), f_(ASIO_MOVE_CAST(F)(f))
|
||||
{
|
||||
}
|
||||
|
||||
void operator()()
|
||||
{
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
try
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
{
|
||||
f_();
|
||||
}
|
||||
#if !defined(ASIO_NO_EXCEPTIONS)
|
||||
catch (...)
|
||||
{
|
||||
p_->set_exception(std::current_exception());
|
||||
}
|
||||
#endif // !defined(ASIO_NO_EXCEPTIONS)
|
||||
}
|
||||
|
||||
private:
|
||||
shared_ptr<std::promise<T> > p_;
|
||||
typename decay<F>::type f_;
|
||||
};
|
||||
|
||||
// An executor that adapts the system_executor to capture any exeption thrown
|
||||
// by a submitted function object and save it into a promise.
|
||||
template <typename T>
|
||||
class promise_executor
|
||||
{
|
||||
public:
|
||||
explicit promise_executor(const shared_ptr<std::promise<T> >& p)
|
||||
: p_(p)
|
||||
{
|
||||
}
|
||||
|
||||
execution_context& context() const ASIO_NOEXCEPT
|
||||
{
|
||||
return system_executor().context();
|
||||
}
|
||||
|
||||
void on_work_started() const ASIO_NOEXCEPT {}
|
||||
void on_work_finished() const ASIO_NOEXCEPT {}
|
||||
|
||||
template <typename F, typename A>
|
||||
void dispatch(ASIO_MOVE_ARG(F) f, const A&) const
|
||||
{
|
||||
promise_invoker<T, F>(p_, ASIO_MOVE_CAST(F)(f))();
|
||||
}
|
||||
|
||||
template <typename F, typename A>
|
||||
void post(ASIO_MOVE_ARG(F) f, const A& a) const
|
||||
{
|
||||
system_executor().post(
|
||||
promise_invoker<T, F>(p_, ASIO_MOVE_CAST(F)(f)), a);
|
||||
}
|
||||
|
||||
template <typename F, typename A>
|
||||
void defer(ASIO_MOVE_ARG(F) f, const A& a) const
|
||||
{
|
||||
system_executor().defer(
|
||||
promise_invoker<T, F>(p_, ASIO_MOVE_CAST(F)(f)), a);
|
||||
}
|
||||
|
||||
friend bool operator==(const promise_executor& a,
|
||||
const promise_executor& b) ASIO_NOEXCEPT
|
||||
{
|
||||
return a.p_ == b.p_;
|
||||
}
|
||||
|
||||
friend bool operator!=(const promise_executor& a,
|
||||
const promise_executor& b) ASIO_NOEXCEPT
|
||||
{
|
||||
return a.p_ != b.p_;
|
||||
}
|
||||
|
||||
private:
|
||||
shared_ptr<std::promise<T> > p_;
|
||||
};
|
||||
|
||||
// The base class for all completion handlers that create promises.
|
||||
template <typename T>
|
||||
class promise_creator
|
||||
{
|
||||
public:
|
||||
typedef promise_executor<T> executor_type;
|
||||
|
||||
executor_type get_executor() const ASIO_NOEXCEPT
|
||||
{
|
||||
return executor_type(p_);
|
||||
}
|
||||
|
||||
typedef std::future<T> future_type;
|
||||
|
||||
future_type get_future()
|
||||
{
|
||||
return p_->get_future();
|
||||
}
|
||||
|
||||
protected:
|
||||
template <typename Allocator>
|
||||
void create_promise(const Allocator& a)
|
||||
{
|
||||
ASIO_REBIND_ALLOC(Allocator, char) b(a);
|
||||
p_ = std::allocate_shared<std::promise<T>>(b, std::allocator_arg, b);
|
||||
}
|
||||
|
||||
shared_ptr<std::promise<T> > p_;
|
||||
};
|
||||
|
||||
// For completion signature void().
|
||||
class promise_handler_0
|
||||
: public promise_creator<void>
|
||||
{
|
||||
public:
|
||||
void operator()()
|
||||
{
|
||||
this->p_->set_value();
|
||||
}
|
||||
};
|
||||
|
||||
// For completion signature void(error_code).
|
||||
class promise_handler_ec_0
|
||||
: public promise_creator<void>
|
||||
{
|
||||
public:
|
||||
void operator()(const asio::error_code& ec)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
this->p_->set_exception(
|
||||
std::make_exception_ptr(
|
||||
asio::system_error(ec)));
|
||||
}
|
||||
else
|
||||
{
|
||||
this->p_->set_value();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// For completion signature void(exception_ptr).
|
||||
class promise_handler_ex_0
|
||||
: public promise_creator<void>
|
||||
{
|
||||
public:
|
||||
void operator()(const std::exception_ptr& ex)
|
||||
{
|
||||
if (ex)
|
||||
{
|
||||
this->p_->set_exception(ex);
|
||||
}
|
||||
else
|
||||
{
|
||||
this->p_->set_value();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// For completion signature void(T).
|
||||
template <typename T>
|
||||
class promise_handler_1
|
||||
: public promise_creator<T>
|
||||
{
|
||||
public:
|
||||
template <typename Arg>
|
||||
void operator()(ASIO_MOVE_ARG(Arg) arg)
|
||||
{
|
||||
this->p_->set_value(ASIO_MOVE_CAST(Arg)(arg));
|
||||
}
|
||||
};
|
||||
|
||||
// For completion signature void(error_code, T).
|
||||
template <typename T>
|
||||
class promise_handler_ec_1
|
||||
: public promise_creator<T>
|
||||
{
|
||||
public:
|
||||
template <typename Arg>
|
||||
void operator()(const asio::error_code& ec,
|
||||
ASIO_MOVE_ARG(Arg) arg)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
this->p_->set_exception(
|
||||
std::make_exception_ptr(
|
||||
asio::system_error(ec)));
|
||||
}
|
||||
else
|
||||
this->p_->set_value(ASIO_MOVE_CAST(Arg)(arg));
|
||||
}
|
||||
};
|
||||
|
||||
// For completion signature void(exception_ptr, T).
|
||||
template <typename T>
|
||||
class promise_handler_ex_1
|
||||
: public promise_creator<T>
|
||||
{
|
||||
public:
|
||||
template <typename Arg>
|
||||
void operator()(const std::exception_ptr& ex,
|
||||
ASIO_MOVE_ARG(Arg) arg)
|
||||
{
|
||||
if (ex)
|
||||
this->p_->set_exception(ex);
|
||||
else
|
||||
this->p_->set_value(ASIO_MOVE_CAST(Arg)(arg));
|
||||
}
|
||||
};
|
||||
|
||||
// For completion signature void(T1, ..., Tn);
|
||||
template <typename T>
|
||||
class promise_handler_n
|
||||
: public promise_creator<T>
|
||||
{
|
||||
public:
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename... Args>
|
||||
void operator()(ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
this->p_->set_value(
|
||||
std::forward_as_tuple(
|
||||
ASIO_MOVE_CAST(Args)(args)...));
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
#define ASIO_PRIVATE_CALL_OP_DEF(n) \
|
||||
template <ASIO_VARIADIC_TPARAMS(n)> \
|
||||
void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{\
|
||||
this->p_->set_value( \
|
||||
std::forward_as_tuple( \
|
||||
ASIO_VARIADIC_MOVE_ARGS(n))); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF)
|
||||
#undef ASIO_PRIVATE_CALL_OP_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
};
|
||||
|
||||
// For completion signature void(error_code, T1, ..., Tn);
|
||||
template <typename T>
|
||||
class promise_handler_ec_n
|
||||
: public promise_creator<T>
|
||||
{
|
||||
public:
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename... Args>
|
||||
void operator()(const asio::error_code& ec,
|
||||
ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
if (ec)
|
||||
{
|
||||
this->p_->set_exception(
|
||||
std::make_exception_ptr(
|
||||
asio::system_error(ec)));
|
||||
}
|
||||
else
|
||||
{
|
||||
this->p_->set_value(
|
||||
std::forward_as_tuple(
|
||||
ASIO_MOVE_CAST(Args)(args)...));
|
||||
}
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
#define ASIO_PRIVATE_CALL_OP_DEF(n) \
|
||||
template <ASIO_VARIADIC_TPARAMS(n)> \
|
||||
void operator()(const asio::error_code& ec, \
|
||||
ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{\
|
||||
if (ec) \
|
||||
{ \
|
||||
this->p_->set_exception( \
|
||||
std::make_exception_ptr( \
|
||||
asio::system_error(ec))); \
|
||||
} \
|
||||
else \
|
||||
{ \
|
||||
this->p_->set_value( \
|
||||
std::forward_as_tuple( \
|
||||
ASIO_VARIADIC_MOVE_ARGS(n))); \
|
||||
} \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF)
|
||||
#undef ASIO_PRIVATE_CALL_OP_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
};
|
||||
|
||||
// For completion signature void(exception_ptr, T1, ..., Tn);
|
||||
template <typename T>
|
||||
class promise_handler_ex_n
|
||||
: public promise_creator<T>
|
||||
{
|
||||
public:
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename... Args>
|
||||
void operator()(const std::exception_ptr& ex,
|
||||
ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
if (ex)
|
||||
this->p_->set_exception(ex);
|
||||
else
|
||||
{
|
||||
this->p_->set_value(
|
||||
std::forward_as_tuple(
|
||||
ASIO_MOVE_CAST(Args)(args)...));
|
||||
}
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
#define ASIO_PRIVATE_CALL_OP_DEF(n) \
|
||||
template <ASIO_VARIADIC_TPARAMS(n)> \
|
||||
void operator()(const std::exception_ptr& ex, \
|
||||
ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{\
|
||||
if (ex) \
|
||||
this->p_->set_exception(ex); \
|
||||
else \
|
||||
{ \
|
||||
this->p_->set_value( \
|
||||
std::forward_as_tuple( \
|
||||
ASIO_VARIADIC_MOVE_ARGS(n))); \
|
||||
} \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF)
|
||||
#undef ASIO_PRIVATE_CALL_OP_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
};
|
||||
|
||||
// Helper template to choose the appropriate concrete promise handler
|
||||
// implementation based on the supplied completion signature.
|
||||
template <typename> class promise_handler_selector;
|
||||
|
||||
template <>
|
||||
class promise_handler_selector<void()>
|
||||
: public promise_handler_0 {};
|
||||
|
||||
template <>
|
||||
class promise_handler_selector<void(asio::error_code)>
|
||||
: public promise_handler_ec_0 {};
|
||||
|
||||
template <>
|
||||
class promise_handler_selector<void(std::exception_ptr)>
|
||||
: public promise_handler_ex_0 {};
|
||||
|
||||
template <typename Arg>
|
||||
class promise_handler_selector<void(Arg)>
|
||||
: public promise_handler_1<Arg> {};
|
||||
|
||||
template <typename Arg>
|
||||
class promise_handler_selector<void(asio::error_code, Arg)>
|
||||
: public promise_handler_ec_1<Arg> {};
|
||||
|
||||
template <typename Arg>
|
||||
class promise_handler_selector<void(std::exception_ptr, Arg)>
|
||||
: public promise_handler_ex_1<Arg> {};
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename... Arg>
|
||||
class promise_handler_selector<void(Arg...)>
|
||||
: public promise_handler_n<std::tuple<Arg...> > {};
|
||||
|
||||
template <typename... Arg>
|
||||
class promise_handler_selector<void(asio::error_code, Arg...)>
|
||||
: public promise_handler_ec_n<std::tuple<Arg...> > {};
|
||||
|
||||
template <typename... Arg>
|
||||
class promise_handler_selector<void(std::exception_ptr, Arg...)>
|
||||
: public promise_handler_ex_n<std::tuple<Arg...> > {};
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
#define ASIO_PRIVATE_PROMISE_SELECTOR_DEF(n) \
|
||||
template <typename Arg, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
class promise_handler_selector< \
|
||||
void(Arg, ASIO_VARIADIC_TARGS(n))> \
|
||||
: public promise_handler_n< \
|
||||
std::tuple<Arg, ASIO_VARIADIC_TARGS(n)> > {}; \
|
||||
\
|
||||
template <typename Arg, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
class promise_handler_selector< \
|
||||
void(asio::error_code, Arg, ASIO_VARIADIC_TARGS(n))> \
|
||||
: public promise_handler_ec_n< \
|
||||
std::tuple<Arg, ASIO_VARIADIC_TARGS(n)> > {}; \
|
||||
\
|
||||
template <typename Arg, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
class promise_handler_selector< \
|
||||
void(std::exception_ptr, Arg, ASIO_VARIADIC_TARGS(n))> \
|
||||
: public promise_handler_ex_n< \
|
||||
std::tuple<Arg, ASIO_VARIADIC_TARGS(n)> > {}; \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_PROMISE_SELECTOR_DEF)
|
||||
#undef ASIO_PRIVATE_PROMISE_SELECTOR_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
// Completion handlers produced from the use_future completion token, when not
|
||||
// using use_future::operator().
|
||||
template <typename Signature, typename Allocator>
|
||||
class promise_handler
|
||||
: public promise_handler_selector<Signature>
|
||||
{
|
||||
public:
|
||||
typedef Allocator allocator_type;
|
||||
typedef void result_type;
|
||||
|
||||
promise_handler(use_future_t<Allocator> u)
|
||||
: allocator_(u.get_allocator())
|
||||
{
|
||||
this->create_promise(allocator_);
|
||||
}
|
||||
|
||||
allocator_type get_allocator() const ASIO_NOEXCEPT
|
||||
{
|
||||
return allocator_;
|
||||
}
|
||||
|
||||
private:
|
||||
Allocator allocator_;
|
||||
};
|
||||
|
||||
template <typename Function, typename Signature, typename Allocator>
|
||||
inline void asio_handler_invoke(Function& f,
|
||||
promise_handler<Signature, Allocator>* h)
|
||||
{
|
||||
typename promise_handler<Signature, Allocator>::executor_type
|
||||
ex(h->get_executor());
|
||||
ex.dispatch(ASIO_MOVE_CAST(Function)(f), std::allocator<void>());
|
||||
}
|
||||
|
||||
template <typename Function, typename Signature, typename Allocator>
|
||||
inline void asio_handler_invoke(const Function& f,
|
||||
promise_handler<Signature, Allocator>* h)
|
||||
{
|
||||
typename promise_handler<Signature, Allocator>::executor_type
|
||||
ex(h->get_executor());
|
||||
ex.dispatch(f, std::allocator<void>());
|
||||
}
|
||||
|
||||
// Helper base class for async_result specialisation.
|
||||
template <typename Signature, typename Allocator>
|
||||
class promise_async_result
|
||||
{
|
||||
public:
|
||||
typedef promise_handler<Signature, Allocator> completion_handler_type;
|
||||
typedef typename completion_handler_type::future_type return_type;
|
||||
|
||||
explicit promise_async_result(completion_handler_type& h)
|
||||
: future_(h.get_future())
|
||||
{
|
||||
}
|
||||
|
||||
return_type get()
|
||||
{
|
||||
return ASIO_MOVE_CAST(return_type)(future_);
|
||||
}
|
||||
|
||||
private:
|
||||
return_type future_;
|
||||
};
|
||||
|
||||
// Return value from use_future::operator().
|
||||
template <typename Function, typename Allocator>
|
||||
class packaged_token
|
||||
{
|
||||
public:
|
||||
packaged_token(Function f, const Allocator& a)
|
||||
: function_(ASIO_MOVE_CAST(Function)(f)),
|
||||
allocator_(a)
|
||||
{
|
||||
}
|
||||
|
||||
//private:
|
||||
Function function_;
|
||||
Allocator allocator_;
|
||||
};
|
||||
|
||||
// Completion handlers produced from the use_future completion token, when
|
||||
// using use_future::operator().
|
||||
template <typename Function, typename Allocator, typename Result>
|
||||
class packaged_handler
|
||||
: public promise_creator<Result>
|
||||
{
|
||||
public:
|
||||
typedef Allocator allocator_type;
|
||||
typedef void result_type;
|
||||
|
||||
packaged_handler(packaged_token<Function, Allocator> t)
|
||||
: function_(ASIO_MOVE_CAST(Function)(t.function_)),
|
||||
allocator_(t.allocator_)
|
||||
{
|
||||
this->create_promise(allocator_);
|
||||
}
|
||||
|
||||
allocator_type get_allocator() const ASIO_NOEXCEPT
|
||||
{
|
||||
return allocator_;
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename... Args>
|
||||
void operator()(ASIO_MOVE_ARG(Args)... args)
|
||||
{
|
||||
(promise_invoke_and_set)(*this->p_,
|
||||
function_, ASIO_MOVE_CAST(Args)(args)...);
|
||||
}
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
void operator()()
|
||||
{
|
||||
(promise_invoke_and_set)(*this->p_, function_);
|
||||
}
|
||||
|
||||
#define ASIO_PRIVATE_CALL_OP_DEF(n) \
|
||||
template <ASIO_VARIADIC_TPARAMS(n)> \
|
||||
void operator()(ASIO_VARIADIC_MOVE_PARAMS(n)) \
|
||||
{\
|
||||
(promise_invoke_and_set)(*this->p_, \
|
||||
function_, ASIO_VARIADIC_MOVE_ARGS(n)); \
|
||||
} \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_CALL_OP_DEF)
|
||||
#undef ASIO_PRIVATE_CALL_OP_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
private:
|
||||
Function function_;
|
||||
Allocator allocator_;
|
||||
};
|
||||
|
||||
template <typename Function,
|
||||
typename Function1, typename Allocator, typename Result>
|
||||
inline void asio_handler_invoke(Function& f,
|
||||
packaged_handler<Function1, Allocator, Result>* h)
|
||||
{
|
||||
typename packaged_handler<Function1, Allocator, Result>::executor_type
|
||||
ex(h->get_executor());
|
||||
ex.dispatch(ASIO_MOVE_CAST(Function)(f), std::allocator<void>());
|
||||
}
|
||||
|
||||
template <typename Function,
|
||||
typename Function1, typename Allocator, typename Result>
|
||||
inline void asio_handler_invoke(const Function& f,
|
||||
packaged_handler<Function1, Allocator, Result>* h)
|
||||
{
|
||||
typename packaged_handler<Function1, Allocator, Result>::executor_type
|
||||
ex(h->get_executor());
|
||||
ex.dispatch(f, std::allocator<void>());
|
||||
}
|
||||
|
||||
// Helper base class for async_result specialisation.
|
||||
template <typename Function, typename Allocator, typename Result>
|
||||
class packaged_async_result
|
||||
{
|
||||
public:
|
||||
typedef packaged_handler<Function, Allocator, Result> completion_handler_type;
|
||||
typedef typename completion_handler_type::future_type return_type;
|
||||
|
||||
explicit packaged_async_result(completion_handler_type& h)
|
||||
: future_(h.get_future())
|
||||
{
|
||||
}
|
||||
|
||||
return_type get()
|
||||
{
|
||||
return ASIO_MOVE_CAST(return_type)(future_);
|
||||
}
|
||||
|
||||
private:
|
||||
return_type future_;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <typename Allocator> template <typename Function>
|
||||
inline detail::packaged_token<typename decay<Function>::type, Allocator>
|
||||
use_future_t<Allocator>::operator()(ASIO_MOVE_ARG(Function) f) const
|
||||
{
|
||||
return detail::packaged_token<typename decay<Function>::type, Allocator>(
|
||||
ASIO_MOVE_CAST(Function)(f), allocator_);
|
||||
}
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
#if defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Allocator, typename Result, typename... Args>
|
||||
class async_result<use_future_t<Allocator>, Result(Args...)>
|
||||
: public detail::promise_async_result<
|
||||
void(typename decay<Args>::type...), Allocator>
|
||||
{
|
||||
public:
|
||||
explicit async_result(
|
||||
typename detail::promise_async_result<void(typename decay<Args>::type...),
|
||||
Allocator>::completion_handler_type& h)
|
||||
: detail::promise_async_result<
|
||||
void(typename decay<Args>::type...), Allocator>(h)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Function, typename Allocator,
|
||||
typename Result, typename... Args>
|
||||
class async_result<detail::packaged_token<Function, Allocator>, Result(Args...)>
|
||||
: public detail::packaged_async_result<Function, Allocator,
|
||||
typename result_of<Function(Args...)>::type>
|
||||
{
|
||||
public:
|
||||
explicit async_result(
|
||||
typename detail::packaged_async_result<Function, Allocator,
|
||||
typename result_of<Function(Args...)>::type>::completion_handler_type& h)
|
||||
: detail::packaged_async_result<Function, Allocator,
|
||||
typename result_of<Function(Args...)>::type>(h)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
#else // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
template <typename Allocator, typename Result>
|
||||
class async_result<use_future_t<Allocator>, Result()>
|
||||
: public detail::promise_async_result<void(), Allocator>
|
||||
{
|
||||
public:
|
||||
explicit async_result(
|
||||
typename detail::promise_async_result<
|
||||
void(), Allocator>::completion_handler_type& h)
|
||||
: detail::promise_async_result<void(), Allocator>(h)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Function, typename Allocator, typename Result>
|
||||
class async_result<detail::packaged_token<Function, Allocator>, Result()>
|
||||
: public detail::packaged_async_result<Function, Allocator,
|
||||
typename result_of<Function()>::type>
|
||||
{
|
||||
public:
|
||||
explicit async_result(
|
||||
typename detail::packaged_async_result<Function, Allocator,
|
||||
typename result_of<Function()>::type>::completion_handler_type& h)
|
||||
: detail::packaged_async_result<Function, Allocator,
|
||||
typename result_of<Function()>::type>(h)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
#define ASIO_PRIVATE_ASYNC_RESULT_DEF(n) \
|
||||
template <typename Allocator, \
|
||||
typename Result, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
class async_result<use_future_t<Allocator>, \
|
||||
Result(ASIO_VARIADIC_TARGS(n))> \
|
||||
: public detail::promise_async_result< \
|
||||
void(ASIO_VARIADIC_DECAY(n)), Allocator> \
|
||||
{ \
|
||||
public: \
|
||||
explicit async_result( \
|
||||
typename detail::promise_async_result< \
|
||||
void(ASIO_VARIADIC_DECAY(n)), \
|
||||
Allocator>::completion_handler_type& h) \
|
||||
: detail::promise_async_result< \
|
||||
void(ASIO_VARIADIC_DECAY(n)), Allocator>(h) \
|
||||
{ \
|
||||
} \
|
||||
}; \
|
||||
\
|
||||
template <typename Function, typename Allocator, \
|
||||
typename Result, ASIO_VARIADIC_TPARAMS(n)> \
|
||||
class async_result<detail::packaged_token<Function, Allocator>, \
|
||||
Result(ASIO_VARIADIC_TARGS(n))> \
|
||||
: public detail::packaged_async_result<Function, Allocator, \
|
||||
typename result_of<Function(ASIO_VARIADIC_TARGS(n))>::type> \
|
||||
{ \
|
||||
public: \
|
||||
explicit async_result( \
|
||||
typename detail::packaged_async_result<Function, Allocator, \
|
||||
typename result_of<Function(ASIO_VARIADIC_TARGS(n))>::type \
|
||||
>::completion_handler_type& h) \
|
||||
: detail::packaged_async_result<Function, Allocator, \
|
||||
typename result_of<Function(ASIO_VARIADIC_TARGS(n))>::type>(h) \
|
||||
{ \
|
||||
} \
|
||||
}; \
|
||||
/**/
|
||||
ASIO_VARIADIC_GENERATE(ASIO_PRIVATE_ASYNC_RESULT_DEF)
|
||||
#undef ASIO_PRIVATE_ASYNC_RESULT_DEF
|
||||
|
||||
#endif // defined(ASIO_HAS_VARIADIC_TEMPLATES)
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_USE_FUTURE_HPP
|
||||
+978
@@ -0,0 +1,978 @@
|
||||
//
|
||||
// impl/write.hpp
|
||||
// ~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_WRITE_HPP
|
||||
#define ASIO_IMPL_WRITE_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/buffer.hpp"
|
||||
#include "asio/completion_condition.hpp"
|
||||
#include "asio/detail/array_fwd.hpp"
|
||||
#include "asio/detail/base_from_completion_cond.hpp"
|
||||
#include "asio/detail/bind_handler.hpp"
|
||||
#include "asio/detail/consuming_buffers.hpp"
|
||||
#include "asio/detail/dependent_type.hpp"
|
||||
#include "asio/detail/handler_alloc_helpers.hpp"
|
||||
#include "asio/detail/handler_cont_helpers.hpp"
|
||||
#include "asio/detail/handler_invoke_helpers.hpp"
|
||||
#include "asio/detail/handler_type_requirements.hpp"
|
||||
#include "asio/detail/non_const_lvalue.hpp"
|
||||
#include "asio/detail/throw_error.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename SyncWriteStream, typename ConstBufferSequence,
|
||||
typename ConstBufferIterator, typename CompletionCondition>
|
||||
std::size_t write_buffer_sequence(SyncWriteStream& s,
|
||||
const ConstBufferSequence& buffers, const ConstBufferIterator&,
|
||||
CompletionCondition completion_condition, asio::error_code& ec)
|
||||
{
|
||||
ec = asio::error_code();
|
||||
asio::detail::consuming_buffers<const_buffer,
|
||||
ConstBufferSequence, ConstBufferIterator> tmp(buffers);
|
||||
while (!tmp.empty())
|
||||
{
|
||||
if (std::size_t max_size = detail::adapt_completion_condition_result(
|
||||
completion_condition(ec, tmp.total_consumed())))
|
||||
tmp.consume(s.write_some(tmp.prepare(max_size), ec));
|
||||
else
|
||||
break;
|
||||
}
|
||||
return tmp.total_consumed();;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
template <typename SyncWriteStream, typename ConstBufferSequence,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
|
||||
CompletionCondition completion_condition, asio::error_code& ec,
|
||||
typename enable_if<
|
||||
is_const_buffer_sequence<ConstBufferSequence>::value
|
||||
>::type*)
|
||||
{
|
||||
return detail::write_buffer_sequence(s, buffers,
|
||||
asio::buffer_sequence_begin(buffers),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename ConstBufferSequence>
|
||||
inline std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
|
||||
typename enable_if<
|
||||
is_const_buffer_sequence<ConstBufferSequence>::value
|
||||
>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write(s, buffers, transfer_all(), ec);
|
||||
asio::detail::throw_error(ec, "write");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename ConstBufferSequence>
|
||||
inline std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
|
||||
asio::error_code& ec,
|
||||
typename enable_if<
|
||||
is_const_buffer_sequence<ConstBufferSequence>::value
|
||||
>::type*)
|
||||
{
|
||||
return write(s, buffers, transfer_all(), ec);
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename ConstBufferSequence,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
|
||||
CompletionCondition completion_condition,
|
||||
typename enable_if<
|
||||
is_const_buffer_sequence<ConstBufferSequence>::value
|
||||
>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write(s, buffers,
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
asio::detail::throw_error(ec, "write");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DYNAMIC_BUFFER_V1)
|
||||
|
||||
template <typename SyncWriteStream, typename DynamicBuffer_v1,
|
||||
typename CompletionCondition>
|
||||
std::size_t write(SyncWriteStream& s,
|
||||
ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
|
||||
CompletionCondition completion_condition, asio::error_code& ec,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
|
||||
&& !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
|
||||
>::type*)
|
||||
{
|
||||
typename decay<DynamicBuffer_v1>::type b(
|
||||
ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers));
|
||||
|
||||
std::size_t bytes_transferred = write(s, b.data(),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
b.consume(bytes_transferred);
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename DynamicBuffer_v1>
|
||||
inline std::size_t write(SyncWriteStream& s,
|
||||
ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
|
||||
&& !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
|
||||
>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write(s,
|
||||
ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
|
||||
transfer_all(), ec);
|
||||
asio::detail::throw_error(ec, "write");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename DynamicBuffer_v1>
|
||||
inline std::size_t write(SyncWriteStream& s,
|
||||
ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
|
||||
asio::error_code& ec,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
|
||||
&& !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
|
||||
>::type*)
|
||||
{
|
||||
return write(s, ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
|
||||
transfer_all(), ec);
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename DynamicBuffer_v1,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t write(SyncWriteStream& s,
|
||||
ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
|
||||
CompletionCondition completion_condition,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
|
||||
&& !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
|
||||
>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write(s,
|
||||
ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
asio::detail::throw_error(ec, "write");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_EXTENSIONS)
|
||||
#if !defined(ASIO_NO_IOSTREAM)
|
||||
|
||||
template <typename SyncWriteStream, typename Allocator,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t write(SyncWriteStream& s,
|
||||
asio::basic_streambuf<Allocator>& b,
|
||||
CompletionCondition completion_condition, asio::error_code& ec)
|
||||
{
|
||||
return write(s, basic_streambuf_ref<Allocator>(b),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename Allocator>
|
||||
inline std::size_t write(SyncWriteStream& s,
|
||||
asio::basic_streambuf<Allocator>& b)
|
||||
{
|
||||
return write(s, basic_streambuf_ref<Allocator>(b));
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename Allocator>
|
||||
inline std::size_t write(SyncWriteStream& s,
|
||||
asio::basic_streambuf<Allocator>& b,
|
||||
asio::error_code& ec)
|
||||
{
|
||||
return write(s, basic_streambuf_ref<Allocator>(b), ec);
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename Allocator,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t write(SyncWriteStream& s,
|
||||
asio::basic_streambuf<Allocator>& b,
|
||||
CompletionCondition completion_condition)
|
||||
{
|
||||
return write(s, basic_streambuf_ref<Allocator>(b),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
|
||||
}
|
||||
|
||||
#endif // !defined(ASIO_NO_IOSTREAM)
|
||||
#endif // !defined(ASIO_NO_EXTENSIONS)
|
||||
#endif // !defined(ASIO_NO_DYNAMIC_BUFFER_V1)
|
||||
|
||||
template <typename SyncWriteStream, typename DynamicBuffer_v2,
|
||||
typename CompletionCondition>
|
||||
std::size_t write(SyncWriteStream& s, DynamicBuffer_v2 buffers,
|
||||
CompletionCondition completion_condition, asio::error_code& ec,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v2<DynamicBuffer_v2>::value
|
||||
>::type*)
|
||||
{
|
||||
std::size_t bytes_transferred = write(s, buffers.data(0, buffers.size()),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
buffers.consume(bytes_transferred);
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename DynamicBuffer_v2>
|
||||
inline std::size_t write(SyncWriteStream& s, DynamicBuffer_v2 buffers,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v2<DynamicBuffer_v2>::value
|
||||
>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write(s,
|
||||
ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
|
||||
transfer_all(), ec);
|
||||
asio::detail::throw_error(ec, "write");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename DynamicBuffer_v2>
|
||||
inline std::size_t write(SyncWriteStream& s, DynamicBuffer_v2 buffers,
|
||||
asio::error_code& ec,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v2<DynamicBuffer_v2>::value
|
||||
>::type*)
|
||||
{
|
||||
return write(s, ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
|
||||
transfer_all(), ec);
|
||||
}
|
||||
|
||||
template <typename SyncWriteStream, typename DynamicBuffer_v2,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t write(SyncWriteStream& s, DynamicBuffer_v2 buffers,
|
||||
CompletionCondition completion_condition,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v2<DynamicBuffer_v2>::value
|
||||
>::type*)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write(s,
|
||||
ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
asio::detail::throw_error(ec, "write");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename AsyncWriteStream, typename ConstBufferSequence,
|
||||
typename ConstBufferIterator, typename CompletionCondition,
|
||||
typename WriteHandler>
|
||||
class write_op
|
||||
: detail::base_from_completion_cond<CompletionCondition>
|
||||
{
|
||||
public:
|
||||
write_op(AsyncWriteStream& stream, const ConstBufferSequence& buffers,
|
||||
CompletionCondition& completion_condition, WriteHandler& handler)
|
||||
: detail::base_from_completion_cond<
|
||||
CompletionCondition>(completion_condition),
|
||||
stream_(stream),
|
||||
buffers_(buffers),
|
||||
start_(0),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
write_op(const write_op& other)
|
||||
: detail::base_from_completion_cond<CompletionCondition>(other),
|
||||
stream_(other.stream_),
|
||||
buffers_(other.buffers_),
|
||||
start_(other.start_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
write_op(write_op&& other)
|
||||
: detail::base_from_completion_cond<CompletionCondition>(
|
||||
ASIO_MOVE_CAST(detail::base_from_completion_cond<
|
||||
CompletionCondition>)(other)),
|
||||
stream_(other.stream_),
|
||||
buffers_(ASIO_MOVE_CAST(buffers_type)(other.buffers_)),
|
||||
start_(other.start_),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec,
|
||||
std::size_t bytes_transferred, int start = 0)
|
||||
{
|
||||
std::size_t max_size;
|
||||
switch (start_ = start)
|
||||
{
|
||||
case 1:
|
||||
max_size = this->check_for_completion(ec, buffers_.total_consumed());
|
||||
do
|
||||
{
|
||||
stream_.async_write_some(buffers_.prepare(max_size),
|
||||
ASIO_MOVE_CAST(write_op)(*this));
|
||||
return; default:
|
||||
buffers_.consume(bytes_transferred);
|
||||
if ((!ec && bytes_transferred == 0) || buffers_.empty())
|
||||
break;
|
||||
max_size = this->check_for_completion(ec, buffers_.total_consumed());
|
||||
} while (max_size > 0);
|
||||
|
||||
handler_(ec, buffers_.total_consumed());
|
||||
}
|
||||
}
|
||||
|
||||
//private:
|
||||
typedef asio::detail::consuming_buffers<const_buffer,
|
||||
ConstBufferSequence, ConstBufferIterator> buffers_type;
|
||||
|
||||
AsyncWriteStream& stream_;
|
||||
buffers_type buffers_;
|
||||
int start_;
|
||||
WriteHandler handler_;
|
||||
};
|
||||
|
||||
template <typename AsyncWriteStream, typename ConstBufferSequence,
|
||||
typename ConstBufferIterator, typename CompletionCondition,
|
||||
typename WriteHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
write_op<AsyncWriteStream, ConstBufferSequence, ConstBufferIterator,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename ConstBufferSequence,
|
||||
typename ConstBufferIterator, typename CompletionCondition,
|
||||
typename WriteHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
write_op<AsyncWriteStream, ConstBufferSequence, ConstBufferIterator,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename ConstBufferSequence,
|
||||
typename ConstBufferIterator, typename CompletionCondition,
|
||||
typename WriteHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
write_op<AsyncWriteStream, ConstBufferSequence, ConstBufferIterator,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
return this_handler->start_ == 0 ? true
|
||||
: asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncWriteStream,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
write_op<AsyncWriteStream, ConstBufferSequence, ConstBufferIterator,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncWriteStream,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
write_op<AsyncWriteStream, ConstBufferSequence, ConstBufferIterator,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename ConstBufferSequence,
|
||||
typename ConstBufferIterator, typename CompletionCondition,
|
||||
typename WriteHandler>
|
||||
inline void start_write_buffer_sequence_op(AsyncWriteStream& stream,
|
||||
const ConstBufferSequence& buffers, const ConstBufferIterator&,
|
||||
CompletionCondition& completion_condition, WriteHandler& handler)
|
||||
{
|
||||
detail::write_op<AsyncWriteStream, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>(
|
||||
stream, buffers, completion_condition, handler)(
|
||||
asio::error_code(), 0, 1);
|
||||
}
|
||||
|
||||
struct initiate_async_write_buffer_sequence
|
||||
{
|
||||
template <typename WriteHandler, typename AsyncWriteStream,
|
||||
typename ConstBufferSequence, typename CompletionCondition>
|
||||
void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
AsyncWriteStream* s, const ConstBufferSequence& buffers,
|
||||
ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a WriteHandler.
|
||||
ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
||||
|
||||
non_const_lvalue<WriteHandler> handler2(handler);
|
||||
non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
|
||||
start_write_buffer_sequence_op(*s, buffers,
|
||||
asio::buffer_sequence_begin(buffers),
|
||||
completion_cond2.value, handler2.value);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncWriteStream, typename ConstBufferSequence,
|
||||
typename ConstBufferIterator, typename CompletionCondition,
|
||||
typename WriteHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::write_op<AsyncWriteStream, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>,
|
||||
Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<WriteHandler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_op<AsyncWriteStream, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<WriteHandler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename AsyncWriteStream, typename ConstBufferSequence,
|
||||
typename ConstBufferIterator, typename CompletionCondition,
|
||||
typename WriteHandler, typename Executor>
|
||||
struct associated_executor<
|
||||
detail::write_op<AsyncWriteStream, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>,
|
||||
Executor>
|
||||
{
|
||||
typedef typename associated_executor<WriteHandler, Executor>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_op<AsyncWriteStream, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<WriteHandler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncWriteStream, typename ConstBufferSequence,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
|
||||
CompletionCondition completion_condition,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
typename enable_if<
|
||||
is_const_buffer_sequence<ConstBufferSequence>::value
|
||||
>::type*)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_write_buffer_sequence(), handler, &s, buffers,
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename ConstBufferSequence,
|
||||
typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
typename enable_if<
|
||||
is_const_buffer_sequence<ConstBufferSequence>::value
|
||||
>::type*)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_write_buffer_sequence(),
|
||||
handler, &s, buffers, transfer_all());
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_DYNAMIC_BUFFER_V1)
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v1,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
class write_dynbuf_v1_op
|
||||
{
|
||||
public:
|
||||
template <typename BufferSequence>
|
||||
write_dynbuf_v1_op(AsyncWriteStream& stream,
|
||||
ASIO_MOVE_ARG(BufferSequence) buffers,
|
||||
CompletionCondition& completion_condition, WriteHandler& handler)
|
||||
: stream_(stream),
|
||||
buffers_(ASIO_MOVE_CAST(BufferSequence)(buffers)),
|
||||
completion_condition_(
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition)),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
write_dynbuf_v1_op(const write_dynbuf_v1_op& other)
|
||||
: stream_(other.stream_),
|
||||
buffers_(other.buffers_),
|
||||
completion_condition_(other.completion_condition_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
write_dynbuf_v1_op(write_dynbuf_v1_op&& other)
|
||||
: stream_(other.stream_),
|
||||
buffers_(ASIO_MOVE_CAST(DynamicBuffer_v1)(other.buffers_)),
|
||||
completion_condition_(
|
||||
ASIO_MOVE_CAST(CompletionCondition)(
|
||||
other.completion_condition_)),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec,
|
||||
std::size_t bytes_transferred, int start = 0)
|
||||
{
|
||||
switch (start)
|
||||
{
|
||||
case 1:
|
||||
async_write(stream_, buffers_.data(),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition_),
|
||||
ASIO_MOVE_CAST(write_dynbuf_v1_op)(*this));
|
||||
return; default:
|
||||
buffers_.consume(bytes_transferred);
|
||||
handler_(ec, static_cast<const std::size_t&>(bytes_transferred));
|
||||
}
|
||||
}
|
||||
|
||||
//private:
|
||||
AsyncWriteStream& stream_;
|
||||
DynamicBuffer_v1 buffers_;
|
||||
CompletionCondition completion_condition_;
|
||||
WriteHandler handler_;
|
||||
};
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v1,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
write_dynbuf_v1_op<AsyncWriteStream, DynamicBuffer_v1,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v1,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
write_dynbuf_v1_op<AsyncWriteStream, DynamicBuffer_v1,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v1,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
write_dynbuf_v1_op<AsyncWriteStream, DynamicBuffer_v1,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncWriteStream,
|
||||
typename DynamicBuffer_v1, typename CompletionCondition,
|
||||
typename WriteHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
write_dynbuf_v1_op<AsyncWriteStream, DynamicBuffer_v1,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncWriteStream,
|
||||
typename DynamicBuffer_v1, typename CompletionCondition,
|
||||
typename WriteHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
write_dynbuf_v1_op<AsyncWriteStream, DynamicBuffer_v1,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_write_dynbuf_v1
|
||||
{
|
||||
template <typename WriteHandler, typename AsyncWriteStream,
|
||||
typename DynamicBuffer_v1, typename CompletionCondition>
|
||||
void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
AsyncWriteStream* s, ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
|
||||
ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a WriteHandler.
|
||||
ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
||||
|
||||
non_const_lvalue<WriteHandler> handler2(handler);
|
||||
non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
|
||||
write_dynbuf_v1_op<AsyncWriteStream,
|
||||
typename decay<DynamicBuffer_v1>::type,
|
||||
CompletionCondition, typename decay<WriteHandler>::type>(
|
||||
*s, ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
|
||||
completion_cond2.value, handler2.value)(
|
||||
asio::error_code(), 0, 1);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v1,
|
||||
typename CompletionCondition, typename WriteHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::write_dynbuf_v1_op<AsyncWriteStream,
|
||||
DynamicBuffer_v1, CompletionCondition, WriteHandler>,
|
||||
Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<WriteHandler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_dynbuf_v1_op<AsyncWriteStream,
|
||||
DynamicBuffer_v1, CompletionCondition, WriteHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<WriteHandler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v1,
|
||||
typename CompletionCondition, typename WriteHandler, typename Executor>
|
||||
struct associated_executor<
|
||||
detail::write_dynbuf_v1_op<AsyncWriteStream,
|
||||
DynamicBuffer_v1, CompletionCondition, WriteHandler>,
|
||||
Executor>
|
||||
{
|
||||
typedef typename associated_executor<WriteHandler, Executor>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_dynbuf_v1_op<AsyncWriteStream,
|
||||
DynamicBuffer_v1, CompletionCondition, WriteHandler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<WriteHandler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncWriteStream,
|
||||
typename DynamicBuffer_v1, typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write(AsyncWriteStream& s,
|
||||
ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
|
||||
&& !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
|
||||
>::type*)
|
||||
{
|
||||
return async_write(s,
|
||||
ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
|
||||
transfer_all(), ASIO_MOVE_CAST(WriteHandler)(handler));
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v1,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write(AsyncWriteStream& s,
|
||||
ASIO_MOVE_ARG(DynamicBuffer_v1) buffers,
|
||||
CompletionCondition completion_condition,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v1<typename decay<DynamicBuffer_v1>::type>::value
|
||||
&& !is_dynamic_buffer_v2<typename decay<DynamicBuffer_v1>::type>::value
|
||||
>::type*)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_write_dynbuf_v1(), handler, &s,
|
||||
ASIO_MOVE_CAST(DynamicBuffer_v1)(buffers),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_EXTENSIONS)
|
||||
#if !defined(ASIO_NO_IOSTREAM)
|
||||
|
||||
template <typename AsyncWriteStream, typename Allocator, typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write(AsyncWriteStream& s,
|
||||
asio::basic_streambuf<Allocator>& b,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler)
|
||||
{
|
||||
return async_write(s, basic_streambuf_ref<Allocator>(b),
|
||||
ASIO_MOVE_CAST(WriteHandler)(handler));
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename Allocator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write(AsyncWriteStream& s,
|
||||
asio::basic_streambuf<Allocator>& b,
|
||||
CompletionCondition completion_condition,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler)
|
||||
{
|
||||
return async_write(s, basic_streambuf_ref<Allocator>(b),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition),
|
||||
ASIO_MOVE_CAST(WriteHandler)(handler));
|
||||
}
|
||||
|
||||
#endif // !defined(ASIO_NO_IOSTREAM)
|
||||
#endif // !defined(ASIO_NO_EXTENSIONS)
|
||||
#endif // !defined(ASIO_NO_DYNAMIC_BUFFER_V1)
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v2,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
class write_dynbuf_v2_op
|
||||
{
|
||||
public:
|
||||
template <typename BufferSequence>
|
||||
write_dynbuf_v2_op(AsyncWriteStream& stream,
|
||||
ASIO_MOVE_ARG(BufferSequence) buffers,
|
||||
CompletionCondition& completion_condition, WriteHandler& handler)
|
||||
: stream_(stream),
|
||||
buffers_(ASIO_MOVE_CAST(BufferSequence)(buffers)),
|
||||
completion_condition_(
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition)),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
write_dynbuf_v2_op(const write_dynbuf_v2_op& other)
|
||||
: stream_(other.stream_),
|
||||
buffers_(other.buffers_),
|
||||
completion_condition_(other.completion_condition_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
write_dynbuf_v2_op(write_dynbuf_v2_op&& other)
|
||||
: stream_(other.stream_),
|
||||
buffers_(ASIO_MOVE_CAST(DynamicBuffer_v2)(other.buffers_)),
|
||||
completion_condition_(
|
||||
ASIO_MOVE_CAST(CompletionCondition)(
|
||||
other.completion_condition_)),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec,
|
||||
std::size_t bytes_transferred, int start = 0)
|
||||
{
|
||||
switch (start)
|
||||
{
|
||||
case 1:
|
||||
async_write(stream_, buffers_.data(0, buffers_.size()),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition_),
|
||||
ASIO_MOVE_CAST(write_dynbuf_v2_op)(*this));
|
||||
return; default:
|
||||
buffers_.consume(bytes_transferred);
|
||||
handler_(ec, static_cast<const std::size_t&>(bytes_transferred));
|
||||
}
|
||||
}
|
||||
|
||||
//private:
|
||||
AsyncWriteStream& stream_;
|
||||
DynamicBuffer_v2 buffers_;
|
||||
CompletionCondition completion_condition_;
|
||||
WriteHandler handler_;
|
||||
};
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v2,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
write_dynbuf_v2_op<AsyncWriteStream, DynamicBuffer_v2,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v2,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
write_dynbuf_v2_op<AsyncWriteStream, DynamicBuffer_v2,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v2,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
write_dynbuf_v2_op<AsyncWriteStream, DynamicBuffer_v2,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncWriteStream,
|
||||
typename DynamicBuffer_v2, typename CompletionCondition,
|
||||
typename WriteHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
write_dynbuf_v2_op<AsyncWriteStream, DynamicBuffer_v2,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncWriteStream,
|
||||
typename DynamicBuffer_v2, typename CompletionCondition,
|
||||
typename WriteHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
write_dynbuf_v2_op<AsyncWriteStream, DynamicBuffer_v2,
|
||||
CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_write_dynbuf_v2
|
||||
{
|
||||
template <typename WriteHandler, typename AsyncWriteStream,
|
||||
typename DynamicBuffer_v2, typename CompletionCondition>
|
||||
void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
AsyncWriteStream* s, ASIO_MOVE_ARG(DynamicBuffer_v2) buffers,
|
||||
ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a WriteHandler.
|
||||
ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
||||
|
||||
non_const_lvalue<WriteHandler> handler2(handler);
|
||||
non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
|
||||
write_dynbuf_v2_op<AsyncWriteStream,
|
||||
typename decay<DynamicBuffer_v2>::type,
|
||||
CompletionCondition, typename decay<WriteHandler>::type>(
|
||||
*s, ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
|
||||
completion_cond2.value, handler2.value)(
|
||||
asio::error_code(), 0, 1);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v2,
|
||||
typename CompletionCondition, typename WriteHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::write_dynbuf_v2_op<AsyncWriteStream,
|
||||
DynamicBuffer_v2, CompletionCondition, WriteHandler>,
|
||||
Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<WriteHandler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_dynbuf_v2_op<AsyncWriteStream,
|
||||
DynamicBuffer_v2, CompletionCondition, WriteHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<WriteHandler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v2,
|
||||
typename CompletionCondition, typename WriteHandler, typename Executor>
|
||||
struct associated_executor<
|
||||
detail::write_dynbuf_v2_op<AsyncWriteStream,
|
||||
DynamicBuffer_v2, CompletionCondition, WriteHandler>,
|
||||
Executor>
|
||||
{
|
||||
typedef typename associated_executor<WriteHandler, Executor>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_dynbuf_v2_op<AsyncWriteStream,
|
||||
DynamicBuffer_v2, CompletionCondition, WriteHandler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<WriteHandler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncWriteStream,
|
||||
typename DynamicBuffer_v2, typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write(AsyncWriteStream& s, DynamicBuffer_v2 buffers,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v2<DynamicBuffer_v2>::value
|
||||
>::type*)
|
||||
{
|
||||
return async_write(s,
|
||||
ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
|
||||
transfer_all(), ASIO_MOVE_CAST(WriteHandler)(handler));
|
||||
}
|
||||
|
||||
template <typename AsyncWriteStream, typename DynamicBuffer_v2,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write(AsyncWriteStream& s, DynamicBuffer_v2 buffers,
|
||||
CompletionCondition completion_condition,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
typename enable_if<
|
||||
is_dynamic_buffer_v2<DynamicBuffer_v2>::value
|
||||
>::type*)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_write_dynbuf_v2(), handler, &s,
|
||||
ASIO_MOVE_CAST(DynamicBuffer_v2)(buffers),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
|
||||
}
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_WRITE_HPP
|
||||
+578
@@ -0,0 +1,578 @@
|
||||
//
|
||||
// impl/write_at.hpp
|
||||
// ~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef ASIO_IMPL_WRITE_AT_HPP
|
||||
#define ASIO_IMPL_WRITE_AT_HPP
|
||||
|
||||
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
# pragma once
|
||||
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
||||
|
||||
#include "asio/associated_allocator.hpp"
|
||||
#include "asio/associated_executor.hpp"
|
||||
#include "asio/buffer.hpp"
|
||||
#include "asio/completion_condition.hpp"
|
||||
#include "asio/detail/array_fwd.hpp"
|
||||
#include "asio/detail/base_from_completion_cond.hpp"
|
||||
#include "asio/detail/bind_handler.hpp"
|
||||
#include "asio/detail/consuming_buffers.hpp"
|
||||
#include "asio/detail/dependent_type.hpp"
|
||||
#include "asio/detail/handler_alloc_helpers.hpp"
|
||||
#include "asio/detail/handler_cont_helpers.hpp"
|
||||
#include "asio/detail/handler_invoke_helpers.hpp"
|
||||
#include "asio/detail/handler_type_requirements.hpp"
|
||||
#include "asio/detail/non_const_lvalue.hpp"
|
||||
#include "asio/detail/throw_error.hpp"
|
||||
|
||||
#include "asio/detail/push_options.hpp"
|
||||
|
||||
namespace asio {
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
|
||||
typename ConstBufferIterator, typename CompletionCondition>
|
||||
std::size_t write_at_buffer_sequence(SyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, const ConstBufferSequence& buffers,
|
||||
const ConstBufferIterator&, CompletionCondition completion_condition,
|
||||
asio::error_code& ec)
|
||||
{
|
||||
ec = asio::error_code();
|
||||
asio::detail::consuming_buffers<const_buffer,
|
||||
ConstBufferSequence, ConstBufferIterator> tmp(buffers);
|
||||
while (!tmp.empty())
|
||||
{
|
||||
if (std::size_t max_size = detail::adapt_completion_condition_result(
|
||||
completion_condition(ec, tmp.total_consumed())))
|
||||
{
|
||||
tmp.consume(d.write_some_at(offset + tmp.total_consumed(),
|
||||
tmp.prepare(max_size), ec));
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
return tmp.total_consumed();;
|
||||
}
|
||||
} // namespace detail
|
||||
|
||||
template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
|
||||
typename CompletionCondition>
|
||||
std::size_t write_at(SyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, const ConstBufferSequence& buffers,
|
||||
CompletionCondition completion_condition, asio::error_code& ec)
|
||||
{
|
||||
return detail::write_at_buffer_sequence(d, offset, buffers,
|
||||
asio::buffer_sequence_begin(buffers),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
|
||||
inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, const ConstBufferSequence& buffers)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write_at(
|
||||
d, offset, buffers, transfer_all(), ec);
|
||||
asio::detail::throw_error(ec, "write_at");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
|
||||
inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, const ConstBufferSequence& buffers,
|
||||
asio::error_code& ec)
|
||||
{
|
||||
return write_at(d, offset, buffers, transfer_all(), ec);
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, const ConstBufferSequence& buffers,
|
||||
CompletionCondition completion_condition)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write_at(d, offset, buffers,
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
asio::detail::throw_error(ec, "write_at");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_EXTENSIONS)
|
||||
#if !defined(ASIO_NO_IOSTREAM)
|
||||
|
||||
template <typename SyncRandomAccessWriteDevice, typename Allocator,
|
||||
typename CompletionCondition>
|
||||
std::size_t write_at(SyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
CompletionCondition completion_condition, asio::error_code& ec)
|
||||
{
|
||||
std::size_t bytes_transferred = write_at(d, offset, b.data(),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
b.consume(bytes_transferred);
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessWriteDevice, typename Allocator>
|
||||
inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write_at(d, offset, b, transfer_all(), ec);
|
||||
asio::detail::throw_error(ec, "write_at");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessWriteDevice, typename Allocator>
|
||||
inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
asio::error_code& ec)
|
||||
{
|
||||
return write_at(d, offset, b, transfer_all(), ec);
|
||||
}
|
||||
|
||||
template <typename SyncRandomAccessWriteDevice, typename Allocator,
|
||||
typename CompletionCondition>
|
||||
inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
CompletionCondition completion_condition)
|
||||
{
|
||||
asio::error_code ec;
|
||||
std::size_t bytes_transferred = write_at(d, offset, b,
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
|
||||
asio::detail::throw_error(ec, "write_at");
|
||||
return bytes_transferred;
|
||||
}
|
||||
|
||||
#endif // !defined(ASIO_NO_IOSTREAM)
|
||||
#endif // !defined(ASIO_NO_EXTENSIONS)
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
class write_at_op
|
||||
: detail::base_from_completion_cond<CompletionCondition>
|
||||
{
|
||||
public:
|
||||
write_at_op(AsyncRandomAccessWriteDevice& device,
|
||||
uint64_t offset, const ConstBufferSequence& buffers,
|
||||
CompletionCondition& completion_condition, WriteHandler& handler)
|
||||
: detail::base_from_completion_cond<
|
||||
CompletionCondition>(completion_condition),
|
||||
device_(device),
|
||||
offset_(offset),
|
||||
buffers_(buffers),
|
||||
start_(0),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
write_at_op(const write_at_op& other)
|
||||
: detail::base_from_completion_cond<CompletionCondition>(other),
|
||||
device_(other.device_),
|
||||
offset_(other.offset_),
|
||||
buffers_(other.buffers_),
|
||||
start_(other.start_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
write_at_op(write_at_op&& other)
|
||||
: detail::base_from_completion_cond<CompletionCondition>(
|
||||
ASIO_MOVE_CAST(detail::base_from_completion_cond<
|
||||
CompletionCondition>)(other)),
|
||||
device_(other.device_),
|
||||
offset_(other.offset_),
|
||||
buffers_(ASIO_MOVE_CAST(buffers_type)(other.buffers_)),
|
||||
start_(other.start_),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec,
|
||||
std::size_t bytes_transferred, int start = 0)
|
||||
{
|
||||
std::size_t max_size;
|
||||
switch (start_ = start)
|
||||
{
|
||||
case 1:
|
||||
max_size = this->check_for_completion(ec, buffers_.total_consumed());
|
||||
do
|
||||
{
|
||||
device_.async_write_some_at(
|
||||
offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
|
||||
ASIO_MOVE_CAST(write_at_op)(*this));
|
||||
return; default:
|
||||
buffers_.consume(bytes_transferred);
|
||||
if ((!ec && bytes_transferred == 0) || buffers_.empty())
|
||||
break;
|
||||
max_size = this->check_for_completion(ec, buffers_.total_consumed());
|
||||
} while (max_size > 0);
|
||||
|
||||
handler_(ec, buffers_.total_consumed());
|
||||
}
|
||||
}
|
||||
|
||||
//private:
|
||||
typedef asio::detail::consuming_buffers<const_buffer,
|
||||
ConstBufferSequence, ConstBufferIterator> buffers_type;
|
||||
|
||||
AsyncRandomAccessWriteDevice& device_;
|
||||
uint64_t offset_;
|
||||
buffers_type buffers_;
|
||||
int start_;
|
||||
WriteHandler handler_;
|
||||
};
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
return this_handler->start_ == 0 ? true
|
||||
: asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline void start_write_at_buffer_sequence_op(AsyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, const ConstBufferSequence& buffers,
|
||||
const ConstBufferIterator&, CompletionCondition& completion_condition,
|
||||
WriteHandler& handler)
|
||||
{
|
||||
detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>(
|
||||
d, offset, buffers, completion_condition, handler)(
|
||||
asio::error_code(), 0, 1);
|
||||
}
|
||||
|
||||
struct initiate_async_write_at_buffer_sequence
|
||||
{
|
||||
template <typename WriteHandler, typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename CompletionCondition>
|
||||
void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
AsyncRandomAccessWriteDevice* d, uint64_t offset,
|
||||
const ConstBufferSequence& buffers,
|
||||
ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a WriteHandler.
|
||||
ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
||||
|
||||
non_const_lvalue<WriteHandler> handler2(handler);
|
||||
non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
|
||||
start_write_at_buffer_sequence_op(*d, offset, buffers,
|
||||
asio::buffer_sequence_begin(buffers),
|
||||
completion_cond2.value, handler2.value);
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler, typename Allocator>
|
||||
struct associated_allocator<
|
||||
detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>,
|
||||
Allocator>
|
||||
{
|
||||
typedef typename associated_allocator<WriteHandler, Allocator>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_at_op<AsyncRandomAccessWriteDevice,
|
||||
ConstBufferSequence, ConstBufferIterator,
|
||||
CompletionCondition, WriteHandler>& h,
|
||||
const Allocator& a = Allocator()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<WriteHandler, Allocator>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice,
|
||||
typename ConstBufferSequence, typename ConstBufferIterator,
|
||||
typename CompletionCondition, typename WriteHandler, typename Executor>
|
||||
struct associated_executor<
|
||||
detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
|
||||
ConstBufferIterator, CompletionCondition, WriteHandler>,
|
||||
Executor>
|
||||
{
|
||||
typedef typename associated_executor<WriteHandler, Executor>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_at_op<AsyncRandomAccessWriteDevice,
|
||||
ConstBufferSequence, ConstBufferIterator,
|
||||
CompletionCondition, WriteHandler>& h,
|
||||
const Executor& ex = Executor()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<WriteHandler, Executor>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write_at(AsyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, const ConstBufferSequence& buffers,
|
||||
CompletionCondition completion_condition,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_write_at_buffer_sequence(), handler, &d, offset,
|
||||
buffers, ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
|
||||
typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write_at(AsyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, const ConstBufferSequence& buffers,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_write_at_buffer_sequence(),
|
||||
handler, &d, offset, buffers, transfer_all());
|
||||
}
|
||||
|
||||
#if !defined(ASIO_NO_EXTENSIONS)
|
||||
#if !defined(ASIO_NO_IOSTREAM)
|
||||
|
||||
namespace detail
|
||||
{
|
||||
template <typename Allocator, typename WriteHandler>
|
||||
class write_at_streambuf_op
|
||||
{
|
||||
public:
|
||||
write_at_streambuf_op(
|
||||
asio::basic_streambuf<Allocator>& streambuf,
|
||||
WriteHandler& handler)
|
||||
: streambuf_(streambuf),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
|
||||
{
|
||||
}
|
||||
|
||||
#if defined(ASIO_HAS_MOVE)
|
||||
write_at_streambuf_op(const write_at_streambuf_op& other)
|
||||
: streambuf_(other.streambuf_),
|
||||
handler_(other.handler_)
|
||||
{
|
||||
}
|
||||
|
||||
write_at_streambuf_op(write_at_streambuf_op&& other)
|
||||
: streambuf_(other.streambuf_),
|
||||
handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
|
||||
{
|
||||
}
|
||||
#endif // defined(ASIO_HAS_MOVE)
|
||||
|
||||
void operator()(const asio::error_code& ec,
|
||||
const std::size_t bytes_transferred)
|
||||
{
|
||||
streambuf_.consume(bytes_transferred);
|
||||
handler_(ec, bytes_transferred);
|
||||
}
|
||||
|
||||
//private:
|
||||
asio::basic_streambuf<Allocator>& streambuf_;
|
||||
WriteHandler handler_;
|
||||
};
|
||||
|
||||
template <typename Allocator, typename WriteHandler>
|
||||
inline void* asio_handler_allocate(std::size_t size,
|
||||
write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_alloc_helpers::allocate(
|
||||
size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Allocator, typename WriteHandler>
|
||||
inline void asio_handler_deallocate(void* pointer, std::size_t size,
|
||||
write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_alloc_helpers::deallocate(
|
||||
pointer, size, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Allocator, typename WriteHandler>
|
||||
inline bool asio_handler_is_continuation(
|
||||
write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
|
||||
{
|
||||
return asio_handler_cont_helpers::is_continuation(
|
||||
this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator, typename WriteHandler>
|
||||
inline void asio_handler_invoke(Function& function,
|
||||
write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
template <typename Function, typename Allocator, typename WriteHandler>
|
||||
inline void asio_handler_invoke(const Function& function,
|
||||
write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
|
||||
{
|
||||
asio_handler_invoke_helpers::invoke(
|
||||
function, this_handler->handler_);
|
||||
}
|
||||
|
||||
struct initiate_async_write_at_streambuf
|
||||
{
|
||||
template <typename WriteHandler, typename AsyncRandomAccessWriteDevice,
|
||||
typename Allocator, typename CompletionCondition>
|
||||
void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
|
||||
AsyncRandomAccessWriteDevice* d, uint64_t offset,
|
||||
basic_streambuf<Allocator>* b,
|
||||
ASIO_MOVE_ARG(CompletionCondition) completion_condition) const
|
||||
{
|
||||
// If you get an error on the following line it means that your handler
|
||||
// does not meet the documented type requirements for a WriteHandler.
|
||||
ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
|
||||
|
||||
non_const_lvalue<WriteHandler> handler2(handler);
|
||||
async_write_at(*d, offset, b->data(),
|
||||
ASIO_MOVE_CAST(CompletionCondition)(completion_condition),
|
||||
write_at_streambuf_op<Allocator, typename decay<WriteHandler>::type>(
|
||||
*b, handler2.value));
|
||||
}
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#if !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename Allocator, typename WriteHandler, typename Allocator1>
|
||||
struct associated_allocator<
|
||||
detail::write_at_streambuf_op<Allocator, WriteHandler>,
|
||||
Allocator1>
|
||||
{
|
||||
typedef typename associated_allocator<WriteHandler, Allocator1>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_at_streambuf_op<Allocator, WriteHandler>& h,
|
||||
const Allocator1& a = Allocator1()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_allocator<WriteHandler, Allocator1>::get(h.handler_, a);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Executor, typename WriteHandler, typename Executor1>
|
||||
struct associated_executor<
|
||||
detail::write_at_streambuf_op<Executor, WriteHandler>,
|
||||
Executor1>
|
||||
{
|
||||
typedef typename associated_executor<WriteHandler, Executor1>::type type;
|
||||
|
||||
static type get(
|
||||
const detail::write_at_streambuf_op<Executor, WriteHandler>& h,
|
||||
const Executor1& ex = Executor1()) ASIO_NOEXCEPT
|
||||
{
|
||||
return associated_executor<WriteHandler, Executor1>::get(h.handler_, ex);
|
||||
}
|
||||
};
|
||||
|
||||
#endif // !defined(GENERATING_DOCUMENTATION)
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice, typename Allocator,
|
||||
typename CompletionCondition, typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write_at(AsyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
CompletionCondition completion_condition,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_write_at_streambuf(), handler, &d, offset,
|
||||
&b, ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
|
||||
}
|
||||
|
||||
template <typename AsyncRandomAccessWriteDevice, typename Allocator,
|
||||
typename WriteHandler>
|
||||
inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
|
||||
void (asio::error_code, std::size_t))
|
||||
async_write_at(AsyncRandomAccessWriteDevice& d,
|
||||
uint64_t offset, asio::basic_streambuf<Allocator>& b,
|
||||
ASIO_MOVE_ARG(WriteHandler) handler)
|
||||
{
|
||||
return async_initiate<WriteHandler,
|
||||
void (asio::error_code, std::size_t)>(
|
||||
detail::initiate_async_write_at_streambuf(),
|
||||
handler, &d, offset, &b, transfer_all());
|
||||
}
|
||||
|
||||
#endif // !defined(ASIO_NO_IOSTREAM)
|
||||
#endif // !defined(ASIO_NO_EXTENSIONS)
|
||||
|
||||
} // namespace asio
|
||||
|
||||
#include "asio/detail/pop_options.hpp"
|
||||
|
||||
#endif // ASIO_IMPL_WRITE_AT_HPP
|
||||
Reference in New Issue
Block a user