如何将异步计时器添加到boost UDP服务器?

时间:2016-12-15 02:17:47

标签: c++ sockets asynchronous boost boost-asio

我在网上得到了这个代码并且一直在尝试为它添加一个计时器,以便它经常读取数据包。我似乎无法弄清楚如何将回调函数传递给boost :: async_wait命令,因为我收到此错误:

server1.cpp: In member function ‘void UDPClient::handle_receive(const boost::system::error_code&, size_t)’:
server1.cpp:51:66: error: invalid use of non-static member function ‘void UDPClient::time_to_receive(const boost::system::error_code&)’
                                  boost::asio::placeholders::error));
                                                                  ^
server1.cpp:33:6: note: declared here
 void UDPClient::time_to_receive(const boost::system::error_code& error)
      ^~~~~~~~~

UDPClient类:

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>
#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>

using boost::asio::ip::udp;

class UDPClient
{
public:
    boost::asio::io_service io_service;
    udp::socket socket;
    udp::endpoint receiver_endpoint;
    boost::asio::deadline_timer timer;
    boost::array<char, 1024> recv_buffer;

    UDPClient();
    void time_to_receive(const boost::system::error_code& error);
    void do_receive();
    void handle_receive(const boost::system::error_code& error, size_t);
};

UDPClient::UDPClient()
    : io_service(),
      socket(io_service, {udp::v4(), 3643}),
      timer(io_service, boost::posix_time::seconds(2))
{
    do_receive();
    io_service.run();
}

void UDPClient::time_to_receive(const boost::system::error_code& error)
{
    do_receive();
}

void UDPClient::do_receive()
{
    socket.async_receive_from(boost::asio::buffer(recv_buffer), receiver_endpoint,
                               boost::bind(&UDPClient::handle_receive, this,
                               boost::asio::placeholders::error,
                               boost::asio::placeholders::bytes_transferred));
}

void UDPClient::handle_receive(const boost::system::error_code& error, size_t bytes_transferred)
{
    std::cout << "Received: '" << std::string(recv_buffer.begin(), recv_buffer.begin()+bytes_transferred) << "'\n";

    timer.async_wait(boost::bind(time_to_receive,
                                 boost::asio::placeholders::error));
}

int main()
{
    UDPClient updclient;
}

我试图用这段代码回答的一个问题是,如果我从带有一堆UDP数据包的客户端垃圾邮件服务器,服务器会在async_wait期间忽略所有数据包吗?

另外,我的主要目标是将此代码放入我的四轴代码中。它是否会像编写实例化这个类的方式一样工作,让它从地面站读取数据包以获得用户输入?

1 个答案:

答案 0 :(得分:2)

使用bind成员函数的方式是错误的。使用方法如下所示:

timer.async_wait(boost::bind(&UDPClient::time_to_receive, this,
                             boost::asio::placeholders::error));

至于为什么会这样,我会建议你阅读增强文档。

此外,我修改了代码,使其实际上像服务器一样运行而不退出。为此,我做了两次更改:

  1. 在main函数中初始化io_service并将其引用传递给类。
  2. 初始化io_service_work个对象。这是io_service的长期工作来源。因此,io_service永远不会从run函数返回,除非work对象被销毁。
  3. 完整来源:

    #include <boost/asio.hpp>
    #include <boost/bind.hpp>
    #include <boost/array.hpp>
    #include <iostream>
    #include <boost/date_time/posix_time/posix_time.hpp>
    
    using boost::asio::ip::udp;
    
    class UDPClient
    {
    public:
        boost::asio::io_service& io_service;
        udp::socket socket;
        udp::endpoint receiver_endpoint;
        boost::asio::deadline_timer timer;
        boost::array<char, 1024> recv_buffer;
    
        UDPClient(boost::asio::io_service&);
        void time_to_receive(const boost::system::error_code& error);
        void do_receive();
        void handle_receive(const boost::system::error_code& error, size_t);
    };
    
    UDPClient::UDPClient(boost::asio::io_service& ios)
        : io_service(ios),
          socket(io_service, {udp::v4(), 3643}),
          timer(io_service, boost::posix_time::seconds(2))
    {
        do_receive();
    }
    
    void UDPClient::time_to_receive(const boost::system::error_code& error)
    {
        do_receive();
    }
    
    void UDPClient::do_receive()
    {
        socket.async_receive_from(boost::asio::buffer(recv_buffer), receiver_endpoint,
                                   boost::bind(&UDPClient::handle_receive, this,
                                   boost::asio::placeholders::error,
                                   boost::asio::placeholders::bytes_transferred));
    }
    
    void UDPClient::handle_receive(const boost::system::error_code& error, size_t bytes_transferred)
    {
        std::cout << "Received: '" << std::string(recv_buffer.begin(), recv_buffer.begin()+bytes_transferred) << "'\n";
        timer.expires_from_now(boost::posix_time::seconds(2));
        timer.async_wait(boost::bind(&UDPClient::time_to_receive, this,
                                     boost::asio::placeholders::error));
    }
    
    int main()
    {
      boost::asio::io_service ios;
      boost::asio::io_service::work wrk(ios);
      UDPClient updclient(ios);
      ios.run();
    }
    

    注意:即使它是服务器,该类也是名称Client。我忽略了:))