Discussion:
[asio-users] issue with io_service to run multiple time
Rajat Singh
2013-03-12 18:49:28 UTC
Permalink
Hi All,

In my question. I am a client and my purpose is only to write at server
whenever required.

I have one singleton class(Caller class) in which I call my client
program(Client) to write any message on server and
then calling boost::asio::io_service::run through one thread.
My client program(Client) is also singleton class.

My program works fine if server is already running.
But If server is not running then fist I have to check whenever server is
running or not before writing the message.

To check each time whether server is available or not I have to call
Caller::run() and running io_service.

Without running io_service I can't check whether server is running or not
as I am using all sync in my program.
Everything is in async (asycn connect, sync write)

My problem is that sometime my program goes to in +D1 state(Uninterruptible
sleep) and then I am not able to close my program.
Is this due to io_service() ?

How can I check whether server is on or not ?

class Caller
{
public:

static Caller* getInstance()
{
static Caller Caller;
return &Caller;
}

void run()
{
pWork.reset( new boost::asio::io_service::work(io_service) );

////////////calling client program////////////////
Client::getInstance(io_service)->start();

thread = boost::thread(boost::bind(&boost::asio::io_service::run,
&io_service));
}

boost::asio::io_service io_service;
boost::shared_ptr<boost::asio::io_service::work> pWork;

boost::thread thread;

private:
Caller()
{
}
~Caller()
{
pWork.reset();
thread.join();
}
};


///////////my client program (Singleton Class)//////////////////
//////message write function///////////

void Client::sendResponse(std::string& messag)

{

boost::system::error_code
error;
boost::asio::ip::tcp::endpoint endpoint =
socket().remote_endpoint(error);

if ( error )
{
DEBUG_2("could not write:
",boost::system::system_error(error).what());
this->stop(); /////this will close the socket////
Caller::getInstance()->run(); //////it will call the caller program
to call the connect function of client program /////

}
else

boost::asio::async_write(


_socket,

boost::asio::buffer( message.c_str(), message.size()),

_strand.wrap(


boost::bind(

&Client::writeHandler,
this,

boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred

}


Client *Client::getInstance(boost::asio::io_service& io_service)
{
if (!Client)
{
Client = new Client(io_service);
}
return Client;
}

Regards
Rajat Singh
+91-8559004930
Igor R
2013-03-12 20:33:54 UTC
Permalink
Post by Rajat Singh
I have one singleton class(Caller class) in which I call my client
program(Client) to write any message on server and
then calling boost::asio::io_service::run through one thread.
My client program(Client) is also singleton class.
My program works fine if server is already running.
But If server is not running then fist I have to check whenever server is
running or not before writing the message.
To check each time whether server is available or not I have to call
Caller::run() and running io_service.
I admit I couldn't understand your question, but if io_service::run()
exits and should be called again -- call io_service::reset() prior to
re-running.
Post by Rajat Singh
boost::asio::async_write(
_socket,
boost::asio::buffer( message.c_str(), message.size()),
The above is incorrect, because asio::buffer function doesn't copy
your string, it just creates an adapter, and the result of c_str()
wouldn't outlive the async.write operation.
Rajat Singh
2013-03-13 04:07:57 UTC
Permalink
In my other program it is working fine

I am putting my message into deque and and then write the message

void Connection::writeImpl(const std::string &message)
{
_outbox.push_back( message );
if ( _outbox.size() > 1 ) {
return;
}
this->write();
}

void Connection::write()
{
const std::string& message = _outbox[0];
boost::asio::async_write(
_socket,
boost::asio::buffer( message.c_str(), message.size() ),
_strand.wrap(
boost::bind(
&Connection::writeHandler,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
)
);
}

void Connection::writeHandler(
const boost::system::error_code& e,
const size_t bytesTransferred
)
{
_outbox.pop_front();

if ( e ) {
DEBUG_2("could not write: ",boost::system::system_error(e).what());
this->close();
}
if ( !_outbox.empty() ) {
this->write();
}
}


my question is how can I check whether server is on or not.......?
do I need to run io_service again and again.... or there is any other way?
Post by Igor R
Post by Rajat Singh
I have one singleton class(Caller class) in which I call my client
program(Client) to write any message on server and
then calling boost::asio::io_service::run through one thread.
My client program(Client) is also singleton class.
My program works fine if server is already running.
But If server is not running then fist I have to check whenever server is
running or not before writing the message.
To check each time whether server is available or not I have to call
Caller::run() and running io_service.
I admit I couldn't understand your question, but if io_service::run()
exits and should be called again -- call io_service::reset() prior to
re-running.
Post by Rajat Singh
boost::asio::async_write(
_socket,
boost::asio::buffer( message.c_str(), message.size()),
The above is incorrect, because asio::buffer function doesn't copy
your string, it just creates an adapter, and the result of c_str()
wouldn't outlive the async.write operation.
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
http://p.sf.net/sfu/appdyn_d2d_mar
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
--
*Rajat Singh*

*
*

*Office Address:*

*uTrade Solutions Private Ltd
*2nd floor, landmark plaza (f3). Quarkcity SEZ, A40A,

Industrial Area, Phase 8 extension, Mohali, 160071, India



E : ***@utradesolutions.com <***@utradesolutions.com>

W : www.utradesolutions.com
Continue reading on narkive:
Loading...