我有一個tcp客戶端輪詢服務器的截止日期的答案,以便如果服務器無法到達客戶端不被阻止。我遇到的問題是,async_wait
似乎永遠不會調用它的處理程序,當它無法連接時有效地阻止客戶端。每撥打tcpPoll
都會有自己的線程(這就是爲什麼我創建了一個新的io_service
),但即使沒有多線程,它也不起作用。而且,NetworkEntity
對象在其生命週期內可以多次呼叫tcpPoll
。boost :: asio :: deadline_timer不會調用處理程序
我的問題是:什麼阻止deadline_timer
調用它的處理程序? (以及如何解決它)
下面是一些相關的代碼,這是工作,只要沒有失敗(連接,寫,讀)(抱歉,如果這是一個有點長):
void NetworkEntity::stop()
{
stopped_ = true;
//close socket
//cancel timeout
}
void NetworkEntity::check_timeout(const boost::system::error_code& error)
{
if (stopped_)
return;
if (timeout_.expires_at() <= boost::asio::deadline_timer::traits_type::now())
{
stop();
timeout_.expires_at(boost::posix_time::pos_infin);
std::cout << address_ << " timed out\n";
}
timeout_.async_wait(boost::bind(&NetworkEntity::check_timeout, this, boost::asio::placeholders::error));
}
std::vector<std::string> NetworkEntity::tcpPoll(const char* message, const char endOfMessage)
{
boost::asio::io_service io_service;
stopped_ = false;
timeout_.expires_from_now(boost::posix_time::seconds(TIMEOUT_));
timeout_.async_wait(boost::bind(&NetworkEntity::check_timeout, this, boost::asio::placeholders::error));
tcp::resolver resolver(io_service);
start_connect(&io_service, resolver.resolve(tcp::resolver::query(address_, port_)), message, endOfMessage);
io_service.run();
//retrieve answer from class
//return answer
}
void NetworkEntity::start_connect(boost::asio::io_service* io_service, tcp::resolver::iterator endpoint_iterator, const std::string message, const char endOfMessage)
{
socket_.reset(new tcp::socket(*io_service));
socket_->async_connect(endpoint_iterator->endpoint(),
boost::bind(&NetworkEntity::handle_connect, this, io_service, boost::asio::placeholders::error, endpoint_iterator, message, endOfMessage));
}
void NetworkEntity::handle_connect(boost::asio::io_service* io_service, const boost::system::error_code& err, tcp::resolver::iterator endpoint_iterator, const std::string message, const char endOfMessage)
{
if(stopped_)
return;
if (err)
{
std::cout << "Connect error: " << err.message() << "\n";
stop();
}
else
{
start_write(message, endOfMessage);
}
}
void NetworkEntity::start_write(const std::string message, const char endOfMessage)
{
std::ostream request_stream(&request_);
request_stream << message;
boost::asio::async_write(*socket_, request_,
boost::bind(&NetworkEntity::handle_write, this, boost::asio::placeholders::error, endOfMessage));
}
void NetworkEntity::handle_write(const boost::system::error_code& error, const char endOfMessage)
{
if (stopped_)
return;
if (!error)
{
//sleep for 500ms to let time for the reciever to process info (had a bug on this one)
start_read(endOfMessage);
}
else
{
std::cout << "write error : " << error.message() << "\n";
stop();
}
}
void NetworkEntity::start_read(const char endOfMessage)
{
boost::asio::async_read_until(*socket_, answer_, endOfMessage,
boost::bind(&NetworkEntity::handle_read, this, boost::asio::placeholders::error));
}
void NetworkEntity::handle_read(const boost::system::error_code& error)
{
if (stopped_)
return;
if (error)
{
std::cout << "read error : " << error.message() << "\n";
stop();
}
else
{
stop();
}
}
Pleeeeeeeeeeeeeease下次讓您的樣品自成一體嗎?這種錯誤可能很明顯。通過這種方式,我們花了很多時間'製作'io服務,deadline_timers,地址/端口值,「猜測」套接字將shared_ptr爲1(爲什麼?),並且request_/answer_(可能)是streambuf。最糟糕的是,我們根據自己的經驗來執行這些操作,而不一定是您真正做到了這一點(讓您更難以看出是什麼絆倒了您) – sehe
注意!我將在下次 –