我有一个程序使用 boost asio 异步连接到 3 个 TCP 套接字,使用 adeadline_timer
作为连接超时。在 Windows 上,一切都按预期工作。5 秒后连接超时。但是,在 Unix(WSL 上的 Ubuntu、Linux Mint VM、macOS)上,connectDeadline 永远不会触发。async_connect 操作永远运行。为什么这不起作用,我怎样才能在 Unix 上也能做到这一点?
代码:注意:connect是从主线程(也是一个GUI线程)调用的。
#include "NetManager.h"
NetManager::NetManager(NetManagerListener& listener) : listener(listener),
connectDeadline(io),
socket1(io),
socket2(io),
socket3(io),
asioThread(&NetManager::handleAsioOperations, this){
}
NetManager::~NetManager() {
running = false;
io.stop();
asioThread.join();
}
void NetManager::connect(){
connectCounter = 0;
hasHandledConnectError = false;
socket1.async_connect(
tcp::endpoint(boost::asio::ip::address::from_string(IP_STRING), PORT_1),
boost::bind(&NetManager::handleConnect, this, _1));
socket2.async_connect(
tcp::endpoint(boost::asio::ip::address::from_string(IP_STRING), PORT_2),
boost::bind(&NetManager::handleConnect, this, _1));
socket3.async_connect(
tcp::endpoint(boost::asio::ip::address::from_string(IP_STRING), PORT_3),
boost::bind(&NetManager::handleConnect, this, _1));
connectDeadline.expires_from_now(boost::posix_time::seconds(CONNECT_TIMEOUT));
connectDeadline.async_wait(boost::bind(&NetManager::handleConnectTimeout, this, _1));
}
void NetManager::disconnect(){
//NOTE: Close also cancels incomplete async operations
socket1.close();
socket2.close();
socket3.close();
}
////////////////////////////////////////////////////////////////////////
/// ASIO Handlers
////////////////////////////////////////////////////////////////////////
void NetManager::handleAsioOperations(){
while(running){
io.run(); // Run any async operations
}
}
void NetManager::handleConnect(const boost::system::error_code &ec){
// When connections are canceled the handler is called with operation_aborted. No need to respond to that.
if(ec && ec != boost::asio::error::operation_aborted && !hasHandledConnectError){
hasHandledConnectError = true; // Likely to be 3 repeated errors. Make sure to only handle the first one
cerr << "Connect Failed: " << ec.message() << endl;
connectDeadline.cancel(); // Don't fire the timeout
disconnect(); // Disconnect any already connected sockets
connectedToRobot = false;
listener.onConnect(false);
}else if (!ec){
connectCounter++;
}
if(connectCounter == 3){
cout << "Successful connect" << endl;
connectDeadline.cancel(); // Don't fire the timeout
connectedToRobot = true;
listener.onConnect(true);
}
}
void NetManager::handleConnectTimeout(const boost::system::error_code &ec){
if(ec != boost::asio::error::operation_aborted){
cerr << "Connect timed out." << endl;
disconnect(); // Disconnect any already connected sockets
connectedToRobot = false;
listener.onConnect(false);
}
}
编辑:
令人困惑的是,这在 Unix 操作系统上运行良好:
#include <boost/asio.hpp>
#include <boost/asio/deadline_timer.hpp>
#include <iostream>
#include <thread>
using namespace boost::asio;
using namespace boost::asio::ip;
int main(){
io_service io;
deadline_timer timer1(io);
tcp::socket sock(io);
timer1.expires_from_now(boost::posix_time::seconds(3));
sock.async_connect(tcp::endpoint(boost::asio::ip::address::from_string("10.50.30.1"), 8090), [](const boost::system::error_code &ec){
std::cout << "SocketError: " << ec.message() << std::endl;
});
timer1.async_wait([&](const boost::system::error_code &ec){
std::cout << "First timer" << std::endl;
sock.close();
});
std::thread worker([&](){
while(true){
io.run();
}
});
worker.detach();
while(true){} // Simulate the unavailable main (GUI) thread
}
输出:
First timer
SocketError: Operation canceled