Discussion:
[asio-users] BOOST::async_wait error when sending/receiving bulk messages
Rajat Singh
2013-03-02 04:33:16 UTC
Permalink
In my application I am sending and receiving bulk of messages and behaving
as a server. In the mean while I check the session timeout of the client.
My program works fine when I don't use the async_wait in my application.
but when I use async_wait then my application gets crashed after sending
and receiving some 100,000 - 200,000 messages and following error comes.

"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"
whenever server receive any message from client then it calls start().

Here is my code implementation

void Connection::start()
{
this->resetTimer();

memset(_messageHeader.c_array(), 0, 8);
boost::asio::async_read(_socket,
boost::asio::buffer(_messageHeader, 8),
_strand.wrap(
boost::bind(
&Connection::handleRead,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
)
);
}

void Connection::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)
)
);
uint32_t type = message.header.type;
uint32_t length = message.header.length;

boost::asio::async_read(_socket,
boost::asio::buffer(_buffer, message.header.length),
_strand.wrap(
boost::bind(
&Connection::messageRead,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred,
type,
length
)
)
);
}
else {
this->close();
}
}
void Connection::messageRead(const boost::system::error_code& e,
std::size_t bytesTransferred, uint32_t type, uint32_t length){
if (!e) {

Message message;

message.header.type = type;
message.header.length = length;

message.body = std::string(_buffer.data(), message.header.length);
memset(_buffer.c_array(), 0, message.header.length);

_timeoutCount = 0;

this->start();
}
else {
this->close();
}}


void Connection::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(
&CclConnection::writeImpl,
shared_from_this(),
stringToSend
)
);
}


void Connection::writeImpl(const std::string &message)
{
_outbox.push_back( message );
if ( _outbox.size() > 1 ) {
return;
}
this->write();
}

void Connection::write()
{
const std::string& message = _outbox[0];
boost::asio::async_write(
_socket,
boost::asio::buffer( message.c_str(), message.size() ),
_strand.wrap(
boost::bind(
&Connection::writeHandler,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
)
);
}

void Connection::writeHandler(
const boost::system::error_code& e,
const size_t bytesTransferred
)
{
_outbox.pop_front();

if ( e ) {
this->close();
}
if ( !_outbox.empty() ) {
this->write();
}
}

// close connection
void Connection::close()
{
boost::system::error_code ignoredCode;
socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both,
ignoredCode);
}

void Connection::resetTimer()
{
if (Context::getSessionTimeout() > 0)
{
// timer of 10 seconds
_timer.expires_from_now(10);
_timer.async_wait(
_strand.wrap(
boost::bind(&Connection::handleTimeout,
shared_from_this(),
boost::asio::placeholders::error)
)
);
}
}


void Connection::handleTimeout(const boost::system::error_code &e)
{
if (!_loggedIn)
return;

if (!e)
{
boost::system::error_code ec;
boost::asio::ip::tcp::endpoint endpoint = socket().remote_endpoint(ec);

if (ec)
{
this->close();
return;
}
else
{
_timeoutCount++;

std::string ping = "ping";
Message message(REQ_PING, ping);
this->sendResponse(message);

// no activity for quite some time now
// so we just disconnect the session
if (_timeoutCount > 30)
{
ERROR_1(TIMEOUT_OCCURED);
this->close();
}
else
this->start();
}
}
}
Sam Miller
2013-04-08 18:55:28 UTC
Permalink
Post by Rajat Singh
In my application I am sending and receiving bulk of messages and
behaving as a server. In the mean while I check the session timeout
of the client. My program works fine when I don't use the async_wait
in my application. but when I use async_wait then my application
gets crashed after sending and receiving some 100,000 - 200,000
messages and following error comes.
I answered this same question on Stack Overflow, your async_wait() handler
invokes UB by initiating an async_read() operation every time.

http://stackoverflow.com/a/15186648/283302

Sam
Rajat Singh
2013-04-09 03:54:19 UTC
Permalink
Many thanks for the answer.

I had putted the question at same time here too.
Post by Sam Miller
Post by Rajat Singh
In my application I am sending and receiving bulk of messages and
behaving as a server. In the mean while I check the session timeout
of the client. My program works fine when I don't use the async_wait
in my application. but when I use async_wait then my application
gets crashed after sending and receiving some 100,000 - 200,000
messages and following error comes.
I answered this same question on Stack Overflow, your async_wait() handler
invokes UB by initiating an async_read() operation every time.
http://stackoverflow.com/a/15186648/283302
Sam
------------------------------------------------------------------------------
Minimize network downtime and maximize team effectiveness.
Reduce network management and security costs.Learn how to hire
the most talented Cisco Certified professionals. Visit the
Employer Resources Portal
http://www.cisco.com/web/learning/employer_resources/index.html
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
--
*Rajat Singh*

*
*

*Office Address:*

*uTrade Solutions Private Ltd
*2nd floor, landmark plaza (f3). Quarkcity SEZ, A40A,

Industrial Area, Phase 8 extension, Mohali, 160071, India



E : ***@utradesolutions.com <***@utradesolutions.com>

W : www.utradesolutions.com
Loading...