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目录到自己的项目中。

项目思路

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

项目编写

  • VncWideget : 主要的渲染窗口
#ifndef VNCVIEWWIDGET_H
#define VNCVIEWWIDGET_H

#include <QWidget>
#include <QThread>

#include "rfb/rfbclient.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/

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以提供一些基本的代码示例来帮助你开始使用qt libvncclient库来实现一个vnc客户端协议。 首先,你需要在你的Qt项目中添加libvncclient库的头文件和链接库。在.pro文件中添加以下代码: ``` INCLUDEPATH += /path/to/libvncclient/headers LIBS += -L/path/to/libvncclient/lib -lvncclient ``` 然后,在你的Qt代码中,你可以使用以下示例代码来连接到远程VNC服务器并获取屏幕图像: ``` #include <rfb/rfbclient.h> #define SERVER "127.0.0.1" #define PORT 5900 void vncClientCallback(rfbClient* client, rfbClientCallbackReason reason, rfbBool* value) { switch(reason) { case RFB_CLIENT_ERROR: qDebug() << "Error occurred: " << client->errorString; break; case RFB_CLIENT_CONNECTED: qDebug() << "Connected to server"; break; case RFB_CLIENT_FRAMEBUFFER_UPDATE: qDebug() << "Framebuffer update received"; rfbFramebufferUpdateRequest(client, 0, 0, client->width, client->height, FALSE); break; case RFB_CLIENT_DISCONNECTED: qDebug() << "Disconnected from server"; break; default: break; } } void startVncClient() { rfbClient* client = rfbGetClient(8, 3, 4); client->MallocFrameBuffer = TRUE; client->serverHost = SERVER; client->serverPort = PORT; client->clientData = NULL; client->clientGoneHook = NULL; client->kbdAddEvent = NULL; client->kbdReleaseEvent = NULL; client->ptrAddEvent = NULL; client->ptrReleaseEvent = NULL; client->setXCutText = NULL; client->getCursorPos = NULL; client->setTranslateFunction = NULL; client->bellFunc = NULL; client->serverFormat.redShift = 16; client->serverFormat.greenShift = 8; client->serverFormat.blueShift = 0; client->serverFormat.bitsPerPixel = 32; client->serverFormat.depth = 24; client->serverFormat.blueMax = 255; client->serverFormat.greenMax = 255; client->serverFormat.redMax = 255; client->serverFormat.trueColour = TRUE; client->serverFormat.bigEndian = FALSE; client->serverFormat.colourMapEntries = 0; client->serverFormat.colourMap = NULL; client->updateRect.x = 0; client->updateRect.y = 0; client->updateRect.w = 0; client->updateRect.h = 0; client->frameBuffer = NULL; client->listenPort = 0; client->passwdData = NULL; client->getPassword = NULL; client->serverName = NULL; client->frameBufferUpdateHook = NULL; client->displayHook = NULL; client->newClientHook = NULL; client->rreEncoding = FALSE; client->hextileEncoding = FALSE; client->zrleEncoding = FALSE; client->tightEncoding = FALSE; client->copyRectEncoding = FALSE; client->lastRecvTime = 0; client->serverEncodings = NULL; client->listenInterface = NULL; client->ipv6port = 0; client->ipv6Interface = NULL; client->sock = -1; client->udpSock = -1; client->udpPort = 0; client->udpPeer = NULL; client->udpSockConnected = FALSE; client->udpRemoteAddrSize = 0; client->udpRemoteAddr = NULL; client->colourMap = NULL; client->colourMapSize = 0; client->colourMapUpdates = NULL; client->colourMapUpdateCount = 0; client->clientUsesMSRLE = FALSE; client->listen6Sock = -1; client->listen6Port = 0; client->listen6Interface = NULL; client->listenLocal6Sock = -1; client->listenLocal6Port = 0; client->listenLocal6Interface = NULL; client->listenLocalSock = -1; client->listenLocalPort = 0; client->listenLocalInterface = NULL; client->listenUnixSock = -1; client->listenUnixPath = NULL; client->listenUnixPort = 0; client->listenUnixInterface = NULL; client->ipv6Only = FALSE; client->ipv6Loopback = FALSE; client->listenLocalOnly = FALSE; client->listen6LocalOnly = FALSE; client->listenUnixOnly = FALSE; client->nonLocalOk = FALSE; client->localhostOk = FALSE; client->serverMode = FALSE; client->fullColour = FALSE; client->colourLevel = 1; client->colourQuantRed = 2; client->colourQuantGreen = 2; client->colourQuantBlue = 2; client->colourMaxRed = 255; client->colourMaxGreen = 255; client->colourMaxBlue = 255; client->deferUpdateTime = FALSE; client->deferPtrUpdateTime = FALSE; client->rawBytesPerPixel = 1; client->serverStarted = FALSE; client->listen6Sock = -1; client->listen6Port = 0; client->listen6Interface = NULL; rfbSetClientCallback(client, vncClientCallback); if (!rfbInitClient(client, &argc, argv)) { qWarning() << "Error initializing VNC client"; return; } rfbRunClient(client, -1, FALSE); } ``` 在上面的代码中,我们定义了一个vncClientCallback函数,该函数将处理与VNC服务器之间的交互,并将获取屏幕图像。startVncClient函数将创建一个rfbClient对象,并使用rfbGetClient函数初始化它。然后,我们设置一些rfbClient对象的属性,例如服务器地址、端口和颜色格式。最后,我们将rfbClient对象的回调设置为vncClientCallback函数,并开始运行VNC客户端。 这只是一个基本的示例代码,并且可能需要根据你的应用程序的具体需求进行修改。但是,我希望这可以帮助你开始使用qt libvncclient库来实现一个vnc客户端协议。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值