|
Boost Users : |
Subject: Re: [Boost-users] async_send handler problem
From: Aaron_Wright_at_[hidden]
Date: 2013-12-17 11:27:12
>>>Yes, I truly understand this but for some weired reason this doesn't
work
>>> at all. In my case I launch the run function in the constructor of the
>>> main class right after the sender class had been instantiated:
>>>
>>> SJ::SJ(){
>>>
>>> /// SENDER INIT
>>> dFC->mySender = new sender(this);
>>>
>>>
>>> boost::thread t =
>>> boost::thread(boost::bind(&boost::asio::io_service::run,
>>> &dFC->io_service));
>>> t.join();
>>>
>>> // OR dFC->io_service.run();
>>> }
>>>
>>>
>>> In there anything wrong with this ?
>>
>>
>>It seems that you call io_serivce::run() before any async operation
>>was issued. In such a case, run() just exits immediately, as I
>>explained in one of my previous comments. In order to avoid such a
>>behavior,
>>associate io_service::work object with this io_service:
>>
http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference/io_serv
>>ice.html
>
>Allright - this helps but still issues (see below).
>
>
>>In general, it's highly recommended to read the Asio documentation -
>>all these issues are described there.
>
>
>Yes, you can assume this but it seems the case is a little more complex
>than it is the case with the default use cases:
>
>This simple example works fine:
>
>----------------------------
>boost::asio::io_service io_service;
>
>int i = 0;
>
>void handle_send(const boost::system::error_code& error, std::size_t
>bytes_transferred){
> i++;
> std::cout << "async_send_to return " << error << ": " <<
>bytes_transferred << " transmitted " << i << std::endl;
>}
>
>
>int main(int argc, char *argv[]){
>if(argc==3){
>
> boost::asio::io_service::work work(io_service);
> boost::thread t = boost::thread
>(boost::bind(&boost::asio::io_service::run, &io_service));
> //t.join();
>
> boost::asio::ip::udp::socket socket(io_service);
> boost::asio::ip::udp::endpoint remote_endpoint;
>
> socket.open(boost::asio::ip::udp::v4());
>
> remote_endpoint = boost::asio::ip::udp::endpoint(
> boost::asio::ip::address::from_string(argv[1]),
>boost::lexical_cast<int>(argv[2]));
>
> std::cout << "Send to " << remote_endpoint << std::endl;
>
> while (1)
> socket.async_send_to(boost::asio::buffer("message", 7),
remote_endpoint,
> boost::bind(&handle_send,
>boost::asio::placeholders::error,
>boost::asio::placeholders::bytes_transferred));
>
>return 0;
>}
>-----------------------------
>
>but with respect to my app this does not work:
>
>I am specifying the io_service thread in my constructor.
>
>SJ::SJ(){
> boost::asio::io_service::work work(dFC->io_service);
> boost::thread myThread(boost::bind(&boost::asio::io_service::run,
>&dFC->io_service));
>}
>
>
>I have a handle in my custom SJ class:
>
>void SJ::handleSend(const boost::system::error_code& error, std::size_t
>bytes_transferred){
> cout << "SENT FROM HANDLER" << endl;
>}
>
>I have a send function:
>
>void SJ::sendIt(){
> cout << "Send Funktion" << endl;
>
> dFC->s->async_send_to(boost::asio::buffer("message", 7),
>remote_endpoint,
> boost::bind(&handleSender, boost::asio::placeholders::error,
>boost::asio::placeholders::bytes_transferred));
>}
>
>
>The send function is called from an *non-class-member* callback thread
>(my->sj->sendIt()):
>
>static int audioCallback( void *data ){
> SJ::callbackdata *my;
> my = (SJ::callbackdata *) data;
>
>
> my->sj->sendIt();
>}
>
>
>Not that everything works fine when I call the send function from within
>my SJ-class but when I call the send function from the external callback
>thread the handler is not triggering anymore.
>
>Does this help to clarify what (or where) my problem is ?
>
>Thanks again
>
>Alex
Isn't this caused because boost::asio::io_service::work is a local
variable to the constructor, so when the constructor is done, work is
destructed, and nothing is left to keep io_service.run() from returning.
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