我遇到了在boost :: asio :: async_read_until之后定期无限等待处理程序调用。这出现在两种情况下:
鉴于每次都没有,在我看来,我错误地使用了async_read_until,async_write和boost :: asio :: strand。
以下是简化服务器的架构:
(服务器的某些命令不需要响应,有时需要在服务器上发生某些事件后向客户端发送命令,即无需从客户端获取数据。值得在同一套接字上说明永远不会同时运行多个async_read_until操作和多个async_write操作。)
客户的一个:
服务器代码:
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
class server_session
: public boost::enable_shared_from_this<server_session>
{
public:
server_session(boost::asio::io_service& io_service)
: _io_service(io_service)
, _socket(io_service)
, _strand(io_service)
, _delimiter('\b')
{}
boost::asio::ip::tcp::socket& socket()
{
return _socket;
}
void read()
{
boost::asio::async_read_until(
_socket,
_streambuf,
_delimiter,
_strand.wrap(
boost::bind(
&server_session::handle_read,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
)
);
}
void handle_read(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
if (ec)
{
std::cerr << "async_read_until error: " << ec.message() << std::endl;
return;
}
std::istream is(&_streambuf);
std::string msg;
std::getline(is, msg, _delimiter);
read();
_io_service.post(
boost::bind(
&server_session::proc_msg,
shared_from_this(),
msg
)
);
}
void proc_msg(const std::string msg)
{
// command proc here
std::cout << "received: " << msg << std::endl;
static std::uint64_t i = 0;
write("resp_" + std::to_string(i++));
}
void write(const std::string& msg)
{
_strand.post(
boost::bind(
&server_session::write_impl,
shared_from_this(),
boost::shared_ptr<std::string>(new std::string(msg + _delimiter))
)
);
}
private:
void write_impl(const boost::shared_ptr<std::string>& text_ptr)
{
boost::asio::async_write(
_socket,
boost::asio::buffer(text_ptr->data(), text_ptr->size()),
_strand.wrap(
boost::bind(
&server_session::handle_write,
shared_from_this(),
text_ptr,
boost::asio::placeholders::error
)
)
);
}
void handle_write(const boost::shared_ptr<std::string>, const boost::system::error_code& ec)
{
if (ec)
{
std::cerr << "async_write error: " << ec.message() << std::endl;
}
}
boost::asio::io_service& _io_service;
boost::asio::ip::tcp::socket _socket;
boost::asio::strand _strand;
boost::asio::streambuf _streambuf;
char _delimiter;
};
class server
{
public:
server(int port)
: _acceptor(
_io_service,
boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),port)
)
{
start_accept();
boost::thread_group thd_grp;
for (int i = 0; i < 2; ++i)
{
thd_grp.create_thread(
boost::bind(&boost::asio::io_service::run, &_io_service)
);
}
thd_grp.join_all();
}
private:
void start_accept()
{
_session_ptr.reset(new server_session(_io_service));
_acceptor.async_accept(
_session_ptr->socket(),
boost::bind(
&server::handle_accept,
this,
boost::asio::placeholders::error
)
);
}
void server::handle_accept(const boost::system::error_code& ec)
{
if (ec)
{
std::cerr << "handle_accept error: " << ec.message() << std::endl;
return;
}
_session_ptr->read();
start_accept();
}
boost::asio::io_service _io_service;
boost::asio::ip::tcp::acceptor _acceptor;
boost::shared_ptr<server_session> _session_ptr;
};
int main(int argc, char** argv)
{
if (argc != 2)
{
std::cerr << "usage: " << argv[0] << " <port>";
return EXIT_FAILURE;
}
server s(std::stoi(argv[1]));
return EXIT_SUCCESS;
}
客户代码:
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
class client
{
public:
client(const std::string& host, const std::string& port)
: _socket(_io_service)
, _resolver(_io_service)
, _query(host, port)
, _delimiter('\b')
{
_iterator = _resolver.resolve(_query);
boost::asio::async_connect(
_socket,
_iterator,
boost::bind(
&client::handle_connect,
this,
boost::asio::placeholders::error
)
);
_io_service.run();
}
void handle_connect(const boost::system::error_code& ec)
{
if (ec)
{
std::cerr << "async_connect error: " << ec.message() << std::endl;
return;
}
write();
}
void write()
{
static std::uint64_t i = 0;
boost::shared_ptr<std::string> msg_ptr(new std::string("req_" + std::to_string(i++) + _delimiter));
boost::asio::async_write(
_socket,
boost::asio::buffer(msg_ptr->data(), msg_ptr->size()),
boost::bind(
&client::handle_write,
this,
msg_ptr,
boost::asio::placeholders::error
)
);
}
void handle_write(const boost::shared_ptr<std::string>, const boost::system::error_code& ec)
{
if (ec)
{
std::cerr << "async_write error: " << ec.message() << std::endl;
return;
}
read();
}
void read()
{
boost::asio::async_read_until(
_socket,
_streambuf,
_delimiter,
boost::bind(
&client::handle_read,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
}
void handle_read(const boost::system::error_code& ec, std::size_t bytes_transferred)
{
if (ec)
{
std::cerr << "async_read_until error: " << ec.message() << std::endl;
return;
}
std::istream is(&_streambuf);
std::string msg;
std::getline(is, msg, _delimiter);
std::cout << "received: " << msg << std::endl;
write();
}
boost::asio::io_service _io_service;
boost::asio::ip::tcp::socket _socket;
boost::asio::ip::tcp::resolver _resolver;
boost::asio::ip::tcp::resolver::query _query;
boost::asio::ip::tcp::resolver::iterator _iterator;
boost::asio::streambuf _streambuf;
char _delimiter;
};
int main(int argc, char** argv)
{
if (argc != 3)
{
std::cerr << "usage: " << argv[0] << " <host> <port>";
return EXIT_FAILURE;
}
client c(argv[1], argv[2]);
return EXIT_SUCCESS;
}
async_write对套接字上的async_read_until是否已经运行,而使用strand是否正确?
是否有可能,async_read_until以某种方式在内部阻塞套接字,实际上,数据不会发送到客户端?
如果代码可能无法正常工作,我将不胜感激
我正在使用boost 1.58,平台 - Win7和Win8。使用上述结果在localhost和LAN上测试。
答案 0 :(得分:0)
我至少可以告诉你,你应该使用asio :: deadline_timer并在每次启动新的异步操作时重置计时器。您应该特别使用async_read_until之类的操作来执行此操作,因为您正在指定完成调用的条件,这可能永远不会满足。查找deadline_timer示例并在每个异步操作上实现async_wait,指定该操作的截止日期。在超时操作中,您可以取消挂起的异步操作,错误输出或重启,无论什么是合适的。