@@ -59,7 +59,7 @@ const unsigned short Socket::defaultPortNumber = DEFAULT_PORT_NUMBER;
5959Socket::Socket ( const std::string& host, const unsigned short port )
6060 : _host( host )
6161 , _socket( new QTcpSocket( ))
62- , _remoteProtocolVersion ( INVALID_NETWORK_PROTOCOL_VERSION )
62+ , _serverProtocolVersion ( INVALID_NETWORK_PROTOCOL_VERSION )
6363{
6464 // disable warnings which occur if no QCoreApplication is present during
6565 // _connect(): QObject::connect: Cannot connect (null)::destroyed() to
@@ -91,6 +91,11 @@ bool Socket::isConnected() const
9191 return _socket->state () == QTcpSocket::ConnectedState;
9292}
9393
94+ int32_t Socket::getServerProtocolVersion () const
95+ {
96+ return _serverProtocolVersion;
97+ }
98+
9499int Socket::getFileDescriptor () const
95100{
96101 return _socket->socketDescriptor ();
@@ -172,11 +177,6 @@ bool Socket::receive( MessageHeader& messageHeader, QByteArray& message )
172177 return true ;
173178}
174179
175- int32_t Socket::getRemoteProtocolVersion () const
176- {
177- return _remoteProtocolVersion;
178- }
179-
180180bool Socket::_receiveHeader ( MessageHeader& messageHeader )
181181{
182182 while ( _socket->bytesAvailable () < qint64 (MessageHeader::serializedSize) )
@@ -193,45 +193,42 @@ bool Socket::_receiveHeader( MessageHeader& messageHeader )
193193
194194bool Socket::_connect ( const std::string& host, const unsigned short port )
195195{
196- // make sure we're disconnected
197- _socket->disconnectFromHost ();
198-
199- // open connection
200196 _socket->connectToHost ( host.c_str (), port );
201-
202197 if ( !_socket->waitForConnected ( RECEIVE_TIMEOUT_MS ))
203198 {
204- std::cerr << " could not connect to host " << host << " :" << port
199+ std::cerr << " could not connect to " << host << " :" << port
205200 << std::endl;
206201 return false ;
207202 }
208203
209- // handshake
210- if ( _checkProtocolVersion ( ))
211- return true ;
204+ if ( !_receiveProtocolVersion ( ))
205+ {
206+ std::cerr << " server protocol version was not received" << std::endl;
207+ _socket->disconnectFromHost ();
208+ return false ;
209+ }
210+
211+ if ( _serverProtocolVersion < NETWORK_PROTOCOL_VERSION )
212+ {
213+ std::cerr << " server uses unsupported protocol: "
214+ << _serverProtocolVersion << " < "
215+ << NETWORK_PROTOCOL_VERSION << std::endl;
216+ _socket->disconnectFromHost ();
217+ return false ;
218+ }
212219
213- std::cerr << " Protocol version check failed for host: " << host << " :"
214- << port << std::endl;
215- _socket->disconnectFromHost ();
216- return false ;
220+ return true ;
217221}
218222
219- bool Socket::_checkProtocolVersion ()
223+ bool Socket::_receiveProtocolVersion ()
220224{
221225 while ( _socket->bytesAvailable () < qint64 (sizeof (int32_t )) )
222226 {
223227 if ( !_socket->waitForReadyRead ( RECEIVE_TIMEOUT_MS ))
224228 return false ;
225229 }
226-
227- _socket->read ((char *)&_remoteProtocolVersion, sizeof (int32_t ));
228-
229- if ( _remoteProtocolVersion == NETWORK_PROTOCOL_VERSION )
230- return true ;
231-
232- std::cerr << " unsupported protocol version " << _remoteProtocolVersion
233- << " != " << NETWORK_PROTOCOL_VERSION << std::endl;
234- return false ;
230+ _socket->read ((char *)&_serverProtocolVersion, sizeof (int32_t ));
231+ return true ;
235232}
236233
237234}
0 commit comments