2

I want to use the feature of asio::io_context::post. But I found it was marked as DEPRECATED

You can see it here (Deprecated: Use boost::asio::post().) Request the io_context to invoke the given handler and return immediately.

Then I want to have a try on boost::asio::post(), but I can't image how to write the code. No example, no code, even no more docs.

Can you help me? Thanks a lot.

1 Answers1

2

I have a real example from my RTSP server using boost.asio:

using udp_buffer = std::array<char, 0xFFFF>;
using shared_udp_socket = std::tuple<boost::asio::ip::udp::socket,
                boost::asio::io_context::strand,
                udp_buffer,
                boost::asio::ip::udp::endpoint>;

void rtsp::rtsp_server::handle_incoming_udp_traffic(const boost::system::error_code &error,
                                                    std::size_t received_bytes,
                                                    rtsp::rtsp_server::shared_udp_socket &incoming_socket) {
    if (error)
        throw std::runtime_error{error.message()};

    auto data = std::make_shared<std::vector<char>>();

    std::copy_n(std::get<2>(incoming_socket).cbegin(), received_bytes, std::back_inserter(*data));
    boost::asio::ip::udp::endpoint received_from_endpoint = std::get<3>(incoming_socket);

    boost::asio::post(std::get<1>(incoming_socket).get_io_context(),
                      std::bind(&rtsp::rtsp_server::handle_new_incoming_message,
                                data, std::ref(incoming_socket),
                                received_from_endpoint,
                                std::ref(this->server_state_))
    );

    start_async_receive(incoming_socket);
}
void rtsp::rtsp_server::handle_new_incoming_message(std::shared_ptr<std::vector<char>> message,
                                                shared_udp_socket &socket_received_from,
                                                boost::asio::ip::udp::endpoint received_from_endpoint,
                                                server::rtsp_server_state &server_state {...}

There you can see how I use boost::asio::post to post the handling of the incoming UDP datagram to the iocontext, while starting to relisten to new incoming datagrams on the udp socket via start_async_receive. If you need further explanation, let me know.

Superlokkus
  • 4,731
  • 1
  • 25
  • 57
  • Why would you ever want to use that `tuple<>` instead of a `struct` with the same members? – sehe Oct 20 '18 at 19:59
  • 1
    Good question, I don't know. Maybe because I think "Thats what `tuple` s are for". After some thinking I would say, to not clutter up a interface with struct definitions, but since this is a private interface that wouldn't count either. Guess I should refactor it sometime. – Superlokkus Oct 20 '18 at 20:27
  • This is what I want. Thank you very much. The code looks so strange ... – user3754671 Oct 20 '18 at 23:06
  • Don't let yourself be sidetracked by my whole tuple shebang, since socket objects aren't thread-safe and the endpoint of the next received from sender is taken by reference by boost, this is my way to demultiplex incoming UDP messages to a handler, which will later reply to the UDP sender endpoint, and synchronising via strands. I says UDP socketS because you already need one for IPv4 and one for IPv6. TCP connection handling is a bit simpler. (RTSP supports both, and connection semantics for UDP, so in other cases you could do this a lot simpler) – Superlokkus Oct 21 '18 at 09:07