简体   繁体   English

使用工作线程增强boost :: asio多个传出SSL连接

[英]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. 我正在更好地学习boost :: asio,最近我已经将它用于一些基本的服务器应用程序。 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). 我想建立多个传出SSL连接(远超过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() . 这些工作线程除了io_service->run()之外没有太多代码。 I used the boost::asio SSL client example as a start and added these worker threads to this source. 我使用boost :: asio SSL客户端示例作为开始,并将这些辅助线程添加到此源。 In the main() I start these worker threads and after that I post tasks for the io_service like this: main()我启动了这些工作线程,然后我像这样发布io_service的任务:

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: doConnect()仅是boost :: asio SSL客户端示例中来自主体的原始代码:

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. 这段代码可以很好地编译,但是在执行过程中发生的是我收到Operation canceled错误。

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() . 在该站点上进行一些阅读之后,我得到了提示,也许示例代码中的构造函数存在this指针超出范围的问题,因此我尝试使用shared_from_this() That resulted in an exception: tr11: weak pointer . 这导致了一个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. 为了摆脱这种弱指针问题,我尝试将代码从构造函数移至其自己的方法Start()并在doConnect()方法中创建对象后调用此方法。 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. 之后,将代码移回构造函数,就像在示例中一样,并删除了所有SSL特定代码,更改为普通套接字类型,然后再次运行。 Now I received a the origional error again: Operation canceled , so it doesn't seem to have anything to do with the SSL code. 现在,我又收到一个原始错误: Operation canceled ,所以它似乎与SSL代码无关。

  4. Along the way (but for sure without the SSL code), I also received an error message saying that there was no network. 一路上(但肯定没有SSL代码),我还收到一条错误消息,说没有网络。

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. 已解决:问题是简单的(但被忽略了)事实,即我没有通过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. 我改回我的选项1尝试,对象现在是shard_ptr,弱指针异常消失了,一切工作都应该正常进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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