boost :: asio async_connect无法取消?

时间:2019-10-26 20:04:25

标签: c++ boost boost-asio

简短的故事:socket.cancel()之后的调用socket.async_connect(...)可以正常工作,即调用完成处理程序;但是对于socket.cancel()之后的asio::async_connect(socket, ...),永远不会调用完成处理程序。

从长远来看,这里是完整的源代码:

#include <iostream>
#include <vector>
#include <chrono>
#include <ctime>
#include <boost/asio.hpp>

namespace sys = boost::system;
namespace asio = boost::asio;
using tcp = asio::ip::tcp;

static const int count = 10;

struct client_t {
    using sock_ptr_t = std::unique_ptr<tcp::socket>;
    using resolve_results_t = tcp::resolver::results_type;

    client_t(asio::io_context& io_context_): io_context{io_context_}, resolver{io_context}{
    }

    void start() {
        tcp::endpoint endpoint(asio::ip::address::from_string("93.184.216.34"), 80);
        resolver.async_resolve("www.example.com", "80", [&](auto ec, auto results){
           this->on_resolve(ec, results);
        });
    }

    void on_resolve(const sys::error_code& ec, resolve_results_t results) {
        auto now = std::chrono::system_clock::now();
        time_t tt = std::chrono::system_clock::to_time_t(now);
        std::cerr << "on_resolve " << this  << " " << ec.message() << ", at " << ctime(&tt);
        if (!ec) {
            sock = std::make_unique<tcp::socket>(io_context);
            /*
            sock->async_connect(*results.begin(), [this](auto& ec){
                this->on_connect(ec);
            });
            */
            asio::async_connect(*sock, results.begin(), results.end(), [this](auto& ec, auto it){
                this->on_connect(ec);
            });
        }
    }

    void on_connect(const sys::error_code& ec) {
        auto now = std::chrono::system_clock::now();
        time_t tt = std::chrono::system_clock::to_time_t(now);
        std::cerr << "on_connect " << this  << " " << ec.message() << ", at " << ctime(&tt);
    }


    void cancel() {
        std::cout << "cancellation attempt for " << this << "\n";
        if (sock) {
            sys::error_code ec;
            sock->cancel(ec);
            std::cout << "cancellation result: " << ec.message() << "\n";
        }
    }

    asio::io_context& io_context;
    sock_ptr_t sock;
    tcp::resolver resolver;

};

using client_ptr_t = std::unique_ptr<client_t>;
using container_t = std::vector<client_ptr_t>;

int main() {
    asio::io_context io_context;
    std::cerr << "starting\n";

    container_t container;
    for(int i = 0; i < count; ++i) {
        auto client = std::make_unique<client_t>(io_context);
        client->start();
        container.emplace_back(std::move(client));
    }

    boost::asio::deadline_timer timer(io_context);

    timer.expires_from_now(boost::posix_time::milliseconds(5));
    timer.async_wait([&](auto& ec){
        for(auto& it : container) {
            it->cancel();
        }
        auto now = std::chrono::system_clock::now();
        time_t tt = std::chrono::system_clock::to_time_t(now);
        std::cout << "cancelled at " << ctime(&tt);
    });

    io_context.run();
    std::cerr << "finished\n";

    return 0;
}

输出样本为:

starting
on_resolve 0x985000 Success, at Sat Oct 26 22:59:22 2019
on_resolve 0x9854c0 Success, at Sat Oct 26 22:59:22 2019
on_resolve 0x985600 Success, at Sat Oct 26 22:59:22 2019
on_resolve 0x985730 Success, at Sat Oct 26 22:59:22 2019
on_resolve 0x985830 Success, at Sat Oct 26 22:59:22 2019
on_resolve 0x985700 Success, at Sat Oct 26 22:59:22 2019
on_resolve 0x985a90 Success, at Sat Oct 26 22:59:22 2019
on_resolve 0x985bb0 Success, at Sat Oct 26 22:59:22 2019
on_resolve 0x985cd0 Success, at Sat Oct 26 22:59:22 2019
on_resolve 0x985e80 Success, at Sat Oct 26 22:59:22 2019
cancellation attempt for 0x985000
cancellation result: Success
cancellation attempt for 0x9854c0
cancellation result: Success
cancellation attempt for 0x985600
cancellation result: Success
cancellation attempt for 0x985730
cancellation result: Success
cancellation attempt for 0x985830
cancellation result: Success
cancellation attempt for 0x985700
cancellation result: Success
cancellation attempt for 0x985a90
cancellation result: Success
cancellation attempt for 0x985bb0
cancellation result: Success
cancellation attempt for 0x985cd0
cancellation result: Success
cancellation attempt for 0x985e80
cancellation result: Success
cancelled at Sat Oct 26 22:59:22 2019

(我必须按ctrl + c才能停止程序)。

但是,如果我使用sock->async_connect(...)(上面代码中的注释部分),则一切正常,即

starting                                                                                                                                                                                      
on_resolve 0x6d6000 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
on_resolve 0x6d64c0 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
on_resolve 0x6d6600 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
on_resolve 0x6d6730 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
on_resolve 0x6d6830 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
on_resolve 0x6d6700 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
on_resolve 0x6d6a90 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
on_resolve 0x6d6bb0 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
on_resolve 0x6d6cd0 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
on_resolve 0x6d6e80 Success, at Sat Oct 26 22:59:03 2019                                                                                                                                      
cancellation attempt for 0x6d6000                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancellation attempt for 0x6d64c0                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancellation attempt for 0x6d6600                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancellation attempt for 0x6d6730                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancellation attempt for 0x6d6830                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancellation attempt for 0x6d6700                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancellation attempt for 0x6d6a90                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancellation attempt for 0x6d6bb0                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancellation attempt for 0x6d6cd0                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancellation attempt for 0x6d6e80                                                                                                                                                             
cancellation result: Success                                                                                                                                                                  
cancelled at Sat Oct 26 22:59:03 2019                                                                                                                                                         
on_connect 0x6d6000 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
on_connect 0x6d64c0 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
on_connect 0x6d6600 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
on_connect 0x6d6730 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
on_connect 0x6d6830 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
on_connect 0x6d6700 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
on_connect 0x6d6a90 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
on_connect 0x6d6bb0 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
on_connect 0x6d6cd0 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
on_connect 0x6d6e80 Operation canceled, at Sat Oct 26 22:59:03 2019                                                                                                                           
finished                                                                                                                                                                                      

为什么呢?我已经阅读了文档,但没有发现任何提示我在做什么错。

1 个答案:

答案 0 :(得分:2)

如文档中所述,asio::async_connect

  

...尝试将套接字连接到一系列端点中的一个...对序列中的每个端点一次,直到成功建立连接为止。

如果提供了多个端点,则取消一个未完成的连接尝试将导致for(auto& p: fs::recursive_directory_iterator(dirname)) { if (fs::is_regular_file(p)) { //do rename } } 从下一个端点重试(循环here's)。

在取消套接字后尝试asio::async_connect。这将阻止close重试。

asio::async_connect