Re: [Cppcms-users] Can I create an asynchronous socket connection from within an asynchronously mou
Brought to you by:
artyom-beilis
From: Artyom B. <art...@ya...> - 2012-10-18 11:53:09
|
> From: Dietrich Müller <die...@gm...> >To: cpp...@li... >Sent: Friday, October 12, 2012 6:32 PM >Subject: [Cppcms-users] Can I create an asynchronous socket connection from within an asynchronously mounted application? > > >Hello, I try to create a socket connection from within my application and I seem to fail. >I tried to setup the code like in this boost asio sample for an asynchronous http client (http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/example/http/client/async_client.cpp) without the resolving part. >Btw this resolver is pretty useful. Can it be integrated into booster aio? > >//the call >socket->async_connect(endpoint, std::bind(&ClassX::onConnected, this, std::placeholders::_1)); > >//the first callback >void ClassX::onConnected(const booster::system::error_code &e) { > if(!e) > //print some member of ClassX > //... > //and write something to the socket > socket->async_write(buff, std::bind(&ClassX::onWritten, this, std::placeholders::_1)); >} > Where buf's content resides? The memory areas pointed by buff should remain valid untill asynchronous write operation is cimpleted. Also if you want any help, show the full code >//onWritten callback >//onRead callback > >Now the problem is that as soon as I access a class member in any callback, the result is a segmentation fault. >If I pass shared_from_this() and not this I get a assertion error in enable_shared_from_this.h:56 that p.get() == this is not true. What is shared from this? I assume you try to make application with shared_from_this? You can't. Because it is not created with shared_ptr, also if application exists all the time than you don't need to care about shared from this stuff and use ordinary this. >I think the problems are related to some synchronisation issues between the main and the application thread, as the synchronous calls work. >Please give me some advice how I correctly use the socket in my application. If you use asynchronous application than its thread and the event loop threads are the same. **Next Time** Show the full code. Artyom Beilis -------------- CppCMS - C++ Web Framework: http://cppcms.com/ CppDB - C++ SQL Connectivity: http://cppcms.com/sql/cppdb/ |