用Qt增强asio [英] Boost asio with Qt

查看:415
本文介绍了用Qt增强asio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用

I am trying to use boost::asio async client example with a simple Qt GUI like:

我的应用中的一些摘要:

A little snippet from my app:

该按钮单击SLOT:

void RestWidget::restGetCall()
{

    networkService ntwkSer("www.boost.org","80");
    connect(&ntwkSer, SIGNAL(responseReady(std::string)), this, SLOT(showResponse(std::string)));
    ntwkSer.get("/LICENSE_1_0.txt");

}

networkService类只是上述链接的boost示例代码的包装.它是从QObject类派生的,用于信号,插槽机制.

The networkService class is just a wrapper of the above linked boost sample code.Its derived from QObject class for signal,slot mechanism.

void networkService::get(const std::string & path)
{
   // boost::thread (boost::bind(&networkService::networkCall,this,path));//this gives me sigabrt
    networkCall(path);//this works fine, and i get output as pictured above.
}

void networkService::networkCall(const std::string path)
{
    tcp::resolver::query query(host_, port_);//also these host,port fields come out to be invalid/garbage.
    //tcp::resolver::query query("www.boost.org","80");//still doesnt resolve the SIGABRT
    resolver_.async_resolve(query,
        boost::bind(&networkService::handle_resolve, this,
        boost::asio::placeholders::error,
        boost::asio::placeholders::iterator,
        path));
    io_service.run();
}

问题是当我从boost :: thread运行io_service.run()时.我得到了SIGABRT.

The problem, is when i run the io_service.run() from the boost::thread. i get SIGABRT.

在调试时,networkService :: networkCall(path)函数中的host_,port_ networkService包装器类字段也无效,该字段在构造时被保存:

also he host_,port_ networkService wrapper class fields inside the networkService::networkCall(path) function on debugging come out to be invalid, which get saved while constructing:

networkService ntwkSer("www.boost.org","80");

boost :: thread的明显原因是使GUI无阻塞,因为io_service()有其自己的事件循环.我的意图是在单独的boost线程中运行boost :: asio异步调用,并使用QT的Q_OBJECT信号插槽机制通知GUI线程.

The obvious reason for boost::thread is to make GUI non-blocking,since io_service() has its own eventloop. My intention is to run boost::asio async calls in a seperate boost thread, and notify the GUI thread with QT's Q_OBJECT signal slot mechanism.

我不知道SIGABRT的原因,也不清楚为什么一旦我开始使用boost :: thread时host_和port_的字段值将变得无效.

I don't get the reason of SIGABRT and also why could the field values of host_ and port_ become invalid once i start using boost::thread.

PS:这种设置在类似的命令行应用程序(没有Qt GUI代码)中通过boost :: thread可以正确运行,即,当networkService类没有被Qt信号/插槽入侵以通知主GUI线程时.在这里,我从boost :: thread内部使用boost :: asio的响应.

PS: This same setup, behaves correctly with boost::thread from a similar commandline application (no Qt GUI code), i.e when the networkService class is not hacked for Qt signal/slot to notify the main GUI thread. Here, i use the boost::asio's response from within the boost::thread.

根据对我问题的回答,我尝试了此操作...我禁用了networkservice类的Q_OBJECT信号/插槽和QObject派生,以确保MOC不会弄乱事情..但是,问题仍然存在,我在Windows上遇到访问冲突与sigabrt在Linux上.还出现了网络服务对象的字段损坏的问题,最终导致访问冲突. 实际上,行为没有变化.

as per responses to my question, i tried this... i disabled Q_OBJECT signal/slot and QObject derivation of the networkservice class, to be sure MOC isnt messing things up.. but still, the issue prevails, i get access violation on windows vs sigabrt on linux. The issue of the networkservice object's fields getting corrupted is also present, eventually getting access violation. In effect no change in behaviour.

在启动线程之前: 从内螺纹 继续访问冲突...

before launching thread: from inside thread access violation on continue...

因此,即使没有MOC,问题仍然存在.

So, even without MOC , the issue is still there.

对不起,我感到很抱歉.我在boost :: thread中使用本地networkService对象时犯了一个严重错误,该对象在线程实际运行时就被限制了!

Edit 2: Im sorry for bothering.. i did a huge mistake, of using a local networkService object from within the boost::thread, which got scoped out when the thread actually ran!

推荐答案

使用Qt事件循环很难获得asio io_service.run()函数以正常播放".

It's difficult to get the asio io_service.run() function to "play well" with the Qt event loop.

使用调用io_service::poll()io_service::poll_one()Qt插槽,然后将该插槽连接到QTimerEvent更加容易.

It's easier to use a Qt slot that calls io_service::poll() or io_service::poll_one() and then connect that slot to a QTimerEvent.

使用QNetworkAccessManager代替asio甚至更容易,请参见 Qt客户示例

And it's even easier to use QNetworkAccessManager instead of asio see Qt Client Example

这篇关于用Qt增强asio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