当前位置: 首页 > news >正文

libvncclient编写多线程qt的VNC客户端

概述

  • 使用qt和libvncclient编写vnc的客户端程序,多线程读写,拒绝卡顿。
  • qt环境:5.15.3
  • libvncclient:0.9.14
  • 下载地址:https://github.com/LibVNC/libvncserver/releases

编译libvncclient

  • 打开CMakeList文件,找到编译开关,注释掉不需要的编译项目:
# all the build configuration switches
option(LIBVNCSERVER_INSTALL "Generate installation target" ON)
option(BUILD_SHARED_LIBS "Build shared libraries" ${UNIX})
option(WITH_ZLIB "Search for the zlib compression library to support additional encodings" ON)
option(WITH_LZO "Search for the LZO compression library to omit internal miniLZO implementation" ON)
option(WITH_JPEG "Search for the libjpeg compression library to support additional encodings" ON)
option(WITH_PNG "Search for the PNG compression library to support additional encodings" ON)
option(WITH_SDL "Search for the Simple Direct Media Layer library to build an example SDL vnc client" ON)
option(WITH_GTK "Search for the GTK library to build an example GTK vnc client" ON)
option(WITH_LIBSSHTUNNEL "Search for libsshtunnel to build an example ssh-tunneled client" ON)
option(WITH_THREADS "Search for a threading library to build with multithreading support" ON)
option(PREFER_WIN32THREADS "When searching for a threading library, prefer win32 threads if they are found" ON)
option(WITH_GNUTLS "Search for the GnuTLS secure communications library to support TLS" OFF)
option(WITH_OPENSSL "Search for the OpenSSL cryptography library to support TLS and use as crypto backend" ON)
option(WITH_SYSTEMD "Search for libsystemd to build with systemd socket activation support" ON)
option(WITH_GCRYPT "Search for Libgcrypt to use as crypto backend" ON)
option(WITH_FFMPEG "Search for FFMPEG to build an example VNC to MPEG encoder" ON)
option(WITH_TIGHTVNC_FILETRANSFER "Enable filetransfer if there is pthreads support" ON)
option(WITH_24BPP "Allow 24 bpp" ON)
option(WITH_IPv6 "Enable IPv6 Support" ON)
option(WITH_WEBSOCKETS "Build with websockets support" ON)
option(WITH_SASL "Build with SASL support" ON)
option(WITH_XCB "Build with XCB support" ON)
option(WITH_EXAMPLES "Build examples" OFF)
option(WITH_TESTS "Build tests" OFF)
option(WITH_QT "Build the Qt client example" OFF)
  • 也可以自己手动去掉不需要的库依赖,我这里没有把server的库依赖去干净。
  • 然后取出项目中的libvncclient.so和头文件中的client目录到自己的项目中。
  • 将build目录中的include中的rfbconfig.h拷贝到头文件目录

项目思路

  • 主要思路如下:
  • 创建一个主窗口QWidget用于画面渲染,创建一个接收线程和一个发送线程。
  • 接收线程负责读取vnc服务器发送的数据并解码装换成QImage,通过信号发送QImage,通知主线程刷新页面。
  • 发送线程用于发送主窗口的鼠标移动,点击等事件,用于实时更新操作。

项目编写

  • VncWideget : 主要的渲染窗口
#ifndef VNCVIEWWIDGET_H
#define VNCVIEWWIDGET_H#include <QWidget>
#include <QThread>#include "rfb/rfbclient.h"
#include "rfb/rfbconfig.h"#include "vnc_client/vncrecvthread.h"
#include "vnc_client/vncsendworker.h"class VncViewWidget : public QWidget
{Q_OBJECT
public:explicit VncViewWidget(QString ip, quint32 port, QWidget *parent = nullptr);~VncViewWidget();void start();void stop();inline bool isStarted(){return _startFlag;}void updateImage(const QImage& image);void paintEvent(QPaintEvent *event) override;void mouseMoveEvent(QMouseEvent* event) override;void mousePressEvent(QMouseEvent *event) override;void mouseReleaseEvent(QMouseEvent *event) override;void closeEvent(QCloseEvent* event) override;private:QImage _image;rfbClient *_cl;QString _ip;quint32 _port;bool _startFlag = false;QThread* _vncSendThread;VNCSendWorker* _vncSendWorker;VNCRecvThread* _vncRecvThread;signals:void sendMouseState(rfbClient* cl, int x, int y, int button);void fullWindowCloseSignal();public slots:
};#endif // VNCVIEWWIDGET_H
  • VNCSendWorker:数据发送工作线程
