Unnamed: 0
int64
0
0
repo_id
stringlengths
5
186
file_path
stringlengths
15
223
content
stringlengths
1
32.8M
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/thread_info_base.hpp
// // detail/thread_info_base.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_THREAD_INFO_BASE_HPP #define ASIO_DETAIL_THREAD_INFO_BASE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <climits> #include <cstddef> #include "asio/detail/memory.hpp" #include "asio/detail/noncopyable.hpp" #if !defined(ASIO_NO_EXCEPTIONS) # include <exception> # include "asio/multiple_exceptions.hpp" #endif // !defined(ASIO_NO_EXCEPTIONS) #include "asio/detail/push_options.hpp" namespace asio { namespace detail { #ifndef ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE # define ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE 2 #endif // ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE class thread_info_base : private noncopyable { public: struct default_tag { enum { cache_size = ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE, begin_mem_index = 0, end_mem_index = cache_size }; }; struct awaitable_frame_tag { enum { cache_size = ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE, begin_mem_index = default_tag::end_mem_index, end_mem_index = begin_mem_index + cache_size }; }; struct executor_function_tag { enum { cache_size = ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE, begin_mem_index = awaitable_frame_tag::end_mem_index, end_mem_index = begin_mem_index + cache_size }; }; struct cancellation_signal_tag { enum { cache_size = ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE, begin_mem_index = executor_function_tag::end_mem_index, end_mem_index = begin_mem_index + cache_size }; }; struct parallel_group_tag { enum { cache_size = ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE, begin_mem_index = cancellation_signal_tag::end_mem_index, end_mem_index = begin_mem_index + cache_size }; }; struct timed_cancel_tag { enum { cache_size = ASIO_RECYCLING_ALLOCATOR_CACHE_SIZE, begin_mem_index = parallel_group_tag::end_mem_index, end_mem_index = begin_mem_index + cache_size }; }; enum { max_mem_index = timed_cancel_tag::end_mem_index }; thread_info_base() #if !defined(ASIO_NO_EXCEPTIONS) : has_pending_exception_(0) #endif // !defined(ASIO_NO_EXCEPTIONS) { for (int i = 0; i < max_mem_index; ++i) reusable_memory_[i] = 0; } ~thread_info_base() { for (int i = 0; i < max_mem_index; ++i) { // The following test for non-null pointers is technically redundant, but // it is significantly faster when using a tight io_context::poll() loop // in latency sensitive applications. if (reusable_memory_[i]) aligned_delete(reusable_memory_[i]); } } static void* allocate(thread_info_base* this_thread, std::size_t size, std::size_t align = ASIO_DEFAULT_ALIGN) { return allocate(default_tag(), this_thread, size, align); } static void deallocate(thread_info_base* this_thread, void* pointer, std::size_t size) { deallocate(default_tag(), this_thread, pointer, size); } template <typename Purpose> static void* allocate(Purpose, thread_info_base* this_thread, std::size_t size, std::size_t align = ASIO_DEFAULT_ALIGN) { std::size_t chunks = (size + chunk_size - 1) / chunk_size; if (this_thread) { for (int mem_index = Purpose::begin_mem_index; mem_index < Purpose::end_mem_index; ++mem_index) { if (this_thread->reusable_memory_[mem_index]) { void* const pointer = this_thread->reusable_memory_[mem_index]; unsigned char* const mem = static_cast<unsigned char*>(pointer); if (static_cast<std::size_t>(mem[0]) >= chunks && reinterpret_cast<std::size_t>(pointer) % align == 0) { this_thread->reusable_memory_[mem_index] = 0; mem[size] = mem[0]; return pointer; } } } for (int mem_index = Purpose::begin_mem_index; mem_index < Purpose::end_mem_index; ++mem_index) { if (this_thread->reusable_memory_[mem_index]) { void* const pointer = this_thread->reusable_memory_[mem_index]; this_thread->reusable_memory_[mem_index] = 0; aligned_delete(pointer); break; } } } void* const pointer = aligned_new(align, chunks * chunk_size + 1); unsigned char* const mem = static_cast<unsigned char*>(pointer); mem[size] = (chunks <= UCHAR_MAX) ? static_cast<unsigned char>(chunks) : 0; return pointer; } template <typename Purpose> static void deallocate(Purpose, thread_info_base* this_thread, void* pointer, std::size_t size) { if (size <= chunk_size * UCHAR_MAX) { if (this_thread) { for (int mem_index = Purpose::begin_mem_index; mem_index < Purpose::end_mem_index; ++mem_index) { if (this_thread->reusable_memory_[mem_index] == 0) { unsigned char* const mem = static_cast<unsigned char*>(pointer); mem[0] = mem[size]; this_thread->reusable_memory_[mem_index] = pointer; return; } } } } aligned_delete(pointer); } void capture_current_exception() { #if !defined(ASIO_NO_EXCEPTIONS) switch (has_pending_exception_) { case 0: has_pending_exception_ = 1; pending_exception_ = std::current_exception(); break; case 1: has_pending_exception_ = 2; pending_exception_ = std::make_exception_ptr<multiple_exceptions>( multiple_exceptions(pending_exception_)); break; default: break; } #endif // !defined(ASIO_NO_EXCEPTIONS) } void rethrow_pending_exception() { #if !defined(ASIO_NO_EXCEPTIONS) if (has_pending_exception_ > 0) { has_pending_exception_ = 0; std::exception_ptr ex( static_cast<std::exception_ptr&&>( pending_exception_)); std::rethrow_exception(ex); } #endif // !defined(ASIO_NO_EXCEPTIONS) } private: #if defined(ASIO_HAS_IO_URING) enum { chunk_size = 8 }; #else // defined(ASIO_HAS_IO_URING) enum { chunk_size = 4 }; #endif // defined(ASIO_HAS_IO_URING) void* reusable_memory_[max_mem_index]; #if !defined(ASIO_NO_EXCEPTIONS) int has_pending_exception_; std::exception_ptr pending_exception_; #endif // !defined(ASIO_NO_EXCEPTIONS) }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_THREAD_INFO_BASE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/std_event.hpp
// // detail/std_event.hpp // ~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_STD_EVENT_HPP #define ASIO_DETAIL_STD_EVENT_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <chrono> #include <condition_variable> #include "asio/detail/assert.hpp" #include "asio/detail/noncopyable.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class std_event : private noncopyable { public: // Constructor. std_event() : state_(0) { } // Destructor. ~std_event() { } // Signal the event. (Retained for backward compatibility.) template <typename Lock> void signal(Lock& lock) { this->signal_all(lock); } // Signal all waiters. template <typename Lock> void signal_all(Lock& lock) { ASIO_ASSERT(lock.locked()); (void)lock; state_ |= 1; cond_.notify_all(); } // Unlock the mutex and signal one waiter. template <typename Lock> void unlock_and_signal_one(Lock& lock) { ASIO_ASSERT(lock.locked()); state_ |= 1; bool have_waiters = (state_ > 1); lock.unlock(); if (have_waiters) cond_.notify_one(); } // Unlock the mutex and signal one waiter who may destroy us. template <typename Lock> void unlock_and_signal_one_for_destruction(Lock& lock) { ASIO_ASSERT(lock.locked()); state_ |= 1; bool have_waiters = (state_ > 1); if (have_waiters) cond_.notify_one(); lock.unlock(); } // If there's a waiter, unlock the mutex and signal it. template <typename Lock> bool maybe_unlock_and_signal_one(Lock& lock) { ASIO_ASSERT(lock.locked()); state_ |= 1; if (state_ > 1) { lock.unlock(); cond_.notify_one(); return true; } return false; } // Reset the event. template <typename Lock> void clear(Lock& lock) { ASIO_ASSERT(lock.locked()); (void)lock; state_ &= ~std::size_t(1); } // Wait for the event to become signalled. template <typename Lock> void wait(Lock& lock) { ASIO_ASSERT(lock.locked()); unique_lock_adapter u_lock(lock); while ((state_ & 1) == 0) { waiter w(state_); cond_.wait(u_lock.unique_lock_); } } // Timed wait for the event to become signalled. template <typename Lock> bool wait_for_usec(Lock& lock, long usec) { ASIO_ASSERT(lock.locked()); unique_lock_adapter u_lock(lock); if ((state_ & 1) == 0) { waiter w(state_); cond_.wait_for(u_lock.unique_lock_, std::chrono::microseconds(usec)); } return (state_ & 1) != 0; } private: // Helper class to temporarily adapt a scoped_lock into a unique_lock so that // it can be passed to std::condition_variable::wait(). struct unique_lock_adapter { template <typename Lock> explicit unique_lock_adapter(Lock& lock) : unique_lock_(lock.mutex().mutex_, std::adopt_lock) { } ~unique_lock_adapter() { unique_lock_.release(); } std::unique_lock<std::mutex> unique_lock_; }; // Helper to increment and decrement the state to track outstanding waiters. class waiter { public: explicit waiter(std::size_t& state) : state_(state) { state_ += 2; } ~waiter() { state_ -= 2; } private: std::size_t& state_; }; std::condition_variable cond_; std::size_t state_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_STD_EVENT_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/throw_exception.hpp
// // detail/throw_exception.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_THROW_EXCEPTION_HPP #define ASIO_DETAIL_THROW_EXCEPTION_HPP #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_BOOST_THROW_EXCEPTION) # include <boost/throw_exception.hpp> #endif // defined(ASIO_BOOST_THROW_EXCEPTION) namespace asio { namespace detail { #if defined(ASIO_HAS_BOOST_THROW_EXCEPTION) using boost::throw_exception; #else // defined(ASIO_HAS_BOOST_THROW_EXCEPTION) // Declare the throw_exception function for all targets. template <typename Exception> void throw_exception( const Exception& e ASIO_SOURCE_LOCATION_DEFAULTED_PARAM); // Only define the throw_exception function when exceptions are enabled. // Otherwise, it is up to the application to provide a definition of this // function. # if !defined(ASIO_NO_EXCEPTIONS) template <typename Exception> void throw_exception( const Exception& e ASIO_SOURCE_LOCATION_PARAM) { throw e; } # endif // !defined(ASIO_NO_EXCEPTIONS) #endif // defined(ASIO_HAS_BOOST_THROW_EXCEPTION) } // namespace detail } // namespace asio #endif // ASIO_DETAIL_THROW_EXCEPTION_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/pipe_select_interrupter.hpp
// // detail/pipe_select_interrupter.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_PIPE_SELECT_INTERRUPTER_HPP #define ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP #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) #if !defined(ASIO_WINDOWS_RUNTIME) #if !defined(__CYGWIN__) #if !defined(__SYMBIAN32__) #if !defined(ASIO_HAS_EVENTFD) #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class pipe_select_interrupter { public: // Constructor. ASIO_DECL pipe_select_interrupter(); // Destructor. ASIO_DECL ~pipe_select_interrupter(); // Recreate the interrupter's descriptors. Used after a fork. ASIO_DECL void recreate(); // Interrupt the select call. ASIO_DECL void interrupt(); // Reset the select interrupter. Returns true if the reset was successful. ASIO_DECL bool reset(); // Get the read descriptor to be passed to select. int read_descriptor() const { return read_descriptor_; } private: // Open the descriptors. Throws on error. ASIO_DECL void open_descriptors(); // Close the descriptors. ASIO_DECL void close_descriptors(); // The read end of a connection used to interrupt the select call. This file // descriptor is passed to select such that when it is time to stop, a single // byte will be written on the other end of the connection and this // descriptor will become readable. int read_descriptor_; // The write end of a connection used to interrupt the select call. A single // byte may be written to this to wake up the select which is waiting for the // other end to become readable. int write_descriptor_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/pipe_select_interrupter.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // !defined(ASIO_HAS_EVENTFD) #endif // !defined(__SYMBIAN32__) #endif // !defined(__CYGWIN__) #endif // !defined(ASIO_WINDOWS_RUNTIME) #endif // !defined(ASIO_WINDOWS) #endif // ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/is_executor.hpp
// // detail/is_executor.hpp // ~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_IS_EXECUTOR_HPP #define ASIO_DETAIL_IS_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/type_traits.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { struct executor_memfns_base { void context(); void on_work_started(); void on_work_finished(); void dispatch(); void post(); void defer(); }; template <typename T> struct executor_memfns_derived : T, executor_memfns_base { }; template <typename T, T> struct executor_memfns_check { }; template <typename> char (&context_memfn_helper(...))[2]; template <typename T> char context_memfn_helper( executor_memfns_check< void (executor_memfns_base::*)(), &executor_memfns_derived<T>::context>*); template <typename> char (&on_work_started_memfn_helper(...))[2]; template <typename T> char on_work_started_memfn_helper( executor_memfns_check< void (executor_memfns_base::*)(), &executor_memfns_derived<T>::on_work_started>*); template <typename> char (&on_work_finished_memfn_helper(...))[2]; template <typename T> char on_work_finished_memfn_helper( executor_memfns_check< void (executor_memfns_base::*)(), &executor_memfns_derived<T>::on_work_finished>*); template <typename> char (&dispatch_memfn_helper(...))[2]; template <typename T> char dispatch_memfn_helper( executor_memfns_check< void (executor_memfns_base::*)(), &executor_memfns_derived<T>::dispatch>*); template <typename> char (&post_memfn_helper(...))[2]; template <typename T> char post_memfn_helper( executor_memfns_check< void (executor_memfns_base::*)(), &executor_memfns_derived<T>::post>*); template <typename> char (&defer_memfn_helper(...))[2]; template <typename T> char defer_memfn_helper( executor_memfns_check< void (executor_memfns_base::*)(), &executor_memfns_derived<T>::defer>*); template <typename T> struct is_executor_class : integral_constant<bool, sizeof(context_memfn_helper<T>(0)) != 1 && sizeof(on_work_started_memfn_helper<T>(0)) != 1 && sizeof(on_work_finished_memfn_helper<T>(0)) != 1 && sizeof(dispatch_memfn_helper<T>(0)) != 1 && sizeof(post_memfn_helper<T>(0)) != 1 && sizeof(defer_memfn_helper<T>(0)) != 1> { }; template <typename T> struct is_executor : conditional<is_class<T>::value, is_executor_class<T>, false_type>::type { }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_IS_EXECUTOR_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/timer_queue.hpp
// // detail/timer_queue.hpp // ~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_TIMER_QUEUE_HPP #define ASIO_DETAIL_TIMER_QUEUE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <cstddef> #include <vector> #include "asio/detail/cstdint.hpp" #include "asio/detail/date_time_fwd.hpp" #include "asio/detail/limits.hpp" #include "asio/detail/op_queue.hpp" #include "asio/detail/timer_queue_base.hpp" #include "asio/detail/wait_op.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Time_Traits> class timer_queue : public timer_queue_base { public: // The time type. typedef typename Time_Traits::time_type time_type; // The duration type. typedef typename Time_Traits::duration_type duration_type; // Per-timer data. class per_timer_data { public: per_timer_data() : heap_index_((std::numeric_limits<std::size_t>::max)()), next_(0), prev_(0) { } private: friend class timer_queue; // The operations waiting on the timer. op_queue<wait_op> op_queue_; // The index of the timer in the heap. std::size_t heap_index_; // Pointers to adjacent timers in a linked list. per_timer_data* next_; per_timer_data* prev_; }; // Constructor. timer_queue() : timers_(), heap_() { } // Add a new timer to the queue. Returns true if this is the timer that is // earliest in the queue, in which case the reactor's event demultiplexing // function call may need to be interrupted and restarted. bool enqueue_timer(const time_type& time, per_timer_data& timer, wait_op* op) { // Enqueue the timer object. if (timer.prev_ == 0 && &timer != timers_) { if (this->is_positive_infinity(time)) { // No heap entry is required for timers that never expire. timer.heap_index_ = (std::numeric_limits<std::size_t>::max)(); } else { // Put the new timer at the correct position in the heap. This is done // first since push_back() can throw due to allocation failure. timer.heap_index_ = heap_.size(); heap_entry entry = { time, &timer }; heap_.push_back(entry); up_heap(heap_.size() - 1); } // Insert the new timer into the linked list of active timers. timer.next_ = timers_; timer.prev_ = 0; if (timers_) timers_->prev_ = &timer; timers_ = &timer; } // Enqueue the individual timer operation. timer.op_queue_.push(op); // Interrupt reactor only if newly added timer is first to expire. return timer.heap_index_ == 0 && timer.op_queue_.front() == op; } // Whether there are no timers in the queue. virtual bool empty() const { return timers_ == 0; } // Get the time for the timer that is earliest in the queue. virtual long wait_duration_msec(long max_duration) const { if (heap_.empty()) return max_duration; return this->to_msec( Time_Traits::to_posix_duration( Time_Traits::subtract(heap_[0].time_, Time_Traits::now())), max_duration); } // Get the time for the timer that is earliest in the queue. virtual long wait_duration_usec(long max_duration) const { if (heap_.empty()) return max_duration; return this->to_usec( Time_Traits::to_posix_duration( Time_Traits::subtract(heap_[0].time_, Time_Traits::now())), max_duration); } // Dequeue all timers not later than the current time. virtual void get_ready_timers(op_queue<operation>& ops) { if (!heap_.empty()) { const time_type now = Time_Traits::now(); while (!heap_.empty() && !Time_Traits::less_than(now, heap_[0].time_)) { per_timer_data* timer = heap_[0].timer_; while (wait_op* op = timer->op_queue_.front()) { timer->op_queue_.pop(); op->ec_ = asio::error_code(); ops.push(op); } remove_timer(*timer); } } } // Dequeue all timers. virtual void get_all_timers(op_queue<operation>& ops) { while (timers_) { per_timer_data* timer = timers_; timers_ = timers_->next_; ops.push(timer->op_queue_); timer->next_ = 0; timer->prev_ = 0; } heap_.clear(); } // Cancel and dequeue operations for the given timer. std::size_t cancel_timer(per_timer_data& timer, op_queue<operation>& ops, std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)()) { std::size_t num_cancelled = 0; if (timer.prev_ != 0 || &timer == timers_) { while (wait_op* op = (num_cancelled != max_cancelled) ? timer.op_queue_.front() : 0) { op->ec_ = asio::error::operation_aborted; timer.op_queue_.pop(); ops.push(op); ++num_cancelled; } if (timer.op_queue_.empty()) remove_timer(timer); } return num_cancelled; } // Cancel and dequeue a specific operation for the given timer. void cancel_timer_by_key(per_timer_data* timer, op_queue<operation>& ops, void* cancellation_key) { if (timer->prev_ != 0 || timer == timers_) { op_queue<wait_op> other_ops; while (wait_op* op = timer->op_queue_.front()) { timer->op_queue_.pop(); if (op->cancellation_key_ == cancellation_key) { op->ec_ = asio::error::operation_aborted; ops.push(op); } else other_ops.push(op); } timer->op_queue_.push(other_ops); if (timer->op_queue_.empty()) remove_timer(*timer); } } // Move operations from one timer to another, empty timer. void move_timer(per_timer_data& target, per_timer_data& source) { target.op_queue_.push(source.op_queue_); target.heap_index_ = source.heap_index_; source.heap_index_ = (std::numeric_limits<std::size_t>::max)(); if (target.heap_index_ < heap_.size()) heap_[target.heap_index_].timer_ = &target; if (timers_ == &source) timers_ = &target; if (source.prev_) source.prev_->next_ = &target; if (source.next_) source.next_->prev_= &target; target.next_ = source.next_; target.prev_ = source.prev_; source.next_ = 0; source.prev_ = 0; } private: // Move the item at the given index up the heap to its correct position. void up_heap(std::size_t index) { while (index > 0) { std::size_t parent = (index - 1) / 2; if (!Time_Traits::less_than(heap_[index].time_, heap_[parent].time_)) break; swap_heap(index, parent); index = parent; } } // Move the item at the given index down the heap to its correct position. void down_heap(std::size_t index) { std::size_t child = index * 2 + 1; while (child < heap_.size()) { std::size_t min_child = (child + 1 == heap_.size() || Time_Traits::less_than( heap_[child].time_, heap_[child + 1].time_)) ? child : child + 1; if (Time_Traits::less_than(heap_[index].time_, heap_[min_child].time_)) break; swap_heap(index, min_child); index = min_child; child = index * 2 + 1; } } // Swap two entries in the heap. void swap_heap(std::size_t index1, std::size_t index2) { heap_entry tmp = heap_[index1]; heap_[index1] = heap_[index2]; heap_[index2] = tmp; heap_[index1].timer_->heap_index_ = index1; heap_[index2].timer_->heap_index_ = index2; } // Remove a timer from the heap and list of timers. void remove_timer(per_timer_data& timer) { // Remove the timer from the heap. std::size_t index = timer.heap_index_; if (!heap_.empty() && index < heap_.size()) { if (index == heap_.size() - 1) { timer.heap_index_ = (std::numeric_limits<std::size_t>::max)(); heap_.pop_back(); } else { swap_heap(index, heap_.size() - 1); timer.heap_index_ = (std::numeric_limits<std::size_t>::max)(); heap_.pop_back(); if (index > 0 && Time_Traits::less_than( heap_[index].time_, heap_[(index - 1) / 2].time_)) up_heap(index); else down_heap(index); } } // Remove the timer from the linked list of active timers. if (timers_ == &timer) timers_ = timer.next_; if (timer.prev_) timer.prev_->next_ = timer.next_; if (timer.next_) timer.next_->prev_= timer.prev_; timer.next_ = 0; timer.prev_ = 0; } // Determine if the specified absolute time is positive infinity. template <typename Time_Type> static bool is_positive_infinity(const Time_Type&) { return false; } // Determine if the specified absolute time is positive infinity. template <typename T, typename TimeSystem> static bool is_positive_infinity( const boost::date_time::base_time<T, TimeSystem>& time) { return time.is_pos_infinity(); } // Helper function to convert a duration into milliseconds. template <typename Duration> long to_msec(const Duration& d, long max_duration) const { if (d.ticks() <= 0) return 0; int64_t msec = d.total_milliseconds(); if (msec == 0) return 1; if (msec > max_duration) return max_duration; return static_cast<long>(msec); } // Helper function to convert a duration into microseconds. template <typename Duration> long to_usec(const Duration& d, long max_duration) const { if (d.ticks() <= 0) return 0; int64_t usec = d.total_microseconds(); if (usec == 0) return 1; if (usec > max_duration) return max_duration; return static_cast<long>(usec); } // The head of a linked list of all active timers. per_timer_data* timers_; struct heap_entry { // The time when the timer should fire. time_type time_; // The associated timer with enqueued operations. per_timer_data* timer_; }; // The heap of timers, with the earliest timer at the front. std::vector<heap_entry> heap_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_TIMER_QUEUE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/wait_op.hpp
// // detail/wait_op.hpp // ~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WAIT_OP_HPP #define ASIO_DETAIL_WAIT_OP_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/operation.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class wait_op : public operation { public: // The error code to be passed to the completion handler. asio::error_code ec_; // The operation key used for targeted cancellation. void* cancellation_key_; protected: wait_op(func_type func) : operation(func), cancellation_key_(0) { } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_WAIT_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/tss_ptr.hpp
// // detail/tss_ptr.hpp // ~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_TSS_PTR_HPP #define ASIO_DETAIL_TSS_PTR_HPP #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_THREADS) # include "asio/detail/null_tss_ptr.hpp" #elif defined(ASIO_HAS_THREAD_KEYWORD_EXTENSION) # include "asio/detail/keyword_tss_ptr.hpp" #elif defined(ASIO_WINDOWS) # include "asio/detail/win_tss_ptr.hpp" #elif defined(ASIO_HAS_PTHREADS) # include "asio/detail/posix_tss_ptr.hpp" #else # error Only Windows and POSIX are supported! #endif #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename T> class tss_ptr #if !defined(ASIO_HAS_THREADS) : public null_tss_ptr<T> #elif defined(ASIO_HAS_THREAD_KEYWORD_EXTENSION) : public keyword_tss_ptr<T> #elif defined(ASIO_WINDOWS) : public win_tss_ptr<T> #elif defined(ASIO_HAS_PTHREADS) : public posix_tss_ptr<T> #endif { public: void operator=(T* value) { #if !defined(ASIO_HAS_THREADS) null_tss_ptr<T>::operator=(value); #elif defined(ASIO_HAS_THREAD_KEYWORD_EXTENSION) keyword_tss_ptr<T>::operator=(value); #elif defined(ASIO_WINDOWS) win_tss_ptr<T>::operator=(value); #elif defined(ASIO_HAS_PTHREADS) posix_tss_ptr<T>::operator=(value); #endif } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_TSS_PTR_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/executor_function.hpp
// // detail/executor_function.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_EXECUTOR_FUNCTION_HPP #define ASIO_DETAIL_EXECUTOR_FUNCTION_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/memory.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { // Lightweight, move-only function object wrapper. class executor_function { public: template <typename F, typename Alloc> explicit executor_function(F f, const Alloc& a) { // Allocate and construct an object to wrap the function. typedef impl<F, Alloc> impl_type; typename impl_type::ptr p = { detail::addressof(a), impl_type::ptr::allocate(a), 0 }; impl_ = new (p.v) impl_type(static_cast<F&&>(f), a); p.v = 0; } executor_function(executor_function&& other) noexcept : impl_(other.impl_) { other.impl_ = 0; } ~executor_function() { if (impl_) impl_->complete_(impl_, false); } void operator()() { if (impl_) { impl_base* i = impl_; impl_ = 0; i->complete_(i, true); } } private: // Base class for polymorphic function implementations. struct impl_base { void (*complete_)(impl_base*, bool); }; // Polymorphic function implementation. template <typename Function, typename Alloc> struct impl : impl_base { ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR( thread_info_base::executor_function_tag, impl); template <typename F> impl(F&& f, const Alloc& a) : function_(static_cast<F&&>(f)), allocator_(a) { complete_ = &executor_function::complete<Function, Alloc>; } Function function_; Alloc allocator_; }; // Helper to complete function invocation. template <typename Function, typename Alloc> static void complete(impl_base* base, bool call) { // Take ownership of the function object. impl<Function, Alloc>* i(static_cast<impl<Function, Alloc>*>(base)); Alloc allocator(i->allocator_); typename impl<Function, Alloc>::ptr p = { detail::addressof(allocator), i, i }; // Make a copy of the function so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the function may be the true owner of the memory // associated with the function. Consequently, a local copy of the function // is required to ensure that any owning sub-object remains valid until // after we have deallocated the memory here. Function function(static_cast<Function&&>(i->function_)); p.reset(); // Make the upcall if required. if (call) { static_cast<Function&&>(function)(); } } impl_base* impl_; }; // Lightweight, non-owning, copyable function object wrapper. class executor_function_view { public: template <typename F> explicit executor_function_view(F& f) noexcept : complete_(&executor_function_view::complete<F>), function_(&f) { } void operator()() { complete_(function_); } private: // Helper to complete function invocation. template <typename F> static void complete(void* f) { (*static_cast<F*>(f))(); } void (*complete_)(void*); void* function_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_EXECUTOR_FUNCTION_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/reactor_op.hpp
// // detail/reactor_op.hpp // ~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REACTOR_OP_HPP #define ASIO_DETAIL_REACTOR_OP_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/operation.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class reactor_op : public operation { public: // The error code to be passed to the completion handler. asio::error_code ec_; // The operation key used for targeted cancellation. void* cancellation_key_; // The number of bytes transferred, to be passed to the completion handler. std::size_t bytes_transferred_; // Status returned by perform function. May be used to decide whether it is // worth performing more operations on the descriptor immediately. enum status { not_done, done, done_and_exhausted }; // Perform the operation. Returns true if it is finished. status perform() { return perform_func_(this); } protected: typedef status (*perform_func_type)(reactor_op*); reactor_op(const asio::error_code& success_ec, perform_func_type perform_func, func_type complete_func) : operation(complete_func), ec_(success_ec), cancellation_key_(0), bytes_transferred_(0), perform_func_(perform_func) { } private: perform_func_type perform_func_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_REACTOR_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/posix_static_mutex.hpp
// // detail/posix_static_mutex.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_POSIX_STATIC_MUTEX_HPP #define ASIO_DETAIL_POSIX_STATIC_MUTEX_HPP #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_PTHREADS) #include <pthread.h> #include "asio/detail/scoped_lock.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { struct posix_static_mutex { typedef asio::detail::scoped_lock<posix_static_mutex> scoped_lock; // Initialise the mutex. void init() { // Nothing to do. } // Lock the mutex. void lock() { (void)::pthread_mutex_lock(&mutex_); // Ignore EINVAL. } // Unlock the mutex. void unlock() { (void)::pthread_mutex_unlock(&mutex_); // Ignore EINVAL. } ::pthread_mutex_t mutex_; }; #define ASIO_POSIX_STATIC_MUTEX_INIT { PTHREAD_MUTEX_INITIALIZER } } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_PTHREADS) #endif // ASIO_DETAIL_POSIX_STATIC_MUTEX_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/handler_work.hpp
// // detail/handler_work.hpp // ~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_HANDLER_WORK_HPP #define ASIO_DETAIL_HANDLER_WORK_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/associated_immediate_executor.hpp" #include "asio/detail/initiate_dispatch.hpp" #include "asio/detail/type_traits.hpp" #include "asio/detail/work_dispatcher.hpp" #include "asio/execution/allocator.hpp" #include "asio/execution/blocking.hpp" #include "asio/execution/executor.hpp" #include "asio/execution/outstanding_work.hpp" #include "asio/executor_work_guard.hpp" #include "asio/prefer.hpp" #include "asio/detail/push_options.hpp" namespace asio { class executor; class io_context; #if !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT) class any_completion_executor; class any_io_executor; #endif // !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT) namespace execution { template <typename...> class any_executor; } // namespace execution namespace detail { template <typename Executor, typename CandidateExecutor = void, typename IoContext = io_context, typename PolymorphicExecutor = executor, typename = void> class handler_work_base { public: explicit handler_work_base(int, int, const Executor& ex) noexcept : executor_(asio::prefer(ex, execution::outstanding_work.tracked)) { } template <typename OtherExecutor> handler_work_base(bool /*base1_owns_work*/, const Executor& ex, const OtherExecutor& /*candidate*/) noexcept : executor_(asio::prefer(ex, execution::outstanding_work.tracked)) { } handler_work_base(const handler_work_base& other) noexcept : executor_(other.executor_) { } handler_work_base(handler_work_base&& other) noexcept : executor_(static_cast<executor_type&&>(other.executor_)) { } bool owns_work() const noexcept { return true; } template <typename Function, typename Handler> void dispatch(Function& function, Handler& handler) { asio::prefer(executor_, execution::allocator((get_associated_allocator)(handler)) ).execute(static_cast<Function&&>(function)); } private: typedef decay_t< prefer_result_t<Executor, execution::outstanding_work_t::tracked_t> > executor_type; executor_type executor_; }; template <typename Executor, typename CandidateExecutor, typename IoContext, typename PolymorphicExecutor> class handler_work_base<Executor, CandidateExecutor, IoContext, PolymorphicExecutor, enable_if_t< !execution::is_executor<Executor>::value && (!is_same<Executor, PolymorphicExecutor>::value || !is_same<CandidateExecutor, void>::value) > > { public: explicit handler_work_base(int, int, const Executor& ex) noexcept : executor_(ex), owns_work_(true) { executor_.on_work_started(); } handler_work_base(bool /*base1_owns_work*/, const Executor& ex, const Executor& candidate) noexcept : executor_(ex), owns_work_(ex != candidate) { if (owns_work_) executor_.on_work_started(); } template <typename OtherExecutor> handler_work_base(bool /*base1_owns_work*/, const Executor& ex, const OtherExecutor& /*candidate*/) noexcept : executor_(ex), owns_work_(true) { executor_.on_work_started(); } handler_work_base(const handler_work_base& other) noexcept : executor_(other.executor_), owns_work_(other.owns_work_) { if (owns_work_) executor_.on_work_started(); } handler_work_base(handler_work_base&& other) noexcept : executor_(static_cast<Executor&&>(other.executor_)), owns_work_(other.owns_work_) { other.owns_work_ = false; } ~handler_work_base() { if (owns_work_) executor_.on_work_finished(); } bool owns_work() const noexcept { return owns_work_; } template <typename Function, typename Handler> void dispatch(Function& function, Handler& handler) { executor_.dispatch(static_cast<Function&&>(function), asio::get_associated_allocator(handler)); } private: Executor executor_; bool owns_work_; }; template <typename Executor, typename IoContext, typename PolymorphicExecutor> class handler_work_base<Executor, void, IoContext, PolymorphicExecutor, enable_if_t< is_same< Executor, typename IoContext::executor_type >::value > > { public: explicit handler_work_base(int, int, const Executor&) { } bool owns_work() const noexcept { return false; } template <typename Function, typename Handler> void dispatch(Function& function, Handler&) { // When using a native implementation, I/O completion handlers are // already dispatched according to the execution context's executor's // rules. We can call the function directly. static_cast<Function&&>(function)(); } }; template <typename Executor, typename IoContext> class handler_work_base<Executor, void, IoContext, Executor> { public: explicit handler_work_base(int, int, const Executor& ex) noexcept #if !defined(ASIO_NO_TYPEID) : executor_( ex.target_type() == typeid(typename IoContext::executor_type) ? Executor() : ex) #else // !defined(ASIO_NO_TYPEID) : executor_(ex) #endif // !defined(ASIO_NO_TYPEID) { if (executor_) executor_.on_work_started(); } handler_work_base(bool /*base1_owns_work*/, const Executor& ex, const Executor& candidate) noexcept : executor_(ex != candidate ? ex : Executor()) { if (executor_) executor_.on_work_started(); } template <typename OtherExecutor> handler_work_base(const Executor& ex, const OtherExecutor&) noexcept : executor_(ex) { executor_.on_work_started(); } handler_work_base(const handler_work_base& other) noexcept : executor_(other.executor_) { if (executor_) executor_.on_work_started(); } handler_work_base(handler_work_base&& other) noexcept : executor_(static_cast<Executor&&>(other.executor_)) { } ~handler_work_base() { if (executor_) executor_.on_work_finished(); } bool owns_work() const noexcept { return !!executor_; } template <typename Function, typename Handler> void dispatch(Function& function, Handler& handler) { executor_.dispatch(static_cast<Function&&>(function), asio::get_associated_allocator(handler)); } private: Executor executor_; }; template <typename... SupportableProperties, typename CandidateExecutor, typename IoContext, typename PolymorphicExecutor> class handler_work_base<execution::any_executor<SupportableProperties...>, CandidateExecutor, IoContext, PolymorphicExecutor> { public: typedef execution::any_executor<SupportableProperties...> executor_type; explicit handler_work_base(int, int, const executor_type& ex) noexcept #if !defined(ASIO_NO_TYPEID) : executor_( ex.target_type() == typeid(typename IoContext::executor_type) ? executor_type() : asio::prefer(ex, execution::outstanding_work.tracked)) #else // !defined(ASIO_NO_TYPEID) : executor_(asio::prefer(ex, execution::outstanding_work.tracked)) #endif // !defined(ASIO_NO_TYPEID) { } handler_work_base(bool base1_owns_work, const executor_type& ex, const executor_type& candidate) noexcept : executor_( !base1_owns_work && ex == candidate ? executor_type() : asio::prefer(ex, execution::outstanding_work.tracked)) { } template <typename OtherExecutor> handler_work_base(bool /*base1_owns_work*/, const executor_type& ex, const OtherExecutor& /*candidate*/) noexcept : executor_(asio::prefer(ex, execution::outstanding_work.tracked)) { } handler_work_base(const handler_work_base& other) noexcept : executor_(other.executor_) { } handler_work_base(handler_work_base&& other) noexcept : executor_(static_cast<executor_type&&>(other.executor_)) { } bool owns_work() const noexcept { return !!executor_; } template <typename Function, typename Handler> void dispatch(Function& function, Handler&) { executor_.execute(static_cast<Function&&>(function)); } private: executor_type executor_; }; #if !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT) template <typename Executor, typename CandidateExecutor, typename IoContext, typename PolymorphicExecutor> class handler_work_base< Executor, CandidateExecutor, IoContext, PolymorphicExecutor, enable_if_t< is_same<Executor, any_completion_executor>::value || is_same<Executor, any_io_executor>::value > > { public: typedef Executor executor_type; explicit handler_work_base(int, int, const executor_type& ex) noexcept #if !defined(ASIO_NO_TYPEID) : executor_( ex.target_type() == typeid(typename IoContext::executor_type) ? executor_type() : asio::prefer(ex, execution::outstanding_work.tracked)) #else // !defined(ASIO_NO_TYPEID) : executor_(asio::prefer(ex, execution::outstanding_work.tracked)) #endif // !defined(ASIO_NO_TYPEID) { } handler_work_base(bool base1_owns_work, const executor_type& ex, const executor_type& candidate) noexcept : executor_( !base1_owns_work && ex == candidate ? executor_type() : asio::prefer(ex, execution::outstanding_work.tracked)) { } template <typename OtherExecutor> handler_work_base(bool /*base1_owns_work*/, const executor_type& ex, const OtherExecutor& /*candidate*/) noexcept : executor_(asio::prefer(ex, execution::outstanding_work.tracked)) { } handler_work_base(const handler_work_base& other) noexcept : executor_(other.executor_) { } handler_work_base(handler_work_base&& other) noexcept : executor_(static_cast<executor_type&&>(other.executor_)) { } bool owns_work() const noexcept { return !!executor_; } template <typename Function, typename Handler> void dispatch(Function& function, Handler&) { executor_.execute(static_cast<Function&&>(function)); } private: executor_type executor_; }; #endif // !defined(ASIO_USE_TS_EXECUTOR_AS_DEFAULT) template <typename Handler, typename IoExecutor, typename = void> class handler_work : handler_work_base<IoExecutor>, handler_work_base<associated_executor_t<Handler, IoExecutor>, IoExecutor> { public: typedef handler_work_base<IoExecutor> base1_type; typedef handler_work_base<associated_executor_t<Handler, IoExecutor>, IoExecutor> base2_type; handler_work(Handler& handler, const IoExecutor& io_ex) noexcept : base1_type(0, 0, io_ex), base2_type(base1_type::owns_work(), asio::get_associated_executor(handler, io_ex), io_ex) { } template <typename Function> void complete(Function& function, Handler& handler) { if (!base1_type::owns_work() && !base2_type::owns_work()) { // When using a native implementation, I/O completion handlers are // already dispatched according to the execution context's executor's // rules. We can call the function directly. static_cast<Function&&>(function)(); } else { base2_type::dispatch(function, handler); } } }; template <typename Handler, typename IoExecutor> class handler_work< Handler, IoExecutor, enable_if_t< is_same< typename associated_executor<Handler, IoExecutor>::asio_associated_executor_is_unspecialised, void >::value > > : handler_work_base<IoExecutor> { public: typedef handler_work_base<IoExecutor> base1_type; handler_work(Handler&, const IoExecutor& io_ex) noexcept : base1_type(0, 0, io_ex) { } template <typename Function> void complete(Function& function, Handler& handler) { if (!base1_type::owns_work()) { // When using a native implementation, I/O completion handlers are // already dispatched according to the execution context's executor's // rules. We can call the function directly. static_cast<Function&&>(function)(); } else { base1_type::dispatch(function, handler); } } }; template <typename Handler, typename IoExecutor> class immediate_handler_work { public: typedef handler_work<Handler, IoExecutor> handler_work_type; explicit immediate_handler_work(handler_work_type&& w) : handler_work_(static_cast<handler_work_type&&>(w)) { } template <typename Function> void complete(Function& function, Handler& handler, const void* io_ex) { typedef associated_immediate_executor_t<Handler, IoExecutor> immediate_ex_type; immediate_ex_type immediate_ex = (get_associated_immediate_executor)( handler, *static_cast<const IoExecutor*>(io_ex)); (initiate_dispatch_with_executor<immediate_ex_type>(immediate_ex))( static_cast<Function&&>(function)); } private: handler_work_type handler_work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_HANDLER_WORK_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/hash_map.hpp
// // detail/hash_map.hpp // ~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_HASH_MAP_HPP #define ASIO_DETAIL_HASH_MAP_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <list> #include <utility> #include "asio/detail/assert.hpp" #include "asio/detail/noncopyable.hpp" #if defined(ASIO_WINDOWS) || defined(__CYGWIN__) # include "asio/detail/socket_types.hpp" #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__) #include "asio/detail/push_options.hpp" namespace asio { namespace detail { inline std::size_t calculate_hash_value(int i) { return static_cast<std::size_t>(i); } inline std::size_t calculate_hash_value(void* p) { return reinterpret_cast<std::size_t>(p) + (reinterpret_cast<std::size_t>(p) >> 3); } #if defined(ASIO_WINDOWS) || defined(__CYGWIN__) inline std::size_t calculate_hash_value(SOCKET s) { return static_cast<std::size_t>(s); } #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__) // Note: assumes K and V are POD types. template <typename K, typename V> class hash_map : private noncopyable { public: // The type of a value in the map. typedef std::pair<K, V> value_type; // The type of a non-const iterator over the hash map. typedef typename std::list<value_type>::iterator iterator; // The type of a const iterator over the hash map. typedef typename std::list<value_type>::const_iterator const_iterator; // Constructor. hash_map() : size_(0), buckets_(0), num_buckets_(0) { } // Destructor. ~hash_map() { delete[] buckets_; } // Get an iterator for the beginning of the map. iterator begin() { return values_.begin(); } // Get an iterator for the beginning of the map. const_iterator begin() const { return values_.begin(); } // Get an iterator for the end of the map. iterator end() { return values_.end(); } // Get an iterator for the end of the map. const_iterator end() const { return values_.end(); } // Check whether the map is empty. bool empty() const { return values_.empty(); } // Find an entry in the map. iterator find(const K& k) { if (num_buckets_) { size_t bucket = calculate_hash_value(k) % num_buckets_; iterator it = buckets_[bucket].first; if (it == values_.end()) return values_.end(); iterator end_it = buckets_[bucket].last; ++end_it; while (it != end_it) { if (it->first == k) return it; ++it; } } return values_.end(); } // Find an entry in the map. const_iterator find(const K& k) const { if (num_buckets_) { size_t bucket = calculate_hash_value(k) % num_buckets_; const_iterator it = buckets_[bucket].first; if (it == values_.end()) return it; const_iterator end_it = buckets_[bucket].last; ++end_it; while (it != end_it) { if (it->first == k) return it; ++it; } } return values_.end(); } // Insert a new entry into the map. std::pair<iterator, bool> insert(const value_type& v) { if (size_ + 1 >= num_buckets_) rehash(hash_size(size_ + 1)); size_t bucket = calculate_hash_value(v.first) % num_buckets_; iterator it = buckets_[bucket].first; if (it == values_.end()) { buckets_[bucket].first = buckets_[bucket].last = values_insert(values_.end(), v); ++size_; return std::pair<iterator, bool>(buckets_[bucket].last, true); } iterator end_it = buckets_[bucket].last; ++end_it; while (it != end_it) { if (it->first == v.first) return std::pair<iterator, bool>(it, false); ++it; } buckets_[bucket].last = values_insert(end_it, v); ++size_; return std::pair<iterator, bool>(buckets_[bucket].last, true); } // Erase an entry from the map. void erase(iterator it) { ASIO_ASSERT(it != values_.end()); ASIO_ASSERT(num_buckets_ != 0); size_t bucket = calculate_hash_value(it->first) % num_buckets_; bool is_first = (it == buckets_[bucket].first); bool is_last = (it == buckets_[bucket].last); if (is_first && is_last) buckets_[bucket].first = buckets_[bucket].last = values_.end(); else if (is_first) ++buckets_[bucket].first; else if (is_last) --buckets_[bucket].last; values_erase(it); --size_; } // Erase a key from the map. void erase(const K& k) { iterator it = find(k); if (it != values_.end()) erase(it); } // Remove all entries from the map. void clear() { // Clear the values. values_.clear(); size_ = 0; // Initialise all buckets to empty. iterator end_it = values_.end(); for (size_t i = 0; i < num_buckets_; ++i) buckets_[i].first = buckets_[i].last = end_it; } private: // Calculate the hash size for the specified number of elements. static std::size_t hash_size(std::size_t num_elems) { static std::size_t sizes[] = { #if defined(ASIO_HASH_MAP_BUCKETS) ASIO_HASH_MAP_BUCKETS #else // ASIO_HASH_MAP_BUCKETS 3, 13, 23, 53, 97, 193, 389, 769, 1543, 3079, 6151, 12289, 24593, 49157, 98317, 196613, 393241, 786433, 1572869, 3145739, 6291469, 12582917, 25165843 #endif // ASIO_HASH_MAP_BUCKETS }; const std::size_t nth_size = sizeof(sizes) / sizeof(std::size_t) - 1; for (std::size_t i = 0; i < nth_size; ++i) if (num_elems < sizes[i]) return sizes[i]; return sizes[nth_size]; } // Re-initialise the hash from the values already contained in the list. void rehash(std::size_t num_buckets) { if (num_buckets == num_buckets_) return; ASIO_ASSERT(num_buckets != 0); iterator end_iter = values_.end(); // Update number of buckets and initialise all buckets to empty. bucket_type* tmp = new bucket_type[num_buckets]; delete[] buckets_; buckets_ = tmp; num_buckets_ = num_buckets; for (std::size_t i = 0; i < num_buckets_; ++i) buckets_[i].first = buckets_[i].last = end_iter; // Put all values back into the hash. iterator iter = values_.begin(); while (iter != end_iter) { std::size_t bucket = calculate_hash_value(iter->first) % num_buckets_; if (buckets_[bucket].last == end_iter) { buckets_[bucket].first = buckets_[bucket].last = iter++; } else if (++buckets_[bucket].last == iter) { ++iter; } else { values_.splice(buckets_[bucket].last, values_, iter++); --buckets_[bucket].last; } } } // Insert an element into the values list by splicing from the spares list, // if a spare is available, and otherwise by inserting a new element. iterator values_insert(iterator it, const value_type& v) { if (spares_.empty()) { return values_.insert(it, v); } else { spares_.front() = v; values_.splice(it, spares_, spares_.begin()); return --it; } } // Erase an element from the values list by splicing it to the spares list. void values_erase(iterator it) { *it = value_type(); spares_.splice(spares_.begin(), values_, it); } // The number of elements in the hash. std::size_t size_; // The list of all values in the hash map. std::list<value_type> values_; // The list of spare nodes waiting to be recycled. Assumes that POD types only // are stored in the hash map. std::list<value_type> spares_; // The type for a bucket in the hash table. struct bucket_type { iterator first; iterator last; }; // The buckets in the hash. bucket_type* buckets_; // The number of buckets in the hash. std::size_t num_buckets_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_HASH_MAP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/io_uring_service.hpp
// // detail/io_uring_service.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_IO_URING_SERVICE_HPP #define ASIO_DETAIL_IO_URING_SERVICE_HPP #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_IO_URING) #include <liburing.h> #include "asio/detail/atomic_count.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/conditionally_enabled_mutex.hpp" #include "asio/detail/io_uring_operation.hpp" #include "asio/detail/limits.hpp" #include "asio/detail/object_pool.hpp" #include "asio/detail/op_queue.hpp" #include "asio/detail/reactor.hpp" #include "asio/detail/scheduler_task.hpp" #include "asio/detail/timer_queue_base.hpp" #include "asio/detail/timer_queue_set.hpp" #include "asio/detail/wait_op.hpp" #include "asio/execution_context.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class io_uring_service : public execution_context_service_base<io_uring_service>, public scheduler_task { private: // The mutex type used by this reactor. typedef conditionally_enabled_mutex mutex; public: enum op_types { read_op = 0, write_op = 1, except_op = 2, max_ops = 3 }; class io_object; // An I/O queue stores operations that must run serially. class io_queue : operation { friend class io_uring_service; io_object* io_object_; op_queue<io_uring_operation> op_queue_; bool cancel_requested_; ASIO_DECL io_queue(); void set_result(int r) { task_result_ = static_cast<unsigned>(r); } ASIO_DECL operation* perform_io(int result); ASIO_DECL static void do_complete(void* owner, operation* base, const asio::error_code& ec, std::size_t bytes_transferred); }; // Per I/O object state. class io_object { friend class io_uring_service; friend class object_pool_access; io_object* next_; io_object* prev_; mutex mutex_; io_uring_service* service_; io_queue queues_[max_ops]; bool shutdown_; ASIO_DECL io_object(bool locking); }; // Per I/O object data. typedef io_object* per_io_object_data; // Constructor. ASIO_DECL io_uring_service(asio::execution_context& ctx); // Destructor. ASIO_DECL ~io_uring_service(); // Destroy all user-defined handler objects owned by the service. ASIO_DECL void shutdown(); // Recreate internal state following a fork. ASIO_DECL void notify_fork( asio::execution_context::fork_event fork_ev); // Initialise the task. ASIO_DECL void init_task(); // Register an I/O object with io_uring. ASIO_DECL void register_io_object(io_object*& io_obj); // Register an internal I/O object with io_uring. ASIO_DECL void register_internal_io_object( io_object*& io_obj, int op_type, io_uring_operation* op); // Register buffers with io_uring. ASIO_DECL void register_buffers(const ::iovec* v, unsigned n); // Unregister buffers from io_uring. ASIO_DECL void unregister_buffers(); // Post an operation for immediate completion. void post_immediate_completion(operation* op, bool is_continuation); // Start a new operation. The operation will be prepared and submitted to the // io_uring when it is at the head of its I/O operation queue. ASIO_DECL void start_op(int op_type, per_io_object_data& io_obj, io_uring_operation* op, bool is_continuation); // Cancel all operations associated with the given I/O object. The handlers // associated with the I/O object will be invoked with the operation_aborted // error. ASIO_DECL void cancel_ops(per_io_object_data& io_obj); // Cancel all operations associated with the given I/O object and key. The // handlers associated with the object and key will be invoked with the // operation_aborted error. ASIO_DECL void cancel_ops_by_key(per_io_object_data& io_obj, int op_type, void* cancellation_key); // Cancel any operations that are running against the I/O object and remove // its registration from the service. The service resources associated with // the I/O object must be released by calling cleanup_io_object. ASIO_DECL void deregister_io_object(per_io_object_data& io_obj); // Perform any post-deregistration cleanup tasks associated with the I/O // object. ASIO_DECL void cleanup_io_object(per_io_object_data& io_obj); // Add a new timer queue to the reactor. template <typename Time_Traits> void add_timer_queue(timer_queue<Time_Traits>& timer_queue); // Remove a timer queue from the reactor. template <typename Time_Traits> void remove_timer_queue(timer_queue<Time_Traits>& timer_queue); // Schedule a new operation in the given timer queue to expire at the // specified absolute time. template <typename Time_Traits> void schedule_timer(timer_queue<Time_Traits>& queue, const typename Time_Traits::time_type& time, typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op); // Cancel the timer operations associated with the given token. Returns the // number of operations that have been posted or dispatched. template <typename Time_Traits> std::size_t cancel_timer(timer_queue<Time_Traits>& queue, typename timer_queue<Time_Traits>::per_timer_data& timer, std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)()); // Cancel the timer operations associated with the given key. template <typename Time_Traits> void cancel_timer_by_key(timer_queue<Time_Traits>& queue, typename timer_queue<Time_Traits>::per_timer_data* timer, void* cancellation_key); // Move the timer operations associated with the given timer. template <typename Time_Traits> void move_timer(timer_queue<Time_Traits>& queue, typename timer_queue<Time_Traits>::per_timer_data& target, typename timer_queue<Time_Traits>::per_timer_data& source); // Wait on io_uring once until interrupted or events are ready to be // dispatched. ASIO_DECL void run(long usec, op_queue<operation>& ops); // Interrupt the io_uring wait. ASIO_DECL void interrupt(); private: // The hint to pass to io_uring_queue_init to size its data structures. enum { ring_size = 16384 }; // The number of operations to submit in a batch. enum { submit_batch_size = 128 }; // The number of operations to complete in a batch. enum { complete_batch_size = 128 }; // The type used for processing eventfd readiness notifications. class event_fd_read_op; // Initialise the ring. ASIO_DECL void init_ring(); // Register the eventfd descriptor for readiness notifications. ASIO_DECL void register_with_reactor(); // Allocate a new I/O object. ASIO_DECL io_object* allocate_io_object(); // Free an existing I/O object. ASIO_DECL void free_io_object(io_object* s); // Helper function to cancel all operations associated with the given I/O // object. This function must be called while the I/O object's mutex is held. // Returns true if there are operations for which cancellation is pending. ASIO_DECL bool do_cancel_ops( per_io_object_data& io_obj, op_queue<operation>& ops); // Helper function to add a new timer queue. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue); // Helper function to remove a timer queue. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue); // Called to recalculate and update the timeout. ASIO_DECL void update_timeout(); // Get the current timeout value. ASIO_DECL __kernel_timespec get_timeout() const; // Get a new submission queue entry, flushing the queue if necessary. ASIO_DECL ::io_uring_sqe* get_sqe(); // Submit pending submission queue entries. ASIO_DECL void submit_sqes(); // Post an operation to submit the pending submission queue entries. ASIO_DECL void post_submit_sqes_op(mutex::scoped_lock& lock); // Push an operation to submit the pending submission queue entries. ASIO_DECL void push_submit_sqes_op(op_queue<operation>& ops); // Helper operation to submit pending submission queue entries. class submit_sqes_op : operation { friend class io_uring_service; io_uring_service* service_; ASIO_DECL submit_sqes_op(io_uring_service* s); ASIO_DECL static void do_complete(void* owner, operation* base, const asio::error_code& ec, std::size_t bytes_transferred); }; // The scheduler implementation used to post completions. scheduler& scheduler_; // Mutex to protect access to internal data. mutex mutex_; // The ring. ::io_uring ring_; // The count of unfinished work. atomic_count outstanding_work_; // The operation used to submit the pending submission queue entries. submit_sqes_op submit_sqes_op_; // The number of pending submission queue entries_. int pending_sqes_; // Whether there is a pending submission operation. bool pending_submit_sqes_op_; // Whether the service has been shut down. bool shutdown_; // The timer queues. timer_queue_set timer_queues_; // The timespec for the pending timeout operation. Must remain valid while the // operation is outstanding. __kernel_timespec timeout_; // Mutex to protect access to the registered I/O objects. mutex registration_mutex_; // Keep track of all registered I/O objects. object_pool<io_object> registered_io_objects_; // Helper class to do post-perform_io cleanup. struct perform_io_cleanup_on_block_exit; friend struct perform_io_cleanup_on_block_exit; // The reactor used to register for eventfd readiness. reactor& reactor_; // The per-descriptor reactor data used for the eventfd. reactor::per_descriptor_data reactor_data_; // The eventfd descriptor used to wait for readiness. int event_fd_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #include "asio/detail/impl/io_uring_service.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/io_uring_service.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // defined(ASIO_HAS_IO_URING) #endif // ASIO_DETAIL_IO_URING_SERVICE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_iocp_handle_read_op.hpp
// // detail/win_iocp_handle_read_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) // Copyright (c) 2008 Rep Invariant Systems, Inc. ([email protected]) // // 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_DETAIL_WIN_IOCP_HANDLE_READ_OP_HPP #define ASIO_DETAIL_WIN_IOCP_HANDLE_READ_OP_HPP #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_IOCP) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/operation.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename MutableBufferSequence, typename Handler, typename IoExecutor> class win_iocp_handle_read_op : public operation { public: ASIO_DEFINE_HANDLER_PTR(win_iocp_handle_read_op); win_iocp_handle_read_op(const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : operation(&win_iocp_handle_read_op::do_complete), buffers_(buffers), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& result_ec, std::size_t bytes_transferred) { asio::error_code ec(result_ec); // Take ownership of the operation object. ASIO_ASSUME(base != 0); win_iocp_handle_read_op* o(static_cast<win_iocp_handle_read_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) if (owner) { // Check whether buffers are still valid. buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence>::validate(o->buffers_); } #endif // defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Map non-portable errors to their portable counterparts. if (ec.value() == ERROR_HANDLE_EOF) ec = asio::error::eof; ASIO_ERROR_LOCATION(ec); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, ec, bytes_transferred); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: MutableBufferSequence buffers_; Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IOCP) #endif // ASIO_DETAIL_WIN_IOCP_HANDLE_READ_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/completion_payload_handler.hpp
// // detail/completion_payload_handler.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_COMPLETION_PAYLOAD_HANDLER_HPP #define ASIO_DETAIL_COMPLETION_PAYLOAD_HANDLER_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/associator.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Payload, typename Handler> class completion_payload_handler { public: completion_payload_handler(Payload&& p, Handler& h) : payload_(static_cast<Payload&&>(p)), handler_(static_cast<Handler&&>(h)) { } void operator()() { payload_.receive(handler_); } Handler& handler() { return handler_; } //private: Payload payload_; Handler handler_; }; } // namespace detail template <template <typename, typename> class Associator, typename Payload, typename Handler, typename DefaultCandidate> struct associator<Associator, detail::completion_payload_handler<Payload, Handler>, DefaultCandidate> : Associator<Handler, DefaultCandidate> { static typename Associator<Handler, DefaultCandidate>::type get( const detail::completion_payload_handler<Payload, Handler>& h) noexcept { return Associator<Handler, DefaultCandidate>::get(h.handler_); } static auto get( const detail::completion_payload_handler<Payload, Handler>& h, const DefaultCandidate& c) noexcept -> decltype(Associator<Handler, DefaultCandidate>::get(h.handler_, c)) { return Associator<Handler, DefaultCandidate>::get(h.handler_, c); } }; } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_COMPLETION_PAYLOAD_HANDLER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_iocp_file_service.hpp
// // detail/win_iocp_file_service.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WIN_IOCP_FILE_SERVICE_HPP #define ASIO_DETAIL_WIN_IOCP_FILE_SERVICE_HPP #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_IOCP) && defined(ASIO_HAS_FILE) #include <string> #include "asio/detail/cstdint.hpp" #include "asio/detail/win_iocp_handle_service.hpp" #include "asio/error.hpp" #include "asio/execution_context.hpp" #include "asio/file_base.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { // Extend win_iocp_handle_service to provide file support. class win_iocp_file_service : public execution_context_service_base<win_iocp_file_service> { public: // The native type of a file. typedef win_iocp_handle_service::native_handle_type native_handle_type; // The implementation type of the file. class implementation_type : win_iocp_handle_service::implementation_type { private: // Only this service will have access to the internal values. friend class win_iocp_file_service; uint64_t offset_; bool is_stream_; }; // Constructor. ASIO_DECL win_iocp_file_service(execution_context& context); // Destroy all user-defined handler objects owned by the service. ASIO_DECL void shutdown(); // Construct a new file implementation. void construct(implementation_type& impl) { handle_service_.construct(impl); impl.offset_ = 0; impl.is_stream_ = false; } // Move-construct a new file implementation. void move_construct(implementation_type& impl, implementation_type& other_impl) { handle_service_.move_construct(impl, other_impl); impl.offset_ = other_impl.offset_; impl.is_stream_ = other_impl.is_stream_; other_impl.offset_ = 0; } // Move-assign from another file implementation. void move_assign(implementation_type& impl, win_iocp_file_service& other_service, implementation_type& other_impl) { handle_service_.move_assign(impl, other_service.handle_service_, other_impl); impl.offset_ = other_impl.offset_; impl.is_stream_ = other_impl.is_stream_; other_impl.offset_ = 0; } // Destroy a file implementation. void destroy(implementation_type& impl) { handle_service_.destroy(impl); } // Set whether the implementation is stream-oriented. void set_is_stream(implementation_type& impl, bool is_stream) { impl.is_stream_ = is_stream; } // Open the file using the specified path name. ASIO_DECL asio::error_code open(implementation_type& impl, const char* path, file_base::flags open_flags, asio::error_code& ec); // Assign a native handle to a file implementation. asio::error_code assign(implementation_type& impl, const native_handle_type& native_handle, asio::error_code& ec) { return handle_service_.assign(impl, native_handle, ec); } // Determine whether the file is open. bool is_open(const implementation_type& impl) const { return handle_service_.is_open(impl); } // Destroy a file implementation. asio::error_code close(implementation_type& impl, asio::error_code& ec) { return handle_service_.close(impl, ec); } // Get the native file representation. native_handle_type native_handle(const implementation_type& impl) const { return handle_service_.native_handle(impl); } // Release ownership of a file. native_handle_type release(implementation_type& impl, asio::error_code& ec) { return handle_service_.release(impl, ec); } // Cancel all operations associated with the file. asio::error_code cancel(implementation_type& impl, asio::error_code& ec) { return handle_service_.cancel(impl, ec); } // Get the size of the file. ASIO_DECL uint64_t size(const implementation_type& impl, asio::error_code& ec) const; // Alter the size of the file. ASIO_DECL asio::error_code resize(implementation_type& impl, uint64_t n, asio::error_code& ec); // Synchronise the file to disk. ASIO_DECL asio::error_code sync_all(implementation_type& impl, asio::error_code& ec); // Synchronise the file data to disk. ASIO_DECL asio::error_code sync_data(implementation_type& impl, asio::error_code& ec); // Seek to a position in the file. ASIO_DECL uint64_t seek(implementation_type& impl, int64_t offset, file_base::seek_basis whence, asio::error_code& ec); // Write the given data. Returns the number of bytes written. template <typename ConstBufferSequence> size_t write_some(implementation_type& impl, const ConstBufferSequence& buffers, asio::error_code& ec) { uint64_t offset = impl.offset_; impl.offset_ += asio::buffer_size(buffers); return handle_service_.write_some_at(impl, offset, buffers, ec); } // Start an asynchronous write. The data being written must be valid for the // lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler, typename IoExecutor> void async_write_some(implementation_type& impl, const ConstBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) { uint64_t offset = impl.offset_; impl.offset_ += asio::buffer_size(buffers); handle_service_.async_write_some_at(impl, offset, buffers, handler, io_ex); } // Write the given data at the specified location. Returns the number of // bytes written. template <typename ConstBufferSequence> size_t write_some_at(implementation_type& impl, uint64_t offset, const ConstBufferSequence& buffers, asio::error_code& ec) { return handle_service_.write_some_at(impl, offset, buffers, ec); } // Start an asynchronous write at the specified location. The data being // written must be valid for the lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler, typename IoExecutor> void async_write_some_at(implementation_type& impl, uint64_t offset, const ConstBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) { handle_service_.async_write_some_at(impl, offset, buffers, handler, io_ex); } // Read some data. Returns the number of bytes read. template <typename MutableBufferSequence> size_t read_some(implementation_type& impl, const MutableBufferSequence& buffers, asio::error_code& ec) { uint64_t offset = impl.offset_; impl.offset_ += asio::buffer_size(buffers); return handle_service_.read_some_at(impl, offset, buffers, ec); } // Start an asynchronous read. The buffer for the data being read must be // valid for the lifetime of the asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_read_some(implementation_type& impl, const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) { uint64_t offset = impl.offset_; impl.offset_ += asio::buffer_size(buffers); handle_service_.async_read_some_at(impl, offset, buffers, handler, io_ex); } // Read some data. Returns the number of bytes read. template <typename MutableBufferSequence> size_t read_some_at(implementation_type& impl, uint64_t offset, const MutableBufferSequence& buffers, asio::error_code& ec) { return handle_service_.read_some_at(impl, offset, buffers, ec); } // Start an asynchronous read. The buffer for the data being read must be // valid for the lifetime of the asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_read_some_at(implementation_type& impl, uint64_t offset, const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) { handle_service_.async_read_some_at(impl, offset, buffers, handler, io_ex); } private: // The implementation used for initiating asynchronous operations. win_iocp_handle_service handle_service_; // Emulation of Windows IO_STATUS_BLOCK structure. struct io_status_block { union u { LONG Status; void* Pointer; }; ULONG_PTR Information; }; // Emulation of flag passed to NtFlushBuffersFileEx. enum { flush_flags_file_data_sync_only = 4 }; // The type of a NtFlushBuffersFileEx function pointer. typedef LONG (NTAPI *nt_flush_buffers_file_ex_fn)( HANDLE, ULONG, void*, ULONG, io_status_block*); // The NTFlushBuffersFileEx function pointer. nt_flush_buffers_file_ex_fn nt_flush_buffers_file_ex_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/win_iocp_file_service.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // defined(ASIO_HAS_IOCP) && defined(ASIO_HAS_FILE) #endif // ASIO_DETAIL_WIN_IOCP_FILE_SERVICE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/socket_option.hpp
// // detail/socket_option.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_SOCKET_OPTION_HPP #define ASIO_DETAIL_SOCKET_OPTION_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <cstddef> #include <stdexcept> #include "asio/detail/socket_types.hpp" #include "asio/detail/throw_exception.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { namespace socket_option { // Helper template for implementing boolean-based options. template <int Level, int Name> class boolean { public: // Default constructor. boolean() : value_(0) { } // Construct with a specific option value. explicit boolean(bool v) : value_(v ? 1 : 0) { } // Set the current value of the boolean. boolean& operator=(bool v) { value_ = v ? 1 : 0; return *this; } // Get the current value of the boolean. bool value() const { return !!value_; } // Convert to bool. operator bool() const { return !!value_; } // Test for false. bool operator!() const { return !value_; } // Get the level of the socket option. template <typename Protocol> int level(const Protocol&) const { return Level; } // Get the name of the socket option. template <typename Protocol> int name(const Protocol&) const { return Name; } // Get the address of the boolean data. template <typename Protocol> int* data(const Protocol&) { return &value_; } // Get the address of the boolean data. template <typename Protocol> const int* data(const Protocol&) const { return &value_; } // Get the size of the boolean data. template <typename Protocol> std::size_t size(const Protocol&) const { return sizeof(value_); } // Set the size of the boolean data. template <typename Protocol> void resize(const Protocol&, std::size_t s) { // On some platforms (e.g. Windows Vista), the getsockopt function will // return the size of a boolean socket option as one byte, even though a // four byte integer was passed in. switch (s) { case sizeof(char): value_ = *reinterpret_cast<char*>(&value_) ? 1 : 0; break; case sizeof(value_): break; default: { std::length_error ex("boolean socket option resize"); asio::detail::throw_exception(ex); } } } private: int value_; }; // Helper template for implementing integer options. template <int Level, int Name> class integer { public: // Default constructor. integer() : value_(0) { } // Construct with a specific option value. explicit integer(int v) : value_(v) { } // Set the value of the int option. integer& operator=(int v) { value_ = v; return *this; } // Get the current value of the int option. int value() const { return value_; } // Get the level of the socket option. template <typename Protocol> int level(const Protocol&) const { return Level; } // Get the name of the socket option. template <typename Protocol> int name(const Protocol&) const { return Name; } // Get the address of the int data. template <typename Protocol> int* data(const Protocol&) { return &value_; } // Get the address of the int data. template <typename Protocol> const int* data(const Protocol&) const { return &value_; } // Get the size of the int data. template <typename Protocol> std::size_t size(const Protocol&) const { return sizeof(value_); } // Set the size of the int data. template <typename Protocol> void resize(const Protocol&, std::size_t s) { if (s != sizeof(value_)) { std::length_error ex("integer socket option resize"); asio::detail::throw_exception(ex); } } private: int value_; }; // Helper template for implementing linger options. template <int Level, int Name> class linger { public: // Default constructor. linger() { value_.l_onoff = 0; value_.l_linger = 0; } // Construct with specific option values. linger(bool e, int t) { enabled(e); timeout ASIO_PREVENT_MACRO_SUBSTITUTION(t); } // Set the value for whether linger is enabled. void enabled(bool value) { value_.l_onoff = value ? 1 : 0; } // Get the value for whether linger is enabled. bool enabled() const { return value_.l_onoff != 0; } // Set the value for the linger timeout. void timeout ASIO_PREVENT_MACRO_SUBSTITUTION(int value) { #if defined(WIN32) value_.l_linger = static_cast<u_short>(value); #else value_.l_linger = value; #endif } // Get the value for the linger timeout. int timeout ASIO_PREVENT_MACRO_SUBSTITUTION() const { return static_cast<int>(value_.l_linger); } // Get the level of the socket option. template <typename Protocol> int level(const Protocol&) const { return Level; } // Get the name of the socket option. template <typename Protocol> int name(const Protocol&) const { return Name; } // Get the address of the linger data. template <typename Protocol> detail::linger_type* data(const Protocol&) { return &value_; } // Get the address of the linger data. template <typename Protocol> const detail::linger_type* data(const Protocol&) const { return &value_; } // Get the size of the linger data. template <typename Protocol> std::size_t size(const Protocol&) const { return sizeof(value_); } // Set the size of the int data. template <typename Protocol> void resize(const Protocol&, std::size_t s) { if (s != sizeof(value_)) { std::length_error ex("linger socket option resize"); asio::detail::throw_exception(ex); } } private: detail::linger_type value_; }; } // namespace socket_option } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_SOCKET_OPTION_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/signal_op.hpp
// // detail/signal_op.hpp // ~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_SIGNAL_OP_HPP #define ASIO_DETAIL_SIGNAL_OP_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/operation.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class signal_op : public operation { public: // The error code to be passed to the completion handler. asio::error_code ec_; // The operation key used for targeted cancellation. void* cancellation_key_; // The signal number to be passed to the completion handler. int signal_number_; protected: signal_op(func_type func) : operation(func), cancellation_key_(0), signal_number_(0) { } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_SIGNAL_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/winrt_socket_send_op.hpp
// // detail/winrt_socket_send_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WINRT_SOCKET_SEND_OP_HPP #define ASIO_DETAIL_WINRT_SOCKET_SEND_OP_HPP #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_RUNTIME) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/winrt_async_op.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename ConstBufferSequence, typename Handler, typename IoExecutor> class winrt_socket_send_op : public winrt_async_op<unsigned int> { public: ASIO_DEFINE_HANDLER_PTR(winrt_socket_send_op); winrt_socket_send_op(const ConstBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : winrt_async_op<unsigned int>(&winrt_socket_send_op::do_complete), buffers_(buffers), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code&, std::size_t) { // Take ownership of the operation object. ASIO_ASSUME(base != 0); winrt_socket_send_op* o(static_cast<winrt_socket_send_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Check whether buffers are still valid. if (owner) { buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence>::validate(o->buffers_); } #endif // defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->result_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: ConstBufferSequence buffers_; Handler handler_; handler_work<Handler, IoExecutor> executor_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_WINDOWS_RUNTIME) #endif // ASIO_DETAIL_WINRT_SOCKET_SEND_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/reactive_socket_service_base.hpp
// // detail/reactive_socket_service_base.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP #define ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP #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_IOCP) \ && !defined(ASIO_WINDOWS_RUNTIME) \ && !defined(ASIO_HAS_IO_URING_AS_DEFAULT) #include "asio/associated_cancellation_slot.hpp" #include "asio/buffer.hpp" #include "asio/cancellation_type.hpp" #include "asio/error.hpp" #include "asio/execution_context.hpp" #include "asio/socket_base.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/reactive_null_buffers_op.hpp" #include "asio/detail/reactive_socket_recv_op.hpp" #include "asio/detail/reactive_socket_recvmsg_op.hpp" #include "asio/detail/reactive_socket_send_op.hpp" #include "asio/detail/reactive_wait_op.hpp" #include "asio/detail/reactor.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/detail/socket_holder.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class reactive_socket_service_base { public: // The native type of a socket. typedef socket_type native_handle_type; // The implementation type of the socket. struct base_implementation_type { // The native socket representation. socket_type socket_; // The current state of the socket. socket_ops::state_type state_; // Per-descriptor data used by the reactor. reactor::per_descriptor_data reactor_data_; }; // Constructor. ASIO_DECL reactive_socket_service_base(execution_context& context); // Destroy all user-defined handler objects owned by the service. ASIO_DECL void base_shutdown(); // Construct a new socket implementation. ASIO_DECL void construct(base_implementation_type& impl); // Move-construct a new socket implementation. ASIO_DECL void base_move_construct(base_implementation_type& impl, base_implementation_type& other_impl) noexcept; // Move-assign from another socket implementation. ASIO_DECL void base_move_assign(base_implementation_type& impl, reactive_socket_service_base& other_service, base_implementation_type& other_impl); // Destroy a socket implementation. ASIO_DECL void destroy(base_implementation_type& impl); // Determine whether the socket is open. bool is_open(const base_implementation_type& impl) const { return impl.socket_ != invalid_socket; } // Destroy a socket implementation. ASIO_DECL asio::error_code close( base_implementation_type& impl, asio::error_code& ec); // Release ownership of the socket. ASIO_DECL socket_type release( base_implementation_type& impl, asio::error_code& ec); // Get the native socket representation. native_handle_type native_handle(base_implementation_type& impl) { return impl.socket_; } // Cancel all operations associated with the socket. ASIO_DECL asio::error_code cancel( base_implementation_type& impl, asio::error_code& ec); // Determine whether the socket is at the out-of-band data mark. bool at_mark(const base_implementation_type& impl, asio::error_code& ec) const { return socket_ops::sockatmark(impl.socket_, ec); } // Determine the number of bytes available for reading. std::size_t available(const base_implementation_type& impl, asio::error_code& ec) const { return socket_ops::available(impl.socket_, ec); } // Place the socket into the state where it will listen for new connections. asio::error_code listen(base_implementation_type& impl, int backlog, asio::error_code& ec) { socket_ops::listen(impl.socket_, backlog, ec); return ec; } // Perform an IO control command on the socket. template <typename IO_Control_Command> asio::error_code io_control(base_implementation_type& impl, IO_Control_Command& command, asio::error_code& ec) { socket_ops::ioctl(impl.socket_, impl.state_, command.name(), static_cast<ioctl_arg_type*>(command.data()), ec); return ec; } // Gets the non-blocking mode of the socket. bool non_blocking(const base_implementation_type& impl) const { return (impl.state_ & socket_ops::user_set_non_blocking) != 0; } // Sets the non-blocking mode of the socket. asio::error_code non_blocking(base_implementation_type& impl, bool mode, asio::error_code& ec) { socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec); return ec; } // Gets the non-blocking mode of the native socket implementation. bool native_non_blocking(const base_implementation_type& impl) const { return (impl.state_ & socket_ops::internal_non_blocking) != 0; } // Sets the non-blocking mode of the native socket implementation. asio::error_code native_non_blocking(base_implementation_type& impl, bool mode, asio::error_code& ec) { socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec); return ec; } // Wait for the socket to become ready to read, ready to write, or to have // pending error conditions. asio::error_code wait(base_implementation_type& impl, socket_base::wait_type w, asio::error_code& ec) { switch (w) { case socket_base::wait_read: socket_ops::poll_read(impl.socket_, impl.state_, -1, ec); break; case socket_base::wait_write: socket_ops::poll_write(impl.socket_, impl.state_, -1, ec); break; case socket_base::wait_error: socket_ops::poll_error(impl.socket_, impl.state_, -1, ec); break; default: ec = asio::error::invalid_argument; break; } return ec; } // Asynchronously wait for the socket to become ready to read, ready to // write, or to have pending error conditions. template <typename Handler, typename IoExecutor> void async_wait(base_implementation_type& impl, socket_base::wait_type w, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_wait_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, handler, io_ex); ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket", &impl, impl.socket_, "async_wait")); int op_type; switch (w) { case socket_base::wait_read: op_type = reactor::read_op; break; case socket_base::wait_write: op_type = reactor::write_op; break; case socket_base::wait_error: op_type = reactor::except_op; break; default: p.p->ec_ = asio::error::invalid_argument; start_op(impl, reactor::read_op, p.p, is_continuation, false, true, false, &io_ex, 0); p.v = p.p = 0; return; } // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.socket_, op_type); } start_op(impl, op_type, p.p, is_continuation, false, false, false, &io_ex, 0); p.v = p.p = 0; } // Send the given data to the peer. template <typename ConstBufferSequence> size_t send(base_implementation_type& impl, const ConstBufferSequence& buffers, socket_base::message_flags flags, asio::error_code& ec) { typedef buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence> bufs_type; if (bufs_type::is_single_buffer) { return socket_ops::sync_send1(impl.socket_, impl.state_, bufs_type::first(buffers).data(), bufs_type::first(buffers).size(), flags, ec); } else { bufs_type bufs(buffers); return socket_ops::sync_send(impl.socket_, impl.state_, bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec); } } // Wait until data can be sent without blocking. size_t send(base_implementation_type& impl, const null_buffers&, socket_base::message_flags, asio::error_code& ec) { // Wait for socket to become ready. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec); return 0; } // Start an asynchronous send. The data being sent must be valid for the // lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler, typename IoExecutor> void async_send(base_implementation_type& impl, const ConstBufferSequence& buffers, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_socket_send_op< ConstBufferSequence, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, impl.socket_, impl.state_, buffers, flags, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.socket_, reactor::write_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket", &impl, impl.socket_, "async_send")); start_op(impl, reactor::write_op, p.p, is_continuation, true, ((impl.state_ & socket_ops::stream_oriented) && buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence>::all_empty(buffers)), true, &io_ex, 0); p.v = p.p = 0; } // Start an asynchronous wait until data can be sent without blocking. template <typename Handler, typename IoExecutor> void async_send(base_implementation_type& impl, const null_buffers&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_null_buffers_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.socket_, reactor::write_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket", &impl, impl.socket_, "async_send(null_buffers)")); start_op(impl, reactor::write_op, p.p, is_continuation, false, false, false, &io_ex, 0); p.v = p.p = 0; } // Receive some data from the peer. Returns the number of bytes received. template <typename MutableBufferSequence> size_t receive(base_implementation_type& impl, const MutableBufferSequence& buffers, socket_base::message_flags flags, asio::error_code& ec) { typedef buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence> bufs_type; if (bufs_type::is_single_buffer) { return socket_ops::sync_recv1(impl.socket_, impl.state_, bufs_type::first(buffers).data(), bufs_type::first(buffers).size(), flags, ec); } else { bufs_type bufs(buffers); return socket_ops::sync_recv(impl.socket_, impl.state_, bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec); } } // Wait until data can be received without blocking. size_t receive(base_implementation_type& impl, const null_buffers&, socket_base::message_flags, asio::error_code& ec) { // Wait for socket to become ready. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec); return 0; } // Start an asynchronous receive. The buffer for the data being received // must be valid for the lifetime of the asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_receive(base_implementation_type& impl, const MutableBufferSequence& buffers, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_socket_recv_op< MutableBufferSequence, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, impl.socket_, impl.state_, buffers, flags, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket", &impl, impl.socket_, "async_receive")); start_op(impl, (flags & socket_base::message_out_of_band) ? reactor::except_op : reactor::read_op, p.p, is_continuation, (flags & socket_base::message_out_of_band) == 0, ((impl.state_ & socket_ops::stream_oriented) && buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence>::all_empty(buffers)), true, &io_ex, 0); p.v = p.p = 0; } // Wait until data can be received without blocking. template <typename Handler, typename IoExecutor> void async_receive(base_implementation_type& impl, const null_buffers&, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_null_buffers_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket", &impl, impl.socket_, "async_receive(null_buffers)")); start_op(impl, (flags & socket_base::message_out_of_band) ? reactor::except_op : reactor::read_op, p.p, is_continuation, false, false, false, &io_ex, 0); p.v = p.p = 0; } // Receive some data with associated flags. Returns the number of bytes // received. template <typename MutableBufferSequence> size_t receive_with_flags(base_implementation_type& impl, const MutableBufferSequence& buffers, socket_base::message_flags in_flags, socket_base::message_flags& out_flags, asio::error_code& ec) { buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence> bufs(buffers); return socket_ops::sync_recvmsg(impl.socket_, impl.state_, bufs.buffers(), bufs.count(), in_flags, out_flags, ec); } // Wait until data can be received without blocking. size_t receive_with_flags(base_implementation_type& impl, const null_buffers&, socket_base::message_flags, socket_base::message_flags& out_flags, asio::error_code& ec) { // Wait for socket to become ready. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec); // Clear out_flags, since we cannot give it any other sensible value when // performing a null_buffers operation. out_flags = 0; return 0; } // Start an asynchronous receive. The buffer for the data being received // must be valid for the lifetime of the asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_receive_with_flags(base_implementation_type& impl, const MutableBufferSequence& buffers, socket_base::message_flags in_flags, socket_base::message_flags& out_flags, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_socket_recvmsg_op< MutableBufferSequence, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, impl.socket_, buffers, in_flags, out_flags, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket", &impl, impl.socket_, "async_receive_with_flags")); start_op(impl, (in_flags & socket_base::message_out_of_band) ? reactor::except_op : reactor::read_op, p.p, is_continuation, (in_flags & socket_base::message_out_of_band) == 0, false, true, &io_ex, 0); p.v = p.p = 0; } // Wait until data can be received without blocking. template <typename Handler, typename IoExecutor> void async_receive_with_flags(base_implementation_type& impl, const null_buffers&, socket_base::message_flags in_flags, socket_base::message_flags& out_flags, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_null_buffers_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket", &impl, impl.socket_, "async_receive_with_flags(null_buffers)")); // Clear out_flags, since we cannot give it any other sensible value when // performing a null_buffers operation. out_flags = 0; start_op(impl, (in_flags & socket_base::message_out_of_band) ? reactor::except_op : reactor::read_op, p.p, is_continuation, false, false, false, &io_ex, 0); p.v = p.p = 0; } protected: // Open a new socket implementation. ASIO_DECL asio::error_code do_open( base_implementation_type& impl, int af, int type, int protocol, asio::error_code& ec); // Assign a native socket to a socket implementation. ASIO_DECL asio::error_code do_assign( base_implementation_type& impl, int type, const native_handle_type& native_socket, asio::error_code& ec); // Start the asynchronous read or write operation. ASIO_DECL void do_start_op(base_implementation_type& impl, int op_type, reactor_op* op, bool is_continuation, bool allow_speculative, bool noop, bool needs_non_blocking, void (*on_immediate)(operation* op, bool, const void*), const void* immediate_arg); // Start the asynchronous operation for handlers that are specialised for // immediate completion. template <typename Op> void start_op(base_implementation_type& impl, int op_type, Op* op, bool is_continuation, bool allow_speculative, bool noop, bool needs_non_blocking, const void* io_ex, ...) { return do_start_op(impl, op_type, op, is_continuation, allow_speculative, noop, needs_non_blocking, &Op::do_immediate, io_ex); } // Start the asynchronous operation for handlers that are not specialised for // immediate completion. template <typename Op> void start_op(base_implementation_type& impl, int op_type, Op* op, bool is_continuation, bool allow_speculative, bool noop, bool needs_non_blocking, const void*, enable_if_t< is_same< typename associated_immediate_executor< typename Op::handler_type, typename Op::io_executor_type >::asio_associated_immediate_executor_is_unspecialised, void >::value >*) { return do_start_op(impl, op_type, op, is_continuation, allow_speculative, noop, needs_non_blocking, &reactor::call_post_immediate_completion, &reactor_); } // Start the asynchronous accept operation. ASIO_DECL void do_start_accept_op(base_implementation_type& impl, reactor_op* op, bool is_continuation, bool peer_is_open, void (*on_immediate)(operation* op, bool, const void*), const void* immediate_arg); // Start the asynchronous accept operation for handlers that are specialised // for immediate completion. template <typename Op> void start_accept_op(base_implementation_type& impl, Op* op, bool is_continuation, bool peer_is_open, const void* io_ex, ...) { return do_start_accept_op(impl, op, is_continuation, peer_is_open, &Op::do_immediate, io_ex); } // Start the asynchronous operation for handlers that are not specialised for // immediate completion. template <typename Op> void start_accept_op(base_implementation_type& impl, Op* op, bool is_continuation, bool peer_is_open, const void*, enable_if_t< is_same< typename associated_immediate_executor< typename Op::handler_type, typename Op::io_executor_type >::asio_associated_immediate_executor_is_unspecialised, void >::value >*) { return do_start_accept_op(impl, op, is_continuation, peer_is_open, &reactor::call_post_immediate_completion, &reactor_); } // Start the asynchronous connect operation. ASIO_DECL void do_start_connect_op(base_implementation_type& impl, reactor_op* op, bool is_continuation, const void* addr, size_t addrlen, void (*on_immediate)(operation* op, bool, const void*), const void* immediate_arg); // Start the asynchronous operation for handlers that are specialised for // immediate completion. template <typename Op> void start_connect_op(base_implementation_type& impl, Op* op, bool is_continuation, const void* addr, size_t addrlen, const void* io_ex, ...) { return do_start_connect_op(impl, op, is_continuation, addr, addrlen, &Op::do_immediate, io_ex); } // Start the asynchronous operation for handlers that are not specialised for // immediate completion. template <typename Op> void start_connect_op(base_implementation_type& impl, Op* op, bool is_continuation, const void* addr, size_t addrlen, const void*, enable_if_t< is_same< typename associated_immediate_executor< typename Op::handler_type, typename Op::io_executor_type >::asio_associated_immediate_executor_is_unspecialised, void >::value >*) { return do_start_connect_op(impl, op, is_continuation, addr, addrlen, &reactor::call_post_immediate_completion, &reactor_); } // Helper class used to implement per-operation cancellation class reactor_op_cancellation { public: reactor_op_cancellation(reactor* r, reactor::per_descriptor_data* p, socket_type d, int o) : reactor_(r), reactor_data_(p), descriptor_(d), op_type_(o) { } void operator()(cancellation_type_t type) { if (!!(type & (cancellation_type::terminal | cancellation_type::partial | cancellation_type::total))) { reactor_->cancel_ops_by_key(descriptor_, *reactor_data_, op_type_, this); } } private: reactor* reactor_; reactor::per_descriptor_data* reactor_data_; socket_type descriptor_; int op_type_; }; // The selector that performs event demultiplexing for the service. reactor& reactor_; // Cached success value to avoid accessing category singleton. const asio::error_code success_ec_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/reactive_socket_service_base.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // !defined(ASIO_HAS_IOCP) // && !defined(ASIO_WINDOWS_RUNTIME) // && !defined(ASIO_HAS_IO_URING_AS_DEFAULT) #endif // ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/select_interrupter.hpp
// // detail/select_interrupter.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_SELECT_INTERRUPTER_HPP #define ASIO_DETAIL_SELECT_INTERRUPTER_HPP #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_RUNTIME) #if defined(ASIO_WINDOWS) || defined(__CYGWIN__) || defined(__SYMBIAN32__) # include "asio/detail/socket_select_interrupter.hpp" #elif defined(ASIO_HAS_EVENTFD) # include "asio/detail/eventfd_select_interrupter.hpp" #else # include "asio/detail/pipe_select_interrupter.hpp" #endif namespace asio { namespace detail { #if defined(ASIO_WINDOWS) || defined(__CYGWIN__) || defined(__SYMBIAN32__) typedef socket_select_interrupter select_interrupter; #elif defined(ASIO_HAS_EVENTFD) typedef eventfd_select_interrupter select_interrupter; #else typedef pipe_select_interrupter select_interrupter; #endif } // namespace detail } // namespace asio #endif // !defined(ASIO_WINDOWS_RUNTIME) #endif // ASIO_DETAIL_SELECT_INTERRUPTER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/io_uring_descriptor_read_at_op.hpp
// // detail/io_uring_descriptor_read_at_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_IO_URING_DESCRIPTOR_READ_AT_OP_HPP #define ASIO_DETAIL_IO_URING_DESCRIPTOR_READ_AT_OP_HPP #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_IO_URING) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/cstdint.hpp" #include "asio/detail/descriptor_ops.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/io_uring_operation.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename MutableBufferSequence> class io_uring_descriptor_read_at_op_base : public io_uring_operation { public: io_uring_descriptor_read_at_op_base( const asio::error_code& success_ec, int descriptor, descriptor_ops::state_type state, uint64_t offset, const MutableBufferSequence& buffers, func_type complete_func) : io_uring_operation(success_ec, &io_uring_descriptor_read_at_op_base::do_prepare, &io_uring_descriptor_read_at_op_base::do_perform, complete_func), descriptor_(descriptor), state_(state), offset_(offset), buffers_(buffers), bufs_(buffers) { } static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe) { ASIO_ASSUME(base != 0); io_uring_descriptor_read_at_op_base* o( static_cast<io_uring_descriptor_read_at_op_base*>(base)); if ((o->state_ & descriptor_ops::internal_non_blocking) != 0) { ::io_uring_prep_poll_add(sqe, o->descriptor_, POLLIN); } else if (o->bufs_.is_single_buffer && o->bufs_.is_registered_buffer) { ::io_uring_prep_read_fixed(sqe, o->descriptor_, o->bufs_.buffers()->iov_base, o->bufs_.buffers()->iov_len, o->offset_, o->bufs_.registered_id().native_handle()); } else { ::io_uring_prep_readv(sqe, o->descriptor_, o->bufs_.buffers(), o->bufs_.count(), o->offset_); } } static bool do_perform(io_uring_operation* base, bool after_completion) { ASIO_ASSUME(base != 0); io_uring_descriptor_read_at_op_base* o( static_cast<io_uring_descriptor_read_at_op_base*>(base)); if ((o->state_ & descriptor_ops::internal_non_blocking) != 0) { if (o->bufs_.is_single_buffer) { return descriptor_ops::non_blocking_read_at1(o->descriptor_, o->offset_, o->bufs_.first(o->buffers_).data(), o->bufs_.first(o->buffers_).size(), o->ec_, o->bytes_transferred_); } else { return descriptor_ops::non_blocking_read_at(o->descriptor_, o->offset_, o->bufs_.buffers(), o->bufs_.count(), o->ec_, o->bytes_transferred_); } } else if (after_completion) { if (!o->ec_ && o->bytes_transferred_ == 0) o->ec_ = asio::error::eof; } if (o->ec_ && o->ec_ == asio::error::would_block) { o->state_ |= descriptor_ops::internal_non_blocking; return false; } return after_completion; } private: int descriptor_; descriptor_ops::state_type state_; uint64_t offset_; MutableBufferSequence buffers_; buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence> bufs_; }; template <typename MutableBufferSequence, typename Handler, typename IoExecutor> class io_uring_descriptor_read_at_op : public io_uring_descriptor_read_at_op_base<MutableBufferSequence> { public: ASIO_DEFINE_HANDLER_PTR(io_uring_descriptor_read_at_op); io_uring_descriptor_read_at_op(const asio::error_code& success_ec, int descriptor, descriptor_ops::state_type state, uint64_t offset, const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : io_uring_descriptor_read_at_op_base<MutableBufferSequence>( success_ec, descriptor, state, offset, buffers, &io_uring_descriptor_read_at_op::do_complete), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); io_uring_descriptor_read_at_op* o (static_cast<io_uring_descriptor_read_at_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->bytes_transferred_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IO_URING) #endif // ASIO_DETAIL_IO_URING_DESCRIPTOR_READ_AT_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/recycling_allocator.hpp
// // detail/recycling_allocator.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_RECYCLING_ALLOCATOR_HPP #define ASIO_DETAIL_RECYCLING_ALLOCATOR_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/memory.hpp" #include "asio/detail/thread_context.hpp" #include "asio/detail/thread_info_base.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename T, typename Purpose = thread_info_base::default_tag> class recycling_allocator { public: typedef T value_type; template <typename U> struct rebind { typedef recycling_allocator<U, Purpose> other; }; recycling_allocator() { } template <typename U> recycling_allocator(const recycling_allocator<U, Purpose>&) { } T* allocate(std::size_t n) { #if !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING) void* p = thread_info_base::allocate(Purpose(), thread_context::top_of_thread_call_stack(), sizeof(T) * n, alignof(T)); #else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING) void* p = asio::aligned_new(align, s); #endif // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING) return static_cast<T*>(p); } void deallocate(T* p, std::size_t n) { #if !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING) thread_info_base::deallocate(Purpose(), thread_context::top_of_thread_call_stack(), p, sizeof(T) * n); #else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING) (void)n; asio::aligned_delete(p); #endif // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING) } }; template <typename Purpose> class recycling_allocator<void, Purpose> { public: typedef void value_type; template <typename U> struct rebind { typedef recycling_allocator<U, Purpose> other; }; recycling_allocator() { } template <typename U> recycling_allocator(const recycling_allocator<U, Purpose>&) { } }; template <typename Allocator, typename Purpose> struct get_recycling_allocator { typedef Allocator type; static type get(const Allocator& a) { return a; } }; template <typename T, typename Purpose> struct get_recycling_allocator<std::allocator<T>, Purpose> { typedef recycling_allocator<T, Purpose> type; static type get(const std::allocator<T>&) { return type(); } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/op_queue.hpp
// // detail/op_queue.hpp // ~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_OP_QUEUE_HPP #define ASIO_DETAIL_OP_QUEUE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/noncopyable.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Operation> class op_queue; class op_queue_access { public: template <typename Operation> static Operation* next(Operation* o) { return static_cast<Operation*>(o->next_); } template <typename Operation1, typename Operation2> static void next(Operation1*& o1, Operation2* o2) { o1->next_ = o2; } template <typename Operation> static void destroy(Operation* o) { o->destroy(); } template <typename Operation> static Operation*& front(op_queue<Operation>& q) { return q.front_; } template <typename Operation> static Operation*& back(op_queue<Operation>& q) { return q.back_; } }; template <typename Operation> class op_queue : private noncopyable { public: // Constructor. op_queue() : front_(0), back_(0) { } // Destructor destroys all operations. ~op_queue() { while (Operation* op = front_) { pop(); op_queue_access::destroy(op); } } // Get the operation at the front of the queue. Operation* front() { return front_; } // Pop an operation from the front of the queue. void pop() { if (front_) { Operation* tmp = front_; front_ = op_queue_access::next(front_); if (front_ == 0) back_ = 0; op_queue_access::next(tmp, static_cast<Operation*>(0)); } } // Push an operation on to the back of the queue. void push(Operation* h) { op_queue_access::next(h, static_cast<Operation*>(0)); if (back_) { op_queue_access::next(back_, h); back_ = h; } else { front_ = back_ = h; } } // Push all operations from another queue on to the back of the queue. The // source queue may contain operations of a derived type. template <typename OtherOperation> void push(op_queue<OtherOperation>& q) { if (Operation* other_front = op_queue_access::front(q)) { if (back_) op_queue_access::next(back_, other_front); else front_ = other_front; back_ = op_queue_access::back(q); op_queue_access::front(q) = 0; op_queue_access::back(q) = 0; } } // Whether the queue is empty. bool empty() const { return front_ == 0; } // Test whether an operation is already enqueued. bool is_enqueued(Operation* o) const { return op_queue_access::next(o) != 0 || back_ == o; } private: friend class op_queue_access; // The front of the queue. Operation* front_; // The back of the queue. Operation* back_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_OP_QUEUE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/pop_options.hpp
// // detail/pop_options.hpp // ~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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) // // No header guard #if defined(__COMO__) // Comeau C++ #elif defined(__DMC__) // Digital Mars C++ #elif defined(__INTEL_COMPILER) || defined(__ICL) \ || defined(__ICC) || defined(__ECC) // Intel C++ # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4) # if !defined(ASIO_DISABLE_VISIBILITY) # pragma GCC visibility pop # endif // !defined(ASIO_DISABLE_VISIBILITY) # endif // (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4) # pragma pop_macro ("emit") # pragma pop_macro ("signal") # pragma pop_macro ("slot") #elif defined(__clang__) // Clang # if defined(__OBJC__) # if !defined(__APPLE_CC__) || (__APPLE_CC__ <= 1) # if defined(ASIO_OBJC_WORKAROUND) # undef Protocol # undef id # undef ASIO_OBJC_WORKAROUND # endif # endif # endif # if !defined(_WIN32) && !defined(__WIN32__) && !defined(WIN32) # if !defined(ASIO_DISABLE_VISIBILITY) # pragma GCC visibility pop # endif // !defined(ASIO_DISABLE_VISIBILITY) # endif // !defined(_WIN32) && !defined(__WIN32__) && !defined(WIN32) # pragma GCC diagnostic pop # pragma pop_macro ("emit") # pragma pop_macro ("signal") # pragma pop_macro ("slot") #elif defined(__GNUC__) // GNU C++ # if defined(__MINGW32__) || defined(__CYGWIN__) # pragma pack (pop) # endif # if defined(__OBJC__) # if !defined(__APPLE_CC__) || (__APPLE_CC__ <= 1) # if defined(ASIO_OBJC_WORKAROUND) # undef Protocol # undef id # undef ASIO_OBJC_WORKAROUND # endif # endif # endif # if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4) # if !defined(ASIO_DISABLE_VISIBILITY) # pragma GCC visibility pop # endif // !defined(ASIO_DISABLE_VISIBILITY) # endif // (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4) # pragma GCC diagnostic pop # pragma pop_macro ("emit") # pragma pop_macro ("signal") # pragma pop_macro ("slot") #elif defined(__KCC) // Kai C++ #elif defined(__sgi) // SGI MIPSpro C++ #elif defined(__DECCXX) // Compaq Tru64 Unix cxx #elif defined(__ghs) // Greenhills C++ #elif defined(__BORLANDC__) && !defined(__clang__) // Borland C++ # pragma option pop # pragma nopushoptwarn # pragma nopackwarning #elif defined(__MWERKS__) // Metrowerks CodeWarrior #elif defined(__SUNPRO_CC) // Sun Workshop Compiler C++ #elif defined(__HP_aCC) // HP aCC #elif defined(__MRC__) || defined(__SC__) // MPW MrCpp or SCpp #elif defined(__IBMCPP__) // IBM Visual Age #elif defined(_MSC_VER) // Microsoft Visual C++ // // Must remain the last #elif since some other vendors (Metrowerks, for example) // also #define _MSC_VER # pragma warning (pop) # pragma pack (pop) # if defined(__cplusplus_cli) || defined(__cplusplus_winrt) # if defined(ASIO_CLR_WORKAROUND) # undef generic # undef ASIO_CLR_WORKAROUND # endif # endif # pragma pop_macro ("emit") # pragma pop_macro ("signal") # pragma pop_macro ("slot") #endif
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/wrapped_handler.hpp
// // detail/wrapped_handler.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WRAPPED_HANDLER_HPP #define ASIO_DETAIL_WRAPPED_HANDLER_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/bind_handler.hpp" #include "asio/detail/handler_cont_helpers.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { struct is_continuation_delegated { template <typename Dispatcher, typename Handler> bool operator()(Dispatcher&, Handler& handler) const { return asio_handler_cont_helpers::is_continuation(handler); } }; struct is_continuation_if_running { template <typename Dispatcher, typename Handler> bool operator()(Dispatcher& dispatcher, Handler&) const { return dispatcher.running_in_this_thread(); } }; template <typename Dispatcher, typename = void> struct wrapped_executor { typedef Dispatcher executor_type; static const Dispatcher& get(const Dispatcher& dispatcher) noexcept { return dispatcher; } }; template <typename Dispatcher> struct wrapped_executor<Dispatcher, void_type<typename Dispatcher::executor_type>> { typedef typename Dispatcher::executor_type executor_type; static executor_type get(const Dispatcher& dispatcher) noexcept { return dispatcher.get_executor(); } }; template <typename Dispatcher, typename Handler, typename IsContinuation = is_continuation_delegated> class wrapped_handler { public: typedef void result_type; typedef typename wrapped_executor<Dispatcher>::executor_type executor_type; wrapped_handler(Dispatcher dispatcher, Handler& handler) : dispatcher_(dispatcher), handler_(static_cast<Handler&&>(handler)) { } wrapped_handler(const wrapped_handler& other) : dispatcher_(other.dispatcher_), handler_(other.handler_) { } wrapped_handler(wrapped_handler&& other) : dispatcher_(other.dispatcher_), handler_(static_cast<Handler&&>(other.handler_)) { } executor_type get_executor() const noexcept { return wrapped_executor<Dispatcher>::get(dispatcher_); } void operator()() { dispatcher_.dispatch(static_cast<Handler&&>(handler_)); } void operator()() const { dispatcher_.dispatch(handler_); } template <typename Arg1> void operator()(const Arg1& arg1) { dispatcher_.dispatch(detail::bind_handler(handler_, arg1)); } template <typename Arg1> void operator()(const Arg1& arg1) const { dispatcher_.dispatch(detail::bind_handler(handler_, arg1)); } template <typename Arg1, typename Arg2> void operator()(const Arg1& arg1, const Arg2& arg2) { dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2)); } template <typename Arg1, typename Arg2> void operator()(const Arg1& arg1, const Arg2& arg2) const { dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2)); } template <typename Arg1, typename Arg2, typename Arg3> void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) { dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3)); } template <typename Arg1, typename Arg2, typename Arg3> void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3) const { dispatcher_.dispatch(detail::bind_handler(handler_, arg1, arg2, arg3)); } template <typename Arg1, typename Arg2, typename Arg3, typename Arg4> void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4) { dispatcher_.dispatch( detail::bind_handler(handler_, arg1, arg2, arg3, arg4)); } template <typename Arg1, typename Arg2, typename Arg3, typename Arg4> void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4) const { dispatcher_.dispatch( detail::bind_handler(handler_, arg1, arg2, arg3, arg4)); } template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5> void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4, const Arg5& arg5) { dispatcher_.dispatch( detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5)); } template <typename Arg1, typename Arg2, typename Arg3, typename Arg4, typename Arg5> void operator()(const Arg1& arg1, const Arg2& arg2, const Arg3& arg3, const Arg4& arg4, const Arg5& arg5) const { dispatcher_.dispatch( detail::bind_handler(handler_, arg1, arg2, arg3, arg4, arg5)); } //private: Dispatcher dispatcher_; Handler handler_; }; template <typename Dispatcher, typename Handler, typename IsContinuation> inline bool asio_handler_is_continuation( wrapped_handler<Dispatcher, Handler, IsContinuation>* this_handler) { return IsContinuation()(this_handler->dispatcher_, this_handler->handler_); } } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_WRAPPED_HANDLER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_iocp_socket_service.hpp
// // detail/win_iocp_socket_service.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP #define ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP #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_IOCP) #include <cstring> #include "asio/error.hpp" #include "asio/execution_context.hpp" #include "asio/socket_base.hpp" #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/mutex.hpp" #include "asio/detail/operation.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/detail/select_reactor.hpp" #include "asio/detail/socket_holder.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/win_iocp_io_context.hpp" #include "asio/detail/win_iocp_null_buffers_op.hpp" #include "asio/detail/win_iocp_socket_accept_op.hpp" #include "asio/detail/win_iocp_socket_connect_op.hpp" #include "asio/detail/win_iocp_socket_recvfrom_op.hpp" #include "asio/detail/win_iocp_socket_send_op.hpp" #include "asio/detail/win_iocp_socket_service_base.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Protocol> class win_iocp_socket_service : public execution_context_service_base<win_iocp_socket_service<Protocol>>, public win_iocp_socket_service_base { public: // The protocol type. typedef Protocol protocol_type; // The endpoint type. typedef typename Protocol::endpoint endpoint_type; // The native type of a socket. class native_handle_type { public: native_handle_type(socket_type s) : socket_(s), have_remote_endpoint_(false) { } native_handle_type(socket_type s, const endpoint_type& ep) : socket_(s), have_remote_endpoint_(true), remote_endpoint_(ep) { } void operator=(socket_type s) { socket_ = s; have_remote_endpoint_ = false; remote_endpoint_ = endpoint_type(); } operator socket_type() const { return socket_; } bool have_remote_endpoint() const { return have_remote_endpoint_; } endpoint_type remote_endpoint() const { return remote_endpoint_; } private: socket_type socket_; bool have_remote_endpoint_; endpoint_type remote_endpoint_; }; // The implementation type of the socket. struct implementation_type : win_iocp_socket_service_base::base_implementation_type { // Default constructor. implementation_type() : protocol_(endpoint_type().protocol()), have_remote_endpoint_(false), remote_endpoint_() { } // The protocol associated with the socket. protocol_type protocol_; // Whether we have a cached remote endpoint. bool have_remote_endpoint_; // A cached remote endpoint. endpoint_type remote_endpoint_; }; // Constructor. win_iocp_socket_service(execution_context& context) : execution_context_service_base< win_iocp_socket_service<Protocol>>(context), win_iocp_socket_service_base(context) { } // Destroy all user-defined handler objects owned by the service. void shutdown() { this->base_shutdown(); } // Move-construct a new socket implementation. void move_construct(implementation_type& impl, implementation_type& other_impl) noexcept { this->base_move_construct(impl, other_impl); impl.protocol_ = other_impl.protocol_; other_impl.protocol_ = endpoint_type().protocol(); impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_; other_impl.have_remote_endpoint_ = false; impl.remote_endpoint_ = other_impl.remote_endpoint_; other_impl.remote_endpoint_ = endpoint_type(); } // Move-assign from another socket implementation. void move_assign(implementation_type& impl, win_iocp_socket_service_base& other_service, implementation_type& other_impl) { this->base_move_assign(impl, other_service, other_impl); impl.protocol_ = other_impl.protocol_; other_impl.protocol_ = endpoint_type().protocol(); impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_; other_impl.have_remote_endpoint_ = false; impl.remote_endpoint_ = other_impl.remote_endpoint_; other_impl.remote_endpoint_ = endpoint_type(); } // Move-construct a new socket implementation from another protocol type. template <typename Protocol1> void converting_move_construct(implementation_type& impl, win_iocp_socket_service<Protocol1>&, typename win_iocp_socket_service< Protocol1>::implementation_type& other_impl) { this->base_move_construct(impl, other_impl); impl.protocol_ = protocol_type(other_impl.protocol_); other_impl.protocol_ = typename Protocol1::endpoint().protocol(); impl.have_remote_endpoint_ = other_impl.have_remote_endpoint_; other_impl.have_remote_endpoint_ = false; impl.remote_endpoint_ = other_impl.remote_endpoint_; other_impl.remote_endpoint_ = typename Protocol1::endpoint(); } // Open a new socket implementation. asio::error_code open(implementation_type& impl, const protocol_type& protocol, asio::error_code& ec) { if (!do_open(impl, protocol.family(), protocol.type(), protocol.protocol(), ec)) { impl.protocol_ = protocol; impl.have_remote_endpoint_ = false; impl.remote_endpoint_ = endpoint_type(); } ASIO_ERROR_LOCATION(ec); return ec; } // Assign a native socket to a socket implementation. asio::error_code assign(implementation_type& impl, const protocol_type& protocol, const native_handle_type& native_socket, asio::error_code& ec) { if (!do_assign(impl, protocol.type(), native_socket, ec)) { impl.protocol_ = protocol; impl.have_remote_endpoint_ = native_socket.have_remote_endpoint(); impl.remote_endpoint_ = native_socket.remote_endpoint(); } ASIO_ERROR_LOCATION(ec); return ec; } // Get the native socket representation. native_handle_type native_handle(implementation_type& impl) { if (impl.have_remote_endpoint_) return native_handle_type(impl.socket_, impl.remote_endpoint_); return native_handle_type(impl.socket_); } // Bind the socket to the specified local endpoint. asio::error_code bind(implementation_type& impl, const endpoint_type& endpoint, asio::error_code& ec) { socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec); ASIO_ERROR_LOCATION(ec); return ec; } // Set a socket option. template <typename Option> asio::error_code set_option(implementation_type& impl, const Option& option, asio::error_code& ec) { socket_ops::setsockopt(impl.socket_, impl.state_, option.level(impl.protocol_), option.name(impl.protocol_), option.data(impl.protocol_), option.size(impl.protocol_), ec); ASIO_ERROR_LOCATION(ec); return ec; } // Set a socket option. template <typename Option> asio::error_code get_option(const implementation_type& impl, Option& option, asio::error_code& ec) const { std::size_t size = option.size(impl.protocol_); socket_ops::getsockopt(impl.socket_, impl.state_, option.level(impl.protocol_), option.name(impl.protocol_), option.data(impl.protocol_), &size, ec); if (!ec) option.resize(impl.protocol_, size); ASIO_ERROR_LOCATION(ec); return ec; } // Get the local endpoint. endpoint_type local_endpoint(const implementation_type& impl, asio::error_code& ec) const { endpoint_type endpoint; std::size_t addr_len = endpoint.capacity(); if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec)) { ASIO_ERROR_LOCATION(ec); return endpoint_type(); } endpoint.resize(addr_len); return endpoint; } // Get the remote endpoint. endpoint_type remote_endpoint(const implementation_type& impl, asio::error_code& ec) const { endpoint_type endpoint = impl.remote_endpoint_; std::size_t addr_len = endpoint.capacity(); if (socket_ops::getpeername(impl.socket_, endpoint.data(), &addr_len, impl.have_remote_endpoint_, ec)) { ASIO_ERROR_LOCATION(ec); return endpoint_type(); } endpoint.resize(addr_len); return endpoint; } // Disable sends or receives on the socket. asio::error_code shutdown(base_implementation_type& impl, socket_base::shutdown_type what, asio::error_code& ec) { socket_ops::shutdown(impl.socket_, what, ec); return ec; } // Send a datagram to the specified endpoint. Returns the number of bytes // sent. template <typename ConstBufferSequence> size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers, const endpoint_type& destination, socket_base::message_flags flags, asio::error_code& ec) { buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence> bufs(buffers); size_t n = socket_ops::sync_sendto(impl.socket_, impl.state_, bufs.buffers(), bufs.count(), flags, destination.data(), destination.size(), ec); ASIO_ERROR_LOCATION(ec); return n; } // Wait until data can be sent without blocking. size_t send_to(implementation_type& impl, const null_buffers&, const endpoint_type&, socket_base::message_flags, asio::error_code& ec) { // Wait for socket to become ready. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec); ASIO_ERROR_LOCATION(ec); return 0; } // Start an asynchronous send. The data being sent must be valid for the // lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler, typename IoExecutor> void async_send_to(implementation_type& impl, const ConstBufferSequence& buffers, const endpoint_type& destination, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) { associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef win_iocp_socket_send_op< ConstBufferSequence, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; operation* o = p.p = new (p.v) op( impl.cancel_token_, buffers, handler, io_ex); ASIO_HANDLER_CREATION((context_, *p.p, "socket", &impl, impl.socket_, "async_send_to")); buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence> bufs(buffers); // Optionally register for per-operation cancellation. if (slot.is_connected()) o = &slot.template emplace<iocp_op_cancellation>(impl.socket_, o); start_send_to_op(impl, bufs.buffers(), bufs.count(), destination.data(), static_cast<int>(destination.size()), flags, o); p.v = p.p = 0; } // Start an asynchronous wait until data can be sent without blocking. template <typename Handler, typename IoExecutor> void async_send_to(implementation_type& impl, const null_buffers&, const endpoint_type&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { // Allocate and construct an operation to wrap the handler. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; reactor_op* o = p.p = new (p.v) op(impl.cancel_token_, handler, io_ex); ASIO_HANDLER_CREATION((context_, *p.p, "socket", &impl, impl.socket_, "async_send_to(null_buffers)")); start_reactor_op(impl, select_reactor::write_op, o); p.v = p.p = 0; } // Receive a datagram with the endpoint of the sender. Returns the number of // bytes received. template <typename MutableBufferSequence> size_t receive_from(implementation_type& impl, const MutableBufferSequence& buffers, endpoint_type& sender_endpoint, socket_base::message_flags flags, asio::error_code& ec) { buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence> bufs(buffers); std::size_t addr_len = sender_endpoint.capacity(); std::size_t n = socket_ops::sync_recvfrom(impl.socket_, impl.state_, bufs.buffers(), bufs.count(), flags, sender_endpoint.data(), &addr_len, ec); if (!ec) sender_endpoint.resize(addr_len); ASIO_ERROR_LOCATION(ec); return n; } // Wait until data can be received without blocking. size_t receive_from(implementation_type& impl, const null_buffers&, endpoint_type& sender_endpoint, socket_base::message_flags, asio::error_code& ec) { // Wait for socket to become ready. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec); // Reset endpoint since it can be given no sensible value at this time. sender_endpoint = endpoint_type(); ASIO_ERROR_LOCATION(ec); return 0; } // Start an asynchronous receive. The buffer for the data being received and // the sender_endpoint object must both be valid for the lifetime of the // asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_receive_from(implementation_type& impl, const MutableBufferSequence& buffers, endpoint_type& sender_endp, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) { associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef win_iocp_socket_recvfrom_op<MutableBufferSequence, endpoint_type, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; operation* o = p.p = new (p.v) op(sender_endp, impl.cancel_token_, buffers, handler, io_ex); ASIO_HANDLER_CREATION((context_, *p.p, "socket", &impl, impl.socket_, "async_receive_from")); buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence> bufs(buffers); // Optionally register for per-operation cancellation. if (slot.is_connected()) o = &slot.template emplace<iocp_op_cancellation>(impl.socket_, o); start_receive_from_op(impl, bufs.buffers(), bufs.count(), sender_endp.data(), flags, &p.p->endpoint_size(), o); p.v = p.p = 0; } // Wait until data can be received without blocking. template <typename Handler, typename IoExecutor> void async_receive_from(implementation_type& impl, const null_buffers&, endpoint_type& sender_endpoint, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) { associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(impl.cancel_token_, handler, io_ex); ASIO_HANDLER_CREATION((context_, *p.p, "socket", &impl, impl.socket_, "async_receive_from(null_buffers)")); // Reset endpoint since it can be given no sensible value at this time. sender_endpoint = endpoint_type(); // Optionally register for per-operation cancellation. operation* iocp_op = p.p; if (slot.is_connected()) { p.p->cancellation_key_ = iocp_op = &slot.template emplace<reactor_op_cancellation>( impl.socket_, iocp_op); } int op_type = start_null_buffers_receive_op(impl, flags, p.p, iocp_op); p.v = p.p = 0; // Update cancellation method if the reactor was used. if (slot.is_connected() && op_type != -1) { static_cast<reactor_op_cancellation*>(iocp_op)->use_reactor( &get_reactor(), &impl.reactor_data_, op_type); } } // Accept a new connection. template <typename Socket> asio::error_code accept(implementation_type& impl, Socket& peer, endpoint_type* peer_endpoint, asio::error_code& ec) { // We cannot accept a socket that is already open. if (peer.is_open()) { ec = asio::error::already_open; ASIO_ERROR_LOCATION(ec); return ec; } std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0; socket_holder new_socket(socket_ops::sync_accept(impl.socket_, impl.state_, peer_endpoint ? peer_endpoint->data() : 0, peer_endpoint ? &addr_len : 0, ec)); // On success, assign new connection to peer socket object. if (new_socket.get() != invalid_socket) { if (peer_endpoint) peer_endpoint->resize(addr_len); peer.assign(impl.protocol_, new_socket.get(), ec); if (!ec) new_socket.release(); } ASIO_ERROR_LOCATION(ec); return ec; } // Start an asynchronous accept. The peer and peer_endpoint objects // must be valid until the accept's handler is invoked. template <typename Socket, typename Handler, typename IoExecutor> void async_accept(implementation_type& impl, Socket& peer, endpoint_type* peer_endpoint, Handler& handler, const IoExecutor& io_ex) { associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef win_iocp_socket_accept_op<Socket, protocol_type, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; bool enable_connection_aborted = (impl.state_ & socket_ops::enable_connection_aborted) != 0; operation* o = p.p = new (p.v) op(*this, impl.socket_, peer, impl.protocol_, peer_endpoint, enable_connection_aborted, handler, io_ex); ASIO_HANDLER_CREATION((context_, *p.p, "socket", &impl, impl.socket_, "async_accept")); // Optionally register for per-operation cancellation. if (slot.is_connected()) { accept_op_cancellation* c = &slot.template emplace<accept_op_cancellation>(impl.socket_, o); p.p->enable_cancellation(c->get_cancel_requested(), c); o = c; } start_accept_op(impl, peer.is_open(), p.p->new_socket(), impl.protocol_.family(), impl.protocol_.type(), impl.protocol_.protocol(), p.p->output_buffer(), p.p->address_length(), o); p.v = p.p = 0; } // Start an asynchronous accept. The peer and peer_endpoint objects // must be valid until the accept's handler is invoked. template <typename PeerIoExecutor, typename Handler, typename IoExecutor> void async_move_accept(implementation_type& impl, const PeerIoExecutor& peer_io_ex, endpoint_type* peer_endpoint, Handler& handler, const IoExecutor& io_ex) { associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef win_iocp_socket_move_accept_op< protocol_type, PeerIoExecutor, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; bool enable_connection_aborted = (impl.state_ & socket_ops::enable_connection_aborted) != 0; operation* o = p.p = new (p.v) op(*this, impl.socket_, impl.protocol_, peer_io_ex, peer_endpoint, enable_connection_aborted, handler, io_ex); ASIO_HANDLER_CREATION((context_, *p.p, "socket", &impl, impl.socket_, "async_accept")); // Optionally register for per-operation cancellation. if (slot.is_connected()) { accept_op_cancellation* c = &slot.template emplace<accept_op_cancellation>(impl.socket_, o); p.p->enable_cancellation(c->get_cancel_requested(), c); o = c; } start_accept_op(impl, false, p.p->new_socket(), impl.protocol_.family(), impl.protocol_.type(), impl.protocol_.protocol(), p.p->output_buffer(), p.p->address_length(), o); p.v = p.p = 0; } // Connect the socket to the specified endpoint. asio::error_code connect(implementation_type& impl, const endpoint_type& peer_endpoint, asio::error_code& ec) { socket_ops::sync_connect(impl.socket_, peer_endpoint.data(), peer_endpoint.size(), ec); ASIO_ERROR_LOCATION(ec); return ec; } // Start an asynchronous connect. template <typename Handler, typename IoExecutor> void async_connect(implementation_type& impl, const endpoint_type& peer_endpoint, Handler& handler, const IoExecutor& io_ex) { associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef win_iocp_socket_connect_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(impl.socket_, handler, io_ex); ASIO_HANDLER_CREATION((context_, *p.p, "socket", &impl, impl.socket_, "async_connect")); // Optionally register for per-operation cancellation. operation* iocp_op = p.p; if (slot.is_connected()) { p.p->cancellation_key_ = iocp_op = &slot.template emplace<reactor_op_cancellation>( impl.socket_, iocp_op); } int op_type = start_connect_op(impl, impl.protocol_.family(), impl.protocol_.type(), peer_endpoint.data(), static_cast<int>(peer_endpoint.size()), p.p, iocp_op); p.v = p.p = 0; // Update cancellation method if the reactor was used. if (slot.is_connected() && op_type != -1) { static_cast<reactor_op_cancellation*>(iocp_op)->use_reactor( &get_reactor(), &impl.reactor_data_, op_type); } } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IOCP) #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/winrt_socket_recv_op.hpp
// // detail/winrt_socket_recv_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WINRT_SOCKET_RECV_OP_HPP #define ASIO_DETAIL_WINRT_SOCKET_RECV_OP_HPP #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_RUNTIME) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/winrt_async_op.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename MutableBufferSequence, typename Handler, typename IoExecutor> class winrt_socket_recv_op : public winrt_async_op<Windows::Storage::Streams::IBuffer^> { public: ASIO_DEFINE_HANDLER_PTR(winrt_socket_recv_op); winrt_socket_recv_op(const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : winrt_async_op<Windows::Storage::Streams::IBuffer^>( &winrt_socket_recv_op::do_complete), buffers_(buffers), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code&, std::size_t) { // Take ownership of the operation object. ASIO_ASSUME(base != 0); winrt_socket_recv_op* o(static_cast<winrt_socket_recv_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Check whether buffers are still valid. if (owner) { buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence>::validate(o->buffers_); } #endif // defined(ASIO_ENABLE_BUFFER_DEBUGGING) std::size_t bytes_transferred = o->result_ ? o->result_->Length : 0; if (bytes_transferred == 0 && !o->ec_ && !buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence>::all_empty(o->buffers_)) { o->ec_ = asio::error::eof; } // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, bytes_transferred); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: MutableBufferSequence buffers_; Handler handler_; handler_work<Handler, IoExecutor> executor_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_WINDOWS_RUNTIME) #endif // ASIO_DETAIL_WINRT_SOCKET_RECV_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/throw_error.hpp
// // detail/throw_error.hpp // ~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_THROW_ERROR_HPP #define ASIO_DETAIL_THROW_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/detail/throw_exception.hpp" #include "asio/error_code.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { ASIO_DECL void do_throw_error( const asio::error_code& err ASIO_SOURCE_LOCATION_PARAM); ASIO_DECL void do_throw_error( const asio::error_code& err, const char* location ASIO_SOURCE_LOCATION_PARAM); inline void throw_error( const asio::error_code& err ASIO_SOURCE_LOCATION_DEFAULTED_PARAM) { if (err) do_throw_error(err ASIO_SOURCE_LOCATION_ARG); } inline void throw_error( const asio::error_code& err, const char* location ASIO_SOURCE_LOCATION_DEFAULTED_PARAM) { if (err) do_throw_error(err, location ASIO_SOURCE_LOCATION_ARG); } } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/throw_error.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // ASIO_DETAIL_THROW_ERROR_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/reactive_socket_send_op.hpp
// // detail/reactive_socket_send_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REACTIVE_SOCKET_SEND_OP_HPP #define ASIO_DETAIL_REACTIVE_SOCKET_SEND_OP_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/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename ConstBufferSequence> class reactive_socket_send_op_base : public reactor_op { public: reactive_socket_send_op_base(const asio::error_code& success_ec, socket_type socket, socket_ops::state_type state, const ConstBufferSequence& buffers, socket_base::message_flags flags, func_type complete_func) : reactor_op(success_ec, &reactive_socket_send_op_base::do_perform, complete_func), socket_(socket), state_(state), buffers_(buffers), flags_(flags) { } static status do_perform(reactor_op* base) { ASIO_ASSUME(base != 0); reactive_socket_send_op_base* o( static_cast<reactive_socket_send_op_base*>(base)); typedef buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence> bufs_type; status result; if (bufs_type::is_single_buffer) { result = socket_ops::non_blocking_send1(o->socket_, bufs_type::first(o->buffers_).data(), bufs_type::first(o->buffers_).size(), o->flags_, o->ec_, o->bytes_transferred_) ? done : not_done; if (result == done) if ((o->state_ & socket_ops::stream_oriented) != 0) if (o->bytes_transferred_ < bufs_type::first(o->buffers_).size()) result = done_and_exhausted; } else { bufs_type bufs(o->buffers_); result = socket_ops::non_blocking_send(o->socket_, bufs.buffers(), bufs.count(), o->flags_, o->ec_, o->bytes_transferred_) ? done : not_done; if (result == done) if ((o->state_ & socket_ops::stream_oriented) != 0) if (o->bytes_transferred_ < bufs.total_size()) result = done_and_exhausted; } ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_send", o->ec_, o->bytes_transferred_)); return result; } private: socket_type socket_; socket_ops::state_type state_; ConstBufferSequence buffers_; socket_base::message_flags flags_; }; template <typename ConstBufferSequence, typename Handler, typename IoExecutor> class reactive_socket_send_op : public reactive_socket_send_op_base<ConstBufferSequence> { public: typedef Handler handler_type; typedef IoExecutor io_executor_type; ASIO_DEFINE_HANDLER_PTR(reactive_socket_send_op); reactive_socket_send_op(const asio::error_code& success_ec, socket_type socket, socket_ops::state_type state, const ConstBufferSequence& buffers, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) : reactive_socket_send_op_base<ConstBufferSequence>(success_ec, socket, state, buffers, flags, &reactive_socket_send_op::do_complete), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); reactive_socket_send_op* o(static_cast<reactive_socket_send_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->bytes_transferred_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } static void do_immediate(operation* base, bool, const void* io_ex) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); reactive_socket_send_op* o(static_cast<reactive_socket_send_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. immediate_handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->bytes_transferred_); p.h = asio::detail::addressof(handler.handler_); p.reset(); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_, io_ex); ASIO_HANDLER_INVOCATION_END; } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_REACTIVE_SOCKET_SEND_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/dev_poll_reactor.hpp
// // detail/dev_poll_reactor.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_DEV_POLL_REACTOR_HPP #define ASIO_DETAIL_DEV_POLL_REACTOR_HPP #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_DEV_POLL) #include <cstddef> #include <vector> #include <sys/devpoll.h> #include "asio/detail/hash_map.hpp" #include "asio/detail/limits.hpp" #include "asio/detail/mutex.hpp" #include "asio/detail/op_queue.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/detail/reactor_op_queue.hpp" #include "asio/detail/scheduler_task.hpp" #include "asio/detail/select_interrupter.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/timer_queue_base.hpp" #include "asio/detail/timer_queue_set.hpp" #include "asio/detail/wait_op.hpp" #include "asio/execution_context.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class dev_poll_reactor : public execution_context_service_base<dev_poll_reactor>, public scheduler_task { public: enum op_types { read_op = 0, write_op = 1, connect_op = 1, except_op = 2, max_ops = 3 }; // Per-descriptor data. struct per_descriptor_data { }; // Constructor. ASIO_DECL dev_poll_reactor(asio::execution_context& ctx); // Destructor. ASIO_DECL ~dev_poll_reactor(); // Destroy all user-defined handler objects owned by the service. ASIO_DECL void shutdown(); // Recreate internal descriptors following a fork. ASIO_DECL void notify_fork( asio::execution_context::fork_event fork_ev); // Initialise the task. ASIO_DECL void init_task(); // Register a socket with the reactor. Returns 0 on success, system error // code on failure. ASIO_DECL int register_descriptor(socket_type, per_descriptor_data&); // Register a descriptor with an associated single operation. Returns 0 on // success, system error code on failure. ASIO_DECL int register_internal_descriptor( int op_type, socket_type descriptor, per_descriptor_data& descriptor_data, reactor_op* op); // Move descriptor registration from one descriptor_data object to another. ASIO_DECL void move_descriptor(socket_type descriptor, per_descriptor_data& target_descriptor_data, per_descriptor_data& source_descriptor_data); // Post a reactor operation for immediate completion. void post_immediate_completion(operation* op, bool is_continuation) const; // Post a reactor operation for immediate completion. ASIO_DECL static void call_post_immediate_completion( operation* op, bool is_continuation, const void* self); // Start a new operation. The reactor operation will be performed when the // given descriptor is flagged as ready, or an error has occurred. ASIO_DECL void start_op(int op_type, socket_type descriptor, per_descriptor_data&, reactor_op* op, bool is_continuation, bool allow_speculative, void (*on_immediate)(operation*, bool, const void*), const void* immediate_arg); // Start a new operation. The reactor operation will be performed when the // given descriptor is flagged as ready, or an error has occurred. void start_op(int op_type, socket_type descriptor, per_descriptor_data& descriptor_data, reactor_op* op, bool is_continuation, bool allow_speculative) { start_op(op_type, descriptor, descriptor_data, op, is_continuation, allow_speculative, &dev_poll_reactor::call_post_immediate_completion, this); } // Cancel all operations associated with the given descriptor. The // handlers associated with the descriptor will be invoked with the // operation_aborted error. ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data&); // Cancel all operations associated with the given descriptor and key. The // handlers associated with the descriptor will be invoked with the // operation_aborted error. ASIO_DECL void cancel_ops_by_key(socket_type descriptor, per_descriptor_data& descriptor_data, int op_type, void* cancellation_key); // Cancel any operations that are running against the descriptor and remove // its registration from the reactor. The reactor resources associated with // the descriptor must be released by calling cleanup_descriptor_data. ASIO_DECL void deregister_descriptor(socket_type descriptor, per_descriptor_data&, bool closing); // Remove the descriptor's registration from the reactor. The reactor // resources associated with the descriptor must be released by calling // cleanup_descriptor_data. ASIO_DECL void deregister_internal_descriptor( socket_type descriptor, per_descriptor_data&); // Perform any post-deregistration cleanup tasks associated with the // descriptor data. ASIO_DECL void cleanup_descriptor_data(per_descriptor_data&); // Add a new timer queue to the reactor. template <typename Time_Traits> void add_timer_queue(timer_queue<Time_Traits>& queue); // Remove a timer queue from the reactor. template <typename Time_Traits> void remove_timer_queue(timer_queue<Time_Traits>& queue); // Schedule a new operation in the given timer queue to expire at the // specified absolute time. template <typename Time_Traits> void schedule_timer(timer_queue<Time_Traits>& queue, const typename Time_Traits::time_type& time, typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op); // Cancel the timer operations associated with the given token. Returns the // number of operations that have been posted or dispatched. template <typename Time_Traits> std::size_t cancel_timer(timer_queue<Time_Traits>& queue, typename timer_queue<Time_Traits>::per_timer_data& timer, std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)()); // Cancel the timer operations associated with the given key. template <typename Time_Traits> void cancel_timer_by_key(timer_queue<Time_Traits>& queue, typename timer_queue<Time_Traits>::per_timer_data* timer, void* cancellation_key); // Move the timer operations associated with the given timer. template <typename Time_Traits> void move_timer(timer_queue<Time_Traits>& queue, typename timer_queue<Time_Traits>::per_timer_data& target, typename timer_queue<Time_Traits>::per_timer_data& source); // Run /dev/poll once until interrupted or events are ready to be dispatched. ASIO_DECL void run(long usec, op_queue<operation>& ops); // Interrupt the select loop. ASIO_DECL void interrupt(); private: // Create the /dev/poll file descriptor. Throws an exception if the descriptor // cannot be created. ASIO_DECL static int do_dev_poll_create(); // Helper function to add a new timer queue. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue); // Helper function to remove a timer queue. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue); // Get the timeout value for the /dev/poll DP_POLL operation. The timeout // value is returned as a number of milliseconds. A return value of -1 // indicates that the poll should block indefinitely. ASIO_DECL int get_timeout(int msec); // Cancel all operations associated with the given descriptor. The do_cancel // function of the handler objects will be invoked. This function does not // acquire the dev_poll_reactor's mutex. ASIO_DECL void cancel_ops_unlocked(socket_type descriptor, const asio::error_code& ec); // Add a pending event entry for the given descriptor. ASIO_DECL ::pollfd& add_pending_event_change(int descriptor); // The scheduler implementation used to post completions. scheduler& scheduler_; // Mutex to protect access to internal data. asio::detail::mutex mutex_; // The /dev/poll file descriptor. int dev_poll_fd_; // Vector of /dev/poll events waiting to be written to the descriptor. std::vector< ::pollfd> pending_event_changes_; // Hash map to associate a descriptor with a pending event change index. hash_map<int, std::size_t> pending_event_change_index_; // The interrupter is used to break a blocking DP_POLL operation. select_interrupter interrupter_; // The queues of read, write and except operations. reactor_op_queue<socket_type> op_queue_[max_ops]; // The timer queues. timer_queue_set timer_queues_; // Whether the service has been shut down. bool shutdown_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #include "asio/detail/impl/dev_poll_reactor.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/dev_poll_reactor.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // defined(ASIO_HAS_DEV_POLL) #endif // ASIO_DETAIL_DEV_POLL_REACTOR_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/work_dispatcher.hpp
// // detail/work_dispatcher.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WORK_DISPATCHER_HPP #define ASIO_DETAIL_WORK_DISPATCHER_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/bind_handler.hpp" #include "asio/detail/type_traits.hpp" #include "asio/associated_executor.hpp" #include "asio/associated_allocator.hpp" #include "asio/executor_work_guard.hpp" #include "asio/execution/executor.hpp" #include "asio/execution/allocator.hpp" #include "asio/execution/blocking.hpp" #include "asio/execution/outstanding_work.hpp" #include "asio/prefer.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Handler, typename Executor, typename = void> struct is_work_dispatcher_required : true_type { }; template <typename Handler, typename Executor> struct is_work_dispatcher_required<Handler, Executor, enable_if_t< is_same< typename associated_executor<Handler, Executor>::asio_associated_executor_is_unspecialised, void >::value >> : false_type { }; template <typename Handler, typename Executor, typename = void> class work_dispatcher { public: template <typename CompletionHandler> work_dispatcher(CompletionHandler&& handler, const Executor& handler_ex) : handler_(static_cast<CompletionHandler&&>(handler)), executor_(asio::prefer(handler_ex, execution::outstanding_work.tracked)) { } work_dispatcher(const work_dispatcher& other) : handler_(other.handler_), executor_(other.executor_) { } work_dispatcher(work_dispatcher&& other) : handler_(static_cast<Handler&&>(other.handler_)), executor_(static_cast<work_executor_type&&>(other.executor_)) { } void operator()() { associated_allocator_t<Handler> alloc((get_associated_allocator)(handler_)); asio::prefer(executor_, execution::allocator(alloc)).execute( asio::detail::bind_handler( static_cast<Handler&&>(handler_))); } private: typedef decay_t< prefer_result_t<const Executor&, execution::outstanding_work_t::tracked_t > > work_executor_type; Handler handler_; work_executor_type executor_; }; #if !defined(ASIO_NO_TS_EXECUTORS) template <typename Handler, typename Executor> class work_dispatcher<Handler, Executor, enable_if_t<!execution::is_executor<Executor>::value>> { public: template <typename CompletionHandler> work_dispatcher(CompletionHandler&& handler, const Executor& handler_ex) : work_(handler_ex), handler_(static_cast<CompletionHandler&&>(handler)) { } work_dispatcher(const work_dispatcher& other) : work_(other.work_), handler_(other.handler_) { } work_dispatcher(work_dispatcher&& other) : work_(static_cast<executor_work_guard<Executor>&&>(other.work_)), handler_(static_cast<Handler&&>(other.handler_)) { } void operator()() { associated_allocator_t<Handler> alloc((get_associated_allocator)(handler_)); work_.get_executor().dispatch( asio::detail::bind_handler( static_cast<Handler&&>(handler_)), alloc); work_.reset(); } private: executor_work_guard<Executor> work_; Handler handler_; }; #endif // !defined(ASIO_NO_TS_EXECUTORS) } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_WORK_DISPATCHER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/posix_serial_port_service.hpp
// // detail/posix_serial_port_service.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) // Copyright (c) 2008 Rep Invariant Systems, Inc. ([email protected]) // // 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_DETAIL_POSIX_SERIAL_PORT_SERVICE_HPP #define ASIO_DETAIL_POSIX_SERIAL_PORT_SERVICE_HPP #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) #if !defined(ASIO_WINDOWS) && !defined(__CYGWIN__) #include <string> #include "asio/error.hpp" #include "asio/execution_context.hpp" #include "asio/serial_port_base.hpp" #include "asio/detail/descriptor_ops.hpp" #if defined(ASIO_HAS_IO_URING_AS_DEFAULT) # include "asio/detail/io_uring_descriptor_service.hpp" #else // defined(ASIO_HAS_IO_URING_AS_DEFAULT) # include "asio/detail/reactive_descriptor_service.hpp" #endif // defined(ASIO_HAS_IO_URING_AS_DEFAULT) #include "asio/detail/push_options.hpp" namespace asio { namespace detail { // Extend a descriptor_service to provide serial port support. class posix_serial_port_service : public execution_context_service_base<posix_serial_port_service> { public: #if defined(ASIO_HAS_IO_URING_AS_DEFAULT) typedef io_uring_descriptor_service descriptor_service; #else // defined(ASIO_HAS_IO_URING_AS_DEFAULT) typedef reactive_descriptor_service descriptor_service; #endif // defined(ASIO_HAS_IO_URING_AS_DEFAULT) // The native type of a serial port. typedef descriptor_service::native_handle_type native_handle_type; // The implementation type of the serial port. typedef descriptor_service::implementation_type implementation_type; ASIO_DECL posix_serial_port_service(execution_context& context); // Destroy all user-defined handler objects owned by the service. ASIO_DECL void shutdown(); // Construct a new serial port implementation. void construct(implementation_type& impl) { descriptor_service_.construct(impl); } // Move-construct a new serial port implementation. void move_construct(implementation_type& impl, implementation_type& other_impl) { descriptor_service_.move_construct(impl, other_impl); } // Move-assign from another serial port implementation. void move_assign(implementation_type& impl, posix_serial_port_service& other_service, implementation_type& other_impl) { descriptor_service_.move_assign(impl, other_service.descriptor_service_, other_impl); } // Destroy a serial port implementation. void destroy(implementation_type& impl) { descriptor_service_.destroy(impl); } // Open the serial port using the specified device name. ASIO_DECL asio::error_code open(implementation_type& impl, const std::string& device, asio::error_code& ec); // Assign a native descriptor to a serial port implementation. asio::error_code assign(implementation_type& impl, const native_handle_type& native_descriptor, asio::error_code& ec) { return descriptor_service_.assign(impl, native_descriptor, ec); } // Determine whether the serial port is open. bool is_open(const implementation_type& impl) const { return descriptor_service_.is_open(impl); } // Destroy a serial port implementation. asio::error_code close(implementation_type& impl, asio::error_code& ec) { return descriptor_service_.close(impl, ec); } // Get the native serial port representation. native_handle_type native_handle(implementation_type& impl) { return descriptor_service_.native_handle(impl); } // Cancel all operations associated with the serial port. asio::error_code cancel(implementation_type& impl, asio::error_code& ec) { return descriptor_service_.cancel(impl, ec); } // Set an option on the serial port. template <typename SettableSerialPortOption> asio::error_code set_option(implementation_type& impl, const SettableSerialPortOption& option, asio::error_code& ec) { return do_set_option(impl, &posix_serial_port_service::store_option<SettableSerialPortOption>, &option, ec); } // Get an option from the serial port. template <typename GettableSerialPortOption> asio::error_code get_option(const implementation_type& impl, GettableSerialPortOption& option, asio::error_code& ec) const { return do_get_option(impl, &posix_serial_port_service::load_option<GettableSerialPortOption>, &option, ec); } // Send a break sequence to the serial port. asio::error_code send_break(implementation_type& impl, asio::error_code& ec) { int result = ::tcsendbreak(descriptor_service_.native_handle(impl), 0); descriptor_ops::get_last_error(ec, result < 0); ASIO_ERROR_LOCATION(ec); return ec; } // Write the given data. Returns the number of bytes sent. template <typename ConstBufferSequence> size_t write_some(implementation_type& impl, const ConstBufferSequence& buffers, asio::error_code& ec) { return descriptor_service_.write_some(impl, buffers, ec); } // Start an asynchronous write. The data being written must be valid for the // lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler, typename IoExecutor> void async_write_some(implementation_type& impl, const ConstBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) { descriptor_service_.async_write_some(impl, buffers, handler, io_ex); } // Read some data. Returns the number of bytes received. template <typename MutableBufferSequence> size_t read_some(implementation_type& impl, const MutableBufferSequence& buffers, asio::error_code& ec) { return descriptor_service_.read_some(impl, buffers, ec); } // Start an asynchronous read. The buffer for the data being received must be // valid for the lifetime of the asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_read_some(implementation_type& impl, const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) { descriptor_service_.async_read_some(impl, buffers, handler, io_ex); } private: // Function pointer type for storing a serial port option. typedef asio::error_code (*store_function_type)( const void*, termios&, asio::error_code&); // Helper function template to store a serial port option. template <typename SettableSerialPortOption> static asio::error_code store_option(const void* option, termios& storage, asio::error_code& ec) { static_cast<const SettableSerialPortOption*>(option)->store(storage, ec); return ec; } // Helper function to set a serial port option. ASIO_DECL asio::error_code do_set_option( implementation_type& impl, store_function_type store, const void* option, asio::error_code& ec); // Function pointer type for loading a serial port option. typedef asio::error_code (*load_function_type)( void*, const termios&, asio::error_code&); // Helper function template to load a serial port option. template <typename GettableSerialPortOption> static asio::error_code load_option(void* option, const termios& storage, asio::error_code& ec) { static_cast<GettableSerialPortOption*>(option)->load(storage, ec); return ec; } // Helper function to get a serial port option. ASIO_DECL asio::error_code do_get_option( const implementation_type& impl, load_function_type load, void* option, asio::error_code& ec) const; // The implementation used for initiating asynchronous operations. descriptor_service descriptor_service_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/posix_serial_port_service.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__) #endif // defined(ASIO_HAS_SERIAL_PORT) #endif // ASIO_DETAIL_POSIX_SERIAL_PORT_SERVICE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/concurrency_hint.hpp
// // detail/concurrency_hint.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_CONCURRENCY_HINT_HPP #define ASIO_DETAIL_CONCURRENCY_HINT_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/noncopyable.hpp" // The concurrency hint ID and mask are used to identify when a "well-known" // concurrency hint value has been passed to the io_context. #define ASIO_CONCURRENCY_HINT_ID 0xA5100000u #define ASIO_CONCURRENCY_HINT_ID_MASK 0xFFFF0000u // If set, this bit indicates that the scheduler should perform locking. #define ASIO_CONCURRENCY_HINT_LOCKING_SCHEDULER 0x1u // If set, this bit indicates that the reactor should perform locking when // managing descriptor registrations. #define ASIO_CONCURRENCY_HINT_LOCKING_REACTOR_REGISTRATION 0x2u // If set, this bit indicates that the reactor should perform locking for I/O. #define ASIO_CONCURRENCY_HINT_LOCKING_REACTOR_IO 0x4u // Helper macro to determine if we have a special concurrency hint. #define ASIO_CONCURRENCY_HINT_IS_SPECIAL(hint) \ ((static_cast<unsigned>(hint) \ & ASIO_CONCURRENCY_HINT_ID_MASK) \ == ASIO_CONCURRENCY_HINT_ID) // Helper macro to determine if locking is enabled for a given facility. #define ASIO_CONCURRENCY_HINT_IS_LOCKING(facility, hint) \ (((static_cast<unsigned>(hint) \ & (ASIO_CONCURRENCY_HINT_ID_MASK \ | ASIO_CONCURRENCY_HINT_LOCKING_ ## facility)) \ ^ ASIO_CONCURRENCY_HINT_ID) != 0) // This special concurrency hint disables locking in both the scheduler and // reactor I/O. This hint has the following restrictions: // // - Care must be taken to ensure that all operations on the io_context and any // of its associated I/O objects (such as sockets and timers) occur in only // one thread at a time. // // - Asynchronous resolve operations fail with operation_not_supported. // // - If a signal_set is used with the io_context, signal_set objects cannot be // used with any other io_context in the program. #define ASIO_CONCURRENCY_HINT_UNSAFE \ static_cast<int>(ASIO_CONCURRENCY_HINT_ID) // This special concurrency hint disables locking in the reactor I/O. This hint // has the following restrictions: // // - Care must be taken to ensure that run functions on the io_context, and all // operations on the io_context's associated I/O objects (such as sockets and // timers), occur in only one thread at a time. #define ASIO_CONCURRENCY_HINT_UNSAFE_IO \ static_cast<int>(ASIO_CONCURRENCY_HINT_ID \ | ASIO_CONCURRENCY_HINT_LOCKING_SCHEDULER \ | ASIO_CONCURRENCY_HINT_LOCKING_REACTOR_REGISTRATION) // The special concurrency hint provides full thread safety. #define ASIO_CONCURRENCY_HINT_SAFE \ static_cast<int>(ASIO_CONCURRENCY_HINT_ID \ | ASIO_CONCURRENCY_HINT_LOCKING_SCHEDULER \ | ASIO_CONCURRENCY_HINT_LOCKING_REACTOR_REGISTRATION \ | ASIO_CONCURRENCY_HINT_LOCKING_REACTOR_IO) // This #define may be overridden at compile time to specify a program-wide // default concurrency hint, used by the zero-argument io_context constructor. #if !defined(ASIO_CONCURRENCY_HINT_DEFAULT) # define ASIO_CONCURRENCY_HINT_DEFAULT -1 #endif // !defined(ASIO_CONCURRENCY_HINT_DEFAULT) // This #define may be overridden at compile time to specify a program-wide // concurrency hint, used by the one-argument io_context constructor when // passed a value of 1. #if !defined(ASIO_CONCURRENCY_HINT_1) # define ASIO_CONCURRENCY_HINT_1 1 #endif // !defined(ASIO_CONCURRENCY_HINT_DEFAULT) #endif // ASIO_DETAIL_CONCURRENCY_HINT_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_iocp_wait_op.hpp
// // detail/win_iocp_wait_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WIN_IOCP_WAIT_OP_HPP #define ASIO_DETAIL_WIN_IOCP_WAIT_OP_HPP #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_IOCP) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Handler, typename IoExecutor> class win_iocp_wait_op : public reactor_op { public: ASIO_DEFINE_HANDLER_PTR(win_iocp_wait_op); win_iocp_wait_op(socket_ops::weak_cancel_token_type cancel_token, Handler& handler, const IoExecutor& io_ex) : reactor_op(asio::error_code(), &win_iocp_wait_op::do_perform, &win_iocp_wait_op::do_complete), cancel_token_(cancel_token), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static status do_perform(reactor_op*) { return done; } static void do_complete(void* owner, operation* base, const asio::error_code& result_ec, std::size_t /*bytes_transferred*/) { asio::error_code ec(result_ec); // Take ownership of the operation object. ASIO_ASSUME(base != 0); win_iocp_wait_op* o(static_cast<win_iocp_wait_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); // The reactor may have stored a result in the operation object. if (o->ec_) ec = o->ec_; // Map non-portable errors to their portable counterparts. if (ec.value() == ERROR_NETNAME_DELETED) { if (o->cancel_token_.expired()) ec = asio::error::operation_aborted; else ec = asio::error::connection_reset; } else if (ec.value() == ERROR_PORT_UNREACHABLE) { ec = asio::error::connection_refused; } ASIO_ERROR_LOCATION(ec); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder1<Handler, asio::error_code> handler(o->handler_, ec); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: socket_ops::weak_cancel_token_type cancel_token_; Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IOCP) #endif // ASIO_DETAIL_WIN_IOCP_WAIT_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/blocking_executor_op.hpp
// // detail/blocking_executor_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_BLOCKING_EXECUTOR_OP_HPP #define ASIO_DETAIL_BLOCKING_EXECUTOR_OP_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/event.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/mutex.hpp" #include "asio/detail/scheduler_operation.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Operation = scheduler_operation> class blocking_executor_op_base : public Operation { public: blocking_executor_op_base(typename Operation::func_type complete_func) : Operation(complete_func), is_complete_(false) { } void wait() { asio::detail::mutex::scoped_lock lock(mutex_); while (!is_complete_) event_.wait(lock); } protected: struct do_complete_cleanup { ~do_complete_cleanup() { asio::detail::mutex::scoped_lock lock(op_->mutex_); op_->is_complete_ = true; op_->event_.unlock_and_signal_one_for_destruction(lock); } blocking_executor_op_base* op_; }; private: asio::detail::mutex mutex_; asio::detail::event event_; bool is_complete_; }; template <typename Handler, typename Operation = scheduler_operation> class blocking_executor_op : public blocking_executor_op_base<Operation> { public: blocking_executor_op(Handler& h) : blocking_executor_op_base<Operation>(&blocking_executor_op::do_complete), handler_(h) { } static void do_complete(void* owner, Operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { ASIO_ASSUME(base != 0); blocking_executor_op* o(static_cast<blocking_executor_op*>(base)); typename blocking_executor_op_base<Operation>::do_complete_cleanup on_exit = { o }; (void)on_exit; ASIO_HANDLER_COMPLETION((*o)); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN(()); static_cast<Handler&&>(o->handler_)(); ASIO_HANDLER_INVOCATION_END; } } private: Handler& handler_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_BLOCKING_EXECUTOR_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/signal_handler.hpp
// // detail/signal_handler.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_SIGNAL_HANDLER_HPP #define ASIO_DETAIL_SIGNAL_HANDLER_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/bind_handler.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/signal_op.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Handler, typename IoExecutor> class signal_handler : public signal_op { public: ASIO_DEFINE_HANDLER_PTR(signal_handler); signal_handler(Handler& h, const IoExecutor& io_ex) : signal_op(&signal_handler::do_complete), handler_(static_cast<Handler&&>(h)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. signal_handler* h(static_cast<signal_handler*>(base)); ptr p = { asio::detail::addressof(h->handler_), h, h }; ASIO_HANDLER_COMPLETION((*h)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( h->work_)); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, int> handler(h->handler_, h->ec_, h->signal_number_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_SIGNAL_HANDLER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/reactive_socket_connect_op.hpp
// // detail/reactive_socket_connect_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REACTIVE_SOCKET_CONNECT_OP_HPP #define ASIO_DETAIL_REACTIVE_SOCKET_CONNECT_OP_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/bind_handler.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class reactive_socket_connect_op_base : public reactor_op { public: reactive_socket_connect_op_base(const asio::error_code& success_ec, socket_type socket, func_type complete_func) : reactor_op(success_ec, &reactive_socket_connect_op_base::do_perform, complete_func), socket_(socket) { } static status do_perform(reactor_op* base) { ASIO_ASSUME(base != 0); reactive_socket_connect_op_base* o( static_cast<reactive_socket_connect_op_base*>(base)); status result = socket_ops::non_blocking_connect( o->socket_, o->ec_) ? done : not_done; ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_connect", o->ec_)); return result; } private: socket_type socket_; }; template <typename Handler, typename IoExecutor> class reactive_socket_connect_op : public reactive_socket_connect_op_base { public: typedef Handler handler_type; typedef IoExecutor io_executor_type; ASIO_DEFINE_HANDLER_PTR(reactive_socket_connect_op); reactive_socket_connect_op(const asio::error_code& success_ec, socket_type socket, Handler& handler, const IoExecutor& io_ex) : reactive_socket_connect_op_base(success_ec, socket, &reactive_socket_connect_op::do_complete), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); reactive_socket_connect_op* o (static_cast<reactive_socket_connect_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder1<Handler, asio::error_code> handler(o->handler_, o->ec_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } static void do_immediate(operation* base, bool, const void* io_ex) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); reactive_socket_connect_op* o (static_cast<reactive_socket_connect_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. immediate_handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder1<Handler, asio::error_code> handler(o->handler_, o->ec_); p.h = asio::detail::addressof(handler.handler_); p.reset(); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_)); w.complete(handler, handler.handler_, io_ex); ASIO_HANDLER_INVOCATION_END; } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_REACTIVE_SOCKET_CONNECT_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_fd_set_adapter.hpp
// // detail/win_fd_set_adapter.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WIN_FD_SET_ADAPTER_HPP #define ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP #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 "asio/detail/noncopyable.hpp" #include "asio/detail/reactor_op_queue.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { // Adapts the FD_SET type to meet the Descriptor_Set concept's requirements. class win_fd_set_adapter : noncopyable { public: enum { default_fd_set_size = 1024 }; win_fd_set_adapter() : capacity_(default_fd_set_size), max_descriptor_(invalid_socket) { fd_set_ = static_cast<win_fd_set*>(::operator new( sizeof(win_fd_set) - sizeof(SOCKET) + sizeof(SOCKET) * (capacity_))); fd_set_->fd_count = 0; } ~win_fd_set_adapter() { ::operator delete(fd_set_); } void reset() { fd_set_->fd_count = 0; max_descriptor_ = invalid_socket; } bool set(socket_type descriptor) { for (u_int i = 0; i < fd_set_->fd_count; ++i) if (fd_set_->fd_array[i] == descriptor) return true; reserve(fd_set_->fd_count + 1); fd_set_->fd_array[fd_set_->fd_count++] = descriptor; return true; } void set(reactor_op_queue<socket_type>& operations, op_queue<operation>&) { reactor_op_queue<socket_type>::iterator i = operations.begin(); while (i != operations.end()) { reactor_op_queue<socket_type>::iterator op_iter = i++; reserve(fd_set_->fd_count + 1); fd_set_->fd_array[fd_set_->fd_count++] = op_iter->first; } } bool is_set(socket_type descriptor) const { return !!__WSAFDIsSet(descriptor, const_cast<fd_set*>(reinterpret_cast<const fd_set*>(fd_set_))); } operator fd_set*() { return reinterpret_cast<fd_set*>(fd_set_); } socket_type max_descriptor() const { return max_descriptor_; } void perform(reactor_op_queue<socket_type>& operations, op_queue<operation>& ops) const { for (u_int i = 0; i < fd_set_->fd_count; ++i) operations.perform_operations(fd_set_->fd_array[i], ops); } private: // This structure is defined to be compatible with the Windows API fd_set // structure, but without being dependent on the value of FD_SETSIZE. We use // the "struct hack" to allow the number of descriptors to be varied at // runtime. struct win_fd_set { u_int fd_count; SOCKET fd_array[1]; }; // Increase the fd_set_ capacity to at least the specified number of elements. void reserve(u_int n) { if (n <= capacity_) return; u_int new_capacity = capacity_ + capacity_ / 2; if (new_capacity < n) new_capacity = n; win_fd_set* new_fd_set = static_cast<win_fd_set*>(::operator new( sizeof(win_fd_set) - sizeof(SOCKET) + sizeof(SOCKET) * (new_capacity))); new_fd_set->fd_count = fd_set_->fd_count; for (u_int i = 0; i < fd_set_->fd_count; ++i) new_fd_set->fd_array[i] = fd_set_->fd_array[i]; ::operator delete(fd_set_); fd_set_ = new_fd_set; capacity_ = new_capacity; } win_fd_set* fd_set_; u_int capacity_; socket_type max_descriptor_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__) #endif // ASIO_DETAIL_WIN_FD_SET_ADAPTER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/base_from_completion_cond.hpp
// // detail/base_from_completion_cond.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_BASE_FROM_COMPLETION_COND_HPP #define ASIO_DETAIL_BASE_FROM_COMPLETION_COND_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/completion_condition.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename CompletionCondition> class base_from_completion_cond { protected: explicit base_from_completion_cond(CompletionCondition& completion_condition) : completion_condition_( static_cast<CompletionCondition&&>(completion_condition)) { } std::size_t check_for_completion( const asio::error_code& ec, std::size_t total_transferred) { return detail::adapt_completion_condition_result( completion_condition_(ec, total_transferred)); } private: CompletionCondition completion_condition_; }; template <> class base_from_completion_cond<transfer_all_t> { protected: explicit base_from_completion_cond(transfer_all_t) { } static std::size_t check_for_completion( const asio::error_code& ec, std::size_t total_transferred) { return transfer_all_t()(ec, total_transferred); } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_BASE_FROM_COMPLETION_COND_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/io_uring_descriptor_read_op.hpp
// // detail/io_uring_descriptor_read_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_IO_URING_DESCRIPTOR_READ_OP_HPP #define ASIO_DETAIL_IO_URING_DESCRIPTOR_READ_OP_HPP #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_IO_URING) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/descriptor_ops.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/io_uring_operation.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename MutableBufferSequence> class io_uring_descriptor_read_op_base : public io_uring_operation { public: io_uring_descriptor_read_op_base(const asio::error_code& success_ec, int descriptor, descriptor_ops::state_type state, const MutableBufferSequence& buffers, func_type complete_func) : io_uring_operation(success_ec, &io_uring_descriptor_read_op_base::do_prepare, &io_uring_descriptor_read_op_base::do_perform, complete_func), descriptor_(descriptor), state_(state), buffers_(buffers), bufs_(buffers) { } static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe) { ASIO_ASSUME(base != 0); io_uring_descriptor_read_op_base* o( static_cast<io_uring_descriptor_read_op_base*>(base)); if ((o->state_ & descriptor_ops::internal_non_blocking) != 0) { ::io_uring_prep_poll_add(sqe, o->descriptor_, POLLIN); } else if (o->bufs_.is_single_buffer && o->bufs_.is_registered_buffer) { ::io_uring_prep_read_fixed(sqe, o->descriptor_, o->bufs_.buffers()->iov_base, o->bufs_.buffers()->iov_len, 0, o->bufs_.registered_id().native_handle()); } else { ::io_uring_prep_readv(sqe, o->descriptor_, o->bufs_.buffers(), o->bufs_.count(), -1); } } static bool do_perform(io_uring_operation* base, bool after_completion) { ASIO_ASSUME(base != 0); io_uring_descriptor_read_op_base* o( static_cast<io_uring_descriptor_read_op_base*>(base)); if ((o->state_ & descriptor_ops::internal_non_blocking) != 0) { if (o->bufs_.is_single_buffer) { return descriptor_ops::non_blocking_read1( o->descriptor_, o->bufs_.first(o->buffers_).data(), o->bufs_.first(o->buffers_).size(), o->ec_, o->bytes_transferred_); } else { return descriptor_ops::non_blocking_read( o->descriptor_, o->bufs_.buffers(), o->bufs_.count(), o->ec_, o->bytes_transferred_); } } else if (after_completion) { if (!o->ec_ && o->bytes_transferred_ == 0) o->ec_ = asio::error::eof; } if (o->ec_ && o->ec_ == asio::error::would_block) { o->state_ |= descriptor_ops::internal_non_blocking; return false; } return after_completion; } private: int descriptor_; descriptor_ops::state_type state_; MutableBufferSequence buffers_; buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence> bufs_; }; template <typename MutableBufferSequence, typename Handler, typename IoExecutor> class io_uring_descriptor_read_op : public io_uring_descriptor_read_op_base<MutableBufferSequence> { public: ASIO_DEFINE_HANDLER_PTR(io_uring_descriptor_read_op); io_uring_descriptor_read_op(const asio::error_code& success_ec, int descriptor, descriptor_ops::state_type state, const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : io_uring_descriptor_read_op_base<MutableBufferSequence>(success_ec, descriptor, state, buffers, &io_uring_descriptor_read_op::do_complete), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); io_uring_descriptor_read_op* o (static_cast<io_uring_descriptor_read_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->bytes_transferred_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IO_URING) #endif // ASIO_DETAIL_IO_URING_DESCRIPTOR_READ_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/handler_tracking.hpp
// // detail/handler_tracking.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_HANDLER_TRACKING_HPP #define ASIO_DETAIL_HANDLER_TRACKING_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" namespace asio { class execution_context; } // namespace asio #if defined(ASIO_CUSTOM_HANDLER_TRACKING) # include ASIO_CUSTOM_HANDLER_TRACKING #elif defined(ASIO_ENABLE_HANDLER_TRACKING) # include "asio/error_code.hpp" # include "asio/detail/cstdint.hpp" # include "asio/detail/static_mutex.hpp" # include "asio/detail/tss_ptr.hpp" #endif // defined(ASIO_ENABLE_HANDLER_TRACKING) #include "asio/detail/push_options.hpp" namespace asio { namespace detail { #if defined(ASIO_CUSTOM_HANDLER_TRACKING) // The user-specified header must define the following macros: // - ASIO_INHERIT_TRACKED_HANDLER // - ASIO_ALSO_INHERIT_TRACKED_HANDLER // - ASIO_HANDLER_TRACKING_INIT // - ASIO_HANDLER_CREATION(args) // - ASIO_HANDLER_COMPLETION(args) // - ASIO_HANDLER_INVOCATION_BEGIN(args) // - ASIO_HANDLER_INVOCATION_END // - ASIO_HANDLER_OPERATION(args) // - ASIO_HANDLER_REACTOR_REGISTRATION(args) // - ASIO_HANDLER_REACTOR_DEREGISTRATION(args) // - ASIO_HANDLER_REACTOR_READ_EVENT // - ASIO_HANDLER_REACTOR_WRITE_EVENT // - ASIO_HANDLER_REACTOR_ERROR_EVENT // - ASIO_HANDLER_REACTOR_EVENTS(args) // - ASIO_HANDLER_REACTOR_OPERATION(args) # if !defined(ASIO_ENABLE_HANDLER_TRACKING) # define ASIO_ENABLE_HANDLER_TRACKING 1 # endif /// !defined(ASIO_ENABLE_HANDLER_TRACKING) #elif defined(ASIO_ENABLE_HANDLER_TRACKING) class handler_tracking { public: class completion; // Base class for objects containing tracked handlers. class tracked_handler { private: // Only the handler_tracking class will have access to the id. friend class handler_tracking; friend class completion; uint64_t id_; protected: // Constructor initialises with no id. tracked_handler() : id_(0) {} // Prevent deletion through this type. ~tracked_handler() {} }; // Initialise the tracking system. ASIO_DECL static void init(); class location { public: // Constructor adds a location to the stack. ASIO_DECL explicit location(const char* file, int line, const char* func); // Destructor removes a location from the stack. ASIO_DECL ~location(); private: // Disallow copying and assignment. location(const location&) = delete; location& operator=(const location&) = delete; friend class handler_tracking; const char* file_; int line_; const char* func_; location* next_; }; // Record the creation of a tracked handler. ASIO_DECL static void creation( execution_context& context, tracked_handler& h, const char* object_type, void* object, uintmax_t native_handle, const char* op_name); class completion { public: // Constructor records that handler is to be invoked with no arguments. ASIO_DECL explicit completion(const tracked_handler& h); // Destructor records only when an exception is thrown from the handler, or // if the memory is being freed without the handler having been invoked. ASIO_DECL ~completion(); // Records that handler is to be invoked with no arguments. ASIO_DECL void invocation_begin(); // Records that handler is to be invoked with one arguments. ASIO_DECL void invocation_begin(const asio::error_code& ec); // Constructor records that handler is to be invoked with two arguments. ASIO_DECL void invocation_begin( const asio::error_code& ec, std::size_t bytes_transferred); // Constructor records that handler is to be invoked with two arguments. ASIO_DECL void invocation_begin( const asio::error_code& ec, int signal_number); // Constructor records that handler is to be invoked with two arguments. ASIO_DECL void invocation_begin( const asio::error_code& ec, const char* arg); // Record that handler invocation has ended. ASIO_DECL void invocation_end(); private: friend class handler_tracking; uint64_t id_; bool invoked_; completion* next_; }; // Record an operation that is not directly associated with a handler. ASIO_DECL static void operation(execution_context& context, const char* object_type, void* object, uintmax_t native_handle, const char* op_name); // Record that a descriptor has been registered with the reactor. ASIO_DECL static void reactor_registration(execution_context& context, uintmax_t native_handle, uintmax_t registration); // Record that a descriptor has been deregistered from the reactor. ASIO_DECL static void reactor_deregistration(execution_context& context, uintmax_t native_handle, uintmax_t registration); // Record a reactor-based operation that is associated with a handler. ASIO_DECL static void reactor_events(execution_context& context, uintmax_t registration, unsigned events); // Record a reactor-based operation that is associated with a handler. ASIO_DECL static void reactor_operation( const tracked_handler& h, const char* op_name, const asio::error_code& ec); // Record a reactor-based operation that is associated with a handler. ASIO_DECL static void reactor_operation( const tracked_handler& h, const char* op_name, const asio::error_code& ec, std::size_t bytes_transferred); // Write a line of output. ASIO_DECL static void write_line(const char* format, ...); private: struct tracking_state; ASIO_DECL static tracking_state* get_state(); }; # define ASIO_INHERIT_TRACKED_HANDLER \ : public asio::detail::handler_tracking::tracked_handler # define ASIO_ALSO_INHERIT_TRACKED_HANDLER \ , public asio::detail::handler_tracking::tracked_handler # define ASIO_HANDLER_TRACKING_INIT \ asio::detail::handler_tracking::init() # define ASIO_HANDLER_LOCATION(args) \ asio::detail::handler_tracking::location tracked_location args # define ASIO_HANDLER_CREATION(args) \ asio::detail::handler_tracking::creation args # define ASIO_HANDLER_COMPLETION(args) \ asio::detail::handler_tracking::completion tracked_completion args # define ASIO_HANDLER_INVOCATION_BEGIN(args) \ tracked_completion.invocation_begin args # define ASIO_HANDLER_INVOCATION_END \ tracked_completion.invocation_end() # define ASIO_HANDLER_OPERATION(args) \ asio::detail::handler_tracking::operation args # define ASIO_HANDLER_REACTOR_REGISTRATION(args) \ asio::detail::handler_tracking::reactor_registration args # define ASIO_HANDLER_REACTOR_DEREGISTRATION(args) \ asio::detail::handler_tracking::reactor_deregistration args # define ASIO_HANDLER_REACTOR_READ_EVENT 1 # define ASIO_HANDLER_REACTOR_WRITE_EVENT 2 # define ASIO_HANDLER_REACTOR_ERROR_EVENT 4 # define ASIO_HANDLER_REACTOR_EVENTS(args) \ asio::detail::handler_tracking::reactor_events args # define ASIO_HANDLER_REACTOR_OPERATION(args) \ asio::detail::handler_tracking::reactor_operation args #else // defined(ASIO_ENABLE_HANDLER_TRACKING) # define ASIO_INHERIT_TRACKED_HANDLER # define ASIO_ALSO_INHERIT_TRACKED_HANDLER # define ASIO_HANDLER_TRACKING_INIT (void)0 # define ASIO_HANDLER_LOCATION(loc) (void)0 # define ASIO_HANDLER_CREATION(args) (void)0 # define ASIO_HANDLER_COMPLETION(args) (void)0 # define ASIO_HANDLER_INVOCATION_BEGIN(args) (void)0 # define ASIO_HANDLER_INVOCATION_END (void)0 # define ASIO_HANDLER_OPERATION(args) (void)0 # define ASIO_HANDLER_REACTOR_REGISTRATION(args) (void)0 # define ASIO_HANDLER_REACTOR_DEREGISTRATION(args) (void)0 # define ASIO_HANDLER_REACTOR_READ_EVENT 0 # define ASIO_HANDLER_REACTOR_WRITE_EVENT 0 # define ASIO_HANDLER_REACTOR_ERROR_EVENT 0 # define ASIO_HANDLER_REACTOR_EVENTS(args) (void)0 # define ASIO_HANDLER_REACTOR_OPERATION(args) (void)0 #endif // defined(ASIO_ENABLE_HANDLER_TRACKING) } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/handler_tracking.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // ASIO_DETAIL_HANDLER_TRACKING_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/date_time_fwd.hpp
// // detail/date_time_fwd.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_DATE_TIME_FWD_HPP #define ASIO_DETAIL_DATE_TIME_FWD_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" namespace boost { namespace date_time { template<class T, class TimeSystem> class base_time; } // namespace date_time namespace posix_time { class ptime; } // namespace posix_time } // namespace boost #endif // ASIO_DETAIL_DATE_TIME_FWD_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/winrt_async_manager.hpp
// // detail/winrt_async_manager.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WINRT_ASYNC_MANAGER_HPP #define ASIO_DETAIL_WINRT_ASYNC_MANAGER_HPP #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_RUNTIME) #include <future> #include "asio/detail/atomic_count.hpp" #include "asio/detail/winrt_async_op.hpp" #include "asio/error.hpp" #include "asio/execution_context.hpp" #if defined(ASIO_HAS_IOCP) # include "asio/detail/win_iocp_io_context.hpp" #else // defined(ASIO_HAS_IOCP) # include "asio/detail/scheduler.hpp" #endif // defined(ASIO_HAS_IOCP) #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class winrt_async_manager : public execution_context_service_base<winrt_async_manager> { public: // Constructor. winrt_async_manager(execution_context& context) : execution_context_service_base<winrt_async_manager>(context), scheduler_(use_service<scheduler_impl>(context)), outstanding_ops_(1) { } // Destructor. ~winrt_async_manager() { } // Destroy all user-defined handler objects owned by the service. void shutdown() { if (--outstanding_ops_ > 0) { // Block until last operation is complete. std::future<void> f = promise_.get_future(); f.wait(); } } void sync(Windows::Foundation::IAsyncAction^ action, asio::error_code& ec) { using namespace Windows::Foundation; using Windows::Foundation::AsyncStatus; auto promise = std::make_shared<std::promise<asio::error_code>>(); auto future = promise->get_future(); action->Completed = ref new AsyncActionCompletedHandler( [promise](IAsyncAction^ action, AsyncStatus status) { switch (status) { case AsyncStatus::Canceled: promise->set_value(asio::error::operation_aborted); break; case AsyncStatus::Error: case AsyncStatus::Completed: default: asio::error_code ec( action->ErrorCode.Value, asio::system_category()); promise->set_value(ec); break; } }); ec = future.get(); } template <typename TResult> TResult sync(Windows::Foundation::IAsyncOperation<TResult>^ operation, asio::error_code& ec) { using namespace Windows::Foundation; using Windows::Foundation::AsyncStatus; auto promise = std::make_shared<std::promise<asio::error_code>>(); auto future = promise->get_future(); operation->Completed = ref new AsyncOperationCompletedHandler<TResult>( [promise](IAsyncOperation<TResult>^ operation, AsyncStatus status) { switch (status) { case AsyncStatus::Canceled: promise->set_value(asio::error::operation_aborted); break; case AsyncStatus::Error: case AsyncStatus::Completed: default: asio::error_code ec( operation->ErrorCode.Value, asio::system_category()); promise->set_value(ec); break; } }); ec = future.get(); return operation->GetResults(); } template <typename TResult, typename TProgress> TResult sync( Windows::Foundation::IAsyncOperationWithProgress< TResult, TProgress>^ operation, asio::error_code& ec) { using namespace Windows::Foundation; using Windows::Foundation::AsyncStatus; auto promise = std::make_shared<std::promise<asio::error_code>>(); auto future = promise->get_future(); operation->Completed = ref new AsyncOperationWithProgressCompletedHandler<TResult, TProgress>( [promise](IAsyncOperationWithProgress<TResult, TProgress>^ operation, AsyncStatus status) { switch (status) { case AsyncStatus::Canceled: promise->set_value(asio::error::operation_aborted); break; case AsyncStatus::Started: break; case AsyncStatus::Error: case AsyncStatus::Completed: default: asio::error_code ec( operation->ErrorCode.Value, asio::system_category()); promise->set_value(ec); break; } }); ec = future.get(); return operation->GetResults(); } void async(Windows::Foundation::IAsyncAction^ action, winrt_async_op<void>* handler) { using namespace Windows::Foundation; using Windows::Foundation::AsyncStatus; auto on_completed = ref new AsyncActionCompletedHandler( [this, handler](IAsyncAction^ action, AsyncStatus status) { switch (status) { case AsyncStatus::Canceled: handler->ec_ = asio::error::operation_aborted; break; case AsyncStatus::Started: return; case AsyncStatus::Completed: case AsyncStatus::Error: default: handler->ec_ = asio::error_code( action->ErrorCode.Value, asio::system_category()); break; } scheduler_.post_deferred_completion(handler); if (--outstanding_ops_ == 0) promise_.set_value(); }); scheduler_.work_started(); ++outstanding_ops_; action->Completed = on_completed; } template <typename TResult> void async(Windows::Foundation::IAsyncOperation<TResult>^ operation, winrt_async_op<TResult>* handler) { using namespace Windows::Foundation; using Windows::Foundation::AsyncStatus; auto on_completed = ref new AsyncOperationCompletedHandler<TResult>( [this, handler](IAsyncOperation<TResult>^ operation, AsyncStatus status) { switch (status) { case AsyncStatus::Canceled: handler->ec_ = asio::error::operation_aborted; break; case AsyncStatus::Started: return; case AsyncStatus::Completed: handler->result_ = operation->GetResults(); // Fall through. case AsyncStatus::Error: default: handler->ec_ = asio::error_code( operation->ErrorCode.Value, asio::system_category()); break; } scheduler_.post_deferred_completion(handler); if (--outstanding_ops_ == 0) promise_.set_value(); }); scheduler_.work_started(); ++outstanding_ops_; operation->Completed = on_completed; } template <typename TResult, typename TProgress> void async( Windows::Foundation::IAsyncOperationWithProgress< TResult, TProgress>^ operation, winrt_async_op<TResult>* handler) { using namespace Windows::Foundation; using Windows::Foundation::AsyncStatus; auto on_completed = ref new AsyncOperationWithProgressCompletedHandler<TResult, TProgress>( [this, handler](IAsyncOperationWithProgress< TResult, TProgress>^ operation, AsyncStatus status) { switch (status) { case AsyncStatus::Canceled: handler->ec_ = asio::error::operation_aborted; break; case AsyncStatus::Started: return; case AsyncStatus::Completed: handler->result_ = operation->GetResults(); // Fall through. case AsyncStatus::Error: default: handler->ec_ = asio::error_code( operation->ErrorCode.Value, asio::system_category()); break; } scheduler_.post_deferred_completion(handler); if (--outstanding_ops_ == 0) promise_.set_value(); }); scheduler_.work_started(); ++outstanding_ops_; operation->Completed = on_completed; } private: // The scheduler implementation used to post completed handlers. #if defined(ASIO_HAS_IOCP) typedef class win_iocp_io_context scheduler_impl; #else typedef class scheduler scheduler_impl; #endif scheduler_impl& scheduler_; // Count of outstanding operations. atomic_count outstanding_ops_; // Used to keep wait for outstanding operations to complete. std::promise<void> promise_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_WINDOWS_RUNTIME) #endif // ASIO_DETAIL_WINRT_ASYNC_MANAGER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/static_mutex.hpp
// // detail/static_mutex.hpp // ~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_STATIC_MUTEX_HPP #define ASIO_DETAIL_STATIC_MUTEX_HPP #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_THREADS) # include "asio/detail/null_static_mutex.hpp" #elif defined(ASIO_WINDOWS) # include "asio/detail/win_static_mutex.hpp" #elif defined(ASIO_HAS_PTHREADS) # include "asio/detail/posix_static_mutex.hpp" #else # include "asio/detail/std_static_mutex.hpp" #endif namespace asio { namespace detail { #if !defined(ASIO_HAS_THREADS) typedef null_static_mutex static_mutex; # define ASIO_STATIC_MUTEX_INIT ASIO_NULL_STATIC_MUTEX_INIT #elif defined(ASIO_WINDOWS) typedef win_static_mutex static_mutex; # define ASIO_STATIC_MUTEX_INIT ASIO_WIN_STATIC_MUTEX_INIT #elif defined(ASIO_HAS_PTHREADS) typedef posix_static_mutex static_mutex; # define ASIO_STATIC_MUTEX_INIT ASIO_POSIX_STATIC_MUTEX_INIT #else typedef std_static_mutex static_mutex; # define ASIO_STATIC_MUTEX_INIT ASIO_STD_STATIC_MUTEX_INIT #endif } // namespace detail } // namespace asio #endif // ASIO_DETAIL_STATIC_MUTEX_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/winrt_ssocket_service.hpp
// // detail/winrt_ssocket_service.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WINRT_SSOCKET_SERVICE_HPP #define ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP #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_RUNTIME) #include "asio/error.hpp" #include "asio/execution_context.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/winrt_socket_connect_op.hpp" #include "asio/detail/winrt_ssocket_service_base.hpp" #include "asio/detail/winrt_utils.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Protocol> class winrt_ssocket_service : public execution_context_service_base<winrt_ssocket_service<Protocol>>, public winrt_ssocket_service_base { public: // The protocol type. typedef Protocol protocol_type; // The endpoint type. typedef typename Protocol::endpoint endpoint_type; // The native type of a socket. typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type; // The implementation type of the socket. struct implementation_type : base_implementation_type { // Default constructor. implementation_type() : base_implementation_type(), protocol_(endpoint_type().protocol()) { } // The protocol associated with the socket. protocol_type protocol_; }; // Constructor. winrt_ssocket_service(execution_context& context) : execution_context_service_base<winrt_ssocket_service<Protocol>>(context), winrt_ssocket_service_base(context) { } // Destroy all user-defined handler objects owned by the service. void shutdown() { this->base_shutdown(); } // Move-construct a new socket implementation. void move_construct(implementation_type& impl, implementation_type& other_impl) noexcept { this->base_move_construct(impl, other_impl); impl.protocol_ = other_impl.protocol_; other_impl.protocol_ = endpoint_type().protocol(); } // Move-assign from another socket implementation. void move_assign(implementation_type& impl, winrt_ssocket_service& other_service, implementation_type& other_impl) { this->base_move_assign(impl, other_service, other_impl); impl.protocol_ = other_impl.protocol_; other_impl.protocol_ = endpoint_type().protocol(); } // Move-construct a new socket implementation from another protocol type. template <typename Protocol1> void converting_move_construct(implementation_type& impl, winrt_ssocket_service<Protocol1>&, typename winrt_ssocket_service< Protocol1>::implementation_type& other_impl) { this->base_move_construct(impl, other_impl); impl.protocol_ = protocol_type(other_impl.protocol_); other_impl.protocol_ = typename Protocol1::endpoint().protocol(); } // Open a new socket implementation. asio::error_code open(implementation_type& impl, const protocol_type& protocol, asio::error_code& ec) { if (is_open(impl)) { ec = asio::error::already_open; return ec; } try { impl.socket_ = ref new Windows::Networking::Sockets::StreamSocket; impl.protocol_ = protocol; ec = asio::error_code(); } catch (Platform::Exception^ e) { ec = asio::error_code(e->HResult, asio::system_category()); } return ec; } // Assign a native socket to a socket implementation. asio::error_code assign(implementation_type& impl, const protocol_type& protocol, const native_handle_type& native_socket, asio::error_code& ec) { if (is_open(impl)) { ec = asio::error::already_open; return ec; } impl.socket_ = native_socket; impl.protocol_ = protocol; ec = asio::error_code(); return ec; } // Bind the socket to the specified local endpoint. asio::error_code bind(implementation_type&, const endpoint_type&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Get the local endpoint. endpoint_type local_endpoint(const implementation_type& impl, asio::error_code& ec) const { endpoint_type endpoint; endpoint.resize(do_get_endpoint(impl, true, endpoint.data(), endpoint.size(), ec)); return endpoint; } // Get the remote endpoint. endpoint_type remote_endpoint(const implementation_type& impl, asio::error_code& ec) const { endpoint_type endpoint; endpoint.resize(do_get_endpoint(impl, false, endpoint.data(), endpoint.size(), ec)); return endpoint; } // Disable sends or receives on the socket. asio::error_code shutdown(implementation_type&, socket_base::shutdown_type, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Set a socket option. template <typename Option> asio::error_code set_option(implementation_type& impl, const Option& option, asio::error_code& ec) { return do_set_option(impl, option.level(impl.protocol_), option.name(impl.protocol_), option.data(impl.protocol_), option.size(impl.protocol_), ec); } // Get a socket option. template <typename Option> asio::error_code get_option(const implementation_type& impl, Option& option, asio::error_code& ec) const { std::size_t size = option.size(impl.protocol_); do_get_option(impl, option.level(impl.protocol_), option.name(impl.protocol_), option.data(impl.protocol_), &size, ec); if (!ec) option.resize(impl.protocol_, size); return ec; } // Connect the socket to the specified endpoint. asio::error_code connect(implementation_type& impl, const endpoint_type& peer_endpoint, asio::error_code& ec) { return do_connect(impl, peer_endpoint.data(), ec); } // Start an asynchronous connect. template <typename Handler, typename IoExecutor> void async_connect(implementation_type& impl, const endpoint_type& peer_endpoint, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); // Allocate and construct an operation to wrap the handler. typedef winrt_socket_connect_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(handler, io_ex); ASIO_HANDLER_CREATION((scheduler_.context(), *p.p, "socket", &impl, 0, "async_connect")); start_connect_op(impl, peer_endpoint.data(), p.p, is_continuation); p.v = p.p = 0; } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_WINDOWS_RUNTIME) #endif // ASIO_DETAIL_WINRT_SSOCKET_SERVICE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/winrt_async_op.hpp
// // detail/winrt_async_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WINRT_ASYNC_OP_HPP #define ASIO_DETAIL_WINRT_ASYNC_OP_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/operation.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename TResult> class winrt_async_op : public operation { public: // The error code to be passed to the completion handler. asio::error_code ec_; // The result of the operation, to be passed to the completion handler. TResult result_; protected: winrt_async_op(func_type complete_func) : operation(complete_func), result_() { } }; template <> class winrt_async_op<void> : public operation { public: // The error code to be passed to the completion handler. asio::error_code ec_; protected: winrt_async_op(func_type complete_func) : operation(complete_func) { } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_WINRT_ASYNC_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/strand_service.hpp
// // detail/strand_service.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_STRAND_SERVICE_HPP #define ASIO_DETAIL_STRAND_SERVICE_HPP #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/mutex.hpp" #include "asio/detail/op_queue.hpp" #include "asio/detail/operation.hpp" #include "asio/detail/scoped_ptr.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { // Default service implementation for a strand. class strand_service : public asio::detail::service_base<strand_service> { private: // Helper class to re-post the strand on exit. struct on_do_complete_exit; // Helper class to re-post the strand on exit. struct on_dispatch_exit; public: // The underlying implementation of a strand. class strand_impl : public operation { public: strand_impl(); private: // Only this service will have access to the internal values. friend class strand_service; friend struct on_do_complete_exit; friend struct on_dispatch_exit; // Mutex to protect access to internal data. asio::detail::mutex mutex_; // Indicates whether the strand is currently "locked" by a handler. This // means that there is a handler upcall in progress, or that the strand // itself has been scheduled in order to invoke some pending handlers. bool locked_; // The handlers that are waiting on the strand but should not be run until // after the next time the strand is scheduled. This queue must only be // modified while the mutex is locked. op_queue<operation> waiting_queue_; // The handlers that are ready to be run. Logically speaking, these are the // handlers that hold the strand's lock. The ready queue is only modified // from within the strand and so may be accessed without locking the mutex. op_queue<operation> ready_queue_; }; typedef strand_impl* implementation_type; // Construct a new strand service for the specified io_context. ASIO_DECL explicit strand_service(asio::io_context& io_context); // Destroy all user-defined handler objects owned by the service. ASIO_DECL void shutdown(); // Construct a new strand implementation. ASIO_DECL void construct(implementation_type& impl); // Request the io_context to invoke the given handler. template <typename Handler> void dispatch(implementation_type& impl, Handler& handler); // Request the io_context to invoke the given handler and return immediately. template <typename Handler> void post(implementation_type& impl, Handler& handler); // Determine whether the strand is running in the current thread. ASIO_DECL bool running_in_this_thread( const implementation_type& impl) const; private: // Helper function to dispatch a handler. ASIO_DECL void do_dispatch(implementation_type& impl, operation* op); // Helper function to post a handler. ASIO_DECL void do_post(implementation_type& impl, operation* op, bool is_continuation); ASIO_DECL static void do_complete(void* owner, operation* base, const asio::error_code& ec, std::size_t bytes_transferred); // The io_context used to obtain an I/O executor. io_context& io_context_; // The io_context implementation used to post completions. io_context_impl& io_context_impl_; // Mutex to protect access to the array of implementations. asio::detail::mutex mutex_; // Number of implementations shared between all strand objects. #if defined(ASIO_STRAND_IMPLEMENTATIONS) enum { num_implementations = ASIO_STRAND_IMPLEMENTATIONS }; #else // defined(ASIO_STRAND_IMPLEMENTATIONS) enum { num_implementations = 193 }; #endif // defined(ASIO_STRAND_IMPLEMENTATIONS) // Pool of implementations. scoped_ptr<strand_impl> implementations_[num_implementations]; // Extra value used when hashing to prevent recycled memory locations from // getting the same strand implementation. std::size_t salt_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #include "asio/detail/impl/strand_service.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/strand_service.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // ASIO_DETAIL_STRAND_SERVICE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/base_from_cancellation_state.hpp
// // detail/base_from_cancellation_state.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_BASE_FROM_CANCELLATION_STATE_HPP #define ASIO_DETAIL_BASE_FROM_CANCELLATION_STATE_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_cancellation_slot.hpp" #include "asio/cancellation_state.hpp" #include "asio/detail/type_traits.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Handler, typename = void> class base_from_cancellation_state { public: typedef cancellation_slot cancellation_slot_type; cancellation_slot_type get_cancellation_slot() const noexcept { return cancellation_state_.slot(); } cancellation_state get_cancellation_state() const noexcept { return cancellation_state_; } protected: explicit base_from_cancellation_state(const Handler& handler) : cancellation_state_( asio::get_associated_cancellation_slot(handler)) { } template <typename Filter> base_from_cancellation_state(const Handler& handler, Filter filter) : cancellation_state_( asio::get_associated_cancellation_slot(handler), filter, filter) { } template <typename InFilter, typename OutFilter> base_from_cancellation_state(const Handler& handler, InFilter&& in_filter, OutFilter&& out_filter) : cancellation_state_( asio::get_associated_cancellation_slot(handler), static_cast<InFilter&&>(in_filter), static_cast<OutFilter&&>(out_filter)) { } void reset_cancellation_state(const Handler& handler) { cancellation_state_ = cancellation_state( asio::get_associated_cancellation_slot(handler)); } template <typename Filter> void reset_cancellation_state(const Handler& handler, Filter filter) { cancellation_state_ = cancellation_state( asio::get_associated_cancellation_slot(handler), filter, filter); } template <typename InFilter, typename OutFilter> void reset_cancellation_state(const Handler& handler, InFilter&& in_filter, OutFilter&& out_filter) { cancellation_state_ = cancellation_state( asio::get_associated_cancellation_slot(handler), static_cast<InFilter&&>(in_filter), static_cast<OutFilter&&>(out_filter)); } cancellation_type_t cancelled() const noexcept { return cancellation_state_.cancelled(); } private: cancellation_state cancellation_state_; }; template <typename Handler> class base_from_cancellation_state<Handler, enable_if_t< is_same< typename associated_cancellation_slot< Handler, cancellation_slot >::asio_associated_cancellation_slot_is_unspecialised, void >::value > > { public: cancellation_state get_cancellation_state() const noexcept { return cancellation_state(); } protected: explicit base_from_cancellation_state(const Handler&) { } template <typename Filter> base_from_cancellation_state(const Handler&, Filter) { } template <typename InFilter, typename OutFilter> base_from_cancellation_state(const Handler&, InFilter&&, OutFilter&&) { } void reset_cancellation_state(const Handler&) { } template <typename Filter> void reset_cancellation_state(const Handler&, Filter) { } template <typename InFilter, typename OutFilter> void reset_cancellation_state(const Handler&, InFilter&&, OutFilter&&) { } constexpr cancellation_type_t cancelled() const noexcept { return cancellation_type::none; } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_BASE_FROM_CANCELLATION_STATE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/socket_holder.hpp
// // detail/socket_holder.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_SOCKET_HOLDER_HPP #define ASIO_DETAIL_SOCKET_HOLDER_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/noncopyable.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { // Implement the resource acquisition is initialisation idiom for sockets. class socket_holder : private noncopyable { public: // Construct as an uninitialised socket. socket_holder() : socket_(invalid_socket) { } // Construct to take ownership of the specified socket. explicit socket_holder(socket_type s) : socket_(s) { } // Destructor. ~socket_holder() { if (socket_ != invalid_socket) { asio::error_code ec; socket_ops::state_type state = 0; socket_ops::close(socket_, state, true, ec); } } // Get the underlying socket. socket_type get() const { return socket_; } // Reset to an uninitialised socket. void reset() { if (socket_ != invalid_socket) { asio::error_code ec; socket_ops::state_type state = 0; socket_ops::close(socket_, state, true, ec); socket_ = invalid_socket; } } // Reset to take ownership of the specified socket. void reset(socket_type s) { reset(); socket_ = s; } // Release ownership of the socket. socket_type release() { socket_type tmp = socket_; socket_ = invalid_socket; return tmp; } private: // The underlying socket. socket_type socket_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_SOCKET_HOLDER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/winrt_resolve_op.hpp
// // detail/winrt_resolve_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WINRT_RESOLVE_OP_HPP #define ASIO_DETAIL_WINRT_RESOLVE_OP_HPP #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_RUNTIME) #include "asio/detail/bind_handler.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/winrt_async_op.hpp" #include "asio/ip/basic_resolver_results.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Protocol, typename Handler, typename IoExecutor> class winrt_resolve_op : public winrt_async_op< Windows::Foundation::Collections::IVectorView< Windows::Networking::EndpointPair^>^> { public: ASIO_DEFINE_HANDLER_PTR(winrt_resolve_op); typedef typename Protocol::endpoint endpoint_type; typedef asio::ip::basic_resolver_query<Protocol> query_type; typedef asio::ip::basic_resolver_results<Protocol> results_type; winrt_resolve_op(const query_type& query, Handler& handler, const IoExecutor& io_ex) : winrt_async_op< Windows::Foundation::Collections::IVectorView< Windows::Networking::EndpointPair^>^>( &winrt_resolve_op::do_complete), query_(query), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code&, std::size_t) { // Take ownership of the operation object. ASIO_ASSUME(base != 0); winrt_resolve_op* o(static_cast<winrt_resolve_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); results_type results = results_type(); if (!o->ec_) { try { results = results_type::create(o->result_, o->query_.hints(), o->query_.host_name(), o->query_.service_name()); } catch (Platform::Exception^ e) { o->ec_ = asio::error_code(e->HResult, asio::system_category()); } } // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, results_type> handler(o->handler_, o->ec_, results); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "...")); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: query_type query_; Handler handler_; handler_work<Handler, IoExecutor> executor_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_WINDOWS_RUNTIME) #endif // ASIO_DETAIL_WINRT_RESOLVE_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/null_reactor.hpp
// // detail/null_reactor.hpp // ~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_NULL_REACTOR_HPP #define ASIO_DETAIL_NULL_REACTOR_HPP #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_IOCP) \ || defined(ASIO_WINDOWS_RUNTIME) \ || defined(ASIO_HAS_IO_URING_AS_DEFAULT) #include "asio/detail/scheduler_operation.hpp" #include "asio/detail/scheduler_task.hpp" #include "asio/execution_context.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class null_reactor : public execution_context_service_base<null_reactor>, public scheduler_task { public: struct per_descriptor_data { }; // Constructor. null_reactor(asio::execution_context& ctx) : execution_context_service_base<null_reactor>(ctx) { } // Destructor. ~null_reactor() { } // Initialise the task. void init_task() { } // Destroy all user-defined handler objects owned by the service. void shutdown() { } // No-op because should never be called. void run(long /*usec*/, op_queue<scheduler_operation>& /*ops*/) { } // No-op. void interrupt() { } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IOCP) // || defined(ASIO_WINDOWS_RUNTIME) // || defined(ASIO_HAS_IO_URING_AS_DEFAULT) #endif // ASIO_DETAIL_NULL_REACTOR_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_thread.hpp
// // detail/win_thread.hpp // ~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WIN_THREAD_HPP #define ASIO_DETAIL_WIN_THREAD_HPP #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(ASIO_WINDOWS_APP) \ && !defined(UNDER_CE) #include <cstddef> #include "asio/detail/noncopyable.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { ASIO_DECL unsigned int __stdcall win_thread_function(void* arg); #if defined(WINVER) && (WINVER < 0x0500) ASIO_DECL void __stdcall apc_function(ULONG data); #else ASIO_DECL void __stdcall apc_function(ULONG_PTR data); #endif template <typename T> class win_thread_base { public: static bool terminate_threads() { return ::InterlockedExchangeAdd(&terminate_threads_, 0) != 0; } static void set_terminate_threads(bool b) { ::InterlockedExchange(&terminate_threads_, b ? 1 : 0); } private: static long terminate_threads_; }; template <typename T> long win_thread_base<T>::terminate_threads_ = 0; class win_thread : private noncopyable, public win_thread_base<win_thread> { public: // Constructor. template <typename Function> win_thread(Function f, unsigned int stack_size = 0) : thread_(0), exit_event_(0) { start_thread(new func<Function>(f), stack_size); } // Destructor. ASIO_DECL ~win_thread(); // Wait for the thread to exit. ASIO_DECL void join(); // Get number of CPUs. ASIO_DECL static std::size_t hardware_concurrency(); private: friend ASIO_DECL unsigned int __stdcall win_thread_function(void* arg); #if defined(WINVER) && (WINVER < 0x0500) friend ASIO_DECL void __stdcall apc_function(ULONG); #else friend ASIO_DECL void __stdcall apc_function(ULONG_PTR); #endif class func_base { public: virtual ~func_base() {} virtual void run() = 0; ::HANDLE entry_event_; ::HANDLE exit_event_; }; struct auto_func_base_ptr { func_base* ptr; ~auto_func_base_ptr() { delete ptr; } }; template <typename Function> class func : public func_base { public: func(Function f) : f_(f) { } virtual void run() { f_(); } private: Function f_; }; ASIO_DECL void start_thread(func_base* arg, unsigned int stack_size); ::HANDLE thread_; ::HANDLE exit_event_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/win_thread.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // defined(ASIO_WINDOWS) // && !defined(ASIO_WINDOWS_APP) // && !defined(UNDER_CE) #endif // ASIO_DETAIL_WIN_THREAD_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/reactive_socket_recv_op.hpp
// // detail/reactive_socket_recv_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REACTIVE_SOCKET_RECV_OP_HPP #define ASIO_DETAIL_REACTIVE_SOCKET_RECV_OP_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/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename MutableBufferSequence> class reactive_socket_recv_op_base : public reactor_op { public: reactive_socket_recv_op_base(const asio::error_code& success_ec, socket_type socket, socket_ops::state_type state, const MutableBufferSequence& buffers, socket_base::message_flags flags, func_type complete_func) : reactor_op(success_ec, &reactive_socket_recv_op_base::do_perform, complete_func), socket_(socket), state_(state), buffers_(buffers), flags_(flags) { } static status do_perform(reactor_op* base) { ASIO_ASSUME(base != 0); reactive_socket_recv_op_base* o( static_cast<reactive_socket_recv_op_base*>(base)); typedef buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence> bufs_type; status result; if (bufs_type::is_single_buffer) { result = socket_ops::non_blocking_recv1(o->socket_, bufs_type::first(o->buffers_).data(), bufs_type::first(o->buffers_).size(), o->flags_, (o->state_ & socket_ops::stream_oriented) != 0, o->ec_, o->bytes_transferred_) ? done : not_done; } else { bufs_type bufs(o->buffers_); result = socket_ops::non_blocking_recv(o->socket_, bufs.buffers(), bufs.count(), o->flags_, (o->state_ & socket_ops::stream_oriented) != 0, o->ec_, o->bytes_transferred_) ? done : not_done; } if (result == done) if ((o->state_ & socket_ops::stream_oriented) != 0) if (o->bytes_transferred_ == 0) result = done_and_exhausted; ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recv", o->ec_, o->bytes_transferred_)); return result; } private: socket_type socket_; socket_ops::state_type state_; MutableBufferSequence buffers_; socket_base::message_flags flags_; }; template <typename MutableBufferSequence, typename Handler, typename IoExecutor> class reactive_socket_recv_op : public reactive_socket_recv_op_base<MutableBufferSequence> { public: typedef Handler handler_type; typedef IoExecutor io_executor_type; ASIO_DEFINE_HANDLER_PTR(reactive_socket_recv_op); reactive_socket_recv_op(const asio::error_code& success_ec, socket_type socket, socket_ops::state_type state, const MutableBufferSequence& buffers, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) : reactive_socket_recv_op_base<MutableBufferSequence>(success_ec, socket, state, buffers, flags, &reactive_socket_recv_op::do_complete), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); reactive_socket_recv_op* o(static_cast<reactive_socket_recv_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->bytes_transferred_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } static void do_immediate(operation* base, bool, const void* io_ex) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); reactive_socket_recv_op* o(static_cast<reactive_socket_recv_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. immediate_handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->bytes_transferred_); p.h = asio::detail::addressof(handler.handler_); p.reset(); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_, io_ex); ASIO_HANDLER_INVOCATION_END; } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_REACTIVE_SOCKET_RECV_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/winrt_ssocket_service_base.hpp
// // detail/winrt_ssocket_service_base.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP #define ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP #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_RUNTIME) #include "asio/buffer.hpp" #include "asio/error.hpp" #include "asio/execution_context.hpp" #include "asio/socket_base.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/winrt_async_manager.hpp" #include "asio/detail/winrt_socket_recv_op.hpp" #include "asio/detail/winrt_socket_send_op.hpp" #if defined(ASIO_HAS_IOCP) # include "asio/detail/win_iocp_io_context.hpp" #else // defined(ASIO_HAS_IOCP) # include "asio/detail/scheduler.hpp" #endif // defined(ASIO_HAS_IOCP) #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class winrt_ssocket_service_base { public: // The native type of a socket. typedef Windows::Networking::Sockets::StreamSocket^ native_handle_type; // The implementation type of the socket. struct base_implementation_type { // Default constructor. base_implementation_type() : socket_(nullptr), next_(0), prev_(0) { } // The underlying native socket. native_handle_type socket_; // Pointers to adjacent socket implementations in linked list. base_implementation_type* next_; base_implementation_type* prev_; }; // Constructor. ASIO_DECL winrt_ssocket_service_base(execution_context& context); // Destroy all user-defined handler objects owned by the service. ASIO_DECL void base_shutdown(); // Construct a new socket implementation. ASIO_DECL void construct(base_implementation_type&); // Move-construct a new socket implementation. ASIO_DECL void base_move_construct(base_implementation_type& impl, base_implementation_type& other_impl) noexcept; // Move-assign from another socket implementation. ASIO_DECL void base_move_assign(base_implementation_type& impl, winrt_ssocket_service_base& other_service, base_implementation_type& other_impl); // Destroy a socket implementation. ASIO_DECL void destroy(base_implementation_type& impl); // Determine whether the socket is open. bool is_open(const base_implementation_type& impl) const { return impl.socket_ != nullptr; } // Destroy a socket implementation. ASIO_DECL asio::error_code close( base_implementation_type& impl, asio::error_code& ec); // Release ownership of the socket. ASIO_DECL native_handle_type release( base_implementation_type& impl, asio::error_code& ec); // Get the native socket representation. native_handle_type native_handle(base_implementation_type& impl) { return impl.socket_; } // Cancel all operations associated with the socket. asio::error_code cancel(base_implementation_type&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Determine whether the socket is at the out-of-band data mark. bool at_mark(const base_implementation_type&, asio::error_code& ec) const { ec = asio::error::operation_not_supported; return false; } // Determine the number of bytes available for reading. std::size_t available(const base_implementation_type&, asio::error_code& ec) const { ec = asio::error::operation_not_supported; return 0; } // Perform an IO control command on the socket. template <typename IO_Control_Command> asio::error_code io_control(base_implementation_type&, IO_Control_Command&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Gets the non-blocking mode of the socket. bool non_blocking(const base_implementation_type&) const { return false; } // Sets the non-blocking mode of the socket. asio::error_code non_blocking(base_implementation_type&, bool, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Gets the non-blocking mode of the native socket implementation. bool native_non_blocking(const base_implementation_type&) const { return false; } // Sets the non-blocking mode of the native socket implementation. asio::error_code native_non_blocking(base_implementation_type&, bool, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Send the given data to the peer. template <typename ConstBufferSequence> std::size_t send(base_implementation_type& impl, const ConstBufferSequence& buffers, socket_base::message_flags flags, asio::error_code& ec) { return do_send(impl, buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence>::first(buffers), flags, ec); } // Wait until data can be sent without blocking. std::size_t send(base_implementation_type&, const null_buffers&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Start an asynchronous send. The data being sent must be valid for the // lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler, typename IoExecutor> void async_send(base_implementation_type& impl, const ConstBufferSequence& buffers, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); // Allocate and construct an operation to wrap the handler. typedef winrt_socket_send_op<ConstBufferSequence, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(buffers, handler, io_ex); ASIO_HANDLER_CREATION((scheduler_.context(), *p.p, "socket", &impl, 0, "async_send")); start_send_op(impl, buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence>::first(buffers), flags, p.p, is_continuation); p.v = p.p = 0; } // Start an asynchronous wait until data can be sent without blocking. template <typename Handler, typename IoExecutor> void async_send(base_implementation_type&, const null_buffers&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler(handler, ec, bytes_transferred)); } // Receive some data from the peer. Returns the number of bytes received. template <typename MutableBufferSequence> std::size_t receive(base_implementation_type& impl, const MutableBufferSequence& buffers, socket_base::message_flags flags, asio::error_code& ec) { return do_receive(impl, buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence>::first(buffers), flags, ec); } // Wait until data can be received without blocking. std::size_t receive(base_implementation_type&, const null_buffers&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Start an asynchronous receive. The buffer for the data being received // must be valid for the lifetime of the asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_receive(base_implementation_type& impl, const MutableBufferSequence& buffers, socket_base::message_flags flags, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); // Allocate and construct an operation to wrap the handler. typedef winrt_socket_recv_op<MutableBufferSequence, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(buffers, handler, io_ex); ASIO_HANDLER_CREATION((scheduler_.context(), *p.p, "socket", &impl, 0, "async_receive")); start_receive_op(impl, buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence>::first(buffers), flags, p.p, is_continuation); p.v = p.p = 0; } // Wait until data can be received without blocking. template <typename Handler, typename IoExecutor> void async_receive(base_implementation_type&, const null_buffers&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler(handler, ec, bytes_transferred)); } protected: // Helper function to obtain endpoints associated with the connection. ASIO_DECL std::size_t do_get_endpoint( const base_implementation_type& impl, bool local, void* addr, std::size_t addr_len, asio::error_code& ec) const; // Helper function to set a socket option. ASIO_DECL asio::error_code do_set_option( base_implementation_type& impl, int level, int optname, const void* optval, std::size_t optlen, asio::error_code& ec); // Helper function to get a socket option. ASIO_DECL void do_get_option( const base_implementation_type& impl, int level, int optname, void* optval, std::size_t* optlen, asio::error_code& ec) const; // Helper function to perform a synchronous connect. ASIO_DECL asio::error_code do_connect( base_implementation_type& impl, const void* addr, asio::error_code& ec); // Helper function to start an asynchronous connect. ASIO_DECL void start_connect_op( base_implementation_type& impl, const void* addr, winrt_async_op<void>* op, bool is_continuation); // Helper function to perform a synchronous send. ASIO_DECL std::size_t do_send( base_implementation_type& impl, const asio::const_buffer& data, socket_base::message_flags flags, asio::error_code& ec); // Helper function to start an asynchronous send. ASIO_DECL void start_send_op(base_implementation_type& impl, const asio::const_buffer& data, socket_base::message_flags flags, winrt_async_op<unsigned int>* op, bool is_continuation); // Helper function to perform a synchronous receive. ASIO_DECL std::size_t do_receive( base_implementation_type& impl, const asio::mutable_buffer& data, socket_base::message_flags flags, asio::error_code& ec); // Helper function to start an asynchronous receive. ASIO_DECL void start_receive_op(base_implementation_type& impl, const asio::mutable_buffer& data, socket_base::message_flags flags, winrt_async_op<Windows::Storage::Streams::IBuffer^>* op, bool is_continuation); // The scheduler implementation used for delivering completions. #if defined(ASIO_HAS_IOCP) typedef class win_iocp_io_context scheduler_impl; #else typedef class scheduler scheduler_impl; #endif scheduler_impl& scheduler_; // The manager that keeps track of outstanding operations. winrt_async_manager& async_manager_; // Mutex to protect access to the linked list of implementations. asio::detail::mutex mutex_; // The head of a linked list of all implementations. base_implementation_type* impl_list_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/winrt_ssocket_service_base.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // defined(ASIO_WINDOWS_RUNTIME) #endif // ASIO_DETAIL_WINRT_SSOCKET_SERVICE_BASE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/chrono.hpp
// // detail/chrono.hpp // ~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_CHRONO_HPP #define ASIO_DETAIL_CHRONO_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <chrono> namespace asio { namespace chrono { using std::chrono::duration; using std::chrono::time_point; using std::chrono::duration_cast; using std::chrono::nanoseconds; using std::chrono::microseconds; using std::chrono::milliseconds; using std::chrono::seconds; using std::chrono::minutes; using std::chrono::hours; using std::chrono::time_point_cast; #if defined(ASIO_HAS_STD_CHRONO_MONOTONIC_CLOCK) typedef std::chrono::monotonic_clock steady_clock; #else // defined(ASIO_HAS_STD_CHRONO_MONOTONIC_CLOCK) using std::chrono::steady_clock; #endif // defined(ASIO_HAS_STD_CHRONO_MONOTONIC_CLOCK) using std::chrono::system_clock; using std::chrono::high_resolution_clock; } // namespace chrono } // namespace asio #endif // ASIO_DETAIL_CHRONO_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/null_signal_blocker.hpp
// // detail/null_signal_blocker.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_NULL_SIGNAL_BLOCKER_HPP #define ASIO_DETAIL_NULL_SIGNAL_BLOCKER_HPP #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_THREADS) \ || defined(ASIO_WINDOWS) \ || defined(ASIO_WINDOWS_RUNTIME) \ || defined(__CYGWIN__) \ || defined(__SYMBIAN32__) #include "asio/detail/noncopyable.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class null_signal_blocker : private noncopyable { public: // Constructor blocks all signals for the calling thread. null_signal_blocker() { } // Destructor restores the previous signal mask. ~null_signal_blocker() { } // Block all signals for the calling thread. void block() { } // Restore the previous signal mask. void unblock() { } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // !defined(ASIO_HAS_THREADS) // || defined(ASIO_WINDOWS) // || defined(ASIO_WINDOWS_RUNTIME) // || defined(__CYGWIN__) // || defined(__SYMBIAN32__) #endif // ASIO_DETAIL_NULL_SIGNAL_BLOCKER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/assert.hpp
// // detail/assert.hpp // ~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_ASSERT_HPP #define ASIO_DETAIL_ASSERT_HPP #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_BOOST_ASSERT) # include <boost/assert.hpp> #else // defined(ASIO_HAS_BOOST_ASSERT) # include <cassert> #endif // defined(ASIO_HAS_BOOST_ASSERT) #if defined(ASIO_HAS_BOOST_ASSERT) # define ASIO_ASSERT(expr) BOOST_ASSERT(expr) #else // defined(ASIO_HAS_BOOST_ASSERT) # define ASIO_ASSERT(expr) assert(expr) #endif // defined(ASIO_HAS_BOOST_ASSERT) #endif // ASIO_DETAIL_ASSERT_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/null_socket_service.hpp
// // detail/null_socket_service.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_NULL_SOCKET_SERVICE_HPP #define ASIO_DETAIL_NULL_SOCKET_SERVICE_HPP #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_RUNTIME) #include "asio/buffer.hpp" #include "asio/error.hpp" #include "asio/execution_context.hpp" #include "asio/post.hpp" #include "asio/socket_base.hpp" #include "asio/detail/bind_handler.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Protocol> class null_socket_service : public execution_context_service_base<null_socket_service<Protocol>> { public: // The protocol type. typedef Protocol protocol_type; // The endpoint type. typedef typename Protocol::endpoint endpoint_type; // The native type of a socket. typedef int native_handle_type; // The implementation type of the socket. struct implementation_type { }; // Constructor. null_socket_service(execution_context& context) : execution_context_service_base<null_socket_service<Protocol>>(context) { } // Destroy all user-defined handler objects owned by the service. void shutdown() { } // Construct a new socket implementation. void construct(implementation_type&) { } // Move-construct a new socket implementation. void move_construct(implementation_type&, implementation_type&) { } // Move-assign from another socket implementation. void move_assign(implementation_type&, null_socket_service&, implementation_type&) { } // Move-construct a new socket implementation from another protocol type. template <typename Protocol1> void converting_move_construct(implementation_type&, null_socket_service<Protocol1>&, typename null_socket_service<Protocol1>::implementation_type&) { } // Destroy a socket implementation. void destroy(implementation_type&) { } // Open a new socket implementation. asio::error_code open(implementation_type&, const protocol_type&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Assign a native socket to a socket implementation. asio::error_code assign(implementation_type&, const protocol_type&, const native_handle_type&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Determine whether the socket is open. bool is_open(const implementation_type&) const { return false; } // Destroy a socket implementation. asio::error_code close(implementation_type&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Release ownership of the socket. native_handle_type release(implementation_type&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Get the native socket representation. native_handle_type native_handle(implementation_type&) { return 0; } // Cancel all operations associated with the socket. asio::error_code cancel(implementation_type&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Determine whether the socket is at the out-of-band data mark. bool at_mark(const implementation_type&, asio::error_code& ec) const { ec = asio::error::operation_not_supported; return false; } // Determine the number of bytes available for reading. std::size_t available(const implementation_type&, asio::error_code& ec) const { ec = asio::error::operation_not_supported; return 0; } // Place the socket into the state where it will listen for new connections. asio::error_code listen(implementation_type&, int, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Perform an IO control command on the socket. template <typename IO_Control_Command> asio::error_code io_control(implementation_type&, IO_Control_Command&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Gets the non-blocking mode of the socket. bool non_blocking(const implementation_type&) const { return false; } // Sets the non-blocking mode of the socket. asio::error_code non_blocking(implementation_type&, bool, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Gets the non-blocking mode of the native socket implementation. bool native_non_blocking(const implementation_type&) const { return false; } // Sets the non-blocking mode of the native socket implementation. asio::error_code native_non_blocking(implementation_type&, bool, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Disable sends or receives on the socket. asio::error_code shutdown(implementation_type&, socket_base::shutdown_type, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Bind the socket to the specified local endpoint. asio::error_code bind(implementation_type&, const endpoint_type&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Set a socket option. template <typename Option> asio::error_code set_option(implementation_type&, const Option&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Set a socket option. template <typename Option> asio::error_code get_option(const implementation_type&, Option&, asio::error_code& ec) const { ec = asio::error::operation_not_supported; return ec; } // Get the local endpoint. endpoint_type local_endpoint(const implementation_type&, asio::error_code& ec) const { ec = asio::error::operation_not_supported; return endpoint_type(); } // Get the remote endpoint. endpoint_type remote_endpoint(const implementation_type&, asio::error_code& ec) const { ec = asio::error::operation_not_supported; return endpoint_type(); } // Send the given data to the peer. template <typename ConstBufferSequence> std::size_t send(implementation_type&, const ConstBufferSequence&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Wait until data can be sent without blocking. std::size_t send(implementation_type&, const null_buffers&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Start an asynchronous send. The data being sent must be valid for the // lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler, typename IoExecutor> void async_send(implementation_type&, const ConstBufferSequence&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Start an asynchronous wait until data can be sent without blocking. template <typename Handler, typename IoExecutor> void async_send(implementation_type&, const null_buffers&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Receive some data from the peer. Returns the number of bytes received. template <typename MutableBufferSequence> std::size_t receive(implementation_type&, const MutableBufferSequence&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Wait until data can be received without blocking. std::size_t receive(implementation_type&, const null_buffers&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Start an asynchronous receive. The buffer for the data being received // must be valid for the lifetime of the asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_receive(implementation_type&, const MutableBufferSequence&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Wait until data can be received without blocking. template <typename Handler, typename IoExecutor> void async_receive(implementation_type&, const null_buffers&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Receive some data with associated flags. Returns the number of bytes // received. template <typename MutableBufferSequence> std::size_t receive_with_flags(implementation_type&, const MutableBufferSequence&, socket_base::message_flags, socket_base::message_flags&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Wait until data can be received without blocking. std::size_t receive_with_flags(implementation_type&, const null_buffers&, socket_base::message_flags, socket_base::message_flags&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Start an asynchronous receive. The buffer for the data being received // must be valid for the lifetime of the asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_receive_with_flags(implementation_type&, const MutableBufferSequence&, socket_base::message_flags, socket_base::message_flags&, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Wait until data can be received without blocking. template <typename Handler, typename IoExecutor> void async_receive_with_flags(implementation_type&, const null_buffers&, socket_base::message_flags, socket_base::message_flags&, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Send a datagram to the specified endpoint. Returns the number of bytes // sent. template <typename ConstBufferSequence> std::size_t send_to(implementation_type&, const ConstBufferSequence&, const endpoint_type&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Wait until data can be sent without blocking. std::size_t send_to(implementation_type&, const null_buffers&, const endpoint_type&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Start an asynchronous send. The data being sent must be valid for the // lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler, typename IoExecutor> void async_send_to(implementation_type&, const ConstBufferSequence&, const endpoint_type&, socket_base::message_flags, Handler& handler) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Start an asynchronous wait until data can be sent without blocking. template <typename Handler, typename IoExecutor> void async_send_to(implementation_type&, const null_buffers&, const endpoint_type&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Receive a datagram with the endpoint of the sender. Returns the number of // bytes received. template <typename MutableBufferSequence> std::size_t receive_from(implementation_type&, const MutableBufferSequence&, endpoint_type&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Wait until data can be received without blocking. std::size_t receive_from(implementation_type&, const null_buffers&, endpoint_type&, socket_base::message_flags, asio::error_code& ec) { ec = asio::error::operation_not_supported; return 0; } // Start an asynchronous receive. The buffer for the data being received and // the sender_endpoint object must both be valid for the lifetime of the // asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_receive_from(implementation_type&, const MutableBufferSequence&, endpoint_type&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Wait until data can be received without blocking. template <typename Handler, typename IoExecutor> void async_receive_from(implementation_type&, const null_buffers&, endpoint_type&, socket_base::message_flags, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; const std::size_t bytes_transferred = 0; asio::post(io_ex, detail::bind_handler( handler, ec, bytes_transferred)); } // Accept a new connection. template <typename Socket> asio::error_code accept(implementation_type&, Socket&, endpoint_type*, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Start an asynchronous accept. The peer and peer_endpoint objects // must be valid until the accept's handler is invoked. template <typename Socket, typename Handler, typename IoExecutor> void async_accept(implementation_type&, Socket&, endpoint_type*, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; asio::post(io_ex, detail::bind_handler(handler, ec)); } // Connect the socket to the specified endpoint. asio::error_code connect(implementation_type&, const endpoint_type&, asio::error_code& ec) { ec = asio::error::operation_not_supported; return ec; } // Start an asynchronous connect. template <typename Handler, typename IoExecutor> void async_connect(implementation_type&, const endpoint_type&, Handler& handler, const IoExecutor& io_ex) { asio::error_code ec = asio::error::operation_not_supported; asio::post(io_ex, detail::bind_handler(handler, ec)); } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_WINDOWS_RUNTIME) #endif // ASIO_DETAIL_NULL_SOCKET_SERVICE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/completion_payload.hpp
// // detail/completion_payload.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_COMPLETION_PAYLOAD_HPP #define ASIO_DETAIL_COMPLETION_PAYLOAD_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/type_traits.hpp" #include "asio/error_code.hpp" #include "asio/detail/completion_message.hpp" #if defined(ASIO_HAS_STD_VARIANT) # include <variant> #else // defined(ASIO_HAS_STD_VARIANT) # include <new> #endif // defined(ASIO_HAS_STD_VARIANT) #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename... Signatures> class completion_payload; template <typename R> class completion_payload<R()> { public: explicit completion_payload(completion_message<R()>) { } template <typename Handler> void receive(Handler& handler) { static_cast<Handler&&>(handler)(); } }; template <typename Signature> class completion_payload<Signature> { public: completion_payload(completion_message<Signature>&& m) : message_(static_cast<completion_message<Signature>&&>(m)) { } template <typename Handler> void receive(Handler& handler) { message_.receive(handler); } private: completion_message<Signature> message_; }; #if defined(ASIO_HAS_STD_VARIANT) template <typename... Signatures> class completion_payload { public: template <typename Signature> completion_payload(completion_message<Signature>&& m) : message_(static_cast<completion_message<Signature>&&>(m)) { } template <typename Handler> void receive(Handler& handler) { std::visit( [&](auto& message) { message.receive(handler); }, message_); } private: std::variant<completion_message<Signatures>...> message_; }; #else // defined(ASIO_HAS_STD_VARIANT) template <typename R1, typename R2> class completion_payload<R1(), R2(asio::error_code)> { public: typedef completion_message<R1()> void_message_type; typedef completion_message<R2(asio::error_code)> error_message_type; completion_payload(void_message_type&&) : message_(0, asio::error_code()), empty_(true) { } completion_payload(error_message_type&& m) : message_(static_cast<error_message_type&&>(m)), empty_(false) { } template <typename Handler> void receive(Handler& handler) { if (empty_) completion_message<R1()>(0).receive(handler); else message_.receive(handler); } private: error_message_type message_; bool empty_; }; template <typename Sig1, typename Sig2> class completion_payload<Sig1, Sig2> { public: typedef completion_message<Sig1> message_1_type; typedef completion_message<Sig2> message_2_type; completion_payload(message_1_type&& m) : index_(1) { new (&storage_.message_1_) message_1_type(static_cast<message_1_type&&>(m)); } completion_payload(message_2_type&& m) : index_(2) { new (&storage_.message_2_) message_2_type(static_cast<message_2_type&&>(m)); } completion_payload(completion_payload&& other) : index_(other.index_) { switch (index_) { case 1: new (&storage_.message_1_) message_1_type( static_cast<message_1_type&&>(other.storage_.message_1_)); break; case 2: new (&storage_.message_2_) message_2_type( static_cast<message_2_type&&>(other.storage_.message_2_)); break; default: break; } } ~completion_payload() { switch (index_) { case 1: storage_.message_1_.~message_1_type(); break; case 2: storage_.message_2_.~message_2_type(); break; default: break; } } template <typename Handler> void receive(Handler& handler) { switch (index_) { case 1: storage_.message_1_.receive(handler); break; case 2: storage_.message_2_.receive(handler); break; default: break; } } private: union storage { storage() {} ~storage() {} char dummy_; message_1_type message_1_; message_2_type message_2_; } storage_; unsigned char index_; }; #endif // defined(ASIO_HAS_STD_VARIANT) } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_COMPLETION_PAYLOAD_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/reactor_op_queue.hpp
// // detail/reactor_op_queue.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REACTOR_OP_QUEUE_HPP #define ASIO_DETAIL_REACTOR_OP_QUEUE_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/hash_map.hpp" #include "asio/detail/noncopyable.hpp" #include "asio/detail/op_queue.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Descriptor> class reactor_op_queue : private noncopyable { public: typedef Descriptor key_type; struct mapped_type : op_queue<reactor_op> { mapped_type() {} mapped_type(const mapped_type&) {} void operator=(const mapped_type&) {} }; typedef typename hash_map<key_type, mapped_type>::value_type value_type; typedef typename hash_map<key_type, mapped_type>::iterator iterator; // Constructor. reactor_op_queue() : operations_() { } // Obtain iterators to all registered descriptors. iterator begin() { return operations_.begin(); } iterator end() { return operations_.end(); } // Add a new operation to the queue. Returns true if this is the only // operation for the given descriptor, in which case the reactor's event // demultiplexing function call may need to be interrupted and restarted. bool enqueue_operation(Descriptor descriptor, reactor_op* op) { std::pair<iterator, bool> entry = operations_.insert(value_type(descriptor, mapped_type())); entry.first->second.push(op); return entry.second; } // Cancel all operations associated with the descriptor identified by the // supplied iterator. Any operations pending for the descriptor will be // cancelled. Returns true if any operations were cancelled, in which case // the reactor's event demultiplexing function may need to be interrupted and // restarted. bool cancel_operations(iterator i, op_queue<operation>& ops, const asio::error_code& ec = asio::error::operation_aborted) { if (i != operations_.end()) { while (reactor_op* op = i->second.front()) { op->ec_ = ec; i->second.pop(); ops.push(op); } operations_.erase(i); return true; } return false; } // Cancel all operations associated with the descriptor. Any operations // pending for the descriptor will be cancelled. Returns true if any // operations were cancelled, in which case the reactor's event // demultiplexing function may need to be interrupted and restarted. bool cancel_operations(Descriptor descriptor, op_queue<operation>& ops, const asio::error_code& ec = asio::error::operation_aborted) { return this->cancel_operations(operations_.find(descriptor), ops, ec); } // Cancel operations associated with the descriptor identified by the // supplied iterator, and the specified cancellation key. Any operations // pending for the descriptor with the key will be cancelled. Returns true if // any operations were cancelled, in which case the reactor's event // demultiplexing function may need to be interrupted and restarted. bool cancel_operations_by_key(iterator i, op_queue<operation>& ops, void* cancellation_key, const asio::error_code& ec = asio::error::operation_aborted) { bool result = false; if (i != operations_.end()) { op_queue<reactor_op> other_ops; while (reactor_op* op = i->second.front()) { i->second.pop(); if (op->cancellation_key_ == cancellation_key) { op->ec_ = ec; ops.push(op); result = true; } else other_ops.push(op); } i->second.push(other_ops); if (i->second.empty()) operations_.erase(i); } return result; } // Cancel all operations associated with the descriptor. Any operations // pending for the descriptor will be cancelled. Returns true if any // operations were cancelled, in which case the reactor's event // demultiplexing function may need to be interrupted and restarted. bool cancel_operations_by_key(Descriptor descriptor, op_queue<operation>& ops, void* cancellation_key, const asio::error_code& ec = asio::error::operation_aborted) { return this->cancel_operations_by_key( operations_.find(descriptor), ops, cancellation_key, ec); } // Whether there are no operations in the queue. bool empty() const { return operations_.empty(); } // Determine whether there are any operations associated with the descriptor. bool has_operation(Descriptor descriptor) const { return operations_.find(descriptor) != operations_.end(); } // Perform the operations corresponding to the descriptor identified by the // supplied iterator. Returns true if there are still unfinished operations // queued for the descriptor. bool perform_operations(iterator i, op_queue<operation>& ops) { if (i != operations_.end()) { while (reactor_op* op = i->second.front()) { if (op->perform()) { i->second.pop(); ops.push(op); } else { return true; } } operations_.erase(i); } return false; } // Perform the operations corresponding to the descriptor. Returns true if // there are still unfinished operations queued for the descriptor. bool perform_operations(Descriptor descriptor, op_queue<operation>& ops) { return this->perform_operations(operations_.find(descriptor), ops); } // Get all operations owned by the queue. void get_all_operations(op_queue<operation>& ops) { iterator i = operations_.begin(); while (i != operations_.end()) { iterator op_iter = i++; ops.push(op_iter->second); operations_.erase(op_iter); } } private: // The operations that are currently executing asynchronously. hash_map<key_type, mapped_type> operations_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_REACTOR_OP_QUEUE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/consuming_buffers.hpp
// // detail/consuming_buffers.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_CONSUMING_BUFFERS_HPP #define ASIO_DETAIL_CONSUMING_BUFFERS_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <cstddef> #include "asio/buffer.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/limits.hpp" #include "asio/registered_buffer.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { // Helper template to determine the maximum number of prepared buffers. template <typename Buffers> struct prepared_buffers_max { enum { value = buffer_sequence_adapter_base::max_buffers }; }; template <typename Elem, std::size_t N> struct prepared_buffers_max<boost::array<Elem, N>> { enum { value = N }; }; template <typename Elem, std::size_t N> struct prepared_buffers_max<std::array<Elem, N>> { enum { value = N }; }; // A buffer sequence used to represent a subsequence of the buffers. template <typename Buffer, std::size_t MaxBuffers> struct prepared_buffers { typedef Buffer value_type; typedef const Buffer* const_iterator; enum { max_buffers = MaxBuffers < 16 ? MaxBuffers : 16 }; prepared_buffers() : count(0) {} const_iterator begin() const { return elems; } const_iterator end() const { return elems + count; } Buffer elems[max_buffers]; std::size_t count; }; // A proxy for a sub-range in a list of buffers. template <typename Buffer, typename Buffers, typename Buffer_Iterator> class consuming_buffers { public: typedef prepared_buffers<Buffer, prepared_buffers_max<Buffers>::value> prepared_buffers_type; // Construct to represent the entire list of buffers. explicit consuming_buffers(const Buffers& buffers) : buffers_(buffers), total_consumed_(0), next_elem_(0), next_elem_offset_(0) { using asio::buffer_size; total_size_ = buffer_size(buffers); } // Determine if we are at the end of the buffers. bool empty() const { return total_consumed_ >= total_size_; } // Get the buffer for a single transfer, with a size. prepared_buffers_type prepare(std::size_t max_size) { prepared_buffers_type result; Buffer_Iterator next = asio::buffer_sequence_begin(buffers_); Buffer_Iterator end = asio::buffer_sequence_end(buffers_); std::advance(next, next_elem_); std::size_t elem_offset = next_elem_offset_; while (next != end && max_size > 0 && (result.count) < result.max_buffers) { Buffer next_buf = Buffer(*next) + elem_offset; result.elems[result.count] = asio::buffer(next_buf, max_size); max_size -= result.elems[result.count].size(); elem_offset = 0; if (result.elems[result.count].size() > 0) ++result.count; ++next; } return result; } // Consume the specified number of bytes from the buffers. void consume(std::size_t size) { total_consumed_ += size; Buffer_Iterator next = asio::buffer_sequence_begin(buffers_); Buffer_Iterator end = asio::buffer_sequence_end(buffers_); std::advance(next, next_elem_); while (next != end && size > 0) { Buffer next_buf = Buffer(*next) + next_elem_offset_; if (size < next_buf.size()) { next_elem_offset_ += size; size = 0; } else { size -= next_buf.size(); next_elem_offset_ = 0; ++next_elem_; ++next; } } } // Get the total number of bytes consumed from the buffers. std::size_t total_consumed() const { return total_consumed_; } private: Buffers buffers_; std::size_t total_size_; std::size_t total_consumed_; std::size_t next_elem_; std::size_t next_elem_offset_; }; // Base class of all consuming_buffers specialisations for single buffers. template <typename Buffer> class consuming_single_buffer { public: // Construct to represent the entire list of buffers. template <typename Buffer1> explicit consuming_single_buffer(const Buffer1& buffer) : buffer_(buffer), total_consumed_(0) { } // Determine if we are at the end of the buffers. bool empty() const { return total_consumed_ >= buffer_.size(); } // Get the buffer for a single transfer, with a size. Buffer prepare(std::size_t max_size) { return asio::buffer(buffer_ + total_consumed_, max_size); } // Consume the specified number of bytes from the buffers. void consume(std::size_t size) { total_consumed_ += size; } // Get the total number of bytes consumed from the buffers. std::size_t total_consumed() const { return total_consumed_; } private: Buffer buffer_; std::size_t total_consumed_; }; template <> class consuming_buffers<mutable_buffer, mutable_buffer, const mutable_buffer*> : public consuming_single_buffer<ASIO_MUTABLE_BUFFER> { public: explicit consuming_buffers(const mutable_buffer& buffer) : consuming_single_buffer<ASIO_MUTABLE_BUFFER>(buffer) { } }; template <> class consuming_buffers<const_buffer, mutable_buffer, const mutable_buffer*> : public consuming_single_buffer<ASIO_CONST_BUFFER> { public: explicit consuming_buffers(const mutable_buffer& buffer) : consuming_single_buffer<ASIO_CONST_BUFFER>(buffer) { } }; template <> class consuming_buffers<const_buffer, const_buffer, const const_buffer*> : public consuming_single_buffer<ASIO_CONST_BUFFER> { public: explicit consuming_buffers(const const_buffer& buffer) : consuming_single_buffer<ASIO_CONST_BUFFER>(buffer) { } }; #if !defined(ASIO_NO_DEPRECATED) template <> class consuming_buffers<mutable_buffer, mutable_buffers_1, const mutable_buffer*> : public consuming_single_buffer<ASIO_MUTABLE_BUFFER> { public: explicit consuming_buffers(const mutable_buffers_1& buffer) : consuming_single_buffer<ASIO_MUTABLE_BUFFER>(buffer) { } }; template <> class consuming_buffers<const_buffer, mutable_buffers_1, const mutable_buffer*> : public consuming_single_buffer<ASIO_CONST_BUFFER> { public: explicit consuming_buffers(const mutable_buffers_1& buffer) : consuming_single_buffer<ASIO_CONST_BUFFER>(buffer) { } }; template <> class consuming_buffers<const_buffer, const_buffers_1, const const_buffer*> : public consuming_single_buffer<ASIO_CONST_BUFFER> { public: explicit consuming_buffers(const const_buffers_1& buffer) : consuming_single_buffer<ASIO_CONST_BUFFER>(buffer) { } }; #endif // !defined(ASIO_NO_DEPRECATED) template <> class consuming_buffers<mutable_buffer, mutable_registered_buffer, const mutable_buffer*> : public consuming_single_buffer<mutable_registered_buffer> { public: explicit consuming_buffers(const mutable_registered_buffer& buffer) : consuming_single_buffer<mutable_registered_buffer>(buffer) { } }; template <> class consuming_buffers<const_buffer, mutable_registered_buffer, const mutable_buffer*> : public consuming_single_buffer<mutable_registered_buffer> { public: explicit consuming_buffers(const mutable_registered_buffer& buffer) : consuming_single_buffer<mutable_registered_buffer>(buffer) { } }; template <> class consuming_buffers<const_buffer, const_registered_buffer, const const_buffer*> : public consuming_single_buffer<const_registered_buffer> { public: explicit consuming_buffers(const const_registered_buffer& buffer) : consuming_single_buffer<const_registered_buffer>(buffer) { } }; template <typename Buffer, typename Elem> class consuming_buffers<Buffer, boost::array<Elem, 2>, typename boost::array<Elem, 2>::const_iterator> { public: // Construct to represent the entire list of buffers. explicit consuming_buffers(const boost::array<Elem, 2>& buffers) : buffers_(buffers), total_consumed_(0) { } // Determine if we are at the end of the buffers. bool empty() const { return total_consumed_ >= Buffer(buffers_[0]).size() + Buffer(buffers_[1]).size(); } // Get the buffer for a single transfer, with a size. boost::array<Buffer, 2> prepare(std::size_t max_size) { boost::array<Buffer, 2> result = {{ Buffer(buffers_[0]), Buffer(buffers_[1]) }}; std::size_t buffer0_size = result[0].size(); result[0] = asio::buffer(result[0] + total_consumed_, max_size); result[1] = asio::buffer( result[1] + (total_consumed_ < buffer0_size ? 0 : total_consumed_ - buffer0_size), max_size - result[0].size()); return result; } // Consume the specified number of bytes from the buffers. void consume(std::size_t size) { total_consumed_ += size; } // Get the total number of bytes consumed from the buffers. std::size_t total_consumed() const { return total_consumed_; } private: boost::array<Elem, 2> buffers_; std::size_t total_consumed_; }; template <typename Buffer, typename Elem> class consuming_buffers<Buffer, std::array<Elem, 2>, typename std::array<Elem, 2>::const_iterator> { public: // Construct to represent the entire list of buffers. explicit consuming_buffers(const std::array<Elem, 2>& buffers) : buffers_(buffers), total_consumed_(0) { } // Determine if we are at the end of the buffers. bool empty() const { return total_consumed_ >= Buffer(buffers_[0]).size() + Buffer(buffers_[1]).size(); } // Get the buffer for a single transfer, with a size. std::array<Buffer, 2> prepare(std::size_t max_size) { std::array<Buffer, 2> result = {{ Buffer(buffers_[0]), Buffer(buffers_[1]) }}; std::size_t buffer0_size = result[0].size(); result[0] = asio::buffer(result[0] + total_consumed_, max_size); result[1] = asio::buffer( result[1] + (total_consumed_ < buffer0_size ? 0 : total_consumed_ - buffer0_size), max_size - result[0].size()); return result; } // Consume the specified number of bytes from the buffers. void consume(std::size_t size) { total_consumed_ += size; } // Get the total number of bytes consumed from the buffers. std::size_t total_consumed() const { return total_consumed_; } private: std::array<Elem, 2> buffers_; std::size_t total_consumed_; }; // Specialisation for null_buffers to ensure that the null_buffers type is // always passed through to the underlying read or write operation. template <typename Buffer> class consuming_buffers<Buffer, null_buffers, const mutable_buffer*> : public asio::null_buffers { public: consuming_buffers(const null_buffers&) { // No-op. } bool empty() { return false; } null_buffers prepare(std::size_t) { return null_buffers(); } void consume(std::size_t) { // No-op. } std::size_t total_consumed() const { return 0; } }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_CONSUMING_BUFFERS_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/reactive_descriptor_service.hpp
// // detail/reactive_descriptor_service.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP #define ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP #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(ASIO_WINDOWS_RUNTIME) \ && !defined(__CYGWIN__) \ && !defined(ASIO_HAS_IO_URING_AS_DEFAULT) #include "asio/associated_cancellation_slot.hpp" #include "asio/associated_immediate_executor.hpp" #include "asio/buffer.hpp" #include "asio/cancellation_type.hpp" #include "asio/execution_context.hpp" #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/descriptor_ops.hpp" #include "asio/detail/descriptor_read_op.hpp" #include "asio/detail/descriptor_write_op.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/noncopyable.hpp" #include "asio/detail/reactive_null_buffers_op.hpp" #include "asio/detail/reactive_wait_op.hpp" #include "asio/detail/reactor.hpp" #include "asio/posix/descriptor_base.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class reactive_descriptor_service : public execution_context_service_base<reactive_descriptor_service> { public: // The native type of a descriptor. typedef int native_handle_type; // The implementation type of the descriptor. class implementation_type : private asio::detail::noncopyable { public: // Default constructor. implementation_type() : descriptor_(-1), state_(0) { } private: // Only this service will have access to the internal values. friend class reactive_descriptor_service; // The native descriptor representation. int descriptor_; // The current state of the descriptor. descriptor_ops::state_type state_; // Per-descriptor data used by the reactor. reactor::per_descriptor_data reactor_data_; }; // Constructor. ASIO_DECL reactive_descriptor_service(execution_context& context); // Destroy all user-defined handler objects owned by the service. ASIO_DECL void shutdown(); // Construct a new descriptor implementation. ASIO_DECL void construct(implementation_type& impl); // Move-construct a new descriptor implementation. ASIO_DECL void move_construct(implementation_type& impl, implementation_type& other_impl) noexcept; // Move-assign from another descriptor implementation. ASIO_DECL void move_assign(implementation_type& impl, reactive_descriptor_service& other_service, implementation_type& other_impl); // Destroy a descriptor implementation. ASIO_DECL void destroy(implementation_type& impl); // Assign a native descriptor to a descriptor implementation. ASIO_DECL asio::error_code assign(implementation_type& impl, const native_handle_type& native_descriptor, asio::error_code& ec); // Determine whether the descriptor is open. bool is_open(const implementation_type& impl) const { return impl.descriptor_ != -1; } // Destroy a descriptor implementation. ASIO_DECL asio::error_code close(implementation_type& impl, asio::error_code& ec); // Get the native descriptor representation. native_handle_type native_handle(const implementation_type& impl) const { return impl.descriptor_; } // Release ownership of the native descriptor representation. ASIO_DECL native_handle_type release(implementation_type& impl); // Release ownership of the native descriptor representation. native_handle_type release(implementation_type& impl, asio::error_code& ec) { ec = success_ec_; return release(impl); } // Cancel all operations associated with the descriptor. ASIO_DECL asio::error_code cancel(implementation_type& impl, asio::error_code& ec); // Perform an IO control command on the descriptor. template <typename IO_Control_Command> asio::error_code io_control(implementation_type& impl, IO_Control_Command& command, asio::error_code& ec) { descriptor_ops::ioctl(impl.descriptor_, impl.state_, command.name(), static_cast<ioctl_arg_type*>(command.data()), ec); ASIO_ERROR_LOCATION(ec); return ec; } // Gets the non-blocking mode of the descriptor. bool non_blocking(const implementation_type& impl) const { return (impl.state_ & descriptor_ops::user_set_non_blocking) != 0; } // Sets the non-blocking mode of the descriptor. asio::error_code non_blocking(implementation_type& impl, bool mode, asio::error_code& ec) { descriptor_ops::set_user_non_blocking( impl.descriptor_, impl.state_, mode, ec); ASIO_ERROR_LOCATION(ec); return ec; } // Gets the non-blocking mode of the native descriptor implementation. bool native_non_blocking(const implementation_type& impl) const { return (impl.state_ & descriptor_ops::internal_non_blocking) != 0; } // Sets the non-blocking mode of the native descriptor implementation. asio::error_code native_non_blocking(implementation_type& impl, bool mode, asio::error_code& ec) { descriptor_ops::set_internal_non_blocking( impl.descriptor_, impl.state_, mode, ec); return ec; } // Wait for the descriptor to become ready to read, ready to write, or to have // pending error conditions. asio::error_code wait(implementation_type& impl, posix::descriptor_base::wait_type w, asio::error_code& ec) { switch (w) { case posix::descriptor_base::wait_read: descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec); break; case posix::descriptor_base::wait_write: descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec); break; case posix::descriptor_base::wait_error: descriptor_ops::poll_error(impl.descriptor_, impl.state_, ec); break; default: ec = asio::error::invalid_argument; break; } ASIO_ERROR_LOCATION(ec); return ec; } // Asynchronously wait for the descriptor to become ready to read, ready to // write, or to have pending error conditions. template <typename Handler, typename IoExecutor> void async_wait(implementation_type& impl, posix::descriptor_base::wait_type w, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_wait_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, handler, io_ex); ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor", &impl, impl.descriptor_, "async_wait")); int op_type; switch (w) { case posix::descriptor_base::wait_read: op_type = reactor::read_op; break; case posix::descriptor_base::wait_write: op_type = reactor::write_op; break; case posix::descriptor_base::wait_error: op_type = reactor::except_op; break; default: p.p->ec_ = asio::error::invalid_argument; start_op(impl, reactor::read_op, p.p, is_continuation, false, true, false, &io_ex, 0); p.v = p.p = 0; return; } // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.descriptor_, op_type); } start_op(impl, op_type, p.p, is_continuation, false, false, false, &io_ex, 0); p.v = p.p = 0; } // Write some data to the descriptor. template <typename ConstBufferSequence> size_t write_some(implementation_type& impl, const ConstBufferSequence& buffers, asio::error_code& ec) { typedef buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence> bufs_type; size_t n; if (bufs_type::is_single_buffer) { n = descriptor_ops::sync_write1(impl.descriptor_, impl.state_, bufs_type::first(buffers).data(), bufs_type::first(buffers).size(), ec); } else { bufs_type bufs(buffers); n = descriptor_ops::sync_write(impl.descriptor_, impl.state_, bufs.buffers(), bufs.count(), bufs.all_empty(), ec); } ASIO_ERROR_LOCATION(ec); return n; } // Wait until data can be written without blocking. size_t write_some(implementation_type& impl, const null_buffers&, asio::error_code& ec) { // Wait for descriptor to become ready. descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec); ASIO_ERROR_LOCATION(ec); return 0; } // Start an asynchronous write. The data being sent must be valid for the // lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler, typename IoExecutor> void async_write_some(implementation_type& impl, const ConstBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef descriptor_write_op<ConstBufferSequence, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, impl.descriptor_, buffers, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.descriptor_, reactor::write_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor", &impl, impl.descriptor_, "async_write_some")); start_op(impl, reactor::write_op, p.p, is_continuation, true, buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence>::all_empty(buffers), true, &io_ex, 0); p.v = p.p = 0; } // Start an asynchronous wait until data can be written without blocking. template <typename Handler, typename IoExecutor> void async_write_some(implementation_type& impl, const null_buffers&, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_null_buffers_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.descriptor_, reactor::write_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor", &impl, impl.descriptor_, "async_write_some(null_buffers)")); start_op(impl, reactor::write_op, p.p, is_continuation, false, false, false, &io_ex, 0); p.v = p.p = 0; } // Read some data from the stream. Returns the number of bytes read. template <typename MutableBufferSequence> size_t read_some(implementation_type& impl, const MutableBufferSequence& buffers, asio::error_code& ec) { typedef buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence> bufs_type; size_t n; if (bufs_type::is_single_buffer) { n = descriptor_ops::sync_read1(impl.descriptor_, impl.state_, bufs_type::first(buffers).data(), bufs_type::first(buffers).size(), ec); } else { bufs_type bufs(buffers); n = descriptor_ops::sync_read(impl.descriptor_, impl.state_, bufs.buffers(), bufs.count(), bufs.all_empty(), ec); } ASIO_ERROR_LOCATION(ec); return n; } // Wait until data can be read without blocking. size_t read_some(implementation_type& impl, const null_buffers&, asio::error_code& ec) { // Wait for descriptor to become ready. descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec); ASIO_ERROR_LOCATION(ec); return 0; } // Start an asynchronous read. The buffer for the data being read must be // valid for the lifetime of the asynchronous operation. template <typename MutableBufferSequence, typename Handler, typename IoExecutor> void async_read_some(implementation_type& impl, const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef descriptor_read_op<MutableBufferSequence, Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, impl.descriptor_, buffers, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.descriptor_, reactor::read_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor", &impl, impl.descriptor_, "async_read_some")); start_op(impl, reactor::read_op, p.p, is_continuation, true, buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence>::all_empty(buffers), true, &io_ex, 0); p.v = p.p = 0; } // Wait until data can be read without blocking. template <typename Handler, typename IoExecutor> void async_read_some(implementation_type& impl, const null_buffers&, Handler& handler, const IoExecutor& io_ex) { bool is_continuation = asio_handler_cont_helpers::is_continuation(handler); associated_cancellation_slot_t<Handler> slot = asio::get_associated_cancellation_slot(handler); // Allocate and construct an operation to wrap the handler. typedef reactive_null_buffers_op<Handler, IoExecutor> op; typename op::ptr p = { asio::detail::addressof(handler), op::ptr::allocate(handler), 0 }; p.p = new (p.v) op(success_ec_, handler, io_ex); // Optionally register for per-operation cancellation. if (slot.is_connected()) { p.p->cancellation_key_ = &slot.template emplace<reactor_op_cancellation>( &reactor_, &impl.reactor_data_, impl.descriptor_, reactor::read_op); } ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor", &impl, impl.descriptor_, "async_read_some(null_buffers)")); start_op(impl, reactor::read_op, p.p, is_continuation, false, false, false, &io_ex, 0); p.v = p.p = 0; } private: // Start the asynchronous operation. ASIO_DECL void do_start_op(implementation_type& impl, int op_type, reactor_op* op, bool is_continuation, bool allow_speculative, bool noop, bool needs_non_blocking, void (*on_immediate)(operation* op, bool, const void*), const void* immediate_arg); // Start the asynchronous operation for handlers that are specialised for // immediate completion. template <typename Op> void start_op(implementation_type& impl, int op_type, Op* op, bool is_continuation, bool allow_speculative, bool noop, bool needs_non_blocking, const void* io_ex, ...) { return do_start_op(impl, op_type, op, is_continuation, allow_speculative, noop, needs_non_blocking, &Op::do_immediate, io_ex); } // Start the asynchronous operation for handlers that are not specialised for // immediate completion. template <typename Op> void start_op(implementation_type& impl, int op_type, Op* op, bool is_continuation, bool allow_speculative, bool noop, bool needs_non_blocking, const void*, enable_if_t< is_same< typename associated_immediate_executor< typename Op::handler_type, typename Op::io_executor_type >::asio_associated_immediate_executor_is_unspecialised, void >::value >*) { return do_start_op(impl, op_type, op, is_continuation, allow_speculative, noop, needs_non_blocking, &reactor::call_post_immediate_completion, &reactor_); } // Helper class used to implement per-operation cancellation class reactor_op_cancellation { public: reactor_op_cancellation(reactor* r, reactor::per_descriptor_data* p, int d, int o) : reactor_(r), reactor_data_(p), descriptor_(d), op_type_(o) { } void operator()(cancellation_type_t type) { if (!!(type & (cancellation_type::terminal | cancellation_type::partial | cancellation_type::total))) { reactor_->cancel_ops_by_key(descriptor_, *reactor_data_, op_type_, this); } } private: reactor* reactor_; reactor::per_descriptor_data* reactor_data_; int descriptor_; int op_type_; }; // The selector that performs event demultiplexing for the service. reactor& reactor_; // Cached success value to avoid accessing category singleton. const asio::error_code success_ec_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/reactive_descriptor_service.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // !defined(ASIO_WINDOWS) // && !defined(ASIO_WINDOWS_RUNTIME) // && !defined(__CYGWIN__) // && !defined(ASIO_HAS_IO_URING_AS_DEFAULT) #endif // ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/signal_blocker.hpp
// // detail/signal_blocker.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_SIGNAL_BLOCKER_HPP #define ASIO_DETAIL_SIGNAL_BLOCKER_HPP #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_THREADS) || defined(ASIO_WINDOWS) \ || defined(ASIO_WINDOWS_RUNTIME) \ || defined(__CYGWIN__) || defined(__SYMBIAN32__) # include "asio/detail/null_signal_blocker.hpp" #elif defined(ASIO_HAS_PTHREADS) # include "asio/detail/posix_signal_blocker.hpp" #else # error Only Windows and POSIX are supported! #endif namespace asio { namespace detail { #if !defined(ASIO_HAS_THREADS) || defined(ASIO_WINDOWS) \ || defined(ASIO_WINDOWS_RUNTIME) \ || defined(__CYGWIN__) || defined(__SYMBIAN32__) typedef null_signal_blocker signal_blocker; #elif defined(ASIO_HAS_PTHREADS) typedef posix_signal_blocker signal_blocker; #endif } // namespace detail } // namespace asio #endif // ASIO_DETAIL_SIGNAL_BLOCKER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/reactor.hpp
// // detail/reactor.hpp // ~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REACTOR_HPP #define ASIO_DETAIL_REACTOR_HPP #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_IOCP) || defined(ASIO_WINDOWS_RUNTIME) # include "asio/detail/null_reactor.hpp" #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT) # include "asio/detail/null_reactor.hpp" #elif defined(ASIO_HAS_EPOLL) # include "asio/detail/epoll_reactor.hpp" #elif defined(ASIO_HAS_KQUEUE) # include "asio/detail/kqueue_reactor.hpp" #elif defined(ASIO_HAS_DEV_POLL) # include "asio/detail/dev_poll_reactor.hpp" #else # include "asio/detail/select_reactor.hpp" #endif namespace asio { namespace detail { #if defined(ASIO_HAS_IOCP) || defined(ASIO_WINDOWS_RUNTIME) typedef null_reactor reactor; #elif defined(ASIO_HAS_IO_URING_AS_DEFAULT) typedef null_reactor reactor; #elif defined(ASIO_HAS_EPOLL) typedef epoll_reactor reactor; #elif defined(ASIO_HAS_KQUEUE) typedef kqueue_reactor reactor; #elif defined(ASIO_HAS_DEV_POLL) typedef dev_poll_reactor reactor; #else typedef select_reactor reactor; #endif } // namespace detail } // namespace asio #endif // ASIO_DETAIL_REACTOR_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/thread.hpp
// // detail/thread.hpp // ~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_THREAD_HPP #define ASIO_DETAIL_THREAD_HPP #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_THREADS) # include "asio/detail/null_thread.hpp" #elif defined(ASIO_HAS_PTHREADS) # include "asio/detail/posix_thread.hpp" #elif defined(ASIO_WINDOWS) # if defined(UNDER_CE) # include "asio/detail/wince_thread.hpp" # elif defined(ASIO_WINDOWS_APP) # include "asio/detail/winapp_thread.hpp" # else # include "asio/detail/win_thread.hpp" # endif #else # include "asio/detail/std_thread.hpp" #endif namespace asio { namespace detail { #if !defined(ASIO_HAS_THREADS) typedef null_thread thread; #elif defined(ASIO_HAS_PTHREADS) typedef posix_thread thread; #elif defined(ASIO_WINDOWS) # if defined(UNDER_CE) typedef wince_thread thread; # elif defined(ASIO_WINDOWS_APP) typedef winapp_thread thread; # else typedef win_thread thread; # endif #else typedef std_thread thread; #endif } // namespace detail } // namespace asio #endif // ASIO_DETAIL_THREAD_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/cstdint.hpp
// // detail/cstdint.hpp // ~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_CSTDINT_HPP #define ASIO_DETAIL_CSTDINT_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <cstdint> namespace asio { using std::int16_t; using std::int_least16_t; using std::uint16_t; using std::uint_least16_t; using std::int32_t; using std::int_least32_t; using std::uint32_t; using std::uint_least32_t; using std::int64_t; using std::int_least64_t; using std::uint64_t; using std::uint_least64_t; using std::uintptr_t; using std::uintmax_t; } // namespace asio #endif // ASIO_DETAIL_CSTDINT_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/completion_handler.hpp
// // detail/completion_handler.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_COMPLETION_HANDLER_HPP #define ASIO_DETAIL_COMPLETION_HANDLER_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/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/operation.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Handler, typename IoExecutor> class completion_handler : public operation { public: ASIO_DEFINE_HANDLER_PTR(completion_handler); completion_handler(Handler& h, const IoExecutor& io_ex) : operation(&completion_handler::do_complete), handler_(static_cast<Handler&&>(h)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. completion_handler* h(static_cast<completion_handler*>(base)); ptr p = { asio::detail::addressof(h->handler_), h, h }; ASIO_HANDLER_COMPLETION((*h)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( h->work_)); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. Handler handler(static_cast<Handler&&>(h->handler_)); p.h = asio::detail::addressof(handler); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN(()); w.complete(handler, handler); ASIO_HANDLER_INVOCATION_END; } } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_COMPLETION_HANDLER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/socket_ops.hpp
// // detail/socket_ops.hpp // ~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_SOCKET_OPS_HPP #define ASIO_DETAIL_SOCKET_OPS_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/error_code.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { namespace socket_ops { // Socket state bits. enum { // The user wants a non-blocking socket. user_set_non_blocking = 1, // The socket has been set non-blocking. internal_non_blocking = 2, // Helper "state" used to determine whether the socket is non-blocking. non_blocking = user_set_non_blocking | internal_non_blocking, // User wants connection_aborted errors, which are disabled by default. enable_connection_aborted = 4, // The user set the linger option. Needs to be checked when closing. user_set_linger = 8, // The socket is stream-oriented. stream_oriented = 16, // The socket is datagram-oriented. datagram_oriented = 32, // The socket may have been dup()-ed. possible_dup = 64 }; typedef unsigned char state_type; struct noop_deleter { void operator()(void*) {} }; typedef shared_ptr<void> shared_cancel_token_type; typedef weak_ptr<void> weak_cancel_token_type; #if !defined(ASIO_WINDOWS_RUNTIME) ASIO_DECL socket_type accept(socket_type s, void* addr, std::size_t* addrlen, asio::error_code& ec); ASIO_DECL socket_type sync_accept(socket_type s, state_type state, void* addr, std::size_t* addrlen, asio::error_code& ec); #if defined(ASIO_HAS_IOCP) ASIO_DECL void complete_iocp_accept(socket_type s, void* output_buffer, DWORD address_length, void* addr, std::size_t* addrlen, socket_type new_socket, asio::error_code& ec); #else // defined(ASIO_HAS_IOCP) ASIO_DECL bool non_blocking_accept(socket_type s, state_type state, void* addr, std::size_t* addrlen, asio::error_code& ec, socket_type& new_socket); #endif // defined(ASIO_HAS_IOCP) ASIO_DECL int bind(socket_type s, const void* addr, std::size_t addrlen, asio::error_code& ec); ASIO_DECL int close(socket_type s, state_type& state, bool destruction, asio::error_code& ec); ASIO_DECL bool set_user_non_blocking(socket_type s, state_type& state, bool value, asio::error_code& ec); ASIO_DECL bool set_internal_non_blocking(socket_type s, state_type& state, bool value, asio::error_code& ec); ASIO_DECL int shutdown(socket_type s, int what, asio::error_code& ec); ASIO_DECL int connect(socket_type s, const void* addr, std::size_t addrlen, asio::error_code& ec); ASIO_DECL void sync_connect(socket_type s, const void* addr, std::size_t addrlen, asio::error_code& ec); #if defined(ASIO_HAS_IOCP) ASIO_DECL void complete_iocp_connect(socket_type s, asio::error_code& ec); #endif // defined(ASIO_HAS_IOCP) ASIO_DECL bool non_blocking_connect(socket_type s, asio::error_code& ec); ASIO_DECL int socketpair(int af, int type, int protocol, socket_type sv[2], asio::error_code& ec); ASIO_DECL bool sockatmark(socket_type s, asio::error_code& ec); ASIO_DECL size_t available(socket_type s, asio::error_code& ec); ASIO_DECL int listen(socket_type s, int backlog, asio::error_code& ec); #if defined(ASIO_WINDOWS) || defined(__CYGWIN__) typedef WSABUF buf; #else // defined(ASIO_WINDOWS) || defined(__CYGWIN__) typedef iovec buf; #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__) ASIO_DECL void init_buf(buf& b, void* data, size_t size); ASIO_DECL void init_buf(buf& b, const void* data, size_t size); ASIO_DECL signed_size_type recv(socket_type s, buf* bufs, size_t count, int flags, asio::error_code& ec); ASIO_DECL signed_size_type recv1(socket_type s, void* data, size_t size, int flags, asio::error_code& ec); ASIO_DECL size_t sync_recv(socket_type s, state_type state, buf* bufs, size_t count, int flags, bool all_empty, asio::error_code& ec); ASIO_DECL size_t sync_recv1(socket_type s, state_type state, void* data, size_t size, int flags, asio::error_code& ec); #if defined(ASIO_HAS_IOCP) ASIO_DECL void complete_iocp_recv(state_type state, const weak_cancel_token_type& cancel_token, bool all_empty, asio::error_code& ec, size_t bytes_transferred); #else // defined(ASIO_HAS_IOCP) ASIO_DECL bool non_blocking_recv(socket_type s, buf* bufs, size_t count, int flags, bool is_stream, asio::error_code& ec, size_t& bytes_transferred); ASIO_DECL bool non_blocking_recv1(socket_type s, void* data, size_t size, int flags, bool is_stream, asio::error_code& ec, size_t& bytes_transferred); #endif // defined(ASIO_HAS_IOCP) ASIO_DECL signed_size_type recvfrom(socket_type s, buf* bufs, size_t count, int flags, void* addr, std::size_t* addrlen, asio::error_code& ec); ASIO_DECL signed_size_type recvfrom1(socket_type s, void* data, size_t size, int flags, void* addr, std::size_t* addrlen, asio::error_code& ec); ASIO_DECL size_t sync_recvfrom(socket_type s, state_type state, buf* bufs, size_t count, int flags, void* addr, std::size_t* addrlen, asio::error_code& ec); ASIO_DECL size_t sync_recvfrom1(socket_type s, state_type state, void* data, size_t size, int flags, void* addr, std::size_t* addrlen, asio::error_code& ec); #if defined(ASIO_HAS_IOCP) ASIO_DECL void complete_iocp_recvfrom( const weak_cancel_token_type& cancel_token, asio::error_code& ec); #else // defined(ASIO_HAS_IOCP) ASIO_DECL bool non_blocking_recvfrom(socket_type s, buf* bufs, size_t count, int flags, void* addr, std::size_t* addrlen, asio::error_code& ec, size_t& bytes_transferred); ASIO_DECL bool non_blocking_recvfrom1(socket_type s, void* data, size_t size, int flags, void* addr, std::size_t* addrlen, asio::error_code& ec, size_t& bytes_transferred); #endif // defined(ASIO_HAS_IOCP) ASIO_DECL signed_size_type recvmsg(socket_type s, buf* bufs, size_t count, int in_flags, int& out_flags, asio::error_code& ec); ASIO_DECL size_t sync_recvmsg(socket_type s, state_type state, buf* bufs, size_t count, int in_flags, int& out_flags, asio::error_code& ec); #if defined(ASIO_HAS_IOCP) ASIO_DECL void complete_iocp_recvmsg( const weak_cancel_token_type& cancel_token, asio::error_code& ec); #else // defined(ASIO_HAS_IOCP) ASIO_DECL bool non_blocking_recvmsg(socket_type s, buf* bufs, size_t count, int in_flags, int& out_flags, asio::error_code& ec, size_t& bytes_transferred); #endif // defined(ASIO_HAS_IOCP) ASIO_DECL signed_size_type send(socket_type s, const buf* bufs, size_t count, int flags, asio::error_code& ec); ASIO_DECL signed_size_type send1(socket_type s, const void* data, size_t size, int flags, asio::error_code& ec); ASIO_DECL size_t sync_send(socket_type s, state_type state, const buf* bufs, size_t count, int flags, bool all_empty, asio::error_code& ec); ASIO_DECL size_t sync_send1(socket_type s, state_type state, const void* data, size_t size, int flags, asio::error_code& ec); #if defined(ASIO_HAS_IOCP) ASIO_DECL void complete_iocp_send( const weak_cancel_token_type& cancel_token, asio::error_code& ec); #else // defined(ASIO_HAS_IOCP) ASIO_DECL bool non_blocking_send(socket_type s, const buf* bufs, size_t count, int flags, asio::error_code& ec, size_t& bytes_transferred); ASIO_DECL bool non_blocking_send1(socket_type s, const void* data, size_t size, int flags, asio::error_code& ec, size_t& bytes_transferred); #endif // defined(ASIO_HAS_IOCP) ASIO_DECL signed_size_type sendto(socket_type s, const buf* bufs, size_t count, int flags, const void* addr, std::size_t addrlen, asio::error_code& ec); ASIO_DECL signed_size_type sendto1(socket_type s, const void* data, size_t size, int flags, const void* addr, std::size_t addrlen, asio::error_code& ec); ASIO_DECL size_t sync_sendto(socket_type s, state_type state, const buf* bufs, size_t count, int flags, const void* addr, std::size_t addrlen, asio::error_code& ec); ASIO_DECL size_t sync_sendto1(socket_type s, state_type state, const void* data, size_t size, int flags, const void* addr, std::size_t addrlen, asio::error_code& ec); #if !defined(ASIO_HAS_IOCP) ASIO_DECL bool non_blocking_sendto(socket_type s, const buf* bufs, size_t count, int flags, const void* addr, std::size_t addrlen, asio::error_code& ec, size_t& bytes_transferred); ASIO_DECL bool non_blocking_sendto1(socket_type s, const void* data, size_t size, int flags, const void* addr, std::size_t addrlen, asio::error_code& ec, size_t& bytes_transferred); #endif // !defined(ASIO_HAS_IOCP) ASIO_DECL socket_type socket(int af, int type, int protocol, asio::error_code& ec); ASIO_DECL int setsockopt(socket_type s, state_type& state, int level, int optname, const void* optval, std::size_t optlen, asio::error_code& ec); ASIO_DECL int getsockopt(socket_type s, state_type state, int level, int optname, void* optval, size_t* optlen, asio::error_code& ec); ASIO_DECL int getpeername(socket_type s, void* addr, std::size_t* addrlen, bool cached, asio::error_code& ec); ASIO_DECL int getsockname(socket_type s, void* addr, std::size_t* addrlen, asio::error_code& ec); ASIO_DECL int ioctl(socket_type s, state_type& state, int cmd, ioctl_arg_type* arg, asio::error_code& ec); ASIO_DECL int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds, timeval* timeout, asio::error_code& ec); ASIO_DECL int poll_read(socket_type s, state_type state, int msec, asio::error_code& ec); ASIO_DECL int poll_write(socket_type s, state_type state, int msec, asio::error_code& ec); ASIO_DECL int poll_error(socket_type s, state_type state, int msec, asio::error_code& ec); ASIO_DECL int poll_connect(socket_type s, int msec, asio::error_code& ec); #endif // !defined(ASIO_WINDOWS_RUNTIME) ASIO_DECL const char* inet_ntop(int af, const void* src, char* dest, size_t length, unsigned long scope_id, asio::error_code& ec); ASIO_DECL int inet_pton(int af, const char* src, void* dest, unsigned long* scope_id, asio::error_code& ec); ASIO_DECL int gethostname(char* name, int namelen, asio::error_code& ec); #if !defined(ASIO_WINDOWS_RUNTIME) ASIO_DECL asio::error_code getaddrinfo(const char* host, const char* service, const addrinfo_type& hints, addrinfo_type** result, asio::error_code& ec); ASIO_DECL asio::error_code background_getaddrinfo( const weak_cancel_token_type& cancel_token, const char* host, const char* service, const addrinfo_type& hints, addrinfo_type** result, asio::error_code& ec); ASIO_DECL void freeaddrinfo(addrinfo_type* ai); ASIO_DECL asio::error_code getnameinfo(const void* addr, std::size_t addrlen, char* host, std::size_t hostlen, char* serv, std::size_t servlen, int flags, asio::error_code& ec); ASIO_DECL asio::error_code sync_getnameinfo(const void* addr, std::size_t addrlen, char* host, std::size_t hostlen, char* serv, std::size_t servlen, int sock_type, asio::error_code& ec); ASIO_DECL asio::error_code background_getnameinfo( const weak_cancel_token_type& cancel_token, const void* addr, std::size_t addrlen, char* host, std::size_t hostlen, char* serv, std::size_t servlen, int sock_type, asio::error_code& ec); #endif // !defined(ASIO_WINDOWS_RUNTIME) ASIO_DECL u_long_type network_to_host_long(u_long_type value); ASIO_DECL u_long_type host_to_network_long(u_long_type value); ASIO_DECL u_short_type network_to_host_short(u_short_type value); ASIO_DECL u_short_type host_to_network_short(u_short_type value); } // namespace socket_ops } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/socket_ops.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // ASIO_DETAIL_SOCKET_OPS_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/null_global.hpp
// // detail/null_global.hpp // ~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_NULL_GLOBAL_HPP #define ASIO_DETAIL_NULL_GLOBAL_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/push_options.hpp" namespace asio { namespace detail { template <typename T> struct null_global_impl { null_global_impl() : ptr_(0) { } // Destructor automatically cleans up the global. ~null_global_impl() { delete ptr_; } static null_global_impl instance_; T* ptr_; }; template <typename T> null_global_impl<T> null_global_impl<T>::instance_; template <typename T> T& null_global() { if (null_global_impl<T>::instance_.ptr_ == 0) null_global_impl<T>::instance_.ptr_ = new T; return *null_global_impl<T>::instance_.ptr_; } } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_NULL_GLOBAL_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/handler_type_requirements.hpp
// // detail/handler_type_requirements.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_HANDLER_TYPE_REQUIREMENTS_HPP #define ASIO_DETAIL_HANDLER_TYPE_REQUIREMENTS_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" // Older versions of gcc have difficulty compiling the sizeof expressions where // we test the handler type requirements. We'll disable checking of handler type // requirements for those compilers, but otherwise enable it by default. #if !defined(ASIO_DISABLE_HANDLER_TYPE_REQUIREMENTS) # if !defined(__GNUC__) || (__GNUC__ >= 4) # define ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS 1 # endif // !defined(__GNUC__) || (__GNUC__ >= 4) #endif // !defined(ASIO_DISABLE_HANDLER_TYPE_REQUIREMENTS) // With C++0x we can use a combination of enhanced SFINAE and static_assert to // generate better template error messages. As this technique is not yet widely // portable, we'll only enable it for tested compilers. #if !defined(ASIO_DISABLE_HANDLER_TYPE_REQUIREMENTS_ASSERT) # if defined(__GNUC__) # if ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ > 4) # if defined(__GXX_EXPERIMENTAL_CXX0X__) # define ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS_ASSERT 1 # endif // defined(__GXX_EXPERIMENTAL_CXX0X__) # endif // ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 5)) || (__GNUC__ > 4) # endif // defined(__GNUC__) # if defined(ASIO_MSVC) # if (_MSC_VER >= 1600) # define ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS_ASSERT 1 # endif // (_MSC_VER >= 1600) # endif // defined(ASIO_MSVC) # if defined(__clang__) # if __has_feature(__cxx_static_assert__) # define ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS_ASSERT 1 # endif // __has_feature(cxx_static_assert) # endif // defined(__clang__) #endif // !defined(ASIO_DISABLE_HANDLER_TYPE_REQUIREMENTS) #if defined(ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS) # include "asio/async_result.hpp" #endif // defined(ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS) namespace asio { namespace detail { #if defined(ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS) # if defined(ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS_ASSERT) template <typename Handler> auto zero_arg_copyable_handler_test(Handler h, void*) -> decltype( sizeof(Handler(static_cast<const Handler&>(h))), (static_cast<Handler&&>(h)()), char(0)); template <typename Handler> char (&zero_arg_copyable_handler_test(Handler, ...))[2]; template <typename Handler, typename Arg1> auto one_arg_handler_test(Handler h, Arg1* a1) -> decltype( sizeof(Handler(static_cast<Handler&&>(h))), (static_cast<Handler&&>(h)(*a1)), char(0)); template <typename Handler> char (&one_arg_handler_test(Handler h, ...))[2]; template <typename Handler, typename Arg1, typename Arg2> auto two_arg_handler_test(Handler h, Arg1* a1, Arg2* a2) -> decltype( sizeof(Handler(static_cast<Handler&&>(h))), (static_cast<Handler&&>(h)(*a1, *a2)), char(0)); template <typename Handler> char (&two_arg_handler_test(Handler, ...))[2]; template <typename Handler, typename Arg1, typename Arg2> auto two_arg_move_handler_test(Handler h, Arg1* a1, Arg2* a2) -> decltype( sizeof(Handler(static_cast<Handler&&>(h))), (static_cast<Handler&&>(h)( *a1, static_cast<Arg2&&>(*a2))), char(0)); template <typename Handler> char (&two_arg_move_handler_test(Handler, ...))[2]; # define ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT(expr, msg) \ static_assert(expr, msg); # else // defined(ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS_ASSERT) # define ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT(expr, msg) # endif // defined(ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS_ASSERT) template <typename T> T& lvref(); template <typename T> T& lvref(T); template <typename T> const T& clvref(); template <typename T> const T& clvref(T); template <typename T> T rvref(); template <typename T> T rvref(T); template <typename T> T rorlvref(); template <typename T> char argbyv(T); template <int> struct handler_type_requirements { }; #define ASIO_LEGACY_COMPLETION_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void()) asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::zero_arg_copyable_handler_test( \ asio::detail::clvref< \ asio_true_handler_type>(), 0)) == 1, \ "CompletionHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::clvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()(), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_READ_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code, std::size_t)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::two_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0), \ static_cast<const std::size_t*>(0))) == 1, \ "ReadHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>(), \ asio::detail::lvref<const std::size_t>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_WRITE_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code, std::size_t)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::two_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0), \ static_cast<const std::size_t*>(0))) == 1, \ "WriteHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>(), \ asio::detail::lvref<const std::size_t>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_ACCEPT_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::one_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0))) == 1, \ "AcceptHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_MOVE_ACCEPT_HANDLER_CHECK( \ handler_type, handler, socket_type) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code, socket_type)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::two_arg_move_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0), \ static_cast<socket_type*>(0))) == 1, \ "MoveAcceptHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>(), \ asio::detail::rvref<socket_type>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_CONNECT_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::one_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0))) == 1, \ "ConnectHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_RANGE_CONNECT_HANDLER_CHECK( \ handler_type, handler, endpoint_type) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code, endpoint_type)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::two_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0), \ static_cast<const endpoint_type*>(0))) == 1, \ "RangeConnectHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>(), \ asio::detail::lvref<const endpoint_type>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_ITERATOR_CONNECT_HANDLER_CHECK( \ handler_type, handler, iter_type) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code, iter_type)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::two_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0), \ static_cast<const iter_type*>(0))) == 1, \ "IteratorConnectHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>(), \ asio::detail::lvref<const iter_type>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_RESOLVE_HANDLER_CHECK( \ handler_type, handler, range_type) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code, range_type)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::two_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0), \ static_cast<const range_type*>(0))) == 1, \ "ResolveHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>(), \ asio::detail::lvref<const range_type>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_WAIT_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::one_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0))) == 1, \ "WaitHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_SIGNAL_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code, int)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::two_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0), \ static_cast<const int*>(0))) == 1, \ "SignalHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>(), \ asio::detail::lvref<const int>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_HANDSHAKE_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::one_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0))) == 1, \ "HandshakeHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_BUFFERED_HANDSHAKE_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code, std::size_t)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::two_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0), \ static_cast<const std::size_t*>(0))) == 1, \ "BufferedHandshakeHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>(), \ asio::detail::lvref<const std::size_t>()), \ char(0))> ASIO_UNUSED_TYPEDEF #define ASIO_SHUTDOWN_HANDLER_CHECK( \ handler_type, handler) \ \ typedef ASIO_HANDLER_TYPE(handler_type, \ void(asio::error_code)) \ asio_true_handler_type; \ \ ASIO_HANDLER_TYPE_REQUIREMENTS_ASSERT( \ sizeof(asio::detail::one_arg_handler_test( \ asio::detail::rvref< \ asio_true_handler_type>(), \ static_cast<const asio::error_code*>(0))) == 1, \ "ShutdownHandler type requirements not met") \ \ typedef asio::detail::handler_type_requirements< \ sizeof( \ asio::detail::argbyv( \ asio::detail::rvref< \ asio_true_handler_type>())) + \ sizeof( \ asio::detail::rorlvref< \ asio_true_handler_type>()( \ asio::detail::lvref<const asio::error_code>()), \ char(0))> ASIO_UNUSED_TYPEDEF #else // !defined(ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS) #define ASIO_LEGACY_COMPLETION_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_READ_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_WRITE_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_ACCEPT_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_MOVE_ACCEPT_HANDLER_CHECK( \ handler_type, handler, socket_type) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_CONNECT_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_RANGE_CONNECT_HANDLER_CHECK( \ handler_type, handler, iter_type) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_ITERATOR_CONNECT_HANDLER_CHECK( \ handler_type, handler, iter_type) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_RESOLVE_HANDLER_CHECK( \ handler_type, handler, iter_type) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_WAIT_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_SIGNAL_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_HANDSHAKE_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_BUFFERED_HANDSHAKE_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #define ASIO_SHUTDOWN_HANDLER_CHECK( \ handler_type, handler) \ typedef int ASIO_UNUSED_TYPEDEF #endif // !defined(ASIO_ENABLE_HANDLER_TYPE_REQUIREMENTS) } // namespace detail } // namespace asio #endif // ASIO_DETAIL_HANDLER_TYPE_REQUIREMENTS_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/regex_fwd.hpp
// // detail/regex_fwd.hpp // ~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REGEX_FWD_HPP #define ASIO_DETAIL_REGEX_FWD_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #if defined(ASIO_HAS_BOOST_REGEX) namespace boost { template <class BidiIterator> struct sub_match; template <class BidiIterator, class Allocator> class match_results; template <class CharT, class Traits> class basic_regex; } // namespace boost #endif // defined(ASIO_HAS_BOOST_REGEX) #endif // ASIO_DETAIL_REGEX_FWD_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/exception.hpp
// // detail/exception.hpp // ~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_EXCEPTION_HPP #define ASIO_DETAIL_EXCEPTION_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <exception> namespace asio { using std::exception_ptr; using std::current_exception; using std::rethrow_exception; } // namespace asio #endif // ASIO_DETAIL_EXCEPTION_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/reactive_wait_op.hpp
// // detail/reactive_wait_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_REACTIVE_WAIT_OP_HPP #define ASIO_DETAIL_REACTIVE_WAIT_OP_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/bind_handler.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Handler, typename IoExecutor> class reactive_wait_op : public reactor_op { public: typedef Handler handler_type; typedef IoExecutor io_executor_type; ASIO_DEFINE_HANDLER_PTR(reactive_wait_op); reactive_wait_op(const asio::error_code& success_ec, Handler& handler, const IoExecutor& io_ex) : reactor_op(success_ec, &reactive_wait_op::do_perform, &reactive_wait_op::do_complete), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static status do_perform(reactor_op*) { return done; } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); reactive_wait_op* o(static_cast<reactive_wait_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder1<Handler, asio::error_code> handler(o->handler_, o->ec_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } static void do_immediate(operation* base, bool, const void* io_ex) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); reactive_wait_op* o(static_cast<reactive_wait_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. immediate_handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder1<Handler, asio::error_code> handler(o->handler_, o->ec_); p.h = asio::detail::addressof(handler.handler_); p.reset(); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_)); w.complete(handler, handler.handler_, io_ex); ASIO_HANDLER_INVOCATION_END; } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_REACTIVE_WAIT_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/cstddef.hpp
// // detail/cstddef.hpp // ~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_CSTDDEF_HPP #define ASIO_DETAIL_CSTDDEF_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <cstddef> namespace asio { using std::nullptr_t; } // namespace asio #endif // ASIO_DETAIL_CSTDDEF_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/posix_mutex.hpp
// // detail/posix_mutex.hpp // ~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_POSIX_MUTEX_HPP #define ASIO_DETAIL_POSIX_MUTEX_HPP #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_PTHREADS) #include <pthread.h> #include "asio/detail/noncopyable.hpp" #include "asio/detail/scoped_lock.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class posix_event; class posix_mutex : private noncopyable { public: typedef asio::detail::scoped_lock<posix_mutex> scoped_lock; // Constructor. ASIO_DECL posix_mutex(); // Destructor. ~posix_mutex() { ::pthread_mutex_destroy(&mutex_); // Ignore EBUSY. } // Lock the mutex. void lock() { (void)::pthread_mutex_lock(&mutex_); // Ignore EINVAL. } // Unlock the mutex. void unlock() { (void)::pthread_mutex_unlock(&mutex_); // Ignore EINVAL. } private: friend class posix_event; ::pthread_mutex_t mutex_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/posix_mutex.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // defined(ASIO_HAS_PTHREADS) #endif // ASIO_DETAIL_POSIX_MUTEX_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/initiate_defer.hpp
// // detail/initiate_defer.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_INITIATE_DEFER_HPP #define ASIO_DETAIL_INITIATE_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/execution/allocator.hpp" #include "asio/execution/blocking.hpp" #include "asio/execution/relationship.hpp" #include "asio/prefer.hpp" #include "asio/require.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class initiate_defer { public: template <typename CompletionHandler> void operator()(CompletionHandler&& handler, enable_if_t< execution::is_executor< associated_executor_t<decay_t<CompletionHandler>> >::value >* = 0) const { associated_executor_t<decay_t<CompletionHandler>> ex( (get_associated_executor)(handler)); associated_allocator_t<decay_t<CompletionHandler>> alloc( (get_associated_allocator)(handler)); asio::prefer( asio::require(ex, execution::blocking.never), execution::relationship.continuation, execution::allocator(alloc) ).execute( asio::detail::bind_handler( static_cast<CompletionHandler&&>(handler))); } template <typename CompletionHandler> void operator()(CompletionHandler&& handler, enable_if_t< !execution::is_executor< associated_executor_t<decay_t<CompletionHandler>> >::value >* = 0) const { associated_executor_t<decay_t<CompletionHandler>> ex( (get_associated_executor)(handler)); associated_allocator_t<decay_t<CompletionHandler>> alloc( (get_associated_allocator)(handler)); ex.defer(asio::detail::bind_handler( static_cast<CompletionHandler&&>(handler)), alloc); } }; template <typename Executor> class initiate_defer_with_executor { public: typedef Executor executor_type; explicit initiate_defer_with_executor(const Executor& ex) : ex_(ex) { } executor_type get_executor() const noexcept { return ex_; } template <typename CompletionHandler> void operator()(CompletionHandler&& handler, enable_if_t< execution::is_executor< conditional_t<true, executor_type, CompletionHandler> >::value >* = 0, enable_if_t< !detail::is_work_dispatcher_required< decay_t<CompletionHandler>, Executor >::value >* = 0) const { associated_allocator_t<decay_t<CompletionHandler>> alloc( (get_associated_allocator)(handler)); asio::prefer( asio::require(ex_, execution::blocking.never), execution::relationship.continuation, execution::allocator(alloc) ).execute( asio::detail::bind_handler( static_cast<CompletionHandler&&>(handler))); } template <typename CompletionHandler> void operator()(CompletionHandler&& handler, enable_if_t< execution::is_executor< conditional_t<true, executor_type, CompletionHandler> >::value >* = 0, enable_if_t< detail::is_work_dispatcher_required< decay_t<CompletionHandler>, Executor >::value >* = 0) const { typedef decay_t<CompletionHandler> handler_t; typedef associated_executor_t<handler_t, Executor> handler_ex_t; handler_ex_t handler_ex((get_associated_executor)(handler, ex_)); associated_allocator_t<handler_t> alloc( (get_associated_allocator)(handler)); asio::prefer( asio::require(ex_, execution::blocking.never), execution::relationship.continuation, execution::allocator(alloc) ).execute( detail::work_dispatcher<handler_t, handler_ex_t>( static_cast<CompletionHandler&&>(handler), handler_ex)); } template <typename CompletionHandler> void operator()(CompletionHandler&& handler, enable_if_t< !execution::is_executor< conditional_t<true, executor_type, CompletionHandler> >::value >* = 0, enable_if_t< !detail::is_work_dispatcher_required< decay_t<CompletionHandler>, Executor >::value >* = 0) const { associated_allocator_t<decay_t<CompletionHandler>> alloc( (get_associated_allocator)(handler)); ex_.defer(asio::detail::bind_handler( static_cast<CompletionHandler&&>(handler)), alloc); } template <typename CompletionHandler> void operator()(CompletionHandler&& handler, enable_if_t< !execution::is_executor< conditional_t<true, executor_type, CompletionHandler> >::value >* = 0, enable_if_t< detail::is_work_dispatcher_required< decay_t<CompletionHandler>, Executor >::value >* = 0) const { typedef decay_t<CompletionHandler> handler_t; typedef associated_executor_t<handler_t, Executor> handler_ex_t; handler_ex_t handler_ex((get_associated_executor)(handler, ex_)); associated_allocator_t<handler_t> alloc( (get_associated_allocator)(handler)); ex_.defer(detail::work_dispatcher<handler_t, handler_ex_t>( static_cast<CompletionHandler&&>(handler), handler_ex), alloc); } private: Executor ex_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_INITIATE_DEFER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/kqueue_reactor.hpp
// // detail/kqueue_reactor.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) // Copyright (c) 2005 Stefan Arentz (stefan at soze 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_DETAIL_KQUEUE_REACTOR_HPP #define ASIO_DETAIL_KQUEUE_REACTOR_HPP #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_KQUEUE) #include <cstddef> #include <sys/types.h> #include <sys/event.h> #include <sys/time.h> #include "asio/detail/conditionally_enabled_mutex.hpp" #include "asio/detail/limits.hpp" #include "asio/detail/object_pool.hpp" #include "asio/detail/op_queue.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/detail/scheduler_task.hpp" #include "asio/detail/select_interrupter.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/timer_queue_base.hpp" #include "asio/detail/timer_queue_set.hpp" #include "asio/detail/wait_op.hpp" #include "asio/error.hpp" #include "asio/execution_context.hpp" // Older versions of Mac OS X may not define EV_OOBAND. #if !defined(EV_OOBAND) # define EV_OOBAND EV_FLAG1 #endif // !defined(EV_OOBAND) #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class scheduler; class kqueue_reactor : public execution_context_service_base<kqueue_reactor>, public scheduler_task { private: // The mutex type used by this reactor. typedef conditionally_enabled_mutex mutex; public: enum op_types { read_op = 0, write_op = 1, connect_op = 1, except_op = 2, max_ops = 3 }; // Per-descriptor queues. struct descriptor_state { descriptor_state(bool locking) : mutex_(locking) {} friend class kqueue_reactor; friend class object_pool_access; descriptor_state* next_; descriptor_state* prev_; mutex mutex_; int descriptor_; int num_kevents_; // 1 == read only, 2 == read and write op_queue<reactor_op> op_queue_[max_ops]; bool shutdown_; }; // Per-descriptor data. typedef descriptor_state* per_descriptor_data; // Constructor. ASIO_DECL kqueue_reactor(asio::execution_context& ctx); // Destructor. ASIO_DECL ~kqueue_reactor(); // Destroy all user-defined handler objects owned by the service. ASIO_DECL void shutdown(); // Recreate internal descriptors following a fork. ASIO_DECL void notify_fork( asio::execution_context::fork_event fork_ev); // Initialise the task. ASIO_DECL void init_task(); // Register a socket with the reactor. Returns 0 on success, system error // code on failure. ASIO_DECL int register_descriptor(socket_type descriptor, per_descriptor_data& descriptor_data); // Register a descriptor with an associated single operation. Returns 0 on // success, system error code on failure. ASIO_DECL int register_internal_descriptor( int op_type, socket_type descriptor, per_descriptor_data& descriptor_data, reactor_op* op); // Move descriptor registration from one descriptor_data object to another. ASIO_DECL void move_descriptor(socket_type descriptor, per_descriptor_data& target_descriptor_data, per_descriptor_data& source_descriptor_data); // Post a reactor operation for immediate completion. void post_immediate_completion(operation* op, bool is_continuation) const; // Post a reactor operation for immediate completion. ASIO_DECL static void call_post_immediate_completion( operation* op, bool is_continuation, const void* self); // Start a new operation. The reactor operation will be performed when the // given descriptor is flagged as ready, or an error has occurred. ASIO_DECL void start_op(int op_type, socket_type descriptor, per_descriptor_data& descriptor_data, reactor_op* op, bool is_continuation, bool allow_speculative, void (*on_immediate)(operation*, bool, const void*), const void* immediate_arg); // Start a new operation. The reactor operation will be performed when the // given descriptor is flagged as ready, or an error has occurred. void start_op(int op_type, socket_type descriptor, per_descriptor_data& descriptor_data, reactor_op* op, bool is_continuation, bool allow_speculative) { start_op(op_type, descriptor, descriptor_data, op, is_continuation, allow_speculative, &kqueue_reactor::call_post_immediate_completion, this); } // Cancel all operations associated with the given descriptor. The // handlers associated with the descriptor will be invoked with the // operation_aborted error. ASIO_DECL void cancel_ops(socket_type descriptor, per_descriptor_data& descriptor_data); // Cancel all operations associated with the given descriptor and key. The // handlers associated with the descriptor will be invoked with the // operation_aborted error. ASIO_DECL void cancel_ops_by_key(socket_type descriptor, per_descriptor_data& descriptor_data, int op_type, void* cancellation_key); // Cancel any operations that are running against the descriptor and remove // its registration from the reactor. The reactor resources associated with // the descriptor must be released by calling cleanup_descriptor_data. ASIO_DECL void deregister_descriptor(socket_type descriptor, per_descriptor_data& descriptor_data, bool closing); // Remove the descriptor's registration from the reactor. The reactor // resources associated with the descriptor must be released by calling // cleanup_descriptor_data. ASIO_DECL void deregister_internal_descriptor( socket_type descriptor, per_descriptor_data& descriptor_data); // Perform any post-deregistration cleanup tasks associated with the // descriptor data. ASIO_DECL void cleanup_descriptor_data( per_descriptor_data& descriptor_data); // Add a new timer queue to the reactor. template <typename Time_Traits> void add_timer_queue(timer_queue<Time_Traits>& queue); // Remove a timer queue from the reactor. template <typename Time_Traits> void remove_timer_queue(timer_queue<Time_Traits>& queue); // Schedule a new operation in the given timer queue to expire at the // specified absolute time. template <typename Time_Traits> void schedule_timer(timer_queue<Time_Traits>& queue, const typename Time_Traits::time_type& time, typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op); // Cancel the timer operations associated with the given token. Returns the // number of operations that have been posted or dispatched. template <typename Time_Traits> std::size_t cancel_timer(timer_queue<Time_Traits>& queue, typename timer_queue<Time_Traits>::per_timer_data& timer, std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)()); // Cancel the timer operations associated with the given key. template <typename Time_Traits> void cancel_timer_by_key(timer_queue<Time_Traits>& queue, typename timer_queue<Time_Traits>::per_timer_data* timer, void* cancellation_key); // Move the timer operations associated with the given timer. template <typename Time_Traits> void move_timer(timer_queue<Time_Traits>& queue, typename timer_queue<Time_Traits>::per_timer_data& target, typename timer_queue<Time_Traits>::per_timer_data& source); // Run the kqueue loop. ASIO_DECL void run(long usec, op_queue<operation>& ops); // Interrupt the kqueue loop. ASIO_DECL void interrupt(); private: // Create the kqueue file descriptor. Throws an exception if the descriptor // cannot be created. ASIO_DECL static int do_kqueue_create(); // Allocate a new descriptor state object. ASIO_DECL descriptor_state* allocate_descriptor_state(); // Free an existing descriptor state object. ASIO_DECL void free_descriptor_state(descriptor_state* s); // Helper function to add a new timer queue. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue); // Helper function to remove a timer queue. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue); // Get the timeout value for the kevent call. ASIO_DECL timespec* get_timeout(long usec, timespec& ts); // The scheduler used to post completions. scheduler& scheduler_; // Mutex to protect access to internal data. mutex mutex_; // The kqueue file descriptor. int kqueue_fd_; // The interrupter is used to break a blocking kevent call. select_interrupter interrupter_; // The timer queues. timer_queue_set timer_queues_; // Whether the service has been shut down. bool shutdown_; // Mutex to protect access to the registered descriptors. mutex registered_descriptors_mutex_; // Keep track of all registered descriptors. object_pool<descriptor_state> registered_descriptors_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #include "asio/detail/impl/kqueue_reactor.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/kqueue_reactor.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // defined(ASIO_HAS_KQUEUE) #endif // ASIO_DETAIL_KQUEUE_REACTOR_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/timer_queue_base.hpp
// // detail/timer_queue_base.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_TIMER_QUEUE_BASE_HPP #define ASIO_DETAIL_TIMER_QUEUE_BASE_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/noncopyable.hpp" #include "asio/detail/op_queue.hpp" #include "asio/detail/operation.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class timer_queue_base : private noncopyable { public: // Constructor. timer_queue_base() : next_(0) {} // Destructor. virtual ~timer_queue_base() {} // Whether there are no timers in the queue. virtual bool empty() const = 0; // Get the time to wait until the next timer. virtual long wait_duration_msec(long max_duration) const = 0; // Get the time to wait until the next timer. virtual long wait_duration_usec(long max_duration) const = 0; // Dequeue all ready timers. virtual void get_ready_timers(op_queue<operation>& ops) = 0; // Dequeue all timers. virtual void get_all_timers(op_queue<operation>& ops) = 0; private: friend class timer_queue_set; // Next timer queue in the set. timer_queue_base* next_; }; template <typename Time_Traits> class timer_queue; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_TIMER_QUEUE_BASE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_iocp_socket_recv_op.hpp
// // detail/win_iocp_socket_recv_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WIN_IOCP_SOCKET_RECV_OP_HPP #define ASIO_DETAIL_WIN_IOCP_SOCKET_RECV_OP_HPP #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_IOCP) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/operation.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename MutableBufferSequence, typename Handler, typename IoExecutor> class win_iocp_socket_recv_op : public operation { public: ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_recv_op); win_iocp_socket_recv_op(socket_ops::state_type state, socket_ops::weak_cancel_token_type cancel_token, const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : operation(&win_iocp_socket_recv_op::do_complete), state_(state), cancel_token_(cancel_token), buffers_(buffers), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& result_ec, std::size_t bytes_transferred) { asio::error_code ec(result_ec); // Take ownership of the operation object. ASIO_ASSUME(base != 0); win_iocp_socket_recv_op* o(static_cast<win_iocp_socket_recv_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Check whether buffers are still valid. if (owner) { buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence>::validate(o->buffers_); } #endif // defined(ASIO_ENABLE_BUFFER_DEBUGGING) socket_ops::complete_iocp_recv(o->state_, o->cancel_token_, buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence>::all_empty(o->buffers_), ec, bytes_transferred); ASIO_ERROR_LOCATION(ec); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, ec, bytes_transferred); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: socket_ops::state_type state_; socket_ops::weak_cancel_token_type cancel_token_; MutableBufferSequence buffers_; Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IOCP) #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_RECV_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/string_view.hpp
// // detail/string_view.hpp // ~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_STRING_VIEW_HPP #define ASIO_DETAIL_STRING_VIEW_HPP #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_STRING_VIEW) #if defined(ASIO_HAS_STD_STRING_VIEW) # include <string_view> #elif defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW) # include <experimental/string_view> #else // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW) # error ASIO_HAS_STRING_VIEW is set but no string_view is available #endif // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW) namespace asio { #if defined(ASIO_HAS_STD_STRING_VIEW) using std::basic_string_view; using std::string_view; #elif defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW) using std::experimental::basic_string_view; using std::experimental::string_view; #endif // defined(ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW) } // namespace asio # define ASIO_STRING_VIEW_PARAM asio::string_view #else // defined(ASIO_HAS_STRING_VIEW) # define ASIO_STRING_VIEW_PARAM const std::string& #endif // defined(ASIO_HAS_STRING_VIEW) #endif // ASIO_DETAIL_STRING_VIEW_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/winapp_thread.hpp
// // detail/winapp_thread.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WINAPP_THREAD_HPP #define ASIO_DETAIL_WINAPP_THREAD_HPP #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(ASIO_WINDOWS_APP) #include "asio/detail/noncopyable.hpp" #include "asio/detail/scoped_ptr.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/throw_error.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { DWORD WINAPI winapp_thread_function(LPVOID arg); class winapp_thread : private noncopyable { public: // Constructor. template <typename Function> winapp_thread(Function f, unsigned int = 0) { scoped_ptr<func_base> arg(new func<Function>(f)); DWORD thread_id = 0; thread_ = ::CreateThread(0, 0, winapp_thread_function, arg.get(), 0, &thread_id); if (!thread_) { DWORD last_error = ::GetLastError(); asio::error_code ec(last_error, asio::error::get_system_category()); asio::detail::throw_error(ec, "thread"); } arg.release(); } // Destructor. ~winapp_thread() { ::CloseHandle(thread_); } // Wait for the thread to exit. void join() { ::WaitForSingleObjectEx(thread_, INFINITE, false); } // Get number of CPUs. static std::size_t hardware_concurrency() { SYSTEM_INFO system_info; ::GetNativeSystemInfo(&system_info); return system_info.dwNumberOfProcessors; } private: friend DWORD WINAPI winapp_thread_function(LPVOID arg); class func_base { public: virtual ~func_base() {} virtual void run() = 0; }; template <typename Function> class func : public func_base { public: func(Function f) : f_(f) { } virtual void run() { f_(); } private: Function f_; }; ::HANDLE thread_; }; inline DWORD WINAPI winapp_thread_function(LPVOID arg) { scoped_ptr<winapp_thread::func_base> func( static_cast<winapp_thread::func_base*>(arg)); func->run(); return 0; } } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_WINDOWS) && defined(ASIO_WINDOWS_APP) #endif // ASIO_DETAIL_WINAPP_THREAD_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/descriptor_ops.hpp
// // detail/descriptor_ops.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_DESCRIPTOR_OPS_HPP #define ASIO_DETAIL_DESCRIPTOR_OPS_HPP #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(ASIO_WINDOWS_RUNTIME) \ && !defined(__CYGWIN__) #include <cstddef> #include "asio/error.hpp" #include "asio/error_code.hpp" #include "asio/detail/cstdint.hpp" #include "asio/detail/socket_types.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { namespace descriptor_ops { // Descriptor state bits. enum { // The user wants a non-blocking descriptor. user_set_non_blocking = 1, // The descriptor has been set non-blocking. internal_non_blocking = 2, // Helper "state" used to determine whether the descriptor is non-blocking. non_blocking = user_set_non_blocking | internal_non_blocking, // The descriptor may have been dup()-ed. possible_dup = 4 }; typedef unsigned char state_type; inline void get_last_error( asio::error_code& ec, bool is_error_condition) { if (!is_error_condition) { asio::error::clear(ec); } else { ec = asio::error_code(errno, asio::error::get_system_category()); } } ASIO_DECL int open(const char* path, int flags, asio::error_code& ec); ASIO_DECL int open(const char* path, int flags, unsigned mode, asio::error_code& ec); ASIO_DECL int close(int d, state_type& state, asio::error_code& ec); ASIO_DECL bool set_user_non_blocking(int d, state_type& state, bool value, asio::error_code& ec); ASIO_DECL bool set_internal_non_blocking(int d, state_type& state, bool value, asio::error_code& ec); typedef iovec buf; ASIO_DECL std::size_t sync_read(int d, state_type state, buf* bufs, std::size_t count, bool all_empty, asio::error_code& ec); ASIO_DECL std::size_t sync_read1(int d, state_type state, void* data, std::size_t size, asio::error_code& ec); ASIO_DECL bool non_blocking_read(int d, buf* bufs, std::size_t count, asio::error_code& ec, std::size_t& bytes_transferred); ASIO_DECL bool non_blocking_read1(int d, void* data, std::size_t size, asio::error_code& ec, std::size_t& bytes_transferred); ASIO_DECL std::size_t sync_write(int d, state_type state, const buf* bufs, std::size_t count, bool all_empty, asio::error_code& ec); ASIO_DECL std::size_t sync_write1(int d, state_type state, const void* data, std::size_t size, asio::error_code& ec); ASIO_DECL bool non_blocking_write(int d, const buf* bufs, std::size_t count, asio::error_code& ec, std::size_t& bytes_transferred); ASIO_DECL bool non_blocking_write1(int d, const void* data, std::size_t size, asio::error_code& ec, std::size_t& bytes_transferred); #if defined(ASIO_HAS_FILE) ASIO_DECL std::size_t sync_read_at(int d, state_type state, uint64_t offset, buf* bufs, std::size_t count, bool all_empty, asio::error_code& ec); ASIO_DECL std::size_t sync_read_at1(int d, state_type state, uint64_t offset, void* data, std::size_t size, asio::error_code& ec); ASIO_DECL bool non_blocking_read_at(int d, uint64_t offset, buf* bufs, std::size_t count, asio::error_code& ec, std::size_t& bytes_transferred); ASIO_DECL bool non_blocking_read_at1(int d, uint64_t offset, void* data, std::size_t size, asio::error_code& ec, std::size_t& bytes_transferred); ASIO_DECL std::size_t sync_write_at(int d, state_type state, uint64_t offset, const buf* bufs, std::size_t count, bool all_empty, asio::error_code& ec); ASIO_DECL std::size_t sync_write_at1(int d, state_type state, uint64_t offset, const void* data, std::size_t size, asio::error_code& ec); ASIO_DECL bool non_blocking_write_at(int d, uint64_t offset, const buf* bufs, std::size_t count, asio::error_code& ec, std::size_t& bytes_transferred); ASIO_DECL bool non_blocking_write_at1(int d, uint64_t offset, const void* data, std::size_t size, asio::error_code& ec, std::size_t& bytes_transferred); #endif // defined(ASIO_HAS_FILE) ASIO_DECL int ioctl(int d, state_type& state, long cmd, ioctl_arg_type* arg, asio::error_code& ec); ASIO_DECL int fcntl(int d, int cmd, asio::error_code& ec); ASIO_DECL int fcntl(int d, int cmd, long arg, asio::error_code& ec); ASIO_DECL int poll_read(int d, state_type state, asio::error_code& ec); ASIO_DECL int poll_write(int d, state_type state, asio::error_code& ec); ASIO_DECL int poll_error(int d, state_type state, asio::error_code& ec); } // namespace descriptor_ops } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/descriptor_ops.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // !defined(ASIO_WINDOWS) // && !defined(ASIO_WINDOWS_RUNTIME) // && !defined(__CYGWIN__) #endif // ASIO_DETAIL_DESCRIPTOR_OPS_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/null_tss_ptr.hpp
// // detail/null_tss_ptr.hpp // ~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_NULL_TSS_PTR_HPP #define ASIO_DETAIL_NULL_TSS_PTR_HPP #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_THREADS) #include "asio/detail/noncopyable.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename T> class null_tss_ptr : private noncopyable { public: // Constructor. null_tss_ptr() : value_(0) { } // Destructor. ~null_tss_ptr() { } // Get the value. operator T*() const { return value_; } // Set the value. void operator=(T* value) { value_ = value; } private: T* value_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // !defined(ASIO_HAS_THREADS) #endif // ASIO_DETAIL_NULL_TSS_PTR_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/global.hpp
// // detail/global.hpp // ~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_GLOBAL_HPP #define ASIO_DETAIL_GLOBAL_HPP #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_THREADS) # include "asio/detail/null_global.hpp" #elif defined(ASIO_WINDOWS) # include "asio/detail/win_global.hpp" #elif defined(ASIO_HAS_PTHREADS) # include "asio/detail/posix_global.hpp" #else # include "asio/detail/std_global.hpp" #endif namespace asio { namespace detail { template <typename T> inline T& global() { #if !defined(ASIO_HAS_THREADS) return null_global<T>(); #elif defined(ASIO_WINDOWS) return win_global<T>(); #elif defined(ASIO_HAS_PTHREADS) return posix_global<T>(); #else return std_global<T>(); #endif } } // namespace detail } // namespace asio #endif // ASIO_DETAIL_GLOBAL_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/io_uring_descriptor_write_at_op.hpp
// // detail/io_uring_descriptor_write_at_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_IO_URING_DESCRIPTOR_WRITE_AT_OP_HPP #define ASIO_DETAIL_IO_URING_DESCRIPTOR_WRITE_AT_OP_HPP #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_IO_URING) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/descriptor_ops.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/io_uring_operation.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename ConstBufferSequence> class io_uring_descriptor_write_at_op_base : public io_uring_operation { public: io_uring_descriptor_write_at_op_base( const asio::error_code& success_ec, int descriptor, descriptor_ops::state_type state, uint64_t offset, const ConstBufferSequence& buffers, func_type complete_func) : io_uring_operation(success_ec, &io_uring_descriptor_write_at_op_base::do_prepare, &io_uring_descriptor_write_at_op_base::do_perform, complete_func), descriptor_(descriptor), state_(state), offset_(offset), buffers_(buffers), bufs_(buffers) { } static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe) { ASIO_ASSUME(base != 0); io_uring_descriptor_write_at_op_base* o( static_cast<io_uring_descriptor_write_at_op_base*>(base)); if ((o->state_ & descriptor_ops::internal_non_blocking) != 0) { ::io_uring_prep_poll_add(sqe, o->descriptor_, POLLOUT); } else if (o->bufs_.is_single_buffer && o->bufs_.is_registered_buffer) { ::io_uring_prep_write_fixed(sqe, o->descriptor_, o->bufs_.buffers()->iov_base, o->bufs_.buffers()->iov_len, o->offset_, o->bufs_.registered_id().native_handle()); } else { ::io_uring_prep_writev(sqe, o->descriptor_, o->bufs_.buffers(), o->bufs_.count(), o->offset_); } } static bool do_perform(io_uring_operation* base, bool after_completion) { ASIO_ASSUME(base != 0); io_uring_descriptor_write_at_op_base* o( static_cast<io_uring_descriptor_write_at_op_base*>(base)); if ((o->state_ & descriptor_ops::internal_non_blocking) != 0) { if (o->bufs_.is_single_buffer) { return descriptor_ops::non_blocking_write_at1(o->descriptor_, o->offset_, o->bufs_.first(o->buffers_).data(), o->bufs_.first(o->buffers_).size(), o->ec_, o->bytes_transferred_); } else { return descriptor_ops::non_blocking_write_at(o->descriptor_, o->offset_, o->bufs_.buffers(), o->bufs_.count(), o->ec_, o->bytes_transferred_); } } if (o->ec_ && o->ec_ == asio::error::would_block) { o->state_ |= descriptor_ops::internal_non_blocking; return false; } return after_completion; } private: int descriptor_; descriptor_ops::state_type state_; uint64_t offset_; ConstBufferSequence buffers_; buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence> bufs_; }; template <typename ConstBufferSequence, typename Handler, typename IoExecutor> class io_uring_descriptor_write_at_op : public io_uring_descriptor_write_at_op_base<ConstBufferSequence> { public: ASIO_DEFINE_HANDLER_PTR(io_uring_descriptor_write_at_op); io_uring_descriptor_write_at_op(const asio::error_code& success_ec, int descriptor, descriptor_ops::state_type state, uint64_t offset, const ConstBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : io_uring_descriptor_write_at_op_base<ConstBufferSequence>( success_ec, descriptor, state, offset, buffers, &io_uring_descriptor_write_at_op::do_complete), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); io_uring_descriptor_write_at_op* o (static_cast<io_uring_descriptor_write_at_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->bytes_transferred_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IO_URING) #endif // ASIO_DETAIL_IO_URING_DESCRIPTOR_WRITE_AT_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/noncopyable.hpp
// // detail/noncopyable.hpp // ~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_NONCOPYABLE_HPP #define ASIO_DETAIL_NONCOPYABLE_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/push_options.hpp" namespace asio { namespace detail { class noncopyable { protected: noncopyable() {} ~noncopyable() {} private: noncopyable(const noncopyable&); const noncopyable& operator=(const noncopyable&); }; } // namespace detail using asio::detail::noncopyable; } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_NONCOPYABLE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/non_const_lvalue.hpp
// // detail/non_const_lvalue.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_NON_CONST_LVALUE_HPP #define ASIO_DETAIL_NON_CONST_LVALUE_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/type_traits.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename T> struct non_const_lvalue { explicit non_const_lvalue(T& t) : value(static_cast<conditional_t< is_same<T, decay_t<T>>::value, decay_t<T>&, T&&>>(t)) { } conditional_t<is_same<T, decay_t<T>>::value, decay_t<T>&, decay_t<T>> value; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_NON_CONST_LVALUE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/buffered_stream_storage.hpp
// // detail/buffered_stream_storage.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_BUFFERED_STREAM_STORAGE_HPP #define ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/buffer.hpp" #include "asio/detail/assert.hpp" #include <cstddef> #include <cstring> #include <vector> #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class buffered_stream_storage { public: // The type of the bytes stored in the buffer. typedef unsigned char byte_type; // The type used for offsets into the buffer. typedef std::size_t size_type; // Constructor. explicit buffered_stream_storage(std::size_t buffer_capacity) : begin_offset_(0), end_offset_(0), buffer_(buffer_capacity) { } /// Clear the buffer. void clear() { begin_offset_ = 0; end_offset_ = 0; } // Return a pointer to the beginning of the unread data. mutable_buffer data() { return asio::buffer(buffer_) + begin_offset_; } // Return a pointer to the beginning of the unread data. const_buffer data() const { return asio::buffer(buffer_) + begin_offset_; } // Is there no unread data in the buffer. bool empty() const { return begin_offset_ == end_offset_; } // Return the amount of unread data the is in the buffer. size_type size() const { return end_offset_ - begin_offset_; } // Resize the buffer to the specified length. void resize(size_type length) { ASIO_ASSERT(length <= capacity()); if (begin_offset_ + length <= capacity()) { end_offset_ = begin_offset_ + length; } else { using namespace std; // For memmove. memmove(&buffer_[0], &buffer_[0] + begin_offset_, size()); end_offset_ = length; begin_offset_ = 0; } } // Return the maximum size for data in the buffer. size_type capacity() const { return buffer_.size(); } // Consume multiple bytes from the beginning of the buffer. void consume(size_type count) { ASIO_ASSERT(begin_offset_ + count <= end_offset_); begin_offset_ += count; if (empty()) clear(); } private: // The offset to the beginning of the unread data. size_type begin_offset_; // The offset to the end of the unread data. size_type end_offset_; // The data in the buffer. std::vector<byte_type> buffer_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/old_win_sdk_compat.hpp
// // detail/old_win_sdk_compat.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_OLD_WIN_SDK_COMPAT_HPP #define ASIO_DETAIL_OLD_WIN_SDK_COMPAT_HPP #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__) // Guess whether we are building against on old Platform SDK. #if !defined(IN6ADDR_ANY_INIT) #define ASIO_HAS_OLD_WIN_SDK 1 #endif // !defined(IN6ADDR_ANY_INIT) #if defined(ASIO_HAS_OLD_WIN_SDK) // Emulation of types that are missing from old Platform SDKs. // // N.B. this emulation is also used if building for a Windows 2000 target with // a recent (i.e. Vista or later) SDK, as the SDK does not provide IPv6 support // in that case. #include "asio/detail/push_options.hpp" namespace asio { namespace detail { enum { sockaddr_storage_maxsize = 128, // Maximum size. sockaddr_storage_alignsize = (sizeof(__int64)), // Desired alignment. sockaddr_storage_pad1size = (sockaddr_storage_alignsize - sizeof(short)), sockaddr_storage_pad2size = (sockaddr_storage_maxsize - (sizeof(short) + sockaddr_storage_pad1size + sockaddr_storage_alignsize)) }; struct sockaddr_storage_emulation { short ss_family; char __ss_pad1[sockaddr_storage_pad1size]; __int64 __ss_align; char __ss_pad2[sockaddr_storage_pad2size]; }; struct in6_addr_emulation { union { u_char Byte[16]; u_short Word[8]; } u; }; #if !defined(s6_addr) # define _S6_un u # define _S6_u8 Byte # define s6_addr _S6_un._S6_u8 #endif // !defined(s6_addr) struct sockaddr_in6_emulation { short sin6_family; u_short sin6_port; u_long sin6_flowinfo; in6_addr_emulation sin6_addr; u_long sin6_scope_id; }; struct ipv6_mreq_emulation { in6_addr_emulation ipv6mr_multiaddr; unsigned int ipv6mr_interface; }; struct addrinfo_emulation { int ai_flags; int ai_family; int ai_socktype; int ai_protocol; size_t ai_addrlen; char* ai_canonname; sockaddr* ai_addr; addrinfo_emulation* ai_next; }; #if !defined(AI_PASSIVE) # define AI_PASSIVE 0x1 #endif #if !defined(AI_CANONNAME) # define AI_CANONNAME 0x2 #endif #if !defined(AI_NUMERICHOST) # define AI_NUMERICHOST 0x4 #endif #if !defined(EAI_AGAIN) # define EAI_AGAIN WSATRY_AGAIN #endif #if !defined(EAI_BADFLAGS) # define EAI_BADFLAGS WSAEINVAL #endif #if !defined(EAI_FAIL) # define EAI_FAIL WSANO_RECOVERY #endif #if !defined(EAI_FAMILY) # define EAI_FAMILY WSAEAFNOSUPPORT #endif #if !defined(EAI_MEMORY) # define EAI_MEMORY WSA_NOT_ENOUGH_MEMORY #endif #if !defined(EAI_NODATA) # define EAI_NODATA WSANO_DATA #endif #if !defined(EAI_NONAME) # define EAI_NONAME WSAHOST_NOT_FOUND #endif #if !defined(EAI_SERVICE) # define EAI_SERVICE WSATYPE_NOT_FOUND #endif #if !defined(EAI_SOCKTYPE) # define EAI_SOCKTYPE WSAESOCKTNOSUPPORT #endif #if !defined(NI_NOFQDN) # define NI_NOFQDN 0x01 #endif #if !defined(NI_NUMERICHOST) # define NI_NUMERICHOST 0x02 #endif #if !defined(NI_NAMEREQD) # define NI_NAMEREQD 0x04 #endif #if !defined(NI_NUMERICSERV) # define NI_NUMERICSERV 0x08 #endif #if !defined(NI_DGRAM) # define NI_DGRAM 0x10 #endif #if !defined(IPPROTO_IPV6) # define IPPROTO_IPV6 41 #endif #if !defined(IPV6_UNICAST_HOPS) # define IPV6_UNICAST_HOPS 4 #endif #if !defined(IPV6_MULTICAST_IF) # define IPV6_MULTICAST_IF 9 #endif #if !defined(IPV6_MULTICAST_HOPS) # define IPV6_MULTICAST_HOPS 10 #endif #if !defined(IPV6_MULTICAST_LOOP) # define IPV6_MULTICAST_LOOP 11 #endif #if !defined(IPV6_JOIN_GROUP) # define IPV6_JOIN_GROUP 12 #endif #if !defined(IPV6_LEAVE_GROUP) # define IPV6_LEAVE_GROUP 13 #endif } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_OLD_WIN_SDK) // Even newer Platform SDKs that support IPv6 may not define IPV6_V6ONLY. #if !defined(IPV6_V6ONLY) # define IPV6_V6ONLY 27 #endif // Some SDKs (e.g. Windows CE) don't define IPPROTO_ICMPV6. #if !defined(IPPROTO_ICMPV6) # define IPPROTO_ICMPV6 58 #endif #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__) #endif // ASIO_DETAIL_OLD_WIN_SDK_COMPAT_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_iocp_socket_send_op.hpp
// // detail/win_iocp_socket_send_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WIN_IOCP_SOCKET_SEND_OP_HPP #define ASIO_DETAIL_WIN_IOCP_SOCKET_SEND_OP_HPP #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_IOCP) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/operation.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename ConstBufferSequence, typename Handler, typename IoExecutor> class win_iocp_socket_send_op : public operation { public: ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_send_op); win_iocp_socket_send_op(socket_ops::weak_cancel_token_type cancel_token, const ConstBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : operation(&win_iocp_socket_send_op::do_complete), cancel_token_(cancel_token), buffers_(buffers), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& result_ec, std::size_t bytes_transferred) { asio::error_code ec(result_ec); // Take ownership of the operation object. ASIO_ASSUME(base != 0); win_iocp_socket_send_op* o(static_cast<win_iocp_socket_send_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Check whether buffers are still valid. if (owner) { buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence>::validate(o->buffers_); } #endif // defined(ASIO_ENABLE_BUFFER_DEBUGGING) socket_ops::complete_iocp_send(o->cancel_token_, ec); ASIO_ERROR_LOCATION(ec); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, ec, bytes_transferred); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: socket_ops::weak_cancel_token_type cancel_token_; ConstBufferSequence buffers_; Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IOCP) #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_SEND_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_iocp_handle_write_op.hpp
// // detail/win_iocp_handle_write_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com) // Copyright (c) 2008 Rep Invariant Systems, Inc. ([email protected]) // // 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_DETAIL_WIN_IOCP_HANDLE_WRITE_OP_HPP #define ASIO_DETAIL_WIN_IOCP_HANDLE_WRITE_OP_HPP #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_IOCP) #include "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/operation.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename ConstBufferSequence, typename Handler, typename IoExecutor> class win_iocp_handle_write_op : public operation { public: ASIO_DEFINE_HANDLER_PTR(win_iocp_handle_write_op); win_iocp_handle_write_op(const ConstBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : operation(&win_iocp_handle_write_op::do_complete), buffers_(buffers), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& result_ec, std::size_t bytes_transferred) { asio::error_code ec(result_ec); // Take ownership of the operation object. ASIO_ASSUME(base != 0); win_iocp_handle_write_op* o(static_cast<win_iocp_handle_write_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); #if defined(ASIO_ENABLE_BUFFER_DEBUGGING) if (owner) { // Check whether buffers are still valid. buffer_sequence_adapter<asio::const_buffer, ConstBufferSequence>::validate(o->buffers_); } #endif // defined(ASIO_ENABLE_BUFFER_DEBUGGING) ASIO_ERROR_LOCATION(ec); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, ec, bytes_transferred); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: ConstBufferSequence buffers_; Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IOCP) #endif // ASIO_DETAIL_WIN_IOCP_HANDLE_WRITE_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_iocp_overlapped_op.hpp
// // detail/win_iocp_overlapped_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WIN_IOCP_OVERLAPPED_OP_HPP #define ASIO_DETAIL_WIN_IOCP_OVERLAPPED_OP_HPP #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_IOCP) #include "asio/detail/bind_handler.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/operation.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Handler, typename IoExecutor> class win_iocp_overlapped_op : public operation { public: ASIO_DEFINE_HANDLER_PTR(win_iocp_overlapped_op); win_iocp_overlapped_op(Handler& handler, const IoExecutor& io_ex) : operation(&win_iocp_overlapped_op::do_complete), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& result_ec, std::size_t bytes_transferred) { asio::error_code ec(result_ec); // Take ownership of the operation object. ASIO_ASSUME(base != 0); win_iocp_overlapped_op* o(static_cast<win_iocp_overlapped_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(ec); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, ec, bytes_transferred); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IOCP) #endif // ASIO_DETAIL_WIN_IOCP_OVERLAPPED_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/service_registry.hpp
// // detail/service_registry.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_SERVICE_REGISTRY_HPP #define ASIO_DETAIL_SERVICE_REGISTRY_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <typeinfo> #include "asio/detail/mutex.hpp" #include "asio/detail/noncopyable.hpp" #include "asio/detail/type_traits.hpp" #include "asio/execution_context.hpp" #include "asio/detail/push_options.hpp" namespace asio { class io_context; namespace detail { template <typename T> class typeid_wrapper {}; class service_registry : private noncopyable { public: // Constructor. ASIO_DECL service_registry(execution_context& owner); // Destructor. ASIO_DECL ~service_registry(); // Shutdown all services. ASIO_DECL void shutdown_services(); // Destroy all services. ASIO_DECL void destroy_services(); // Notify all services of a fork event. ASIO_DECL void notify_fork(execution_context::fork_event fork_ev); // Get the service object corresponding to the specified service type. Will // create a new service object automatically if no such object already // exists. Ownership of the service object is not transferred to the caller. template <typename Service> Service& use_service(); // Get the service object corresponding to the specified service type. Will // create a new service object automatically if no such object already // exists. Ownership of the service object is not transferred to the caller. // This overload is used for backwards compatibility with services that // inherit from io_context::service. template <typename Service> Service& use_service(io_context& owner); // Add a service object. Throws on error, in which case ownership of the // object is retained by the caller. template <typename Service> void add_service(Service* new_service); // Check whether a service object of the specified type already exists. template <typename Service> bool has_service() const; private: // Initalise a service's key when the key_type typedef is not available. template <typename Service> static void init_key(execution_context::service::key& key, ...); #if !defined(ASIO_NO_TYPEID) // Initalise a service's key when the key_type typedef is available. template <typename Service> static void init_key(execution_context::service::key& key, enable_if_t<is_base_of<typename Service::key_type, Service>::value>*); #endif // !defined(ASIO_NO_TYPEID) // Initialise a service's key based on its id. ASIO_DECL static void init_key_from_id( execution_context::service::key& key, const execution_context::id& id); #if !defined(ASIO_NO_TYPEID) // Initialise a service's key based on its id. template <typename Service> static void init_key_from_id(execution_context::service::key& key, const service_id<Service>& /*id*/); #endif // !defined(ASIO_NO_TYPEID) // Check if a service matches the given id. ASIO_DECL static bool keys_match( const execution_context::service::key& key1, const execution_context::service::key& key2); // The type of a factory function used for creating a service instance. typedef execution_context::service*(*factory_type)(void*); // Factory function for creating a service instance. template <typename Service, typename Owner> static execution_context::service* create(void* owner); // Destroy a service instance. ASIO_DECL static void destroy(execution_context::service* service); // Helper class to manage service pointers. struct auto_service_ptr; friend struct auto_service_ptr; struct auto_service_ptr { execution_context::service* ptr_; ~auto_service_ptr() { destroy(ptr_); } }; // Get the service object corresponding to the specified service key. Will // create a new service object automatically if no such object already // exists. Ownership of the service object is not transferred to the caller. ASIO_DECL execution_context::service* do_use_service( const execution_context::service::key& key, factory_type factory, void* owner); // Add a service object. Throws on error, in which case ownership of the // object is retained by the caller. ASIO_DECL void do_add_service( const execution_context::service::key& key, execution_context::service* new_service); // Check whether a service object with the specified key already exists. ASIO_DECL bool do_has_service( const execution_context::service::key& key) const; // Mutex to protect access to internal data. mutable asio::detail::mutex mutex_; // The owner of this service registry and the services it contains. execution_context& owner_; // The first service in the list of contained services. execution_context::service* first_service_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #include "asio/detail/impl/service_registry.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/service_registry.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // ASIO_DETAIL_SERVICE_REGISTRY_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/buffer_sequence_adapter.hpp
// // detail/buffer_sequence_adapter.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP #define ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/buffer.hpp" #include "asio/detail/array_fwd.hpp" #include "asio/detail/socket_types.hpp" #include "asio/registered_buffer.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class buffer_sequence_adapter_base { #if defined(ASIO_WINDOWS_RUNTIME) public: // The maximum number of buffers to support in a single operation. enum { max_buffers = 1 }; protected: typedef Windows::Storage::Streams::IBuffer^ native_buffer_type; ASIO_DECL static void init_native_buffer( native_buffer_type& buf, const asio::mutable_buffer& buffer); ASIO_DECL static void init_native_buffer( native_buffer_type& buf, const asio::const_buffer& buffer); #elif defined(ASIO_WINDOWS) || defined(__CYGWIN__) public: // The maximum number of buffers to support in a single operation. enum { max_buffers = 64 < max_iov_len ? 64 : max_iov_len }; protected: typedef WSABUF native_buffer_type; static void init_native_buffer(WSABUF& buf, const asio::mutable_buffer& buffer) { buf.buf = static_cast<char*>(buffer.data()); buf.len = static_cast<ULONG>(buffer.size()); } static void init_native_buffer(WSABUF& buf, const asio::const_buffer& buffer) { buf.buf = const_cast<char*>(static_cast<const char*>(buffer.data())); buf.len = static_cast<ULONG>(buffer.size()); } #else // defined(ASIO_WINDOWS) || defined(__CYGWIN__) public: // The maximum number of buffers to support in a single operation. enum { max_buffers = 64 < max_iov_len ? 64 : max_iov_len }; protected: typedef iovec native_buffer_type; static void init_iov_base(void*& base, void* addr) { base = addr; } template <typename T> static void init_iov_base(T& base, void* addr) { base = static_cast<T>(addr); } static void init_native_buffer(iovec& iov, const asio::mutable_buffer& buffer) { init_iov_base(iov.iov_base, buffer.data()); iov.iov_len = buffer.size(); } static void init_native_buffer(iovec& iov, const asio::const_buffer& buffer) { init_iov_base(iov.iov_base, const_cast<void*>(buffer.data())); iov.iov_len = buffer.size(); } #endif // defined(ASIO_WINDOWS) || defined(__CYGWIN__) }; // Helper class to translate buffers into the native buffer representation. template <typename Buffer, typename Buffers> class buffer_sequence_adapter : buffer_sequence_adapter_base { public: enum { is_single_buffer = false }; enum { is_registered_buffer = false }; explicit buffer_sequence_adapter(const Buffers& buffer_sequence) : count_(0), total_buffer_size_(0) { buffer_sequence_adapter::init( asio::buffer_sequence_begin(buffer_sequence), asio::buffer_sequence_end(buffer_sequence)); } native_buffer_type* buffers() { return buffers_; } std::size_t count() const { return count_; } std::size_t total_size() const { return total_buffer_size_; } registered_buffer_id registered_id() const { return registered_buffer_id(); } bool all_empty() const { return total_buffer_size_ == 0; } static bool all_empty(const Buffers& buffer_sequence) { return buffer_sequence_adapter::all_empty( asio::buffer_sequence_begin(buffer_sequence), asio::buffer_sequence_end(buffer_sequence)); } static void validate(const Buffers& buffer_sequence) { buffer_sequence_adapter::validate( asio::buffer_sequence_begin(buffer_sequence), asio::buffer_sequence_end(buffer_sequence)); } static Buffer first(const Buffers& buffer_sequence) { return buffer_sequence_adapter::first( asio::buffer_sequence_begin(buffer_sequence), asio::buffer_sequence_end(buffer_sequence)); } enum { linearisation_storage_size = 8192 }; static Buffer linearise(const Buffers& buffer_sequence, const asio::mutable_buffer& storage) { return buffer_sequence_adapter::linearise( asio::buffer_sequence_begin(buffer_sequence), asio::buffer_sequence_end(buffer_sequence), storage); } private: template <typename Iterator> void init(Iterator begin, Iterator end) { Iterator iter = begin; for (; iter != end && count_ < max_buffers; ++iter, ++count_) { Buffer buffer(*iter); init_native_buffer(buffers_[count_], buffer); total_buffer_size_ += buffer.size(); } } template <typename Iterator> static bool all_empty(Iterator begin, Iterator end) { Iterator iter = begin; std::size_t i = 0; for (; iter != end && i < max_buffers; ++iter, ++i) if (Buffer(*iter).size() > 0) return false; return true; } template <typename Iterator> static void validate(Iterator begin, Iterator end) { Iterator iter = begin; for (; iter != end; ++iter) { Buffer buffer(*iter); buffer.data(); } } template <typename Iterator> static Buffer first(Iterator begin, Iterator end) { Iterator iter = begin; for (; iter != end; ++iter) { Buffer buffer(*iter); if (buffer.size() != 0) return buffer; } return Buffer(); } template <typename Iterator> static Buffer linearise(Iterator begin, Iterator end, const asio::mutable_buffer& storage) { asio::mutable_buffer unused_storage = storage; Iterator iter = begin; while (iter != end && unused_storage.size() != 0) { Buffer buffer(*iter); ++iter; if (buffer.size() == 0) continue; if (unused_storage.size() == storage.size()) { if (iter == end) return buffer; if (buffer.size() >= unused_storage.size()) return buffer; } unused_storage += asio::buffer_copy(unused_storage, buffer); } return Buffer(storage.data(), storage.size() - unused_storage.size()); } native_buffer_type buffers_[max_buffers]; std::size_t count_; std::size_t total_buffer_size_; }; template <typename Buffer> class buffer_sequence_adapter<Buffer, asio::mutable_buffer> : buffer_sequence_adapter_base { public: enum { is_single_buffer = true }; enum { is_registered_buffer = false }; explicit buffer_sequence_adapter( const asio::mutable_buffer& buffer_sequence) { init_native_buffer(buffer_, Buffer(buffer_sequence)); total_buffer_size_ = buffer_sequence.size(); } native_buffer_type* buffers() { return &buffer_; } std::size_t count() const { return 1; } std::size_t total_size() const { return total_buffer_size_; } registered_buffer_id registered_id() const { return registered_buffer_id(); } bool all_empty() const { return total_buffer_size_ == 0; } static bool all_empty(const asio::mutable_buffer& buffer_sequence) { return buffer_sequence.size() == 0; } static void validate(const asio::mutable_buffer& buffer_sequence) { buffer_sequence.data(); } static Buffer first(const asio::mutable_buffer& buffer_sequence) { return Buffer(buffer_sequence); } enum { linearisation_storage_size = 1 }; static Buffer linearise(const asio::mutable_buffer& buffer_sequence, const Buffer&) { return Buffer(buffer_sequence); } private: native_buffer_type buffer_; std::size_t total_buffer_size_; }; template <typename Buffer> class buffer_sequence_adapter<Buffer, asio::const_buffer> : buffer_sequence_adapter_base { public: enum { is_single_buffer = true }; enum { is_registered_buffer = false }; explicit buffer_sequence_adapter( const asio::const_buffer& buffer_sequence) { init_native_buffer(buffer_, Buffer(buffer_sequence)); total_buffer_size_ = buffer_sequence.size(); } native_buffer_type* buffers() { return &buffer_; } std::size_t count() const { return 1; } std::size_t total_size() const { return total_buffer_size_; } registered_buffer_id registered_id() const { return registered_buffer_id(); } bool all_empty() const { return total_buffer_size_ == 0; } static bool all_empty(const asio::const_buffer& buffer_sequence) { return buffer_sequence.size() == 0; } static void validate(const asio::const_buffer& buffer_sequence) { buffer_sequence.data(); } static Buffer first(const asio::const_buffer& buffer_sequence) { return Buffer(buffer_sequence); } enum { linearisation_storage_size = 1 }; static Buffer linearise(const asio::const_buffer& buffer_sequence, const Buffer&) { return Buffer(buffer_sequence); } private: native_buffer_type buffer_; std::size_t total_buffer_size_; }; #if !defined(ASIO_NO_DEPRECATED) template <typename Buffer> class buffer_sequence_adapter<Buffer, asio::mutable_buffers_1> : buffer_sequence_adapter_base { public: enum { is_single_buffer = true }; enum { is_registered_buffer = false }; explicit buffer_sequence_adapter( const asio::mutable_buffers_1& buffer_sequence) { init_native_buffer(buffer_, Buffer(buffer_sequence)); total_buffer_size_ = buffer_sequence.size(); } native_buffer_type* buffers() { return &buffer_; } std::size_t count() const { return 1; } std::size_t total_size() const { return total_buffer_size_; } registered_buffer_id registered_id() const { return registered_buffer_id(); } bool all_empty() const { return total_buffer_size_ == 0; } static bool all_empty(const asio::mutable_buffers_1& buffer_sequence) { return buffer_sequence.size() == 0; } static void validate(const asio::mutable_buffers_1& buffer_sequence) { buffer_sequence.data(); } static Buffer first(const asio::mutable_buffers_1& buffer_sequence) { return Buffer(buffer_sequence); } enum { linearisation_storage_size = 1 }; static Buffer linearise(const asio::mutable_buffers_1& buffer_sequence, const Buffer&) { return Buffer(buffer_sequence); } private: native_buffer_type buffer_; std::size_t total_buffer_size_; }; template <typename Buffer> class buffer_sequence_adapter<Buffer, asio::const_buffers_1> : buffer_sequence_adapter_base { public: enum { is_single_buffer = true }; enum { is_registered_buffer = false }; explicit buffer_sequence_adapter( const asio::const_buffers_1& buffer_sequence) { init_native_buffer(buffer_, Buffer(buffer_sequence)); total_buffer_size_ = buffer_sequence.size(); } native_buffer_type* buffers() { return &buffer_; } std::size_t count() const { return 1; } std::size_t total_size() const { return total_buffer_size_; } registered_buffer_id registered_id() const { return registered_buffer_id(); } bool all_empty() const { return total_buffer_size_ == 0; } static bool all_empty(const asio::const_buffers_1& buffer_sequence) { return buffer_sequence.size() == 0; } static void validate(const asio::const_buffers_1& buffer_sequence) { buffer_sequence.data(); } static Buffer first(const asio::const_buffers_1& buffer_sequence) { return Buffer(buffer_sequence); } enum { linearisation_storage_size = 1 }; static Buffer linearise(const asio::const_buffers_1& buffer_sequence, const Buffer&) { return Buffer(buffer_sequence); } private: native_buffer_type buffer_; std::size_t total_buffer_size_; }; #endif // !defined(ASIO_NO_DEPRECATED) template <typename Buffer> class buffer_sequence_adapter<Buffer, asio::mutable_registered_buffer> : buffer_sequence_adapter_base { public: enum { is_single_buffer = true }; enum { is_registered_buffer = true }; explicit buffer_sequence_adapter( const asio::mutable_registered_buffer& buffer_sequence) { init_native_buffer(buffer_, buffer_sequence.buffer()); total_buffer_size_ = buffer_sequence.size(); registered_id_ = buffer_sequence.id(); } native_buffer_type* buffers() { return &buffer_; } std::size_t count() const { return 1; } std::size_t total_size() const { return total_buffer_size_; } registered_buffer_id registered_id() const { return registered_id_; } bool all_empty() const { return total_buffer_size_ == 0; } static bool all_empty( const asio::mutable_registered_buffer& buffer_sequence) { return buffer_sequence.size() == 0; } static void validate( const asio::mutable_registered_buffer& buffer_sequence) { buffer_sequence.data(); } static Buffer first( const asio::mutable_registered_buffer& buffer_sequence) { return Buffer(buffer_sequence.buffer()); } enum { linearisation_storage_size = 1 }; static Buffer linearise( const asio::mutable_registered_buffer& buffer_sequence, const Buffer&) { return Buffer(buffer_sequence.buffer()); } private: native_buffer_type buffer_; std::size_t total_buffer_size_; registered_buffer_id registered_id_; }; template <typename Buffer> class buffer_sequence_adapter<Buffer, asio::const_registered_buffer> : buffer_sequence_adapter_base { public: enum { is_single_buffer = true }; enum { is_registered_buffer = true }; explicit buffer_sequence_adapter( const asio::const_registered_buffer& buffer_sequence) { init_native_buffer(buffer_, buffer_sequence.buffer()); total_buffer_size_ = buffer_sequence.size(); registered_id_ = buffer_sequence.id(); } native_buffer_type* buffers() { return &buffer_; } std::size_t count() const { return 1; } std::size_t total_size() const { return total_buffer_size_; } registered_buffer_id registered_id() const { return registered_id_; } bool all_empty() const { return total_buffer_size_ == 0; } static bool all_empty( const asio::const_registered_buffer& buffer_sequence) { return buffer_sequence.size() == 0; } static void validate( const asio::const_registered_buffer& buffer_sequence) { buffer_sequence.data(); } static Buffer first( const asio::const_registered_buffer& buffer_sequence) { return Buffer(buffer_sequence.buffer()); } enum { linearisation_storage_size = 1 }; static Buffer linearise( const asio::const_registered_buffer& buffer_sequence, const Buffer&) { return Buffer(buffer_sequence.buffer()); } private: native_buffer_type buffer_; std::size_t total_buffer_size_; registered_buffer_id registered_id_; }; template <typename Buffer, typename Elem> class buffer_sequence_adapter<Buffer, boost::array<Elem, 2>> : buffer_sequence_adapter_base { public: enum { is_single_buffer = false }; enum { is_registered_buffer = false }; explicit buffer_sequence_adapter( const boost::array<Elem, 2>& buffer_sequence) { init_native_buffer(buffers_[0], Buffer(buffer_sequence[0])); init_native_buffer(buffers_[1], Buffer(buffer_sequence[1])); total_buffer_size_ = buffer_sequence[0].size() + buffer_sequence[1].size(); } native_buffer_type* buffers() { return buffers_; } std::size_t count() const { return 2; } std::size_t total_size() const { return total_buffer_size_; } registered_buffer_id registered_id() const { return registered_buffer_id(); } bool all_empty() const { return total_buffer_size_ == 0; } static bool all_empty(const boost::array<Elem, 2>& buffer_sequence) { return buffer_sequence[0].size() == 0 && buffer_sequence[1].size() == 0; } static void validate(const boost::array<Elem, 2>& buffer_sequence) { buffer_sequence[0].data(); buffer_sequence[1].data(); } static Buffer first(const boost::array<Elem, 2>& buffer_sequence) { return Buffer(buffer_sequence[0].size() != 0 ? buffer_sequence[0] : buffer_sequence[1]); } enum { linearisation_storage_size = 8192 }; static Buffer linearise(const boost::array<Elem, 2>& buffer_sequence, const asio::mutable_buffer& storage) { if (buffer_sequence[0].size() == 0) return Buffer(buffer_sequence[1]); if (buffer_sequence[1].size() == 0) return Buffer(buffer_sequence[0]); return Buffer(storage.data(), asio::buffer_copy(storage, buffer_sequence)); } private: native_buffer_type buffers_[2]; std::size_t total_buffer_size_; }; template <typename Buffer, typename Elem> class buffer_sequence_adapter<Buffer, std::array<Elem, 2>> : buffer_sequence_adapter_base { public: enum { is_single_buffer = false }; enum { is_registered_buffer = false }; explicit buffer_sequence_adapter( const std::array<Elem, 2>& buffer_sequence) { init_native_buffer(buffers_[0], Buffer(buffer_sequence[0])); init_native_buffer(buffers_[1], Buffer(buffer_sequence[1])); total_buffer_size_ = buffer_sequence[0].size() + buffer_sequence[1].size(); } native_buffer_type* buffers() { return buffers_; } std::size_t count() const { return 2; } std::size_t total_size() const { return total_buffer_size_; } registered_buffer_id registered_id() const { return registered_buffer_id(); } bool all_empty() const { return total_buffer_size_ == 0; } static bool all_empty(const std::array<Elem, 2>& buffer_sequence) { return buffer_sequence[0].size() == 0 && buffer_sequence[1].size() == 0; } static void validate(const std::array<Elem, 2>& buffer_sequence) { buffer_sequence[0].data(); buffer_sequence[1].data(); } static Buffer first(const std::array<Elem, 2>& buffer_sequence) { return Buffer(buffer_sequence[0].size() != 0 ? buffer_sequence[0] : buffer_sequence[1]); } enum { linearisation_storage_size = 8192 }; static Buffer linearise(const std::array<Elem, 2>& buffer_sequence, const asio::mutable_buffer& storage) { if (buffer_sequence[0].size() == 0) return Buffer(buffer_sequence[1]); if (buffer_sequence[1].size() == 0) return Buffer(buffer_sequence[0]); return Buffer(storage.data(), asio::buffer_copy(storage, buffer_sequence)); } private: native_buffer_type buffers_[2]; std::size_t total_buffer_size_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #if defined(ASIO_HEADER_ONLY) # include "asio/detail/impl/buffer_sequence_adapter.ipp" #endif // defined(ASIO_HEADER_ONLY) #endif // ASIO_DETAIL_BUFFER_SEQUENCE_ADAPTER_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/descriptor_read_op.hpp
// // detail/descriptor_read_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_DESCRIPTOR_READ_OP_HPP #define ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP #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 "asio/detail/bind_handler.hpp" #include "asio/detail/buffer_sequence_adapter.hpp" #include "asio/detail/descriptor_ops.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/reactor_op.hpp" #include "asio/dispatch.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename MutableBufferSequence> class descriptor_read_op_base : public reactor_op { public: descriptor_read_op_base(const asio::error_code& success_ec, int descriptor, const MutableBufferSequence& buffers, func_type complete_func) : reactor_op(success_ec, &descriptor_read_op_base::do_perform, complete_func), descriptor_(descriptor), buffers_(buffers) { } static status do_perform(reactor_op* base) { ASIO_ASSUME(base != 0); descriptor_read_op_base* o(static_cast<descriptor_read_op_base*>(base)); typedef buffer_sequence_adapter<asio::mutable_buffer, MutableBufferSequence> bufs_type; status result; if (bufs_type::is_single_buffer) { result = descriptor_ops::non_blocking_read1(o->descriptor_, bufs_type::first(o->buffers_).data(), bufs_type::first(o->buffers_).size(), o->ec_, o->bytes_transferred_) ? done : not_done; } else { bufs_type bufs(o->buffers_); result = descriptor_ops::non_blocking_read(o->descriptor_, bufs.buffers(), bufs.count(), o->ec_, o->bytes_transferred_) ? done : not_done; } ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_read", o->ec_, o->bytes_transferred_)); return result; } private: int descriptor_; MutableBufferSequence buffers_; }; template <typename MutableBufferSequence, typename Handler, typename IoExecutor> class descriptor_read_op : public descriptor_read_op_base<MutableBufferSequence> { public: typedef Handler handler_type; typedef IoExecutor io_executor_type; ASIO_DEFINE_HANDLER_PTR(descriptor_read_op); descriptor_read_op(const asio::error_code& success_ec, int descriptor, const MutableBufferSequence& buffers, Handler& handler, const IoExecutor& io_ex) : descriptor_read_op_base<MutableBufferSequence>(success_ec, descriptor, buffers, &descriptor_read_op::do_complete), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } static void do_complete(void* owner, operation* base, const asio::error_code& /*ec*/, std::size_t /*bytes_transferred*/) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); descriptor_read_op* o(static_cast<descriptor_read_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->bytes_transferred_); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } static void do_immediate(operation* base, bool, const void* io_ex) { // Take ownership of the handler object. ASIO_ASSUME(base != 0); descriptor_read_op* o(static_cast<descriptor_read_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. immediate_handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(o->ec_); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder2<Handler, asio::error_code, std::size_t> handler(o->handler_, o->ec_, o->bytes_transferred_); p.h = asio::detail::addressof(handler.handler_); p.reset(); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_)); w.complete(handler, handler.handler_, io_ex); ASIO_HANDLER_INVOCATION_END; } private: Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // !defined(ASIO_WINDOWS) && !defined(__CYGWIN__) #endif // ASIO_DETAIL_DESCRIPTOR_READ_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/win_iocp_socket_accept_op.hpp
// // detail/win_iocp_socket_accept_op.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP #define ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP #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_IOCP) #include "asio/detail/bind_handler.hpp" #include "asio/detail/fenced_block.hpp" #include "asio/detail/handler_alloc_helpers.hpp" #include "asio/detail/handler_work.hpp" #include "asio/detail/memory.hpp" #include "asio/detail/operation.hpp" #include "asio/detail/socket_ops.hpp" #include "asio/detail/win_iocp_socket_service_base.hpp" #include "asio/error.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { template <typename Socket, typename Protocol, typename Handler, typename IoExecutor> class win_iocp_socket_accept_op : public operation { public: ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_accept_op); win_iocp_socket_accept_op(win_iocp_socket_service_base& socket_service, socket_type socket, Socket& peer, const Protocol& protocol, typename Protocol::endpoint* peer_endpoint, bool enable_connection_aborted, Handler& handler, const IoExecutor& io_ex) : operation(&win_iocp_socket_accept_op::do_complete), socket_service_(socket_service), socket_(socket), peer_(peer), protocol_(protocol), peer_endpoint_(peer_endpoint), enable_connection_aborted_(enable_connection_aborted), proxy_op_(0), cancel_requested_(0), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } socket_holder& new_socket() { return new_socket_; } void* output_buffer() { return output_buffer_; } DWORD address_length() { return sizeof(sockaddr_storage_type) + 16; } void enable_cancellation(long* cancel_requested, operation* proxy_op) { cancel_requested_ = cancel_requested; proxy_op_ = proxy_op; } static void do_complete(void* owner, operation* base, const asio::error_code& result_ec, std::size_t /*bytes_transferred*/) { asio::error_code ec(result_ec); // Take ownership of the operation object. ASIO_ASSUME(base != 0); win_iocp_socket_accept_op* o(static_cast<win_iocp_socket_accept_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; if (owner) { typename Protocol::endpoint peer_endpoint; std::size_t addr_len = peer_endpoint.capacity(); socket_ops::complete_iocp_accept(o->socket_, o->output_buffer(), o->address_length(), peer_endpoint.data(), &addr_len, o->new_socket_.get(), ec); // Restart the accept operation if we got the connection_aborted error // and the enable_connection_aborted socket option is not set. if (ec == asio::error::connection_aborted && !o->enable_connection_aborted_) { o->reset(); if (o->proxy_op_) o->proxy_op_->reset(); o->socket_service_.restart_accept_op(o->socket_, o->new_socket_, o->protocol_.family(), o->protocol_.type(), o->protocol_.protocol(), o->output_buffer(), o->address_length(), o->cancel_requested_, o->proxy_op_ ? o->proxy_op_ : o); p.v = p.p = 0; return; } // If the socket was successfully accepted, transfer ownership of the // socket to the peer object. if (!ec) { o->peer_.assign(o->protocol_, typename Socket::native_handle_type( o->new_socket_.get(), peer_endpoint), ec); if (!ec) o->new_socket_.release(); } // Pass endpoint back to caller. if (o->peer_endpoint_) *o->peer_endpoint_ = peer_endpoint; } ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(ec); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::binder1<Handler, asio::error_code> handler(o->handler_, ec); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_)); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: win_iocp_socket_service_base& socket_service_; socket_type socket_; socket_holder new_socket_; Socket& peer_; Protocol protocol_; typename Protocol::endpoint* peer_endpoint_; unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2]; bool enable_connection_aborted_; operation* proxy_op_; long* cancel_requested_; Handler handler_; handler_work<Handler, IoExecutor> work_; }; template <typename Protocol, typename PeerIoExecutor, typename Handler, typename IoExecutor> class win_iocp_socket_move_accept_op : public operation { public: ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_move_accept_op); win_iocp_socket_move_accept_op( win_iocp_socket_service_base& socket_service, socket_type socket, const Protocol& protocol, const PeerIoExecutor& peer_io_ex, typename Protocol::endpoint* peer_endpoint, bool enable_connection_aborted, Handler& handler, const IoExecutor& io_ex) : operation(&win_iocp_socket_move_accept_op::do_complete), socket_service_(socket_service), socket_(socket), peer_(peer_io_ex), protocol_(protocol), peer_endpoint_(peer_endpoint), enable_connection_aborted_(enable_connection_aborted), cancel_requested_(0), proxy_op_(0), handler_(static_cast<Handler&&>(handler)), work_(handler_, io_ex) { } socket_holder& new_socket() { return new_socket_; } void* output_buffer() { return output_buffer_; } DWORD address_length() { return sizeof(sockaddr_storage_type) + 16; } void enable_cancellation(long* cancel_requested, operation* proxy_op) { cancel_requested_ = cancel_requested; proxy_op_ = proxy_op; } static void do_complete(void* owner, operation* base, const asio::error_code& result_ec, std::size_t /*bytes_transferred*/) { asio::error_code ec(result_ec); // Take ownership of the operation object. ASIO_ASSUME(base != 0); win_iocp_socket_move_accept_op* o( static_cast<win_iocp_socket_move_accept_op*>(base)); ptr p = { asio::detail::addressof(o->handler_), o, o }; if (owner) { typename Protocol::endpoint peer_endpoint; std::size_t addr_len = peer_endpoint.capacity(); socket_ops::complete_iocp_accept(o->socket_, o->output_buffer(), o->address_length(), peer_endpoint.data(), &addr_len, o->new_socket_.get(), ec); // Restart the accept operation if we got the connection_aborted error // and the enable_connection_aborted socket option is not set. if (ec == asio::error::connection_aborted && !o->enable_connection_aborted_) { o->reset(); if (o->proxy_op_) o->proxy_op_->reset(); o->socket_service_.restart_accept_op(o->socket_, o->new_socket_, o->protocol_.family(), o->protocol_.type(), o->protocol_.protocol(), o->output_buffer(), o->address_length(), o->cancel_requested_, o->proxy_op_ ? o->proxy_op_ : o); p.v = p.p = 0; return; } // If the socket was successfully accepted, transfer ownership of the // socket to the peer object. if (!ec) { o->peer_.assign(o->protocol_, typename Protocol::socket::native_handle_type( o->new_socket_.get(), peer_endpoint), ec); if (!ec) o->new_socket_.release(); } // Pass endpoint back to caller. if (o->peer_endpoint_) *o->peer_endpoint_ = peer_endpoint; } ASIO_HANDLER_COMPLETION((*o)); // Take ownership of the operation's outstanding work. handler_work<Handler, IoExecutor> w( static_cast<handler_work<Handler, IoExecutor>&&>( o->work_)); ASIO_ERROR_LOCATION(ec); // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Even if we're not about to make an upcall, a // sub-object of the handler may be the true owner of the memory associated // with the handler. Consequently, a local copy of the handler is required // to ensure that any owning sub-object remains valid until after we have // deallocated the memory here. detail::move_binder2<Handler, asio::error_code, peer_socket_type> handler(0, static_cast<Handler&&>(o->handler_), ec, static_cast<peer_socket_type&&>(o->peer_)); p.h = asio::detail::addressof(handler.handler_); p.reset(); // Make the upcall if required. if (owner) { fenced_block b(fenced_block::half); ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "...")); w.complete(handler, handler.handler_); ASIO_HANDLER_INVOCATION_END; } } private: typedef typename Protocol::socket::template rebind_executor<PeerIoExecutor>::other peer_socket_type; win_iocp_socket_service_base& socket_service_; socket_type socket_; socket_holder new_socket_; peer_socket_type peer_; Protocol protocol_; typename Protocol::endpoint* peer_endpoint_; unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2]; bool enable_connection_aborted_; long* cancel_requested_; operation* proxy_op_; Handler handler_; handler_work<Handler, IoExecutor> work_; }; } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // defined(ASIO_HAS_IOCP) #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/std_static_mutex.hpp
// // detail/std_static_mutex.hpp // ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_STD_STATIC_MUTEX_HPP #define ASIO_DETAIL_STD_STATIC_MUTEX_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <mutex> #include "asio/detail/noncopyable.hpp" #include "asio/detail/scoped_lock.hpp" #include "asio/detail/push_options.hpp" namespace asio { namespace detail { class std_event; class std_static_mutex : private noncopyable { public: typedef asio::detail::scoped_lock<std_static_mutex> scoped_lock; // Constructor. std_static_mutex(int) { } // Destructor. ~std_static_mutex() { } // Initialise the mutex. void init() { // Nothing to do. } // Lock the mutex. void lock() { mutex_.lock(); } // Unlock the mutex. void unlock() { mutex_.unlock(); } private: friend class std_event; std::mutex mutex_; }; #define ASIO_STD_STATIC_MUTEX_INIT 0 } // namespace detail } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_DETAIL_STD_STATIC_MUTEX_HPP
0
repos/asio/asio/include/asio
repos/asio/asio/include/asio/detail/utility.hpp
// // detail/utility.hpp // ~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2024 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_DETAIL_UTILITY_HPP #define ASIO_DETAIL_UTILITY_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include <utility> namespace asio { namespace detail { #if defined(ASIO_HAS_STD_INDEX_SEQUENCE) using std::index_sequence; using std::index_sequence_for; using std::make_index_sequence; #else // defined(ASIO_HAS_STD_INDEX_SEQUENCE) template <std::size_t...> struct index_sequence { }; template <typename T, typename U> struct join_index_sequences; template <std::size_t... I, std::size_t... J> struct join_index_sequences<index_sequence<I...>, index_sequence<J...>> { using type = index_sequence<I..., J...>; }; template <std::size_t First, std::size_t Last> struct index_pack : join_index_sequences< typename index_pack<First, First + (Last - First + 1) / 2 - 1>::type, typename index_pack<First + (Last - First + 1) / 2, Last>::type > { }; template <std::size_t N> struct index_pack<N, N> { using type = index_sequence<N>; }; template <std::size_t Begin, std::size_t End> struct index_range : index_pack<Begin, End - 1> { }; template <std::size_t N> struct index_range<N, N> { using type = index_sequence<>; }; template <typename... T> using index_sequence_for = typename index_range<0, sizeof...(T)>::type; template <std::size_t N> using make_index_sequence = typename index_range<0, N>::type; #endif // defined(ASIO_HAS_STD_INDEX_SEQUENCE) } // namespace detail } // namespace asio #endif // ASIO_DETAIL_UTILITY_HPP