Boost logo

Boost Users :

From: j.c. (jolix_at_[hidden])
Date: 2008-04-02 18:26:49


Hi,
Relevant code is bold.

Thanks,
j.c.

     /**
      * A TLS connection base class.
      */
     class tls_connection_base
     {
         public:

             /**
              * Constructor
              * @param ios
              */
             tls_connection_base(io_service_t & ios)
                 : timer_(ios)
                 , context_(ios, ssl_context_t::tlsv1_client)
                 , socket_(ios, context_)
             {
                 context_.set_verify_mode(ssl_context_t::verify_peer);

                 try
                 {
                     context_.load_verify_file("ca.pem");
                 }
                 catch (...)
                 {
                     std::cout <<
"tls_connection_base::tls_connection_base: " <<
                                  "failed to load certificate file!" <<
                     std::endl;
                 }
             }

             /**
              * Virtual Destructor
              */
             virtual ~tls_connection_base()
             {
                 // ...
             }

             /**
              * Performs an asyncronous connect to desired endpoint.
              * @param endpoint The endpoint to connect to.
              */
             void async_connect(const stream_endpoint_t & endpoint)
             {
                endpoint_ = endpoint;

                 socket_.lowest_layer().async_connect(endpoint,
                     boost::bind(&tls_connection_base::handle_connect,
this,
                     boost::asio::placeholders::error)
                 );

                  
timer_.expires_from_now(boost::posix_time::seconds(15));
                 timer_.async_wait(boost::bind(
                     &tls_connection_base::handle_timeout, this)
                 );
             }

             void handle_connect(const error_code_t & error)
             {
                 if (!error)
                 {
                      
socket_.async_handshake(boost::asio::ssl::stream_base::client,
                          
boost::bind(&tls_connection_base::handle_handshake, this,
                         boost::asio::placeholders::error)
                     );
                     std::cout <<
"tls_connection_base::handle_connect: " <<
                                  "Successful connection:
local_endpoint(" <<
                                   
socket_.lowest_layer().local_endpoint() <<
                                  ")" <<
                     std::endl;
                 }
                 else
                 {
                     std::cout <<
"tls_connection_base::handle_connect: " <<
                                  "Connect error: " << error.message()
<<
                     std::endl;
                 }
             }

             void handle_handshake(const error_code_t & error)
             {
                 if (!error)
                 {
                     std::cout <<
"tls_connection_base::handle_handshake: " <<
                                  "Handshake success: " <<
                     std::endl;
                 }
                 else
                 {
                     std::cout <<
"tls_connection_base::handle_handshake: " <<
                                  "Handshake failed: " <<
error.message() <<
                     std::endl;
                 }
             }

             void handle_timeout()
             {
                 std::cout << "tls_connection_base::handle_timeout: " <<
                              "Connection timed out! " <<
                 std::endl;

                 socket_.lowest_layer().close();
                
                    // If we try to re-connect here, which is fired by a
asio::deadline_timer
                    // we get caught up in a loop.
                    // How do we do an async_connect here without it going haywire?
                    async_connect.(endpoint_);
             }

         private:
             deadline_timer_t timer_;
             ssl_context_t context_;
             tls_socket_t socket_;
            stream_endpoint_t endpoint_;
     };



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