#ifndef VNCSENDWORKER_H
#define VNCSENDWORKER_H#include <QObject>#include "rfb/rfbclient.h"class VNCSendWorker : public QObject
{Q_OBJECT
public:explicit VNCSendWorker( QObject *parent = nullptr);signals:public slots:void sendMouseUpdateMsg(rfbClient* cl, int x, int y, int button);};#endif // VNCSENDWORKER_H

VNCRecvThread :接收线程

#ifndef VNCRECVTHREAD_H
#define VNCRECVTHREAD_H#include <QThread>#include <QImage>#include "rfb/rfbclient.h"class VNCRecvThread : public QThread
{Q_OBJECT
public:VNCRecvThread(QObject* parent = nullptr);inline void startRun(rfbClient* cl){if(_runFlag)return;_cl = cl;_cl->FinishedFrameBufferUpdate = frameBufferUpdated;rfbClientSetClientData(_cl, nullptr, this);_runFlag = true;this->start();}inline void stopRun(){if(!_runFlag)return;_runFlag = false;if(_cl)rfbClientSetClientData(_cl, nullptr, nullptr);if(_cl)_cl->FinishedFrameBufferUpdate = nullptr;_cl = nullptr;}static void frameBufferUpdated(rfbClient* cl);protected:void run() override;private:bool _runFlag = false;rfbClient* _cl;signals:void updateImageSignal(QImage);
};#endif // VNCRECVTHREAD_H

项目完整代码

gitee:https://gitee.com/li-gouhi2333/vncclient/tree/master/

http://www.lryc.cn/news/432562.html

相关文章:

  • 视频处理基础之gradio框架实现
  • 黑马点评2——商户查询缓存(P37店铺类型查询业务添加缓存练习题答案)redis缓存、更新、穿透、雪崩、击穿、工具封装
  • 概率DP (由一道绿题引起的若干问题。目前为一些老题,蒟蒻的尝试学习1.0)
  • [Python]生成器和yield关键字
  • Nginx 负载均衡+高可用 集群部署(Keepalived+LVS DR模式)
  • 算法 | 基础 | 出现奇数次的数字
  • log4j 控制台和文件输出乱码问题解决
  • 在国产芯片上实现YOLOv5/v8图像AI识别-【4.2】RK3588获取USB摄像头图像推流RTSP更多内容见视频
  • TCP/IP协议栈详解及其在现代网络中的应用
  • 亚信安全荣获“2024年网络安全优秀创新成果大赛”优胜奖
  • 如何从硬盘恢复已删除/丢失的文件?硬盘恢复已删除的文件技巧
  • [Linux]:权限
  • 启动Spring Boot报错
  • 部署project_exam_system项目——及容器的编排
  • 网络工程师学习笔记——无线通信网
  • Vue(十三) 路由、路由嵌套、query、param传参、propos、replace属性。编程式路由导航,特有的生命周期函数,路由守卫
  • ArgoUML与StarUML的安装
  • 828华为云征文|华为云服务器Flexus X搭建悟空crm管理系统——助力企业云上管理(解决APP Referer校验失败问题)
  • 计算机毕业设计选题推荐-健康健身追踪系统-运动健身系统-Java/Python项目实战
  • FPGA开发:初识FPGA × 开发环境
  • 电脑驱动分类
  • 理解C++全局对象析构顺序与 IPC 资源管理:避免 coredump
  • 云计算之大数据(下)
  • 硬件工程师笔试面试知识器件篇——二极管
  • 操作系统安全保护
  • STM32硬件篇:W25Q64
  • uni-app 获取当前位置的经纬度以及地址信息
  • 【CSS】尺寸单位
  • Agent(智能体)和 MetaGPT,一句话实现整个需求应用代码
  • [数据结构] 哈希结构的哈希冲突解决哈希冲突