⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 win_iocp_socket_service.hpp

📁 Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
💻 HPP
📖 第 1 页 / 共 5 页
字号:
      // Take ownership of the operation object.      typedef send_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);      // 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(handler_op->handler_);      (void)handler;      // Free the memory associated with the handler.      ptr.reset();    }    boost::asio::io_service::work work_;    weak_cancel_token_type cancel_token_;    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(implementation_type& impl, const ConstBufferSequence& buffers,      socket_base::message_flags flags, Handler handler)  {    if (!is_open(impl))    {      this->get_io_service().post(bind_handler(handler,            boost::asio::error::bad_descriptor, 0));      return;    }#if defined(BOOST_ASIO_ENABLE_CANCELIO)    // 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);#endif // defined(BOOST_ASIO_ENABLE_CANCELIO)    // Allocate and construct an operation to wrap the handler.    typedef send_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, iocp_service_,        impl.cancel_token_, 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;    size_t total_buffer_size = 0;    for (; iter != end && i < max_buffers; ++iter, ++i)    {      boost::asio::const_buffer buffer(*iter);      bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));      bufs[i].buf = const_cast<char*>(          boost::asio::buffer_cast<const char*>(buffer));      total_buffer_size += boost::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)    {      boost::asio::io_service::work work(this->get_io_service());      ptr.reset();      boost::system::error_code error;      iocp_service_.post(bind_handler(handler, error, 0));      return;    }    // Send the data.    DWORD bytes_transferred = 0;    int result = ::WSASend(impl.socket_, bufs, i,        &bytes_transferred, flags, ptr.get(), 0);    DWORD last_error = ::WSAGetLastError();    // Check if the operation completed immediately.    if (result != 0 && last_error != WSA_IO_PENDING)    {      boost::asio::io_service::work work(this->get_io_service());      ptr.reset();      boost::system::error_code ec(last_error,          boost::asio::error::get_system_category());      iocp_service_.post(bind_handler(handler, ec, bytes_transferred));    }    else    {      ptr.release();    }  }  template <typename Handler>  class null_buffers_operation  {  public:    null_buffers_operation(boost::asio::io_service& io_service, Handler handler)      : work_(io_service),        handler_(handler)    {    }    bool perform(boost::system::error_code&,        std::size_t& bytes_transferred)    {      bytes_transferred = 0;      return true;    }    void complete(const boost::system::error_code& ec,        std::size_t bytes_transferred)    {      work_.get_io_service().post(bind_handler(            handler_, ec, bytes_transferred));    }  private:    boost::asio::io_service::work work_;    Handler handler_;  };  // Start an asynchronous wait until data can be sent without blocking.  template <typename Handler>  void async_send(implementation_type& impl, const null_buffers&,      socket_base::message_flags, Handler handler)  {    if (!is_open(impl))    {      this->get_io_service().post(bind_handler(handler,            boost::asio::error::bad_descriptor, 0));    }    else    {      // Check if the reactor was already obtained from the io_service.      reactor_type* reactor = static_cast<reactor_type*>(            interlocked_compare_exchange_pointer(              reinterpret_cast<void**>(&reactor_), 0, 0));      if (!reactor)      {        reactor = &(boost::asio::use_service<reactor_type>(              this->get_io_service()));        interlocked_exchange_pointer(            reinterpret_cast<void**>(&reactor_), reactor);      }      reactor->start_write_op(impl.socket_, impl.reactor_data_,          null_buffers_operation<Handler>(this->get_io_service(), handler),          false);    }  }  // 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,      boost::system::error_code& ec)  {    if (!is_open(impl))    {      ec = boost::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)    {      boost::asio::const_buffer buffer(*iter);      bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));      bufs[i].buf = const_cast<char*>(          boost::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(), static_cast<int>(destination.size()), 0, 0);    if (result != 0)    {      DWORD last_error = ::WSAGetLastError();      if (last_error == ERROR_PORT_UNREACHABLE)        last_error = WSAECONNREFUSED;      ec = boost::system::error_code(last_error,          boost::asio::error::get_system_category());      return 0;    }    ec = boost::system::error_code();    return bytes_transferred;  }  // Wait until data can be sent without blocking.  size_t send_to(implementation_type& impl, const null_buffers&,      socket_base::message_flags, const endpoint_type&,      boost::system::error_code& ec)  {    if (!is_open(impl))    {      ec = boost::asio::error::bad_descriptor;      return 0;    }    // Wait for socket to become ready.    socket_ops::poll_write(impl.socket_, ec);    return 0;  }  template <typename ConstBufferSequence, typename Handler>  class send_to_operation    : public operation  {  public:    send_to_operation(win_iocp_io_service& io_service,        const ConstBufferSequence& buffers, Handler handler)      : operation(io_service,          &send_to_operation<ConstBufferSequence, Handler>::do_completion_impl,          &send_to_operation<ConstBufferSequence, Handler>::destroy_impl),        work_(io_service.get_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(BOOST_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)      {        boost::asio::const_buffer buffer(*iter);        boost::asio::buffer_cast<const char*>(buffer);        ++iter;      }#endif // defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)      // Map non-portable errors to their portable counterparts.      boost::system::error_code ec(last_error,          boost::asio::error::get_system_category());      if (ec.value() == ERROR_PORT_UNREACHABLE)      {        ec = boost::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.      boost_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);      // 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(handler_op->handler_);      (void)handler;      // Free the memory associated with the handler.      ptr.reset();    }    boost::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,            boost::asio::error::bad_descriptor, 0));      return;    }#if defined(BOOST_ASIO_ENABLE_CANCELIO)    // 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);#endif // defined(BOOST_ASIO_ENABLE_CANCELIO)    // 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, iocp_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)    {      boost::asio::const_buffer buffer(*iter);      bufs[i].len = static_cast<u_long>(boost::asio::buffer_size(buffer));      bufs[i].buf = const_cast<char*>(          boost::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(), static_cast<int>(destination.size()), ptr.get(), 0);    DWORD last_error = ::WSAGetLastError();    // Check if the operation completed immediately.    if (result != 0 && last_error != WSA_IO_PENDING)    {      boost::asio::io_service::work work(this->get_io_service());      ptr.reset();      boost::system::error_code ec(last_error,          boost::asio::error::get_system_category());      iocp_service_.post(bind_handler(handler, ec, bytes_transferred));    }    else    {      ptr.release();    }  }  // Start an asynchronous wait until data can be sent without blocking.  template <typename Handler>  void async_send_to(implementation_type& impl, const null_buffers&,      socket_base::message_flags, const endpoint_type&, Handler handler)  {    if (!is_open(impl))    {      this->get_io_service().post(bind_handler(handler,            boost::asio::error::bad_descriptor, 0));    }    else    {      // Check if the reactor was already obtained from the io_service.      reactor_type* reactor = static_cast<reactor_type*>(            interlocked_compare_exchange_pointer(              reinterpret_cast<void**>(&reactor_), 0, 0));      if (!reactor)      {        reactor = &(boost::asio::use_service<reactor_type>(              this->get_io_service()));        interlocked_exchange_pointer(            reinterpret_cast<void**>(&reactor_), reactor);      }      reactor->start_write_op(impl.socket_, impl.reactor_data_,          null_buffers_operation<Handler>(this->get_io_service(), handler),          false);    }  }  // 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, boost::system::error_code& ec)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -