From b3fbfcd3738a0ff864439499390513b95ca671aa Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Wed, 23 Jun 2021 18:26:10 +0300 Subject: QLocalSocket: reimplement readLineData() function The base implementation reads data using repeated calls to getChar(), which is quite slow. Change-Id: Ie46624df63791b2cdd3c8a28fe3327427d942505 Reviewed-by: Oswald Buddenhagen --- src/corelib/io/qwindowspipereader.cpp | 24 ++++++++++++++++++++++++ src/corelib/io/qwindowspipereader_p.h | 1 + 2 files changed, 25 insertions(+) (limited to 'src/corelib/io') diff --git a/src/corelib/io/qwindowspipereader.cpp b/src/corelib/io/qwindowspipereader.cpp index 539902c233..c3ac51df94 100644 --- a/src/corelib/io/qwindowspipereader.cpp +++ b/src/corelib/io/qwindowspipereader.cpp @@ -208,6 +208,30 @@ qint64 QWindowsPipeReader::read(char *data, qint64 maxlen) return readSoFar; } +/*! + Reads a line from the internal buffer, but no more than \c{maxlen} + characters. A terminating '\0' byte is always appended to \c{data}, + so \c{maxlen} must be larger than 1. + */ +qint64 QWindowsPipeReader::readLine(char *data, qint64 maxlen) +{ + QMutexLocker locker(&mutex); + qint64 readSoFar = 0; + + if (actualReadBufferSize > 0) { + readSoFar = readBuffer.readLine(data, qMin(actualReadBufferSize + 1, maxlen)); + actualReadBufferSize -= readSoFar; + } + + if (!pipeBroken) { + startAsyncReadHelper(&locker); + if (readSoFar == 0) + return -2; // signal EWOULDBLOCK + } + + return readSoFar; +} + /*! Returns \c true if a complete line of data can be read from the buffer. */ diff --git a/src/corelib/io/qwindowspipereader_p.h b/src/corelib/io/qwindowspipereader_p.h index 77c90645c6..e2190d67d9 100644 --- a/src/corelib/io/qwindowspipereader_p.h +++ b/src/corelib/io/qwindowspipereader_p.h @@ -78,6 +78,7 @@ public: bool isPipeClosed() const { return pipeBroken; } qint64 bytesAvailable() const; qint64 read(char *data, qint64 maxlen); + qint64 readLine(char *data, qint64 maxlen); bool canReadLine() const; DWORD checkPipeState(); bool checkForReadyRead() { return consumePendingAndEmit(false); } -- cgit v1.2.3