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

烂漫一生 提交于 2019-12-11 07:15:59

问题


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?


回答1:


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
);


来源:https://stackoverflow.com/questions/48673558/boost-asio-async-receive-from-function-overload-issue-dynamic-pointer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!