warp a callback-base C library with qt signal-slot mechanism

转载 2011年01月24日 13:43:00

http://chloerei.com/2010/04/11/111


warp a callback-base C library with qt signal-slot mechanism
Tags: Qt
Posted under 技术 by Rei on 四月 11th, 2010

Sometime we need to call some funtion from C library, etc. libpcap, libnids. But these C library is base on callback mechanism, how to let it work with qt signal-slot mechanism?

After some search, I found a way to wrap a callback-base C library using QThread and C++ singleton class. (refer to a page on qtcentre.org)

for example, wrap libnids. Libnids is an implementation of an E-component of Network Intrusion Detection System. It use callback to pass network packet info.

To wrap this library using Qt signal, there are some points:

1. Include C head file

When include a C head file in a C++ source, must extern it to “C”

File: nidsthread.cpp3
4
5 extern "C"{
#include "nids.h"
}

2. Call C funtion in a new thread

If don’t call funtion from libnids in a new thread, it will block the main thread.

Qt provide thread by QThread, so I create a class named NidsThread inherit QThread.

File: nidsthread.h7
8
9
10
11
12
13
14
15
16
17
18
19 class NidsThread : public QThread
{
Q_OBJECT
public:
NidsThread();
// init libnids in run(), it will execute in a new thread
void run();

private:
proxy pro;
// callback funtion must be static
static void callback(struct tcp_stream *a_tcp, void ** this_time_not_needed);
};


As if all nids funtion must be call in static class funtion, etc. init(), the callback funtion, exit(). (Actually I don’t know why, I hope someone tell me. : P)

Then call nids_init() in NidsThread::run, and register callback funtion.

File: nidsthread.cpp7
8
9
10
11
12
13
14
15
16
17
18
19
20 void NidsThread::run()
{
if (!nids_init()) {
qDebug(nids_errbuf);
}
nids_register_tcp((void*)(callback));
nids_run();
}

void NidsThread::callback(struct tcp_stream *a_tcp, void ** this_time_not_needed)
{
// use Proxy to emit a signal for this callback funtion
Proxy::singleton()->callback();
}


The proxy class will be create later.
3. Create a Proxy class to emit signals

We can call nids funtion in a new Thread now, but qt can’t emit a signal without a object. So we need to create a proxy to emit signal.

At this example, I define a tcpCallback signal in the Proxy class, and a callback funtion to emit it.10
11
12
13
14
15
16
17
18
19
20
21
22 class proxy : public QThread
{
Q_OBJECT
public:
Proxy();
// just use to emit a signal
void callback(struct tcp_stream *a_tcp){ emit tcpCallback(a_tcp);}
static Proxy* singleton();
signals:
void tcpCallback(struct tcp_stream *a_tcp);
private:
static Proxy *m_singleton;
};


And make it singleton1
2
3
4
5
6
7
8
9
10
11
12 #include "proxy.h"

Proxy * Proxy::m_singleton = new Proxy();

Proxy::Proxy()
{
}

Proxy* Proxy::singleton()
{
return m_singleton;
}


Now we can get the proxy singleton by calling Proxy::singleton(), and connect it’s signal, witch will emit in NidsThread callback funtion.

Let the proxy singleton will reduce some code for pass object between two thread. (If know a better method, tell me)
4. Connect signal15
16
17
18
19 MainWindow w;

NidsThread nid;
QObject::connect(Proxy::singleton(), SIGNAL(ipCallback()), &w, SLOT(parseCallback()));
nid.start();


At this example I didn’t pass the struct tcp_stream through signal. As if signal can only deliver Type registered in QMetaType. registered struct tcp_stream or pick-up the useful data from the struct and change to QMetaType … I don’t keep on. : P

a exmaple source. dependent Libnids
nidsproject.tar

 

 

 

 

 

实现signal和slot机制(2)-跨线程投递

前言在1中,我们实现了一个基础的signal+slot的模块件调用机制,不过那个direct调用,在这篇中,我们将支持夸线程调用,即在对象的线程上下文中,调用对象的函数。对象线程绑定在qt中,有一个很...
  • zhx6044
  • zhx6044
  • 2015年06月04日 21:08
  • 2378

QT中SIGNAL与SLOT连接的几种方式

signal/slot在底层会使用三种方式传递消息。
  • J8121
  • J8121
  • 2016年07月17日 11:35
  • 1820

实现类似于Qt的Signal和Slot通讯机制

Signal和Slot机制其实qt提供的对象间通讯机制。实现细节思路实现类似与信号与槽的机制,无非就是做一些薄记工作,qt中通过虚函数和moc来实现。那我们怎么实现?我的思路是signal是一个对象,...
  • zhx6044
  • zhx6044
  • 2015年05月08日 20:40
  • 1805

比较两个解耦利器:callback vs signal/slot

callback与signal/slot是曾经在Windows客户端项目中大规模使用过的解耦利器,常用于UI层/逻辑层解耦,两个功能模块间搭建桥梁。由于两个的源码分别来自不同的开源库,我稍作过修改,源...
  • hzyong_c
  • hzyong_c
  • 2013年09月27日 14:19
  • 1614

Qt中的signal和slot

  • 2014年07月09日 23:31
  • 3KB
  • 下载

QT中的SIGNAL和SLOT

  • 2012年02月27日 16:29
  • 15KB
  • 下载

【QT】父组件与子组件的相互通信例子(用signal-slot实现)

  • 2017年02月22日 20:09
  • 367KB
  • 下载

qt signal-slot测试代码

  • 2013年12月27日 18:41
  • 4KB
  • 下载

You’re doing it wrong…(QThread with SIGNAL-SLOT)

You’re doing it wrong… PublishedJune 17, 2010 | By Bradley T. Hughes We use IRC extensively to ...
  • sonydvd123
  • sonydvd123
  • 2013年03月28日 23:07
  • 2566

Qt中Signal与Slot参数问题<一>

Referenced by: http://www.ibm.com/developerworks/cn/linux/guitoolkit/qt/signal-slot/; 应注意的问题 信...
  • u012369789
  • u012369789
  • 2013年10月16日 17:13
  • 1663
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:warp a callback-base C library with qt signal-slot mechanism
举报原因:
原因补充:

(最多只允许输入30个字)