4#include "rfb/SSecurityVncAuth.h" 
    5#include "rfb/SMsgWriter.h" 
    6#include "rfb/screenTypes.h" 
    7#include "rfb/fenceTypes.h" 
    8#include "RabbitCommonTools.h" 
   10#include "InputDevice.h" 
   12#include <QApplication> 
   15Q_DECLARE_LOGGING_CATEGORY(TigerVNC)
 
   19    const char* file = 
"/home/build-RabbitRemoteControl-Desktop_Qt_5_12_11_GCC_64bit-Debug/password";
 
   20    char* pPass = 
new char[9];
 
   21    strcpy(pPass, 
"123456");
 
   22    rfb::PlainPasswd password(pPass);
 
   23    rfb::ObfuscatedPasswd oPassword(password);
 
   28        out.write(oPassword.takeBuf(), oPassword.length);
 
   31    rfb::SSecurityVncAuth::vncAuthPasswdFile.setParam(file);
 
   33bool g_setfile = 
false;
 
   35CConnection::CConnection(QSharedPointer<CChannel> channel,
 
   37    : QObject(), rfb::SConnection(),
 
   40      m_PixelFormat(32, 24, false, true, 255, 255, 255, 16, 8, 0),
 
   41      inProcessMessages(false),
 
   42      pendingSyncFence(false),
 
   47      m_EncodeManager(this),
 
   48      continuousUpdates(false)
 
   52    check = connect(m_Channel.data(), SIGNAL(sigConnected()),
 
   53                    this, SLOT(slotConnected()));
 
   55    check = connect(m_Channel.data(), SIGNAL(readyRead()),
 
   56                         this, SLOT(slotReadyRead()));
 
   58    check = connect(m_Channel.data(), SIGNAL(sigDisconnected()),
 
   59                         this, SLOT(slotDisconnected()));
 
   61    check = connect(m_Channel.data(), SIGNAL(sigError(
int, 
const QString&)),
 
   62                    this, SLOT(slotError(
int, 
const QString&)));
 
   65    m_InputDevice = CInputDevice::GenerateObject();
 
   68CConnection::~CConnection()
 
   70    qDebug(TigerVNC) << 
"CConnection::~CConnection";
 
   73void CConnection::slotConnected()
 
   76        m_InStream = QSharedPointer<rdr::InStream>(
new CInStreamChannel(m_Channel.data()));
 
   77        m_OutStream = QSharedPointer<rdr::OutStream>(
new COutStreamChannel(m_Channel.data()));
 
   78        setStreams(m_InStream.data(), m_OutStream.data());
 
   80        char* pass = 
new char[128];
 
   81        strcpy(pass, m_pPara->getPassword().toStdString().c_str());
 
   82        rfb::PlainPasswd password(pass);
 
   83        rfb::ObfuscatedPasswd oPassword(password);
 
   84        rfb::SSecurityVncAuth::vncAuthPasswd.setParam(oPassword.buf, oPassword.length);
 
   86        client.setDimensions(CDesktop::Instance()->Width(), CDesktop::Instance()->Height());
 
   89    } 
catch(rdr::Exception& e) {
 
   90        std::string szErr (
"initialistProtocol() fail:");
 
   92        qCritical(TigerVNC) << szErr.c_str();
 
   93        emit sigError(-1, szErr.c_str());
 
   94    } 
catch(std::exception& e) {
 
   95        std::string szErr (
"initialistProtocol() fail:");
 
   97        qCritical(TigerVNC) << szErr.c_str();
 
   98        emit sigError(-1, szErr.c_str());
 
  100        std::string szErr (
"initialistProtocol() fail.");
 
  101        qCritical(TigerVNC) << szErr.c_str();
 
  102        emit sigError(-1, szErr.c_str());
 
  106void CConnection::slotReadyRead()
 
  108    qDebug(TigerVNC) << 
"CConnection::slotReadyRead()";
 
  109    if (state() == RFBSTATE_CLOSING) 
return;
 
  111        inProcessMessages = 
true;
 
  115        getOutStream()->cork(
true);
 
  118            if (pendingSyncFence)
 
  125                writer()->writeFence(fenceFlags, fenceDataLen, fenceData);
 
  127                pendingSyncFence = 
false;
 
  132        getOutStream()->cork(
false);
 
  134        inProcessMessages = 
false;
 
  141            QImage img = CDesktop::Instance()->GetDesktop();
 
  142            slotDesktopUpdate(img, img.rect());
 
  144    } 
