forked from azrilrahim/qt-rfb-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfbclientcls.cpp
1349 lines (1136 loc) · 41 KB
/
rfbclientcls.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013 Azril Azam
*
* Desc
*
* This file is a part of the 100% Qt5 VNC RFB-CLIENT implementation without
* using any 3rd party rfb library
*
* rfbclientcls.cpp
*
*
* Qt5 RFB-CLIENT is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Qt5 RFB-CLIENT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Qt HTML platform plugin. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include "rfbclientcls.h"
rfbclientcls::rfbclientcls(QObject *parent) :
QObject(parent)
{
pixelFormatStruct pixel_format;
this->vncClientTCP.setParent(this);
this->vncClientIPC.setParent(this);
this->opsThreadTimer.setParent(this);
this->serverConnected = false;
this->socketMode = 10; //default TCP
this->pauseMode = false;
xEmitDisconnecSignal = false;
//set default client pixel information
pixel_format.bpp = 32;
pixel_format.depth = 24;
pixel_format.true_color_flag = 1;
pixel_format.big_endian_flag = 0;
pixel_format.red_max = 0xFF;
pixel_format.green_max = 0xFF;
pixel_format.blue_max = 0xFF;
pixel_format.red_shift = 0x10;
pixel_format.green_shift = 0x08;
pixel_format.blue_shift = 0x00;
qDebug("Setting client pixel");
this->clientPixelF = pixel_format;
qDebug("Setting client color format");
this->clientColorFormat = this->getColorFormat(&this->clientPixelF);
qDebug("load default image");
this->VNCIMAGE = QImage(":/new/prefix1/creditimg.png");
}
rfbclientcls::~rfbclientcls()
{
qDebug() << "rfbclientcls is going to be deleted";
this->disconnectFromHost();
/*this->opsThreadTimer.stop();
disconnect(&this->opsThreadTimer,SIGNAL(timeout()),this,SLOT(opsThreadTimerTimeOutSlot()));
this->vncClientTCP.abort();
this->vncClientIPC.abort();
this->opsThreadTimer.stop();
disconnect(&this->opsThreadTimer,SIGNAL(timeout()),this,SLOT(opsThreadTimerTimeOutSlot()));*/
/*if (this->opsThreadTimer != NULL)
{
disconnect(this->opsThreadTimer,SIGNAL(timeout()),this,SLOT(opsThreadTimerTimeOutSlot()));
delete this->opsThreadTimer;
this->opsThreadTimer = NULL;
}
if (this->vncClientTCP != NULL)
{
//disconnect(this->vncClientTCP,SIGNAL(disconnected()),this,SLOT(vncSockDisconnectedSlot()));
this->vncClientTCP->abort();
delete this->vncClientTCP;
this->vncClientTCP = NULL;
}
if (this->vncClientIPC != NULL)
{
//disconnect(this->vncClientIPC,SIGNAL(disconnected()),this,SLOT(vncSockDisconnectedSlot()));
this->vncClientIPC->abort();
delete this->vncClientIPC;
this->vncClientIPC = NULL;
}*/
qDebug("rfbclientcls deleted");
}
void rfbclientcls::opsThreadTimerTimeOutSlot()
{
this->processRFBOperationProtocol();
}
void rfbclientcls::processRFBOperationProtocol()
{
switch (this->socketMode) {
case 0:
if (this->vncClientTCP.state() != QTcpSocket::ConnectedState) {
this->disconnectFromHost();
return;
}
break;
case 1:
if (this->vncClientIPC.state() != QLocalSocket::ConnectedState) {
this->disconnectFromHost();
return;
}
break;
}
this->getRFBOpsType();
this->opsThreadTimer.start(1);
}
void rfbclientcls::newUpdateRFBFB()
{
unsigned char padding;
quint16 totalRects;
quint16 currentRect;
rfbRectHeader rfbRH;
qint64 bppSize;
quint64 totalPixelSize;
quint32 cursorSize;
unsigned char *pixelData;
unsigned char *cursorData;
unsigned char *px;
unsigned char *p;
QImage srcImg;
//get the padding
if (this->readFromRFBServer(&padding, 1) != 1) {
return;
}
//get number of rectangle
if (this->readFromRFBServer((unsigned char *)&totalRects, 2) != 2) {
return;
}
totalRects = this->swap16(totalRects);
//process each rect
for (currentRect = 0; currentRect < totalRects; currentRect++) {
//process all events;
qApp->processEvents();
switch (this->socketMode) {
case 0:
if (this->vncClientTCP.state() != QTcpSocket::ConnectedState) {
return;
}
break;
case 1:
if (this->vncClientIPC.state() != QLocalSocket::ConnectedState) {
return;
}
}
//get rect information
if (this->readFromRFBServer((unsigned char *)&rfbRH, sizeof(rfbRH)) == sizeof(rfbRH)) {
//swap the value
rfbRH.x = swap16(rfbRH.x);
rfbRH.y = swap16(rfbRH.y);
rfbRH.width = swap16(rfbRH.width);
rfbRH.height = swap16(rfbRH.height);
rfbRH.encoding = swap32(rfbRH.encoding);
if (rfbRH.width * rfbRH.height > 0) {
switch (rfbRH.encoding) {
case 0: //Get Raw Data
//Get Pixel Size
bppSize = (this->serverPixelF.bpp >> 3);
totalPixelSize = rfbRH.width * rfbRH.height * bppSize;
//create pixel buffer
pixelData = (unsigned char *)malloc(totalPixelSize);
if (this->readFromRFBServer(pixelData, totalPixelSize) == totalPixelSize) {
//create new image
px = 0;
p = 0;
srcImg = QImage(rfbRH.width, rfbRH.height, QImage::Format_RGB888);
px = pixelData;
p = (unsigned char *)srcImg.bits();
for (int cy = 0; cy < rfbRH.height; cy++) {
qApp->processEvents();
for (int cx = 0; cx < rfbRH.width; cx++) {
p[0] = px[2];
p[1] = px[1];
p[2] = px[0];
px += 4;
p += 3;
}
}
//update image raster
QPainter painter(&this->VNCIMAGE);
painter.drawImage(rfbRH.x, rfbRH.y, srcImg);
painter.end();
}
//clear pixel data
free(pixelData);
break;
case -223: //this->updateFBGetDesktopSizePseudo();
this->VNCIMAGE = QImage(rfbRH.width, rfbRH.height, QImage::Format_RGB888);
this->serverFBHeight = rfbRH.height;
this->serverFBWidth = rfbRH.width;
emit this->rfbResizeSignal(rfbRH.width, rfbRH.height, this->serverVNCName);
break;
case -239://this->updateFBGetCursorPseudo();
cursorSize = rfbRH.width * rfbRH.height;
if (cursorSize > 0) {
cursorData = (unsigned char *)malloc(cursorSize);
if (this->readFromRFBServer(cursorData, cursorSize) == cursorSize)
//emit this->rfbCursorPositionSignal(rfbRH.x,rfbRH.y);
{
free(cursorData);
}
}
break;
}
}
}
//process next rectangle
}
//if all good then display the image
if ((currentRect >= totalRects) && (!this->pauseMode)) {
emit this->rfbFrameBufferUpdateSignal();
}
}
void rfbclientcls::getRFBOpsType()
{
unsigned char msgType;
if (this->readFromRFBServer((unsigned char *)&msgType, 1) != 1) {
return;
}
switch (msgType) {
case 0:
this->newUpdateRFBFB();
break;
//case 0: this->RFBOpsStage = 1;break; //framebufferupdate
// case 1: this->RFBOpsStage = 2;break; //setcolormapentries
// case 2: this->RFBOpsStage = 3; break; //ServerBell
// case 3: this->RFBOpsStage = 4; break; //server cut text
}
return;
}
/*void rfbclientcls::updateFBGetDesktopSizePseudo(quint16 w, quint16 h)
{
this->VNCIMAGE = QImage(w,h,QImage::Format_RGB888);
this->serverFBHeight = w;
this->serverFBWidth = h;
emit this->rfbResizeSignal(w,h,this->serverVNCName);
}
void rfbclientcls::updateFBGetCursorPseudo()
{
unsigned char *cursorData;
quint32 size = this->gRFBRH.width * this->gRFBRH.height;
if ((size) <=0)
{
this->RFBOpsStage = 13;
return;
}
cursorData = (unsigned char*)malloc(size);
if (this->readFromRFBServer(cursorData,size) != size)
{
free (cursorData);
return; //read until equal to size;
}
//emit this->rfbCursorPositionSignal(this->gRFBRH.x,this->gRFBRH.y);
free (cursorData);
}*/
quint64 rfbclientcls::readFromRFBServer(unsigned char *data, quint64 size)
{
//qint64 bSize;
while (1) {
qApp->processEvents();
switch (this->socketMode) {
case 0:
if (this->vncClientTCP.state() != QTcpSocket::ConnectedState) {
return 0;
}
if ((quint64)this->vncClientTCP.bytesAvailable() >= size) {
this->vncClientTCP.read((char *)data, (qint64)size);
return size;
}
break;
case 1:
if (this->vncClientIPC.state() != QLocalSocket::ConnectedState) {
return 0;
}
if ((quint64)this->vncClientIPC.bytesAvailable() >= size) {
this->vncClientIPC.read((char *)data, (qint64)size);
return size;
}
break;
}
}
return 0;
}
QImage rfbclientcls::getVNCImage()
{
return this->VNCIMAGE;
}
bool rfbclientcls::pauseRFB()
{
this->pauseMode = true;
//make image black in white
this->pauseResumeImg = this->VNCIMAGE;
this->VNCIMAGE = this->VNCIMAGE.convertToFormat(QImage::Format_Mono);
this->disconnectFromHost();
emit this->rfbPauseSignal();
return true;
}
bool rfbclientcls::resumeRFB()
{
this->VNCIMAGE = this->pauseResumeImg;//this->VNCIMAGE.convertToFormat(QImage::Format_RGB888);
this->pauseMode = false;
return true;
switch (socketMode) {
case 0:
if (this->connectToHostTCP(this->serverName, this->serverPort)) {
this->pauseMode = false;
return true;
}
break;
case 1:
if (this->connectToHostIPC(this->serverName)) {
this->pauseMode = false;
return true;
}
break;
}
return false;
/*if (this->serverConnected)
{
this->sendClientFrameBufferRequestUpdate(0,0,this->VNCIMAGE.width(),this->VNCIMAGE.height(),0);
this->pauseMode = false;
return true;
}
return false;*/
}
qint32 rfbclientcls::getColorFormat(pixelFormatStruct *pixel)
{
// qDebug() << " check color format for following parameter:";
// qDebug() << " ... - BPP = " << pixel->bpp << ", Depth = " << pixel->depth << ", True color = " << pixel->true_color_flag;
// qDebug() << " ... - Red max = " << pixel->red_max << ", Green max = " << pixel->green_max << ", Blue max = " << pixel->blue_max;
// qDebug() << " ... - Red shift = " << pixel->red_shift << ", Green shift = " << pixel->green_shift << ", Blue shift = " << pixel->blue_shift;
if (pixel->bpp == 32 && pixel->depth == 24 && pixel->true_color_flag == 1 &&
pixel->red_max == 0xFF && pixel->green_max == 0xFF && pixel->blue_max == 0xFF &&
pixel->red_shift == 0x10 && pixel->green_shift == 0x08 && pixel->blue_shift == 0x00) {
// qDebug() << " color format identified as RGB888";
return PIXEL_FORMAT_RGB_888;
}
if (pixel->bpp == 16 && pixel->depth == 16 && pixel->true_color_flag == 1 &&
pixel->red_max == 0x1F && pixel->green_max == 0x3F && pixel->blue_max == 0x1F &&
pixel->red_shift == 0x0B && pixel->green_shift == 0x05 && pixel->blue_shift == 0x00) {
// qDebug() << " color format identified as RGB565";
return PIXEL_FORMAT_RGB_565;
}
if (pixel->bpp == 16 && pixel->depth == 15 && pixel->true_color_flag == 1 &&
pixel->red_max == 0x1F && pixel->green_max == 0x1F && pixel->blue_max == 0x1F &&
pixel->red_shift == 0x0A && pixel->green_shift == 0x05 && pixel->blue_shift == 0x00) {
// qDebug()<< " color format identified as RGB555";
return PIXEL_FORMAT_RGB_555;
}
qDebug() << " ERROR - unknown Color format";
return PIXEL_FORMAT_NONE;
}
void rfbclientcls::vncSockDisconnectedSlot()
{
this->disconnectFromHost();
return;
}
bool rfbclientcls::connectToHostTCP(QString server, qint16 port)
{
this->socketMode = 0;
return this->connectToHost(server, port);
}
bool rfbclientcls::connectToHostIPC(QString server)
{
this->socketMode = 1;
return this->connectToHost(server, 0);
}
bool rfbclientcls::connectToHost(QString server, qint16 port)
{
this->serverName = server;
this->serverPort = port;
QFile sf;
this->opsThreadTimer.stop();
disconnect(&this->opsThreadTimer, SIGNAL(timeout()), this, SLOT(opsThreadTimerTimeOutSlot()));
disconnect(&this->vncClientTCP, SIGNAL(disconnected()), this, SLOT(vncSockDisconnectedSlot()));
this->vncClientTCP.abort();
disconnect(&this->vncClientIPC, SIGNAL(disconnected()), this, SLOT(vncSockDisconnectedSlot()));
this->vncClientIPC.abort();
//restart fresh
switch (this->socketMode) {
case 0:
connect(&this->vncClientTCP, SIGNAL(disconnected()), this, SLOT(vncSockDisconnectedSlot()));
this->vncClientTCP.connectToHost(server, port);
if (!this->vncClientTCP.waitForConnected(3000)) {
qDebug() << "rfbclientcls: fail to connect to host." << this->vncClientTCP.errorString();
this->disconnectFromHost();
return false;
}
break;
case 1:
//check if local server IPC file exists
sf.setFileName(server);
if (!sf.exists()) {
qDebug() << "VNC Connect: IPC Server not exists! Exiting";
return false;
}
connect(&this->vncClientIPC, SIGNAL(disconnected()), this, SLOT(vncSockDisconnectedSlot()));
this->vncClientIPC.connectToServer(server);
if (!this->vncClientIPC.waitForConnected(3000)) {
qDebug() << "rfbclientcls: fail to connect to host." << this->vncClientTCP.errorString();
this->disconnectFromHost();
return false;
}
}
qDebug("vnc sock connected");
this->serverConnected = true;
this->pauseMode = false;
emit this->rfbHostConnectedSignal();
if (!this->performHandshakingProtocol()) {
this->disconnectFromHost();
return false;
}
qDebug() << "Timer start";
this->opsThreadTimer.setSingleShot(true);
connect(&this->opsThreadTimer, SIGNAL(timeout()), this, SLOT(opsThreadTimerTimeOutSlot()), Qt::DirectConnection);
this->opsThreadTimer.start(1);
return true;
}
void rfbclientcls::disconnectFromHost()
{
qint16 w, h;
this->opsThreadTimer.stop();
disconnect(&this->opsThreadTimer, SIGNAL(timeout()), this, SLOT(opsThreadTimerTimeOutSlot()));
disconnect(&this->vncClientTCP, SIGNAL(disconnected()), this, SLOT(vncSockDisconnectedSlot()));
this->vncClientTCP.abort();
disconnect(&this->vncClientIPC, SIGNAL(disconnected()), this, SLOT(vncSockDisconnectedSlot()));
this->vncClientIPC.abort();
qDebug("sock disconnected");
this->serverConnected = false;
if (!this->pauseMode) {
//if disconnection request is not from pause
this->VNCIMAGE = QImage(":/new/prefix1/creditimg.png");
w = this->VNCIMAGE.width();
h = this->VNCIMAGE.height();
emit this->rfbResizeSignal(w, h, "");
}
//if disconnect request from pause.
//then reset the pause
this->pauseMode = false;
emit this->rfbHostDisconnectedSignal();
}
bool rfbclientcls::performHandshakingProtocol()
{
if (!this->handleVersionProtocol()) {
return false;
}
if (!this->handleSecurityProtocol()) {
return false;
}
if (!this->handleClientInitProtocol()) {
return false;
}
if (!this->handleServerInitProtocol()) {
return false;
}
return true;
}
bool rfbclientcls::handleVersionProtocol()
{
unsigned char vncServerData[12];
QString enc;
qDebug("Handling version protocol");
//step 1. Get the version protocol from server
if (this->readFromHost((unsigned char *)vncServerData, 12) != 12) {
qWarning() << "couldn't read enough";
return false;
}
qDebug() << "Got version stuff";
enc.clear();
this->serverMajorVersion = enc.append((QChar)vncServerData[6]).toInt();
enc.clear();
this->serverMinorVersion = enc.append((QChar)vncServerData[10]).toInt();
qDebug(" server version %d.%d", serverMajorVersion, serverMinorVersion);
//client to server
enc.clear();
this->clientMinorVersion = 0;
if (this->serverMajorVersion >= 3) {
this->clientMajorVersion = 3;
switch (this->serverMinorVersion) {
case 3:
this->clientMinorVersion = 3;
enc.append("RFB 003.003\n");
break;
case 7:
this->clientMinorVersion = 7;
enc.append("RFB 003.007\n");
break;
case 8:
this->clientMinorVersion = 8;
enc.append("RFB 003.008\n");
break;
}
}
if (this->clientMinorVersion == 0) {
return false;
}
//test
//enc.clear();
//enc.append("RFB 004.000\n");
//send data to server
qint32 writers = this->writeToHost((unsigned char *)enc.toStdString().c_str(), 12);
if (writers == 12) {
return true;
}
return false;
}
bool rfbclientcls::handleSecurityProtocol()
{
unsigned char vncServerData[20];
unsigned char vncClientData[10];
QString enc;
qDebug("Handling security protocol");
//step 1. Get the version protocol from server
if (this->readFromHost(vncServerData, 2) != 2) {
return false;
}
qint8 numberOfSecurity = 0;
qint8 securityID = vncServerData[1];
numberOfSecurity = vncServerData[0];
numberOfSecurity = numberOfSecurity;
//get security info
//qDebug() << " number of security " << numberOfSecurity;
//qDebug() << " security id" << securityID;
switch (securityID) {
//no security
case 0x01:
vncClientData[0] = 0x01;
qDebug(" Server requires no security");
break;
case 0x02:
qDebug(" Requires VNC authentication");
break;
default:
qDebug(" unknown security id (%d)", vncServerData[1]);
}
//send to server for security cleareance
qDebug(" requesting no security option");
if (this->writeToHost((unsigned char *)vncClientData, 1) != 1) {
return false;
}
qDebug(" waiting for security clearence");
//wait for server acknowledgement
if (this->readFromHost((unsigned char *)vncServerData, 4) != 4) {
return false;
}
if (enc.fromLocal8Bit((char *)vncServerData, 4).toInt() == 0) {
qDebug(" security pass");
return true;
}
qDebug(" Security fail");
return false;
}
bool rfbclientcls::handleClientInitProtocol()
{
QString vncClientData;
qDebug("Handling Client Init Protocol");
vncClientData.clear();
vncClientData.append("\x01");
qDebug(" writing server a share flag");
if (this->writeToHost((unsigned char *)vncClientData.toStdString().c_str(), 1) != 1) {
return false;
}
return true;
}
bool rfbclientcls::handleServerInitProtocol()
{
rfbServerInitStruct rfbSID;
unsigned char *serverName = 0;
//unsigned char vncServerData[26];
qDebug("Handling Server Init Protocol");
if (this->readFromHost((unsigned char *)&rfbSID, sizeof(rfbSID)) != sizeof(rfbSID)) {
return false;
}
rfbSID.fbWidth = swap16(rfbSID.fbWidth);
rfbSID.fbHeight = swap16(rfbSID.fbHeight);
rfbSID.fbPixel.red_max = swap16(rfbSID.fbPixel.red_max);
rfbSID.fbPixel.green_max = swap16(rfbSID.fbPixel.green_max);
rfbSID.fbPixel.blue_max = swap16(rfbSID.fbPixel.blue_max);
rfbSID.fbNameLength = swap32(rfbSID.fbNameLength);
//set server frame buffer information
qDebug(" setting server frame buffer information");
qDebug() << "Height" << rfbSID.fbHeight;
qDebug() << "Width" << rfbSID.fbWidth;
//qDebug() << "pixel" << rfbSID.fbPixel.;
this->serverFBHeight = rfbSID.fbHeight;
this->serverFBWidth = rfbSID.fbWidth;
this->serverPixelF = rfbSID.fbPixel;
this->serverColorFormat = this->getColorFormat(&this->serverPixelF);
//read server name
serverName = (unsigned char *)malloc(rfbSID.fbNameLength + 1);
qDebug() << " reading server name for" << rfbSID.fbNameLength;
if (this->readFromHost(serverName, rfbSID.fbNameLength) != rfbSID.fbNameLength) {
return false;
}
serverName[rfbSID.fbNameLength] = 0x00;
this->serverVNCName = QString((char *)serverName).trimmed();
//create a new VNC Image;
this->VNCIMAGE = QImage(rfbSID.fbWidth, rfbSID.fbHeight, QImage::Format_RGB888);
this->serverFBHeight = rfbSID.fbHeight;
this->serverFBWidth = rfbSID.fbWidth;
emit this->rfbResizeSignal(rfbSID.fbWidth, rfbSID.fbHeight, this->serverVNCName);
//this->updateFBGetDesktopSizePseudo(rfbSID.fbWidth,rfbSID.fbHeight);
qDebug("SERVER NAME IS %s", this->serverVNCName.toStdString().c_str());
free(serverName);
//create vncImage
//qDebug(" Creating new RFB Image QImage RGB888");
//this->VNCIMAGE = QImage(this->serverFBWidth,this->serverFBHeight,QImage::Format_RGB888);
//check if serverColorFormat == clientColorFormat
if (this->serverColorFormat != this->clientColorFormat) {
//set server color format
qDebug(" Resetting server pixel format");
if (!this->sendClientSetPixelFormat(this->clientPixelF)) {
return false;
}
}
//encodings
if (!this->sendClientSetEncodings(0)) { //raw
return false;
}
if (!this->sendClientSetEncodings(-239)) { //cursor pseudo
return false;
}
if (!this->sendClientSetEncodings(-223)) { //desktopsize pseudo
return false;
}
//qDebug(" emiting resize at %d x %d",rfbSID.fbWidth,rfbSID.fbHeight);
//qDebug("name length is %d",rfbSID.fbNameLength);
//qDebug("server name is %s",QString(vncServerData.mid(24,rfbSID.fbNameLength)).toStdString().c_str());
return this->sendClientFrameBufferRequestUpdate(0, 0, rfbSID.fbWidth, rfbSID.fbHeight, 0);
return true;
}
bool rfbclientcls::sendClientFrameBufferRequestUpdate(int x, int y, int width, int height, int incremental)
{
unsigned char vncClientData[10];
char inc = 0x01;
if (incremental == 0) {
//qDebug("frame request is not incremental");
inc = 0x00;
}
vncClientData[0] = 3;
vncClientData[1] = inc; //(incremental) ? 1 : 0;
vncClientData[2] = (x >> 8) & 0xFF;
vncClientData[3] = (x >> 0) & 0xFF;
vncClientData[4] = (y >> 8) & 0xFF;
vncClientData[5] = (y >> 0) & 0xFF;
vncClientData[6] = (width >> 8) & 0xFF;
vncClientData[7] = (width >> 0) & 0xFF;
vncClientData[8] = (height >> 8) & 0xFF;
vncClientData[9] = (height >> 0) & 0xFF;
if (this->writeToHost(vncClientData, 10) != 10) {
qDebug(" fail to write request update");
return false;
}
qDebug(" success request update");
return true;
}
bool rfbclientcls::sendClientSetEncodings(qint32 encID)
{
SET_ENCODING_STRUCT enc;
qint32 encType;
enc.msgType = 2;
enc.padding = 2;
enc.numOfEncodings = 1; //numofencoding in LE
enc.numOfEncodings = swap16(enc.numOfEncodings); //numofencoding in BE
enc.encoding = swap32(encID);
if (this->writeToHost((unsigned char *)&enc, 8) != 8) {
qDebug(" fail to set encoding");
return false;
}
return true;
//send raw encoding
encType = 0;
encType = swap32(encType);
if (this->writeToHost((unsigned char *)&encType, 4) != 4) {
qDebug(" fail to set encoding");
return false;
}
//send desktop resize
encType = -223;
encType = swap32(encType);
if (this->writeToHost((unsigned char *)&encType, 4) != 4) {
qDebug(" fail to set encoding");
return false;
}
return true;
}
bool rfbclientcls::sendClientSetPixelFormat(pixelFormatStruct pixel)
{
unsigned char vncClientData[20];
vncClientData[0] = 0; //message type
vncClientData[1] = 0x00;
vncClientData[2] = 0x00;
vncClientData[3] = 0x00;
vncClientData[4] = pixel.bpp;
vncClientData[5] = pixel.depth;
vncClientData[6] = pixel.big_endian_flag;
vncClientData[7] = pixel.true_color_flag;
vncClientData[8] = (pixel.red_max >> 8) & 0xFF;
vncClientData[9] = (pixel.red_max >> 0) & 0xFF;
vncClientData[10] = (pixel.green_max >> 8) & 0xFF;
vncClientData[11] = (pixel.green_max >> 0) & 0xFF;
vncClientData[12] = (pixel.blue_max >> 8) & 0xFF;
vncClientData[13] = (pixel.blue_max >> 0) & 0xFF;
vncClientData[14] = pixel.red_shift;
vncClientData[15] = pixel.green_shift;
vncClientData[16] = pixel.blue_shift;
vncClientData[17] = 0x00;
vncClientData[18] = 0x00;
vncClientData[19] = 0x00;
if (this->writeToHost(vncClientData, 20) != 20) {
qDebug(" fail to set pixel format");
return false;
}
return true;
}
QPixmap rfbclientcls::getVNCPixmap()
{
return QPixmap::fromImage(this->VNCIMAGE);
}
quint16 rfbclientcls::swap16(quint16 src)
{
quint16 tgt;
char *tgtX = (char *)&tgt;
char *srcX = (char *)&src;
tgtX[0] = srcX[1];
tgtX[1] = srcX[0];
return tgt;
}
qint16 rfbclientcls::swap16(qint16 src)
{
qint16 tgt;
char *tgtX = (char *)&tgt;
char *srcX = (char *)&src;
tgtX[0] = srcX[1];
tgtX[1] = srcX[0];
return tgt;
}
quint32 rfbclientcls::swap32(quint32 src)
{
quint32 tgt;
char *tgtX = (char *)&tgt;
char *srcX = (char *)&src;
tgtX[0] = srcX[3];
tgtX[1] = srcX[2];
tgtX[2] = srcX[1];
tgtX[3] = srcX[0];
return tgt;
}
qint32 rfbclientcls::swap32(qint32 src)
{
qint32 tgt;
char *tgtX = (char *)&tgt;
char *srcX = (char *)&src;
tgtX[0] = srcX[3];
tgtX[1] = srcX[2];
tgtX[2] = srcX[1];
tgtX[3] = srcX[0];
return tgt;
}
qint64 rfbclientcls::writeToHost(unsigned char *src, qint64 size)
{
qint64 rs = 0;
switch (this->socketMode) {
case 0:
rs = this->vncClientTCP.write((char *)src, size);
if (!this->vncClientTCP.waitForBytesWritten()) {
return 0;
}
break;
case 1:
rs = this->vncClientIPC.write((char *)src, size);
if (!this->vncClientIPC.waitForBytesWritten()) {
return 0;
}
break;
}
return rs;
}
qint64 rfbclientcls::writeToHostNonBlock(unsigned char *src, qint64 size)
{
qint64 rs = 0;
switch (this->socketMode) {
case 0:
if (this->vncClientTCP.state() == QTcpSocket::ConnectedState) {
rs = this->vncClientTCP.write((char *)src, size);
}
break;
case 1:
if (this->vncClientIPC.state() == QLocalSocket::ConnectedState) {
rs = this->vncClientIPC.write((char *)src, size);
}
break;
}
return rs;
}
qint64 rfbclientcls::readFromHost(unsigned char *tgt, qint64 size)
{
switch (this->socketMode) {
case 0:
if (this->vncClientTCP.state() == QTcpSocket::ConnectedState) {
while (1) {
QCoreApplication::processEvents();
if (this->vncClientTCP.bytesAvailable() == 0) {
this->vncClientTCP.waitForReadyRead(); // wait for data to arrive
}
if (this->vncClientTCP.bytesAvailable() >= size) {
return this->vncClientTCP.read((char *)tgt, size);
}
}
}
break;
case 1:
if (this->vncClientIPC.state() == QLocalSocket::ConnectedState) {
while (1) {
QCoreApplication::processEvents();
if (this->vncClientIPC.bytesAvailable() == 0) {
this->vncClientIPC.waitForReadyRead(); // wait for data to arrive
}