mirror of
https://github.com/itay-grudev/SingleApplication.git
synced 2025-01-28 06:30:19 +08:00
Add simple acknowledge protocol to ensure the server has received all data
The server now acknowledges every received message by sending a \r. By waiting for the acknowledgement, clients should no longer terminate too early, causing bytes getting lost in Qt's internal socket handling. Message handling in the server is simplified because we can now rely on the readyRead signal being raised for every frame. This should finally solve #125 and #121. What remains is to correctly handle data sent using SingleApplication::sendMessage.
This commit is contained in:
parent
fda9c4b178
commit
0c458ec8c9
@ -248,10 +248,7 @@ bool SingleApplication::sendMessage( const QByteArray &message, int timeout )
|
||||
if( ! d->connectToPrimary( timeout, SingleApplicationPrivate::Reconnect ) )
|
||||
return false;
|
||||
|
||||
d->socket->write( message );
|
||||
bool dataWritten = d->socket->waitForBytesWritten( timeout );
|
||||
d->socket->flush();
|
||||
return dataWritten;
|
||||
return d->writeConfirmedMessage( timeout, message );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -272,11 +272,31 @@ bool SingleApplicationPrivate::connectToPrimary( int msecs, ConnectionType conne
|
||||
#endif
|
||||
headerStream << static_cast <quint64>( initMsg.length() );
|
||||
|
||||
socket->write( header );
|
||||
socket->write( initMsg );
|
||||
bool result = socket->waitForBytesWritten( static_cast<int>(msecs - time.elapsed()) );
|
||||
if( !writeConfirmedMessage( static_cast<int>(msecs - time.elapsed()), header ) )
|
||||
return false;
|
||||
|
||||
if( !writeConfirmedMessage( static_cast<int>(msecs - time.elapsed()), initMsg ) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SingleApplicationPrivate::writeAck( QLocalSocket *sock ) {
|
||||
sock->putChar('\n');
|
||||
}
|
||||
|
||||
bool SingleApplicationPrivate::writeConfirmedMessage( int msecs, const QByteArray &msg )
|
||||
{
|
||||
socket->write( msg );
|
||||
socket->flush();
|
||||
return result;
|
||||
|
||||
bool result = socket->waitForReadyRead( msecs ); // await ack byte
|
||||
if (result) {
|
||||
socket->read( 1 );
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
quint16 SingleApplicationPrivate::blockChecksum() const
|
||||
@ -379,9 +399,7 @@ void SingleApplicationPrivate::readInitMessageHeader( QLocalSocket *sock )
|
||||
info.stage = StageBody;
|
||||
info.msgLen = msgLen;
|
||||
|
||||
if ( sock->bytesAvailable() >= (qint64) msgLen ){
|
||||
readInitMessageBody( sock );
|
||||
}
|
||||
writeAck( sock );
|
||||
}
|
||||
|
||||
void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
||||
@ -448,15 +466,15 @@ void SingleApplicationPrivate::readInitMessageBody( QLocalSocket *sock )
|
||||
Q_EMIT q->instanceStarted();
|
||||
}
|
||||
|
||||
if (sock->bytesAvailable() > 0){
|
||||
this->slotDataAvailable( sock, instanceId );
|
||||
}
|
||||
writeAck( sock );
|
||||
}
|
||||
|
||||
void SingleApplicationPrivate::slotDataAvailable( QLocalSocket *dataSocket, quint32 instanceId )
|
||||
{
|
||||
Q_Q(SingleApplication);
|
||||
Q_EMIT q->receivedMessage( instanceId, dataSocket->readAll() );
|
||||
|
||||
writeAck( dataSocket );
|
||||
}
|
||||
|
||||
void SingleApplicationPrivate::slotClientConnectionClosed( QLocalSocket *closedSocket, quint32 instanceId )
|
||||
|
@ -81,6 +81,8 @@ public:
|
||||
QString primaryUser() const;
|
||||
void readInitMessageHeader(QLocalSocket *socket);
|
||||
void readInitMessageBody(QLocalSocket *socket);
|
||||
bool writeConfirmedMessage(int msecs, const QByteArray &msg);
|
||||
void writeAck(QLocalSocket *sock);
|
||||
static void randomSleep();
|
||||
void addAppData(const QString &data);
|
||||
QStringList appData() const;
|
||||
|
Loading…
Reference in New Issue
Block a user