Boost logo

Boost Users :

Subject: Re: [Boost-users] pass asio socket to another class
From: cap (cap.nick_at_[hidden])
Date: 2014-12-12 11:00:15


Sorry, I've forgot to specify a /Listener/ class:

void Listener::Init(void)
{
        for (UINT i = 0; i < 10; ++i)
        {
                IoServicePtr service(new IoService());
                m_PtrIoServices.push_back(service);
                m_WorkIoServices.push_back(IoService::work(*service));
                m_Threads.create_thread(boost::bind(&IoService::IO_SERVICE_THREAD_VOID,
service));
        }
}

void Listener::Run(void)
{
        Listener listenerHttp(httpPort, &HttpRequest::CreateInstance);
        Listener listenerSsl(sslPort, &SslHandshake::CreateInstance);

        m_Threads.join_all();
}

So, I have the same source code for unsecured HTTP requests, the code works
perfectly with /boost::asio::ip::tcp::socket/ but
*boost::asio::ssl::stream<boost::asio::ip::tcp::socket>* sometime hangups.

Also I've wrote small example from boost
<http://www.boost.org/doc/libs/1_56_0/doc/html/boost_asio/example/cpp03/ssl/client.cpp>
:

class client : public boost::enable_shared_from_this<client>
{
public:
        client(
                boost::asio::io_service& io_service,
                boost::asio::ssl::context& context,
                boost::asio::ip::tcp::resolver::iterator endpoint_iterator)
                : socket_(io_service, context),
                        stand_(io_service)
        {
                socket_.set_verify_mode(boost::asio::ssl::verify_peer);
                socket_.set_verify_callback(
                        boost::bind(&client::verify_certificate, this, _1, _2));

                boost::asio::async_connect(
                        socket_.lowest_layer(),
                        endpoint_iterator,
                        boost::bind(
                                &client::handle_connect,
                                this,
                                boost::asio::placeholders::error));
        }

        bool verify_certificate(
                bool preverified,
                boost::asio::ssl::verify_context& ctx)
        {
                return true; //preverified;
        }

        void handle_connect(
                const boost::system::error_code& error)
        {
                if (!error)
                {
                        socket_.async_handshake(
                                boost::asio::ssl::stream_base::client,
                                stand_.wrap(
                                        boost::bind(
                                                &client::handle_handshake,
                                                this,
                                                boost::asio::placeholders::error)));
                }
                else
                {
                        std::cout << "Connect failed: " << error.message() << "\n";
                }
        }

        void handle_handshake(
                const boost::system::error_code& error)
        {
                if (!error)
                {
                        memset(request_, 0, max_length);
                        strcpy_s(request_, "GET https://www.google.com.ua/ HTTP/1.1");
                        size_t request_length = strlen(request_);

                        boost::asio::async_write(
                                socket_,
                                boost::asio::buffer(request_, request_length),
                                stand_.wrap(
                                        boost::bind(
                                                &client::handle_write,
                                                this,
                                                boost::asio::placeholders::error,
                                                boost::asio::placeholders::bytes_transferred)));
                }
                else
                {
                        std::cout << "Handshake failed: " << error.message() << "\n";
                }
        }

        void handle_write(
                const boost::system::error_code& error,
                size_t bytes_transferred)
        {
                if (!error)
                {
                        boost::asio::async_read(
                                socket_,
                                boost::asio::buffer(reply_, bytes_transferred),
                                stand_.wrap(
                                        boost::bind(
                                                &client::handle_read,
                                                this,
                                                boost::asio::placeholders::error,
                                                boost::asio::placeholders::bytes_transferred)));
                }
                else
                {
                        std::cout << "Write failed: " << error.message() << "\n";
                }
        }

        void handle_read(
                const boost::system::error_code& error,
                size_t bytes_transferred)
        {
                if (!error)
                {
                        std::cout << "Reply: ";
                        std::cout.write(reply_, bytes_transferred);
                        std::cout << "\n";
                }
                else
                {
                        std::cout << "Read failed: " << error.message() << "\n";
                }
        }

private:
        boost::asio::ssl::stream<boost::asio::ip::tcp::socket> socket_;
        boost::asio::strand stand_;
        char request_[max_length];
        char reply_[max_length];
};

void TestClient(void)
{
        try
        {
                boost::asio::io_service io_service;

                boost::asio::ip::tcp::resolver resolver(io_service);
                boost::asio::ip::tcp::resolver::query query("www.google.com.ua", "443");
                boost::asio::ip::tcp::resolver::iterator iterator =
resolver.resolve(query);

                boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);

                client c(io_service, ctx, iterator);

                io_service.run();
        }
        catch (std::exception& e)
        {
                std::cerr << "Exception: " << e.what() << "\n";
        }
}

/boost::asio::async_read/ calls without errors but /handle_read/ never
calls.
boost 1.56.0, OpenSSL 1.0.1i, VisualStudio 2012 (v110), Windows 8.1
Example is launched from DLL.

--
View this message in context: http://boost.2283326.n4.nabble.com/pass-asio-socket-to-another-class-tp4670017p4670093.html
Sent from the Boost - Users mailing list archive at Nabble.com.

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