Boost logo

Boost Users :

Subject: Re: [Boost-users] pass asio socket to another class
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2014-12-11 21:02:06


On 11/12/2014 02:55, cap wrote:
> 1. If I is not waiting for finishing of transferring, instance of
> SslHandshake will be destroyed automatically (in the ~SslHandshake I will
> destroy both socket objects and transferring in the SslRequest will be
> aborted).
> 2. If I start transferring with waiting, after a few iterations
> SslRequest::ReadSocketOut will be called successfully, but the
> readSocketHandle will not be called (so, will be called but with timeout
> error).
>
> So, I need to handshake in one class and transfer data into another class.
>
> How to prevent destroying instance of SslHandshake until data transferring
> will be done into SslRequest or how to fix socket reading without callback
> calling after a few iterations?

 From the code you posted, it looks like you need to go back to basics
and understand how the Asio service threading model works. Have a
closer look at the async examples -- the basic idea is that nothing
happens without an io_service::run call being in progress on some thread.

In many of the examples this is done by queuing up the initial
operations to be performed first and then running the io_service on the
main thread (which then returns once all the operations are completed),
but it's more common in real code to have the io_service on a dedicated
worker thread (with some io_service::work in scope to prevent it exiting
when there's nothing to do) and queuing up operations as needed (though
this way you need to be more careful about cross threaded operations).

Either way works (they're both good in different cases), but what you
should never do is to spin up a thread specifically for one operation.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net