catch (rdr::Exception e) {
 
  145        qCritical(TigerVNC) << 
"Exception:" << e.str();
 
  146        emit sigError(-1, QString(
"processMsg exception:") + e.str());
 
  147    } 
catch (std::exception e) {
 
  148        qCritical(TigerVNC) << 
"std Exception:" << e.what();
 
  149        emit sigError(-1, QString(
"std Exception:") + e.what());
 
  151        qCritical(TigerVNC) << 
"exception";
 
  152        emit sigError(-1, QString(
"Exception:"));
 
  156void CConnection::slotDisconnected()
 
  158    qDebug(TigerVNC) << 
"The connect disconnect";
 
  159    emit sigDisconnected();
 
  162void CConnection::slotError(
int nErr, 
const QString& szErr)
 
  164    emit sigError(nErr, szErr);
 
  167void CConnection::queryConnection(
const char *userName)
 
  169    qDebug(TigerVNC) << 
"queryConnection:" << userName;
 
  171    SConnection::queryConnection(userName);
 
  174void CConnection::authSuccess()
 
  176    qDebug(TigerVNC) << 
"CConnection::authSuccess(): state:" << state();
 
  178    QString name = RabbitCommon::CTools::GetHostName()
 
  179            + 
"@" + RabbitCommon::CTools::GetCurrentUser();
 
  180    client.setName(name.toStdString().c_str());
 
  185    int w = CDesktop::Instance()->Width();
 
  186    int h = CDesktop::Instance()->Height();
 
  187    client.setDimensions(w, h);
 
  188    client.setPF(m_PixelFormat);
 
  190    client.pf().print(buffer, 256);
 
  191    qInfo(TigerVNC, 
"Set server pixel format:%s; width:%d, height:%d",
 
  195    m_Updates.add_changed(rfb::Rect(0, 0, w, w));
 
  198void CConnection::clientInit(
bool shared)
 
  200    qDebug(TigerVNC) << 
"clientInit shared:" << shared;
 
  201    SConnection::clientInit(shared);
 
  203    bool check = connect(CDesktop::Instance(), SIGNAL(sigUpdate(QImage, QRect)),
 
  204                    this, SLOT(slotDesktopUpdate(QImage, QRect)));
 
  208void CConnection::setDesktopSize(
int fb_width, 
int fb_height, 
const rfb::ScreenSet &layout)
 
  210    qDebug(TigerVNC, 
"setDesktopSize: %d:%d", fb_width, fb_height);
 
  215        writer()->writeDesktopSize(rfb::reasonClient, rfb::resultSuccess);
 
  218void CConnection::setPixelFormat(
const rfb::PixelFormat &pf)
 
  220    SConnection::setPixelFormat(pf);
 
  222    pf.print(buffer, 256);
 
  223    qDebug(TigerVNC) << 
"Set pixel format:" << buffer;
 
  229void CConnection::framebufferUpdateRequest(
const rfb::Rect &r, 
bool incremental)
 
  235    if (!accessCheck(AccessView)) 
return;
 
  237    SConnection::framebufferUpdateRequest(r, incremental);
 
  241    if (!r.enclosed_by(rfb::Rect(0, 0, client.width(), client.height()))) {
 
  242        qCritical(TigerVNC, 
"FramebufferUpdateRequest %dx%d at %d,%d exceeds framebuffer %dx%d",
 
  243                   r.width(), r.height(), r.tl.x, r.tl.y,
 
  244                   client.width(), client.height());
 
  245        safeRect = r.intersect(rfb::Rect(0, 0, client.width(), client.height()));
 
  252    rfb::Region reqRgn(safeRect);
 
  253    if (!incremental || !continuousUpdates)
 
  254        requested.assign_union(reqRgn);
 
  258        m_Updates.add_changed(reqRgn);
 
  262        if (client.supportsEncoding(rfb::pseudoEncodingExtendedDesktopSize))
 
  263            writer()->writeDesktopSize(rfb::reasonServer);
 
  272void CConnection::fence(rdr::U32 flags, 
unsigned len, 
const char data[])
 
  274    qDebug(TigerVNC, 
"fence: flags:%d,len:%d", flags, len);
 
  277    if (flags & rfb::fenceFlagRequest) {
 
  278        if (flags & rfb::fenceFlagSyncNext) {
 
  279            pendingSyncFence = 
true;
 
  281            fenceFlags = flags & (rfb::fenceFlagBlockBefore | rfb::fenceFlagBlockAfter | rfb::fenceFlagSyncNext);
 
  286                fenceData = 
new char[len];
 
  287                memcpy(fenceData, data, len);
 
  294        flags = flags & (rfb::fenceFlagBlockBefore | rfb::fenceFlagBlockAfter);
 
  296            writer()->writeFence(flags, len, data);
 
  301        qCritical(TigerVNC) << 
"Fence response of unexpected size received";
 
  313        qCritical(TigerVNC) << 
"Fence response of unexpected type received";
 
  317void CConnection::enableContinuousUpdates(
bool enable, 
int x, 
int y, 
int w, 
int h)
 
  319    qDebug(TigerVNC, 
"enableContinuousUpdates: %d, %d,%d,%d,%d",
 
  325    if (!client.supportsFence() || !client.supportsContinuousUpdates())
 
  326      throw rdr::Exception(
"Client tried to enable continuous updates when not allowed");
 
  328    continuousUpdates = enable;
 
  330    rect.setXYWH(x, y, w, h);
 
  331    cuRegion.reset(rect);
 
  336      writer()->writeEndOfContinuousUpdates();
 
  340void CConnection::keyEvent(rdr::U32 keysym, rdr::U32 keycode, 
bool down)
 
  342    qDebug(TigerVNC, 
"keysym:%d;keycode:%d;down:%d", keysym, keycode, down);
 
  344        m_InputDevice->KeyEvent(keysym, keycode, down);
 
  347void CConnection::pointerEvent(
const rfb::Point &pos, 
int buttonMask)
 
  350    CInputDevice::MouseButtons button;
 
  352        button |= CInputDevice::LeftButton;
 
  354        button |= CInputDevice::MouseButton::MiddleButton;
 
  356        button |= CInputDevice::MouseButton::RightButton;
 
  358        button |= CInputDevice::MouseButton::UWheelButton;
 
  359    if(buttonMask & 0x10)
 
  360        button |= CInputDevice::MouseButton::DWheelButton;
 
  361    if(buttonMask & 0x20)
 
  362        button |= CInputDevice::MouseButton::LWheelButton;
 
  363    if(buttonMask & 0x40)
 
  364        button |= CInputDevice::MouseButton::RWheelButton;
 
  367        m_InputDevice->MouseEvent(button, QPoint(pos.x, pos.y));
 
  370void CConnection::clientCutText(
const char *str)
 
  372    qDebug(TigerVNC) << 
"cut text:" << str;
 
  375QSharedPointer<rfb::PixelBuffer> CConnection::GetBufferFromQImage(QImage &img)
 
  380    QSharedPointer<rfb::PixelBuffer> pb(
 
  381                new rfb::FullFramePixelBuffer(m_PixelFormat,
 
  386                                              / (m_PixelFormat.bpp / 8)));
 
  390void CConnection::writeNoDataUpdate()
 
  395  writer()->writeNoDataUpdate();
 
  401void CConnection::writeDataUpdate(QImage img, QRect rect)
 
  405    const rfb::RenderedCursor *cursor = 
nullptr;
 
  407    if(!writer()) 
return;
 
  410    if (continuousUpdates)
 
  411      req = cuRegion.union_(requested);
 
  424    m_Updates.getUpdateInfo(&ui, req);
 
  426    QSharedPointer<rfb::PixelBuffer> buffer = GetBufferFromQImage(img);
 
  427    m_EncodeManager.writeUpdate(ui, buffer.data(), cursor);
 
  431void CConnection::slotDesktopUpdate(QImage img, QRect rect)
 
  433    qDebug(TigerVNC) << 
"Update screen";
 
  434    if(img.isNull() || !writer())
 
  436        qCritical(TigerVNC) << 
"Image is null";
 
  440    writeDataUpdate(img, rect);