@@ -59,18 +59,21 @@ VNCClient::VNCClient()
59
59
: _socket(NULL ),
60
60
_versionEstablished(false ),
61
61
_securityEstablished(false ),
62
+ _autenticationPassed(false ),
62
63
_handshakeFinished(false ),
63
64
_communicationError(false ),
64
65
_isReady(false ),
65
66
_establishedVersion(38 ),
66
67
_establishedSecurity(Invalid),
67
- _serverParameters(NULL )
68
+ _serverParameters(NULL ),
69
+ _password(NULL )
68
70
{
69
71
}
70
72
71
73
VNCClient::~VNCClient ()
72
74
{
73
75
delete _serverParameters;
76
+ delete _password;
74
77
}
75
78
76
79
VNCClient* VNCClient::getInstance ()
@@ -94,7 +97,30 @@ bool VNCClient::Init(QString remoteHost, quint16 port)
94
97
addr.setAddress (remoteHost);
95
98
}
96
99
100
+ // QObject::connect(_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(readSocket(qint64)));
101
+ QObject::connect (_socket, SIGNAL (readyRead ()), this , SLOT (readSocket ()));
102
+ QObject::connect (_socket, SIGNAL (error (QAbstractSocket::SocketError)),
103
+ this , SLOT (onError (QAbstractSocket::SocketError)));
104
+ QObject::connect (_socket, SIGNAL (stateChanged (QAbstractSocket::SocketState)),
105
+ this , SLOT (onStateChanged (QAbstractSocket::SocketState)));
106
+
97
107
_socket->connectToHost (addr, port);
108
+ _socket->waitForConnected ();
109
+
110
+ return _socket->isOpen ();
111
+ }
112
+
113
+ bool VNCClient::Init (QString remoteHost, quint16 port, QString* password)
114
+ {
115
+ _password = password;
116
+ _socket = new QTcpSocket ();
117
+ QHostAddress addr;
118
+
119
+ if (!addr.setAddress (remoteHost))
120
+ {
121
+ remoteHost.replace (QRegExp (" http*://" ), " " );
122
+ addr.setAddress (remoteHost);
123
+ }
98
124
99
125
// QObject::connect(_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(readSocket(qint64)));
100
126
QObject::connect (_socket, SIGNAL (readyRead ()), this , SLOT (readSocket ()));
@@ -103,6 +129,9 @@ bool VNCClient::Init(QString remoteHost, quint16 port)
103
129
QObject::connect (_socket, SIGNAL (stateChanged (QAbstractSocket::SocketState)),
104
130
this , SLOT (onStateChanged (QAbstractSocket::SocketState)));
105
131
132
+ _socket->connectToHost (addr, port);
133
+ _socket->waitForConnected ();
134
+
106
135
return _socket->isOpen ();
107
136
}
108
137
@@ -130,6 +159,12 @@ QByteArray VNCClient::readSocket()
130
159
establishSecurity (data);
131
160
return data;
132
161
}
162
+
163
+ // Go through autentication
164
+ if (!_autenticationPassed && VNCAuthentication == _establishedSecurity)
165
+ {
166
+ // todo
167
+ }
133
168
if (!_handshakeFinished)
134
169
{
135
170
finishHandshaking (data);
@@ -164,6 +199,12 @@ QByteArray VNCClient::readSocket(qint64 size)
164
199
establishSecurity (data);
165
200
return data;
166
201
}
202
+
203
+ // Go through autentication
204
+ if (!_autenticationPassed && VNCAuthentication == _establishedSecurity)
205
+ {
206
+ // todo
207
+ }
167
208
if (!_handshakeFinished)
168
209
{
169
210
finishHandshaking (data);
@@ -179,8 +220,17 @@ QByteArray VNCClient::readSocket(qint64 size)
179
220
180
221
qint64 VNCClient::writeToSocket (QByteArray &data)
181
222
{
182
- int bytesNmb = _socket->write (data);
183
- _socket->flush ();
223
+ int bytesNmb = 0 ;
224
+
225
+ if (QAbstractSocket::ConnectedState == _socket->state ())
226
+ {
227
+ bytesNmb = _socket->write (data);
228
+ _socket->flush ();
229
+ }
230
+ else
231
+ {
232
+ std::cout << " #### Socket isn't in connected state. Couldn't write to socket" << std::endl;
233
+ }
184
234
185
235
return bytesNmb;
186
236
}
@@ -304,15 +354,27 @@ bool VNCClient::establishSecurity(QByteArray& data)
304
354
}
305
355
case None:
306
356
{
307
- char one = 0x01 ;
357
+ if (NULL == _password)
358
+ {
359
+ char one = 0x01 ;
360
+ QByteArray response (1 , one);
361
+ writeToSocket (response);
362
+ _securityEstablished = true ;
363
+ _establishedSecurity = None;
364
+ return true ;
365
+ }
366
+ break ;
367
+ }
368
+ case VNCAuthentication:
369
+ {
370
+ char one = 0x02 ;
308
371
QByteArray response (1 , one);
309
372
writeToSocket (response);
310
373
_securityEstablished = true ;
311
374
_establishedSecurity = None;
312
375
return true ;
313
376
break ;
314
377
}
315
- case VNCAuthentication: break ;
316
378
case RA2: break ;
317
379
case RA2ne: break ;
318
380
case Tight: break ;
@@ -337,15 +399,31 @@ bool VNCClient::establishSecurity(QByteArray& data)
337
399
break ;
338
400
}
339
401
case None:
402
+ {
403
+ if (NULL == _password)
404
+ {
405
+ _securityEstablished = true ;
406
+ _establishedSecurity = None;
407
+ return true ;
408
+ }
409
+ break ;
410
+ }
411
+ case VNCAuthentication:
340
412
{
341
413
_securityEstablished = true ;
342
- _establishedSecurity = None ;
414
+ _establishedSecurity = VNCAuthentication ;
343
415
return true ;
344
- break ;
345
416
}
346
- case VNCAuthentication: break ;
417
+ break ;
347
418
}
348
419
}
420
+
421
+ return false ;
422
+ }
423
+
424
+ bool VNCClient::passAutentication (QByteArray &data)
425
+ {
426
+ return false ;
349
427
}
350
428
351
429
bool VNCClient::finishHandshaking (QByteArray &data)
0 commit comments