c++ - Cancel connection is boost::asio -
from standard ssl client example. call function.
boost::asio::async_connect(socket_.lowest_layer(), endpoint_iterator, boost::bind(&sslclient::handle_connect, this, boost::asio::placeholders::error)); but function called, , program connecting. cancel request , stop connection! how can that?
special case: have objects in thread. there way in case?
now if try this, program doesn't respond. don't see way force stop!
there several ways achieve want ¹.
you hard-stop service (
service.stop()). leaves no control on running operations. it's "nuclear" approach, say.the controlled way call
cancel()cancel asynchronous operations associated socket.
socket_.cancel()
now, have additional task of maintaining lifetime of connection object (presumably this in bound completion handler). common pattern use make connection class derive enable_shared_from_this , bind completion handler shared_from_this() instead of this.
that way, shared connection object automatically "go away" after last pending async operation has been canceled, , don't have worry leaking connection objects.
¹ short of exit, abort, quick_exit etc. :)
Comments
Post a Comment