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 ¹.

  1. you hard-stop service (service.stop()). leaves no control on running operations. it's "nuclear" approach, say.

  2. 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

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -