一口气, 了解 Qt 的所有 IPC 方式 | Qt 速学

大家好,我是老吴。

今天整理一下 Qt 里几个重要的 IPC 方式。

Internet Socket

Qt 里的 Qt Network 模块,提供了一些类,让网络编程变得更容易,且支持跨平台。

具体地,有偏上层的 QNetworkAccessManager、QNetworkRequest、QNetworkReply。

以及偏底层的 QTcpSocket、QTcpServer、QUdpSocket。

示例

https://doc.qt.io/qt-5/qtnetwork-downloadmanager-example.html

这个例子演示了如何使用 QNetworkAccessManager 实现一个命令行下载工具,类似 wget 命令。

运行效果:

$ ./downloadmanager https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

Downloading https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb...

[##################################################] 100% 21.1 MB/s

Succeeded.
1/1 files downloaded successfully

支持进度显示、多文件排队下载。

代码分析

// send request
void DownloadManager::startNextDownload()
{
    QNetworkRequest request(url);

    // manager is QNetworkAccessManager, 
    // currentDownload is QNetworkReply
    currentDownload = manager.get(request);

    connect(currentDownload, SIGNAL(readyRead()),
            SLOT(downloadReadyRead()));
}

// download data
void DownloadManager::downloadReadyRead()
{
    // output is QFile
    output.write(currentDownload->readAll());
}

3 个步骤:

  • 发 Request,

  • 拿到 Reply,

  • 从 Reply 中读写数据。

更详细的说明:

https://doc.qt.io/qt-5/qtnetwork-index.html

Local Socket

Local Socket 用于在同一台计算机上运行的进程之间的通信,相关的类是 QLocalServer and QLocalSocket。

虽然 Internet Socket 可用于同一目的,但 Local Socket 的效率更高。

Local Socket 仅复制数据,它们并不执行协议处理,不需要添加或删除网络报头,无需计算校验和,不要产生顺序号,无需发送确认报文。

另外,后续如果有需要的话,可以很容易地升级成跨主机的版本。

示例

1. fortune server

https://doc.qt.io/qt-5/qtcore-ipc-localfortuneserver-example.html

这个例子演示如何使用 QLocalServer 实现一个随机应答服务器。

2. fortune client

https://doc.qt.io/qt-5/qtcore-ipc-localfortuneclient-example.html

这个例子演示如何使用 QLocalSocket 去连接 Local Socket Server。

运行效果:

点击 client 端的 "Get Forturn" 按键,会发起一个连接,server 端发现有新连接后,会随机发送一句话过来。

代码分析

Server 端:

// create a local socket server
Server::Server(QWidget *parent)
    : QDialog(parent)
{
    server = new QLocalServer(this);
    server->listen("fortune")

    connect(server, &QLocalServer::newConnection, this, &Server::sendFortune);
}

// send data
void Server::sendFortune()
{
    QLocalSocket *clientConnection = server->nextPendingConnection();
    clientConnection->write(block);
    clientConnection->flush();
    clientConnection->disconnectFromServer();
}

4 个步骤:

  • new 一个 socket;

  • 用 listen() 监听;

  • 用 nextPendingConnection() 获取连接;

  • 常规的 read()/write() 操作;

Client 端的代码也很简单,请自行查看。

Shared Memory

QSharedMemory 用于支持跨平台的共享内存,它允许多个线程和进程安全地访问共享内存段。

此外,QSystemSemaphore 可用于控制对系统共享资源的访问和进程之间的通信。

示例

https://doc.qt.io/qt-5/qsharedmemory.html

这个例子演示进程间如何使用 QSharedMemory 以共享内存的方式进行通信。

需要启动 2 次该程序,其中一个程序先加载一张图片,然后另外一个程序通过共享内存来访问到同一张图片。

运行效果:

代码分析

创建 shared memory:

void Dialog::loadFromFile()
{
    [...]
    // load buffer into share memory
    // buffer is QBuffer
    sharedMemory.create(size))
    sharedMemory.lock();
    char *to = (char*)sharedMemory.data();
    const char *from = buffer.data().data(); 
    memcpy(to, from, qMin(sharedMemory.size(), size));
    sharedMemory.unlock();
}

访问 shared memory:

void Dialog::loadFromMemory()
{
    
    sharedMemory.attach();
    sharedMemory.lock();
    buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
    buffer.open(QBuffer::ReadOnly);
    in >> image;
    sharedMemory.unlock();

    sharedMemory.detach();
    ui.label->setPixmap(QPixmap::fromImage(image));
}

接口很简洁:

  • create() 创建一块共享内存;

  • attach() 和 detach() 用于访问;

  • lock() 和 unlock() 用于同步;

D-Bus protocol

D-Bus 是一种进程间通信 (IPC) 和远程过程调用 (RPC) 机制,最初是为 Linux 开发的,目的是用一个统一的协议替换现有的 IPC 方案。

D-Bus 实际上是基于 Unix Socket 的。它只提供了一个标准化的总线架构,允许许多进程相互通信。

Qt 提供了 Qt DBus 模块,把信号槽机制扩展到进程级别,使得开发者可以在一个进程中发出信号,由其它进程的槽函数响应信号。

示例

https://doc.qt.io/qt-5/qtdbus-chat-example.html

这个例子演示了如何使用 Qt DBus 实现一个基于 D-Bus 的简易聊天室。

运行效果:

代码分析

ChatMainWindow::ChatMainWindow()
    : m_nickname(QLatin1String("nickname"))
{
    [...]
    connect(sendButton, SIGNAL(clicked(bool)), this, SLOT(sendClickedSlot()));
    
    // add our D-Bus interface and connect to D-Bus
    new ChatAdaptor(this);
    QDBusConnection::sessionBus().registerObject("/", this);

    org::example::chat *iface;
    iface = new org::example::chat(QString(), QString(), QDBusConnection::sessionBus(), this);

    QDBusConnection::sessionBus().connect(QString(), QString(), "org.example.chat", "message", this, SLOT(messageSlot(QString,QString)));
    [...]
}

接口感觉还是比较复杂,这里就不展开分析了。

更详细的说明:

https://doc.qt.io/qt-5/qtdbus-index.html

https://unix.stackexchange.com/questions/604258/what-is-d-bus-practically-useful-for

QProcess

QProcess 类可以用来启动外部程序作为子进程,并与它们进行通信。

示例代码

QProcess gzip;
gzip.start("gzip", QStringList() << "-c");
if (!gzip.waitForStarted())
    return false;

gzip.write("Qt rocks!");
gzip.closeWriteChannel();

if (!gzip.waitForFinished())
    return false;

QByteArray result = gzip.readAll();

这里通过 QProcess 调用 gzip 命令来解压文件,通讯的接口就是 read() / write()。

Qt 官方没有提供示例,想看实例的话可以参考我之前的文章:

小伙子,要不要给你的 Linux 系统写一个launcher

到此,Qt 里几个重要的 IPC 机制就介绍完毕了,感谢阅读。

—— The End ——

推荐阅读:

专辑 | Linux 系统编程

专辑 | Linux 驱动开发

专辑 | 每天一点 C

专辑 | 开源软件

专辑 | Qt 入门

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值