ASIO async_accept() and c++2011 lambas

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

ASIO async_accept() and c++2011 lambas

Claude
Hi,
I found this example for use the async_accept() method:

void accept_handler(const boost::system::error_code& error)
{
  if (!error)
  {
    // Accept succeeded.
  }
}

...

boost::asio::ip::tcp::acceptor acceptor(io_service);
...
boost::asio::ip::tcp::socket socket(io_service);
acceptor.async_accept(socket, accept_handler);

It is possible use the async_accept with a (c++0x) lambda function instead of the function accept_handler()?
Reply | Threaded
Open this post in threaded view
|

Re: ASIO async_accept() and c++2011 lambas

niXman
Hi Claude!

Yes, it posible.

acceptor.async_accept(socket,
   [](const boost::system::error_code& e) {
   }
);

niXman.

2011/7/27 Claude <[hidden email]>
Hi,
I found this example for use the async_accept() method:

void accept_handler(const boost::system::error_code& error)
{
 if (!error)
 {
   // Accept succeeded.
 }
}

...

boost::asio::ip::tcp::acceptor acceptor(io_service);
...
boost::asio::ip::tcp::socket socket(io_service);
acceptor.async_accept(socket, accept_handler);

It is possible use the async_accept with a (c++0x) lambda function instead
of the function accept_handler()?

--
View this message in context: http://boost.2283326.n4.nabble.com/ASIO-async-accept-and-c-2011-lambas-tp3697807p3697807.html
Sent from the Boost - Users mailing list archive at Nabble.com.
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: ASIO async_accept() and c++2011 lambas

Igor R.
In reply to this post by Claude
> I found this example for use the async_accept() method:
>
> void accept_handler(const boost::system::error_code& error)
> {
>  if (!error)
>  {
>    // Accept succeeded.
>  }
> }
>
> ...
>
> boost::asio::ip::tcp::acceptor acceptor(io_service);
> ...
> boost::asio::ip::tcp::socket socket(io_service);
> acceptor.async_accept(socket, accept_handler);
>
> It is possible use the async_accept with a (c++0x) lambda function instead
> of the function accept_handler()?

Sure, you can.

...async_accept(socket, [=] (const boost::system::error_code &error)
{doSomething();});

Or, in a more readable form:
auto handler = [=] (const boost::system::error_code &error)
{
  doSomething();
};

...async_accept(socket, handler);
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: ASIO async_accept() and c++2011 lambas

Claude
Thanks!

But it is possible to use the  "error" variable outside the lambda functions?

I would like to get something like this:

async_accept(socket, [=] (const boost::system::error_code &error)
                                      { // });

if (!error)
{
 //here there are a valid connection
}
Reply | Threaded
Open this post in threaded view
|

Re: ASIO async_accept() and c++2011 lambas

Igor R.
> I would like to get something like this:
>
> async_accept(socket, [=] (const boost::system::error_code &error)
>                                      { // });
>
> if (!error)
> {
>  //here there are a valid connection
> }


"error" is just an argument of the anonymous function (lambda), and
it's accessible only inside that function (like any other function
argument).
What do you try to achieve?
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: ASIO async_accept() and c++2011 lambas

Claude
Ok..I want to avoid using other functions.

It is possible t copy the value of error in a lambda external variable?

     const boost::system::error_code myError;
     acceptor.async_accept(*socket,[&myError] (const boost::system::error_code &error)
                                        {
                                         myError = error; //???
                                         } );

Reply | Threaded
Open this post in threaded view
|

Re: ASIO async_accept() and c++2011 lambas

Igor R.
> It is possible t copy the value of error in a lambda external variable?
>
>     const boost::system::error_code myError;
>     acceptor.async_accept(*socket,[&myError] (const
> boost::system::error_code &error)
>                                        {
>                                         myError = error; //???
>                                         } );


Yes, you can capture a global object by reference and modify it (if it
non-const) inside a lambda.
But this is realy out of boost scope. For details, please read the following:
http://msdn.microsoft.com/en-us/library/dd293608.aspx
http://msdn.microsoft.com/en-us/library/dd293603.aspx
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: ASIO async_accept() and c++2011 lambas

Andrew Holden
In reply to this post by Claude
On Wednesday, July 27, 2011 5:19 AM, Claude wrote:
> But it is possible to use the  "error" variable outside the lambda
functions?

>
> I would like to get something like this:
>
> async_accept(socket, [=] (const boost::system::error_code &error)
>                                      { // });
>
> if (!error)
> {
>   //here there are a valid connection
> }

I'm not sure this can be made to do what you want.  When async_accept
returns, you do NOT have a valid connection.  The connection will
actually be accepted long after async_accept returns.  By "long after",
I mean any time span from milliseconds to weeks.  You truly want to test
the "error" variable inside the lambda because it is highly likely that
whatever function originally called async_accept has already returned
and gone out of scope.
_______________________________________________
Boost-users mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/boost-users