Beispiel #1
0
 // 异步连接回调
 void on_async_connect(error_code const& ec, socket_ptr sp, endpoint addr)
 {
     if (ec)
     {
         // 错误
         ec_ = ec;
         async_connecting_.reset();
         notify_onconnect();    // 通知连接结果
     }
     else if (sp->lowest_layer().remote_endpoint() == sp->lowest_layer().local_endpoint())
     {
         // 回环假链接, 重试.
         async_connecting_.reset();
         if (!async_connect(addr))
         {
             if (!ec_) ec_ = make_error_code(errc::reconnect_error);
             notify_onconnect();
         }
     }
     else
     {
         // 连接成功, 握手
         async_handshake(sp, addr);
     }
 }
Beispiel #2
0
 // 异步连接回调(带超时)
 void on_async_connect_timed(error_code const& ec, socket_ptr sp
     , endpoint addr, boost::posix_time::time_duration timed)
 {
     if (ec)
     {
         // 错误
         ec_ = ec;
         async_connecting_.reset();
         notify_onconnect();
     }
     else if (sp->lowest_layer().remote_endpoint() == sp->lowest_layer().local_endpoint())
     {
         // 回环假链接, 重试.
         async_connecting_.reset();
         if (!async_connect_timed(addr, timed))
         {
             if (!ec_) ec_ = make_error_code(errc::reconnect_error);
             notify_onconnect();
         }
         return ;
     }
     else
     {
         // 连接成功, 握手
         async_handshake(sp, addr);
     }
 }
Beispiel #3
0
        virtual void accept()
        {
            //Create new socket for this connection
            //Shared_ptr is used to pass temporary objects to the asynchronous functions
            auto socket = std::make_shared<HTTPS>(*io_service, context);

            acceptor->async_accept((*socket).lowest_layer(), [this, socket](const boost::system::error_code &ec)
            {
                //Immediately start accepting a new connection (if io_service hasn't been stopped)
                if (ec != boost::asio::error::operation_aborted)
                    accept();


                if (!ec)
                {
                    boost::asio::ip::tcp::no_delay option(true);
                    socket->lowest_layer().set_option(option);

                    //Set timeout on the following boost::asio::ssl::stream::async_handshake
                    auto timer = get_timeout_timer(socket, config.timeout_request);
                    socket->async_handshake(boost::asio::ssl::stream_base::server, [this, socket, timer]
                            (const boost::system::error_code &ec)
                    {
                        if (timer)
                            timer->cancel();
                        if (!ec)
                            read_request_and_content(socket);
                        else if (on_error)
                            on_error(std::shared_ptr<Request>(new Request(*socket)), ec);
                    });
                }
                else if (on_error)
                    on_error(std::shared_ptr<Request>(new Request(*socket)), ec);
            });
        }