Boost-ASIO async_receive_from函数重载问题(+动态指针) [英] Boost-ASIO async_receive_from function overload issue (+ dynamic pointer)

查看:527
本文介绍了Boost-ASIO async_receive_from函数重载问题(+动态指针)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Boost库创建一个简单的异步接收器.我遵循了示例以及书籍,这是到目前为止我能做的:

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

问题在于 async_receive_from 函数会导致错误:

The problem is that async_receive_from function causes an error:

严重性代码描述项目文件行抑制状态 错误(活动)没有重载函数实例"boost :: asio :: basic_datagram_socket :: async_receive_from [with Protocol = boost :: asio :: ip :: udp,DatagramSocketService = boost :: asio :: datagram_socket_service

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?

奖金问题:

虽然我在这里,但我想知道在这种情况下使用动态指针是否可以接受.我需要对接收到的数据加倍缓冲,以便给自己一些时间来处理前一帧.在这种情况下,每次我接收数据后, this-> buffer-> currentBufferAddr 都会发生变化,每次都指向不同的缓冲区.

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?

推荐答案

首先,您的参数似乎有点混乱. async_receive_from flags参数作为第三参数,而将其作为第二.

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.

对于第二部分,您将必须使所有实例的函数静态以使其作为处理程序传递,或者将其与this绑定以便调用正确的实例.

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.

绑定它:

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

,或者如果您将readCallback设为静态:

or if you make your readCallback static:

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

这篇关于Boost-ASIO async_receive_from函数重载问题(+动态指针)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