我想创建一个自治线程,专门用于使用boost库(asio)从UDP套接字接收数据。该线程应该是由UDP套接字接收的一些数据触发的无限循环。在我的应用程序中,我需要使用异步接收操作。
如果我使用同步函数receive_from,一切都按预期工作。
但是,如果我使用async_receive_from,则从不调用处理程序。由于我使用信号量来检测是否已收到某些数据,因此程序会锁定并且永远不会触发循环。
我已经验证(使用网络分析器)发送方设备正确地在UDP套接字上发送数据。
我已在以下代码中隔离了该问题。
#include <boost\array.hpp>
#include <boost\asio.hpp>
#include <boost\thread.hpp>
#include <boost\interprocess\sync\interprocess_semaphore.hpp>
#include <iostream>
typedef boost::interprocess::interprocess_semaphore Semaphore;
using namespace boost::asio::ip;
class ReceiveUDP
{
public:
boost::thread* m_pThread;
boost::asio::io_service m_io_service;
udp::endpoint m_local_endpoint;
udp::endpoint m_sender_endpoint;
udp::socket m_socket;
size_t m_read_bytes;
Semaphore m_receive_semaphore;
ReceiveUDP() :
m_socket(m_io_service),
m_local_endpoint(boost::asio::ip::address::from_string("192.168.0.254"), 11),
m_sender_endpoint(boost::asio::ip::address::from_string("192.168.0.11"), 5550),
m_receive_semaphore(0)
{
Start();
}
void Start()
{
m_pThread = new boost::thread(&ReceiveUDP::_ThreadFunction, this);
}
void _HandleReceiveFrom(
const boost::system::error_code& error,
size_t received_bytes)
{
m_receive_semaphore.post();
m_read_bytes = received_bytes;
}
void _ThreadFunction()
{
try
{
boost::array<char, 100> recv_buf;
m_socket.open(udp::v4());
m_socket.bind(m_local_endpoint);
m_io_service.run();
while (1)
{
#if 1 // THIS WORKS
m_read_bytes = m_socket.receive_from(
boost::asio::buffer(recv_buf), m_sender_endpoint);
#else // THIS DOESN'T WORK
m_socket.async_receive_from(
boost::asio::buffer(recv_buf),
m_sender_endpoint,
boost::bind(&ReceiveUDP::_HandleReceiveFrom, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
/* The program locks on this wait since _HandleReceiveFrom
is never called. */
m_receive_semaphore.wait();
#endif
std::cout.write(recv_buf.data(), m_read_bytes);
}
m_socket.close();
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
};
void main()
{
ReceiveUDP receive_thread;
receive_thread.m_pThread->join();
}
信号量上的timed_wait是首选,但出于调试目的,我使用了阻塞等待,如上面的代码所示。
我错过了什么吗?我的错误在哪里?
答案 0 :(得分:8)
您对io_service.run()
的来电正在退出,因为io_service
没有工作要做。然后代码进入while
循环并调用m_socket.async_receive_from
。此时io_service
没有运行,它从不读取数据并调用您的处理程序。
您需要在调用io_service run之前安排要完成的工作:
即:
// Configure io service
ReceiveUDP receiver;
m_socket.open(udp::v4());
m_socket.bind(m_local_endpoint);
m_socket.async_receive_from(
boost::asio::buffer(recv_buf),
m_sender_endpoint,
boost::bind(&ReceiveUDP::_HandleReceiveFrom, receiver,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
处理程序函数将执行以下操作:
// start the io service
void HandleReceiveFrom(
const boost::system::error_code& error,
size_t received_bytes)
{
m_receive_semaphore.post();
// schedule the next asynchronous read
m_socket.async_receive_from(
boost::asio::buffer(recv_buf),
m_sender_endpoint,
boost::bind(&ReceiveUDP::_HandleReceiveFrom, receiver,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
m_read_bytes = received_bytes;
}
然后你的线程只是等待信号量:
while (1)
{
m_receive_semaphore.wait();
std::cout.write(recv_buf.data(), m_read_bytes);
}
注意:
答案 1 :(得分:0)
m_io_service.run()
立即返回,因此没有人派遣完成处理程序。请注意,io_service::run
是基于asio的应用程序的一种“消息循环”,只要您希望asio功能可用,它就应该运行(这是一个简单的描述,但它对您的情况来说已经足够了)。
此外,您不应该在循环中调用async.operation。相反,在前一个的完成处理程序中发出后续的async.operation - 以确保2个async.reads不会同时运行。
请参阅asio示例以查看典型的asio应用程序设计。