@@ -59,18 +59,21 @@ VNCClient::VNCClient()
5959 : _socket(NULL ),
6060 _versionEstablished(false ),
6161 _securityEstablished(false ),
62+ _autenticationPassed(false ),
6263 _handshakeFinished(false ),
6364 _communicationError(false ),
6465 _isReady(false ),
6566 _establishedVersion(38 ),
6667 _establishedSecurity(Invalid),
67- _serverParameters(NULL )
68+ _serverParameters(NULL ),
69+ _password(NULL )
6870{
6971}
7072
7173VNCClient::~VNCClient ()
7274{
7375 delete _serverParameters;
76+ delete _password;
7477}
7578
7679VNCClient* VNCClient::getInstance ()
@@ -94,7 +97,30 @@ bool VNCClient::Init(QString remoteHost, quint16 port)
9497 addr.setAddress (remoteHost);
9598 }
9699
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+
97107 _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+ }
98124
99125 // QObject::connect(_socket, SIGNAL(bytesWritten(qint64)), this, SLOT(readSocket(qint64)));
100126 QObject::connect (_socket, SIGNAL (readyRead ()), this , SLOT (readSocket ()));
@@ -103,6 +129,9 @@ bool VNCClient::Init(QString remoteHost, quint16 port)
103129 QObject::connect (_socket, SIGNAL (stateChanged (QAbstractSocket::SocketState)),
104130 this , SLOT (onStateChanged (QAbstractSocket::SocketState)));
105131
132+ _socket->connectToHost (addr, port);
133+ _socket->waitForConnected ();
134+
106135 return _socket->isOpen ();
107136}
108137
@@ -130,6 +159,12 @@ QByteArray VNCClient::readSocket()
130159 establishSecurity (data);
131160 return data;
132161 }
162+
163+ // Go through autentication
164+ if (!_autenticationPassed && VNCAuthentication == _establishedSecurity)
165+ {
166+ // todo
167+ }
133168 if (!_handshakeFinished)
134169 {
135170 finishHandshaking (data);
@@ -164,6 +199,12 @@ QByteArray VNCClient::readSocket(qint64 size)
164199 establishSecurity (data);
165200 return data;
166201 }
202+
203+ // Go through autentication
204+ if (!_autenticationPassed && VNCAuthentication == _establishedSecurity)
205+ {
206+ // todo
207+ }
167208 if (!_handshakeFinished)
168209 {
169210 finishHandshaking (data);
@@ -179,8 +220,17 @@ QByteArray VNCClient::readSocket(qint64 size)
179220
180221qint64 VNCClient::writeToSocket (QByteArray &data)
181222{
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+ }
184234
185235 return bytesNmb;
186236}
@@ -304,15 +354,27 @@ bool VNCClient::establishSecurity(QByteArray& data)
304354 }
305355 case None:
306356 {
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 ;
308371 QByteArray response (1 , one);
309372 writeToSocket (response);
310373 _securityEstablished = true ;
311374 _establishedSecurity = None;
312375 return true ;
313376 break ;
314377 }
315- case VNCAuthentication: break ;
316378 case RA2: break ;
317379 case RA2ne: break ;
318380 case Tight: break ;
@@ -337,15 +399,31 @@ bool VNCClient::establishSecurity(QByteArray& data)
337399 break ;
338400 }
339401 case None:
402+ {
403+ if (NULL == _password)
404+ {
405+ _securityEstablished = true ;
406+ _establishedSecurity = None;
407+ return true ;
408+ }
409+ break ;
410+ }
411+ case VNCAuthentication:
340412 {
341413 _securityEstablished = true ;
342- _establishedSecurity = None ;
414+ _establishedSecurity = VNCAuthentication ;
343415 return true ;
344- break ;
345416 }
346- case VNCAuthentication: break ;
417+ break ;
347418 }
348419 }
420+
421+ return false ;
422+ }
423+
424+ bool VNCClient::passAutentication (QByteArray &data)
425+ {
426+ return false ;
349427}
350428
351429bool VNCClient::finishHandshaking (QByteArray &data)
0 commit comments