📄 win_iocp_socket_service.hpp
字号:
DWORD last_error = ::WSAGetLastError(); // Check if the operation completed immediately. if (result != 0 && last_error != WSA_IO_PENDING) { asio::io_service::work work(this->get_io_service()); ptr.reset(); asio::error_code ec(last_error, asio::error::get_system_category()); iocp_service_.post(bind_handler(handler, ec, bytes_transferred)); } else { ptr.release(); } } // 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) { if (!is_open(impl)) { ec = asio::error::bad_descriptor; return 0; } // Copy buffers into WSABUF array. ::WSABUF bufs[max_buffers]; typename ConstBufferSequence::const_iterator iter = buffers.begin(); typename ConstBufferSequence::const_iterator end = buffers.end(); DWORD i = 0; for (; iter != end && i < max_buffers; ++iter, ++i) { asio::const_buffer buffer(*iter); bufs[i].len = static_cast<u_long>(asio::buffer_size(buffer)); bufs[i].buf = const_cast<char*>( asio::buffer_cast<const char*>(buffer)); } // Send the data. DWORD bytes_transferred = 0; int result = ::WSASendTo(impl.socket_, bufs, i, &bytes_transferred, flags, destination.data(), destination.size(), 0, 0); if (result != 0) { DWORD last_error = ::WSAGetLastError(); if (last_error == ERROR_PORT_UNREACHABLE) last_error = WSAECONNREFUSED; ec = asio::error_code(last_error, asio::error::get_system_category()); return 0; } ec = asio::error_code(); return bytes_transferred; } template <typename ConstBufferSequence, typename Handler> class send_to_operation : public operation { public: send_to_operation(asio::io_service& io_service, const ConstBufferSequence& buffers, Handler handler) : operation( &send_to_operation<ConstBufferSequence, Handler>::do_completion_impl, &send_to_operation<ConstBufferSequence, Handler>::destroy_impl), work_(io_service), buffers_(buffers), handler_(handler) { } private: static void do_completion_impl(operation* op, DWORD last_error, size_t bytes_transferred) { // Take ownership of the operation object. typedef send_to_operation<ConstBufferSequence, Handler> op_type; op_type* handler_op(static_cast<op_type*>(op)); typedef handler_alloc_traits<Handler, op_type> alloc_traits; handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Check whether buffers are still valid. typename ConstBufferSequence::const_iterator iter = handler_op->buffers_.begin(); typename ConstBufferSequence::const_iterator end = handler_op->buffers_.end(); while (iter != end) { asio::const_buffer buffer(*iter); asio::buffer_cast<const char*>(buffer); ++iter; }#endif // defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Map non-portable errors to their portable counterparts. asio::error_code ec(last_error, asio::error::get_system_category()); if (ec.value() == ERROR_PORT_UNREACHABLE) { ec = asio::error::connection_refused; } // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Handler handler(handler_op->handler_); // Free the memory associated with the handler. ptr.reset(); // Call the handler. asio_handler_invoke_helpers::invoke( detail::bind_handler(handler, ec, bytes_transferred), &handler); } static void destroy_impl(operation* op) { // Take ownership of the operation object. typedef send_to_operation<ConstBufferSequence, Handler> op_type; op_type* handler_op(static_cast<op_type*>(op)); typedef handler_alloc_traits<Handler, op_type> alloc_traits; handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op); } asio::io_service::work work_; ConstBufferSequence buffers_; Handler handler_; }; // Start an asynchronous send. The data being sent must be valid for the // lifetime of the asynchronous operation. template <typename ConstBufferSequence, typename Handler> void async_send_to(implementation_type& impl, const ConstBufferSequence& buffers, const endpoint_type& destination, socket_base::message_flags flags, Handler handler) { if (!is_open(impl)) { this->get_io_service().post(bind_handler(handler, asio::error::bad_descriptor, 0)); return; } // Update the ID of the thread from which cancellation is safe. if (impl.safe_cancellation_thread_id_ == 0) impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId(); else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId()) impl.safe_cancellation_thread_id_ = ~DWORD(0); // Allocate and construct an operation to wrap the handler. typedef send_to_operation<ConstBufferSequence, Handler> value_type; typedef handler_alloc_traits<Handler, value_type> alloc_traits; raw_handler_ptr<alloc_traits> raw_ptr(handler); handler_ptr<alloc_traits> ptr(raw_ptr, this->get_io_service(), buffers, handler); // Copy buffers into WSABUF array. ::WSABUF bufs[max_buffers]; typename ConstBufferSequence::const_iterator iter = buffers.begin(); typename ConstBufferSequence::const_iterator end = buffers.end(); DWORD i = 0; for (; iter != end && i < max_buffers; ++iter, ++i) { asio::const_buffer buffer(*iter); bufs[i].len = static_cast<u_long>(asio::buffer_size(buffer)); bufs[i].buf = const_cast<char*>( asio::buffer_cast<const char*>(buffer)); } // Send the data. DWORD bytes_transferred = 0; int result = ::WSASendTo(impl.socket_, bufs, i, &bytes_transferred, flags, destination.data(), destination.size(), ptr.get(), 0); DWORD last_error = ::WSAGetLastError(); // Check if the operation completed immediately. if (result != 0 && last_error != WSA_IO_PENDING) { asio::io_service::work work(this->get_io_service()); ptr.reset(); asio::error_code ec(last_error, asio::error::get_system_category()); iocp_service_.post(bind_handler(handler, ec, bytes_transferred)); } else { ptr.release(); } } // Receive some data from the peer. Returns the number of bytes received. template <typename MutableBufferSequence> size_t receive(implementation_type& impl, const MutableBufferSequence& buffers, socket_base::message_flags flags, asio::error_code& ec) { if (!is_open(impl)) { ec = asio::error::bad_descriptor; return 0; } // Copy buffers into WSABUF array. ::WSABUF bufs[max_buffers]; typename MutableBufferSequence::const_iterator iter = buffers.begin(); typename MutableBufferSequence::const_iterator end = buffers.end(); DWORD i = 0; size_t total_buffer_size = 0; for (; iter != end && i < max_buffers; ++iter, ++i) { asio::mutable_buffer buffer(*iter); bufs[i].len = static_cast<u_long>(asio::buffer_size(buffer)); bufs[i].buf = asio::buffer_cast<char*>(buffer); total_buffer_size += asio::buffer_size(buffer); } // A request to receive 0 bytes on a stream socket is a no-op. if (impl.protocol_.type() == SOCK_STREAM && total_buffer_size == 0) { ec = asio::error_code(); return 0; } // Receive some data. DWORD bytes_transferred = 0; DWORD recv_flags = flags; int result = ::WSARecv(impl.socket_, bufs, i, &bytes_transferred, &recv_flags, 0, 0); if (result != 0) { DWORD last_error = ::WSAGetLastError(); if (last_error == ERROR_NETNAME_DELETED) last_error = WSAECONNRESET; else if (last_error == ERROR_PORT_UNREACHABLE) last_error = WSAECONNREFUSED; ec = asio::error_code(last_error, asio::error::get_system_category()); return 0; } if (bytes_transferred == 0) { ec = asio::error::eof; return 0; } ec = asio::error_code(); return bytes_transferred; } template <typename MutableBufferSequence, typename Handler> class receive_operation : public operation { public: receive_operation(asio::io_service& io_service, weak_cancel_token_type cancel_token, const MutableBufferSequence& buffers, Handler handler) : operation( &receive_operation< MutableBufferSequence, Handler>::do_completion_impl, &receive_operation< MutableBufferSequence, Handler>::destroy_impl), work_(io_service), cancel_token_(cancel_token), buffers_(buffers), handler_(handler) { } private: static void do_completion_impl(operation* op, DWORD last_error, size_t bytes_transferred) { // Take ownership of the operation object. typedef receive_operation<MutableBufferSequence, Handler> op_type; op_type* handler_op(static_cast<op_type*>(op)); typedef handler_alloc_traits<Handler, op_type> alloc_traits; handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op);#if defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Check whether buffers are still valid. typename MutableBufferSequence::const_iterator iter = handler_op->buffers_.begin(); typename MutableBufferSequence::const_iterator end = handler_op->buffers_.end(); while (iter != end) { asio::mutable_buffer buffer(*iter); asio::buffer_cast<char*>(buffer); ++iter; }#endif // defined(ASIO_ENABLE_BUFFER_DEBUGGING) // Map non-portable errors to their portable counterparts. asio::error_code ec(last_error, asio::error::get_system_category()); if (ec.value() == ERROR_NETNAME_DELETED) { if (handler_op->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; } // Check for connection closed. else if (!ec && bytes_transferred == 0) { ec = asio::error::eof; } // Make a copy of the handler so that the memory can be deallocated before // the upcall is made. Handler handler(handler_op->handler_); // Free the memory associated with the handler. ptr.reset(); // Call the handler. asio_handler_invoke_helpers::invoke( detail::bind_handler(handler, ec, bytes_transferred), &handler); } static void destroy_impl(operation* op) { // Take ownership of the operation object. typedef receive_operation<MutableBufferSequence, Handler> op_type; op_type* handler_op(static_cast<op_type*>(op)); typedef handler_alloc_traits<Handler, op_type> alloc_traits; handler_ptr<alloc_traits> ptr(handler_op->handler_, handler_op); } asio::io_service::work work_; weak_cancel_token_type cancel_token_; MutableBufferSequence buffers_; Handler handler_; }; // 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> void async_receive(implementation_type& impl, const MutableBufferSequence& buffers, socket_base::message_flags flags, Handler handler) { if (!is_open(impl)) { this->get_io_service().post(bind_handler(handler, asio::error::bad_descriptor, 0)); return; } // Update the ID of the thread from which cancellation is safe. if (impl.safe_cancellation_thread_id_ == 0) impl.safe_cancellation_thread_id_ = ::GetCurrentThreadId(); else if (impl.safe_cancellation_thread_id_ != ::GetCurrentThreadId()) impl.safe_cancellation_thread_id_ = ~DWORD(0); // Allocate and construct an operation to wrap the handler. typedef receive_operation<MutableBufferSequence, Handler> value_type; typedef handler_alloc_traits<Handler, value_type> alloc_traits; raw_handler_ptr<alloc_traits> raw_ptr(handler); handler_ptr<alloc_traits> ptr(raw_ptr, this->get_io_service(), impl.cancel_token_, buffers, handler); // Copy buffers into WSABUF array. ::WSABUF bufs[max_buffers]; typename MutableBufferSequence::const_iterator iter = buffers.begin(); typename MutableBufferSequence::const_iterator end = buffers.end(); DWORD i = 0; size_t total_buffer_size = 0; for (; iter != end && i < max_buffers; ++iter, ++i) { asio::mutable_buffer buffer(*iter); bufs[i].len = static_cast<u_long>(asio::buffer_size(buffer)); bufs[i].buf = asio::buffer_cast<char*>(buffer); total_buffer_size += asio::buffer_size(buffer); } // A request to receive 0 bytes on a stream socket is a no-op. if (impl.protocol_.type() == SOCK_STREAM && total_buffer_size == 0) { asio::io_service::work work(this->get_io_service()); ptr.reset(); asio::error_code error; iocp_service_.post(bind_handler(handler, error, 0)); return; } // Receive some data. DWORD bytes_transferred = 0; DWORD recv_flags = flags; int result = ::WSARecv(impl.socket_, bufs, i, &bytes_transferred, &recv_flags, ptr.get(), 0); DWORD last_error = ::WSAGetLastError(); if (result != 0 && last_error != WSA_IO_PENDING) { asio::io_service::work work(this->get_io_service()); ptr.reset();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -