Boost logo

Boost :

From: berserker_r (berserker_r_at_[hidden])
Date: 2006-11-23 10:07:46


I made the test with the CVS version of boost and I still fall in the
problem that I have reported some days ago :(
Btw I think that a bug in the Windows API GetQueuedCompletionStatus sounds
very strange to me because I think that implementations like this one are
very common or I'm wrong?

//////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "boost/asio.hpp"
#include "boost/thread.hpp"
#include "boost/bind.hpp"

//////////////////////////////////////////////////////////////////////////////////////////////////////////

class Session
{
public:
        Session(boost::asio::io_service &service) : m_socket(service) { }
        ~Session() { m_socket.close(); }

        boost::asio::ip::tcp::socket & socket() { return m_socket; }

        void run()
        {
                m_socket.async_read_some(boost::asio::buffer(m_buffer),
                boost::bind(&Session::read_handler, this, _1, _2));
        }

        std::string generate_response()
        {
                Sleep(10000); // It takes a lot of time to say Hello :)
                static int counter = 0;
                counter++;
                std::stringstream stream;
                stream << "<html><body><h2>Hello " << counter << "</h2></body></html>";
                return stream.str();
        }

        void read_handler(const boost::system::error_code &e, size_t
bytes_transferred)
        {
                if(!e)
                {
                        boost::asio::async_write(m_socket,
boost::asio::buffer(generate_response()),
                        boost::bind(&Session::write_handler, this, _1, _2));
                }
        }

        void write_handler(const boost::system::error_code &e, size_t
bytes_transferred)
        {
                delete this;
        }

private:
        boost::asio::ip::tcp::socket m_socket;
        boost::array<char, 8192> m_buffer;
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////

class Server
{
public:
        Server(int port) : m_service(),
                                 m_acceptor(m_service)
        {
                boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(),
port);
                m_acceptor.open(endpoint.protocol());
                m_acceptor.bind(endpoint);
                m_acceptor.listen();
                accept();
        }

        void accept()
        {
                Session *connection = new Session(m_service);
                m_acceptor.async_accept(connection->socket(),
boost::bind(&Server::accept_handler, this, _1, connection));
        }

        void run(int threads = 10)
        {
                boost::thread_group threads_pool;
                while(threads-- > 0)
                        threads_pool.create_thread(boost::bind(&boost::asio::io_service::run,
&m_service));

                m_service.run();
        }

        void accept_handler(const boost::system::error_code &e, Session
*connection)
        {
                if(!e)
                {
                        connection->run();
                        accept();
                }
        }

protected:
        boost::asio::io_service m_service;
        boost::asio::ip::tcp::acceptor m_acceptor;
};

//////////////////////////////////////////////////////////////////////////////////////////////////////////

int main(int argc, char *argv)
{
        Server server(7070);
        server.run();
        return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////

As reported in my previous message all handlers are invoked with the
params _1/_2 due to a multiple definition link error.
Bye


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk