简体   繁体   中英

Boost-ASIO async_receive_from function overload issue (+ dynamic pointer)

I am trying to create a simple asynchronous receiver using the Boost library. I followed the examples as well as books, and this is what I was able to cook up so far:

class networkUDPClient {
public:
    utilityTripleBuffer * buffer;

    char testBuffer[20] = { 0 };

    const char * hostAddress;
    unsigned int port;

    boost::asio::io_service service;
    boost::asio::ip::udp::socket socket;
    boost::asio::ip::udp::endpoint listenerEndpoint;
    boost::asio::ip::udp::endpoint senderEndpoint;

    // Function Definitions
    void readCallback(const boost::system::error_code & err, std::size_t read_bytes) {

    }

    // Constructors
    networkUDPClient(const char * hostAddress, unsigned int port, unsigned int bufferSize) :
            socket(service),
            listenerEndpoint(boost::asio::ip::address_v4::from_string(hostAddress), port)
        {
            this->buffer = new utilityTripleBuffer(bufferSize, nullptr);

            this->hostAddress = hostAddress;
            this->port = port;

            socket.open(this->listenerEndpoint.protocol());
            socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));
            socket.bind(this->listenerEndpoint);
            socket.async_receive_from(boost::asio::buffer(this->buffer->currentBufferAddr, this->buffer->bufferSize), 0, senderEndpoint, readCallback);
            service.run();
        };
    ~networkUDPClient()
        {
            service.stop();

            delete this->buffer;
        };
};

The problem is that async_receive_from function causes an error:

Severity Code Description Project File Line Suppression State Error (active) no instance of overloaded function "boost::asio::basic_datagram_socket::async_receive_from [with Protocol=boost::asio::ip::udp, DatagramSocketService=boost::asio::datagram_socket_service

I double checked the reference website as well as all my books, I believe I pass correct arguments and initialize them properly. What could be causing the issue?

BONUS QUESTION:

While I'm here, I would like to know whether using a dynamic pointer is acceptable in this case. I need to double buffer the data I receive, to give myself some time to process the previous frame. In this case this->buffer->currentBufferAddr will be changing after each time I receive data, pointing at different buffer every time.

Is this an acceptable way of doing things?

First off, your parameters seem to be a little mixed up. async_receive_from has the flags parameter as the third parameter while you have it as the second .

For the second part, you will have to either make the function static for all instances in order to pass it as handler, or bind it with this so the correct instance is called.

Bind it:

socket.async_receive_from
(
    boost::asio::buffer(buffer.data(), buffer.size()),
    senderEndpoint,
    0,
    std::bind(&readCallback, this, std::placeholders::_1, std::placeholders::_2)
);

or if you make your readCallback static:

socket.async_receive_from
(
    boost::asio::buffer(buffer.data(), buffer.size()),
    senderEndpoint,
    0,
    readCallback
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM