问题
I received the following exception in handleRead()
function from boost::asio::read()
when I read and write more than 100000 of messages.
terminate called after throwing an instance of
'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
what(): asio.ssl error
Implementation is below
Read Function
void start()
{
boost::asio::async_read(_socket,
boost::asio::buffer(_messageHeader, 8),
_strand.wrap(
boost::bind(
handleRead,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
)
);
}
void handleRead(const boost::system::error_code& e,
std::size_t bytesTransferred)
{
if (!e)
{
BOOST_STATIC_ASSERT( sizeof(MessageHeader) == 8 );
Message message;
message.header.fromString(
std::string(_messageHeader.data(),
sizeof(MessageHeader)
)
);
boost::asio::read(_socket,
boost::asio::buffer(_buffer, message.header.length));
message.body = std::string(_buffer.data(), message.header.length);
this->start();
}
else {
this->close();
}
}
Implemented message queue to write message
void sendResponse(const Message& response)
{
std::string stringToSend(response.toString());
boost::system::error_code error;
boost::asio::ip::tcp::endpoint endpoint = socket().remote_endpoint(error);
if ( error ) {
this->close();
}
_strand.post(
boost::bind(
writeImpl,
shared_from_this(),
stringToSend
)
);
}
void writeImpl(const std::string &message)
{
_outbox.push_back( message );
if ( _outbox.size() > 1 ) {
return;
}
this->write();
}
void write()
{
const std::string& message = _outbox[0];
boost::asio::async_write(
_socket,
boost::asio::buffer( message.c_str(), message.size() ),
_strand.wrap(
boost::bind(
writeHandler,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
)
);
}
void writeHandler(
const boost::system::error_code& e,
const size_t bytesTransferred
)
{
_outbox.pop_front();
if ( e ) {
this->close();
}
if ( !_outbox.empty() ) {
this->write();
}
}
回答1:
Looks like this is a client app? If this is a server, then you might want to rethink the synchronous read being done in HandleRead and make everything asynchronous. But, for a client app or a server that has few connections it is probably ok.
100,000 is a very interesting number - to people, not to computers. It is a number that suggests that someone somewhere has set an artificial limit. So, if your app is a client, then check the server code to make sure it is not closing the socket connection after 100,000 messages and vice-a-versa if your app is the server. Maybe there is a hard coded array somewhere that is dimensioned to 100,000 and throwing an error when the limit is hit which has a side effect of closing the socket connection.
Whereever you test for an error condition from an ASIO operation, add some logging info when an error is detected. At a minimum, write out some debug data to the console. For example, in your handleRead method, right before calling the close method, add some debug logging. In my app, I have a class that writes it out to both the console and a log file.
来源:https://stackoverflow.com/questions/14869128/boost-asio-error-when-read-write-more-than-100000-messages