Boost logo

Boost Users :

Subject: [Boost-users] [Asio]How to peocess signal with asio?
From: sjdf (sjdf.maillist_at_[hidden])
Date: 2009-07-07 10:11:36


//----------------------------------------------------------------------------
// Hi all,
// I have a program like below.
// Two questions:
// 1. The program can process multi connections, each connection is a
// session that has some states. The program should catch signal SIGINT
// and SIGTERM, then each session should do different actions according to
// their state. But how to do this? Does it have to have a connection
// manager?
//
// 2. When the function Server::handle_accept is called, a
SessionController
// object will be created. The constructor of SessionController will apply
for
// some resource, if failed, the constructor would throw an exception.
// So, if there is an exception thrown, what should we do? I don't want to
// exit the process, may be the resource will be available after some
seconds,
// then I hope the program continue to accept new connections.
//----------------------------------------------------------------------------
class SessionController;
typedef shared_ptr<SessionController> SessionController_Ptr;

Server::Server(boost::asio::io_service& io_service)
    : io_service_(io_service),
    acceptor_(io_service, tcp::endpoint(
                boost::asio::ip::address::from_string("0.0.0.0"), 10001))
{
    SessionController_Ptr new_connection(new
SessionController(io_service_));
    acceptor_.async_accept(new_connection->socket(),
            boost::bind(&Server::handle_accept, this, new_connection,
                boost::asio::placeholders::error));
}

//----------------------------------------------------------------------------
void Server::handle_accept(SessionController_Ptr new_connection,
        const boost::system::error_code& error)
{
    if (!error)
    {
        new_connection->start();
        try
        {
            new_connection.reset(new SessionController(io_service_));
            acceptor_.async_accept(new_connection->socket(),
                    boost::bind(&Server::handle_accept,
                        this, new_connection,
boost::asio::placeholders::error));
        }
        catch (...)
        {
            // What should we do?
        }
    }
}



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