简体   繁体   中英

boost::asio multiple outgoing SSL connections using worker threads

I am in the process of better learning boost::asio, in the recent past I already used it for some basic server applications. So I guess I know (a bit of) the basics.

But today I have a problem that I cannot seem to solve:

I would like to make a number of outgoing SSL connections (way more then 100). For this reason I use a number of worker threads (because I read doing it that way scales better). These worker threads don't have much code besides io_service->run() . I used the boost::asio SSL client example as a start and added these worker threads to this source. In the main() I start these worker threads and after that I post tasks for the io_service like this:

int main( int argc, char * argv[] )

    boost::shared_ptr< boost::asio::io_service > io_service(
            new boost::asio::io_service
    );
    boost::shared_ptr< boost::asio::io_service::work > work(
            new boost::asio::io_service::work( *io_service )
    );
    boost::shared_ptr< boost::asio::io_service::strand > strand(
            new boost::asio::io_service::strand( *io_service )
    );

    boost::thread_group worker_threads;
    for( int x = 0; x < 4; ++x )
    {
            worker_threads.create_thread( boost::bind( &WorkerThread, io_service ) );
    }

    io_service->post( boost::bind( &doConnect, io_service, "127.0.0.1", "7777" ) );
    io_service->post( boost::bind( &doConnect, io_service, "192.168.2.3", "7777" ) );

    work.reset();

    worker_threads.join_all();

    return 0;

}

The doConnect() is nothing more then the origional code from the main as found in the boost::asio SSL client example:

void doConnect( boost::shared_ptr< boost::asio::io_service > io_service, string host, string port ) {

    boost::asio::ip::tcp::resolver resolver(*io_service);
    boost::asio::ip::tcp::resolver::query query(host, boost::lexical_cast< std::string >( port ));
    boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);

    boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23);
    ctx.load_verify_file("server.crt");

    client c(io_service, ctx, iterator);
}

This code compiles fine, but what happens during execution is that I receive an Operation canceled error.

I tried a number of things:

  1. After some reading on this site I got the tip that maybe the constructor in the example code had the problem that the this pointer got out of scope, so I tried to use shared_from_this() . That resulted in an exception: tr11: weak pointer .

  2. To get rid of this weak pointer problem I tried to move the code from the constructor to it's own method Start() and call this method after the object has been created in the doConnect() method. Without result, the weak pointer problem was still there.

  3. After that I moved the code back to the constructor, so like in the example, and removed all the SSL specific code, changed to a normal socket type and run it again. Now I received a the origional error again: Operation canceled , so it doesn't seem to have anything to do with the SSL code.

  4. Along the way (but for sure without the SSL code), I also received an error message saying that there was no network.

Can anybody shine his or her light in this issue, because I cannot find much information about the error message itself or the reason why (besides the suggestion that the socket is closed or out of scope (and also then closed))..

If more info is needed, please tell me, I'll post more text or code...

Solved: The problem was the simple (but overlooked) fact that I did not instantiate the client object through a shared_ptr. I changed back to my option 1 attempt, and the object now being a shard_ptr, the weak pointer exception is gone, and everything works like it should be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM