summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qwindowspipereader.cpp
blob: bf03737c3933f12fca9f8a7f1b22dd25cf9140b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2021 Alex Trotsenko <alex1973tr@gmail.com>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qwindowspipereader_p.h"
#include <qscopedvaluerollback.h>
#include <qcoreapplication.h>
#include <QMutexLocker>

QT_BEGIN_NAMESPACE

static const DWORD minReadBufferSize = 4096;

QWindowsPipeReader::QWindowsPipeReader(QObject *parent)
    : QObject(parent),
      handle(INVALID_HANDLE_VALUE),
      eventHandle(CreateEvent(NULL, FALSE, FALSE, NULL)),
      syncHandle(CreateEvent(NULL, FALSE, FALSE, NULL)),
      waitObject(NULL),
      readBufferMaxSize(0),
      actualReadBufferSize(0),
      pendingReadBytes(0),
      lastError(ERROR_SUCCESS),
      state(Stopped),
      readSequenceStarted(false),
      pipeBroken(false),
      readyReadPending(false),
      winEventActPosted(false),
      inReadyRead(false)
{
    ZeroMemory(&overlapped, sizeof(OVERLAPPED));
    overlapped.hEvent = eventHandle;
    waitObject = CreateThreadpoolWait(waitCallback, this, NULL);
    if (waitObject == NULL)
        qErrnoWarning("QWindowsPipeReader: CreateThreadpollWait failed.");
}

QWindowsPipeReader::~QWindowsPipeReader()
{
    stop();

    // Wait for thread pool callback to complete, as it can be still
    // executing some completion code.
    WaitForThreadpoolWaitCallbacks(waitObject, FALSE);
    CloseThreadpoolWait(waitObject);
    CloseHandle(eventHandle);
    CloseHandle(syncHandle);
}

/*!
    Sets the handle to read from. The handle must be valid.
    Do not call this function while the pipe is running.
 */
void QWindowsPipeReader::setHandle(HANDLE hPipeReadEnd)
{
    readBuffer.clear();
    actualReadBufferSize = 0;
    readyReadPending = false;
    pendingReadBytes = 0;
    handle = hPipeReadEnd;
    pipeBroken = false;
    lastError = ERROR_SUCCESS;
}

/*!
    Stops the asynchronous read sequence.
    If the read sequence is running then the I/O operation is canceled.
 */
void QWindowsPipeReader::stop()
{
    cancelAsyncRead(Stopped);
}

/*!
    Stops the asynchronous read sequence.
    Reads all pending bytes into the internal buffer.
 */
void QWindowsPipeReader::drainAndStop()
{
    cancelAsyncRead(Draining);

    // Note that signals are not emitted in the call below, as the caller
    // is expected to do that synchronously.
    consumePending();
}

/*!
    Stops the asynchronous read sequence.
 */
void QWindowsPipeReader::cancelAsyncRead(State newState)
{
    if (state != Running)
        return;

    QMutexLocker locker(&mutex);
    state = newState;
    if (readSequenceStarted) {
        // This can legitimately fail due to the GetOverlappedResult()
        // in the callback not being locked. We ignore ERROR_NOT_FOUND
        // in this case.
        if (!CancelIoEx(handle, &overlapped)) {
            const DWORD dwError = GetLastError();
            if (dwError != ERROR_NOT_FOUND) {
                qErrnoWarning(dwError, "QWindowsPipeReader: CancelIoEx on handle %p failed.",
                              handle);
            }
        }

        // Wait for callback to complete.
        do {
            locker.unlock();
            waitForNotification(QDeadlineTimer(-1));
            locker.relock();
        } while (readSequenceStarted);
    }
}

/*!
    Sets the size of internal read buffer.
 */
void QWindowsPipeReader::setMaxReadBufferSize(qint64 size)
{
    QMutexLocker locker(&mutex);
    readBufferMaxSize = size;
}

/*!
    Returns \c true if async operation is in progress, there is
    pending data to read, or a read error is pending.
*/
bool QWindowsPipeReader::isReadOperationActive() const
{
    QMutexLocker locker(&mutex);
    return readSequenceStarted || readyReadPending
           || (lastError != ERROR_SUCCESS && !pipeBroken);
}

/*!
    Returns the number of bytes we've read so far.
 */
qint64 QWindowsPipeReader::bytesAvailable() const
{
    return actualReadBufferSize;
}

/*!
    Copies at most \c{maxlen} bytes from the internal read buffer to \c{data}.
 */
qint64 QWindowsPipeReader::read(char *data, qint64 maxlen)
{
    if (pipeBroken && actualReadBufferSize == 0)
        return 0;  // signal EOF

    mutex.lock();
    qint64 readSoFar;
    // If startAsyncRead() has read data, copy it to its destination.
    if (maxlen == 1 && actualReadBufferSize > 0) {
        *data = readBuffer.getChar();
        actualReadBufferSize--;
        readSoFar = 1;
    } else {
        readSoFar = readBuffer.read(data, qMin(actualReadBufferSize, maxlen));
        actualReadBufferSize -= readSoFar;
    }
    mutex.unlock();

    if (!pipeBroken) {
        if (state == Running)
            startAsyncRead();
        if (readSoFar == 0)
            return -2;      // signal EWOULDBLOCK
    }

    return readSoFar;
}

/*!
    Returns \c true if a complete line of data can be read from the buffer.
 */
bool QWindowsPipeReader::canReadLine() const
{
    QMutexLocker locker(&mutex);
    return readBuffer.indexOf('\n', actualReadBufferSize) >= 0;
}

/*!
    Starts an asynchronous read sequence on the pipe.
 */
void QWindowsPipeReader::startAsyncRead()
{
    QMutexLocker locker(&mutex);

    if (readSequenceStarted || lastError != ERROR_SUCCESS)
        return;

    state = Running;
    startAsyncReadLocked();

    // Do not post the event, if the read operation will be completed asynchronously.
    if (!readyReadPending && lastError == ERROR_SUCCESS)
        return;

    if (!winEventActPosted) {
        winEventActPosted = true;
        locker.unlock();
        QCoreApplication::postEvent(this, new QEvent(QEvent::WinEventAct));
    } else {
        locker.unlock();
    }

    SetEvent(syncHandle);
}

/*!
    Starts a new read sequence. Thread-safety should be ensured
    by the caller.
 */
void QWindowsPipeReader::startAsyncReadLocked()
{
    // Determine the number of bytes to read.
    qint64 bytesToRead = qMax(checkPipeState(), state == Running ? minReadBufferSize : 0);

    // This can happen only while draining; just do nothing in this case.
    if (bytesToRead == 0)
        return;

    while (lastError == ERROR_SUCCESS) {
        if (readBufferMaxSize && bytesToRead > (readBufferMaxSize - readBuffer.size())) {
            bytesToRead = readBufferMaxSize - readBuffer.size();
            if (bytesToRead <= 0) {
                // Buffer is full. User must read data from the buffer
                // before we can read more from the pipe.
                return;
            }
        }

        char *ptr = readBuffer.reserve(bytesToRead);

        // ReadFile() returns true, if the read operation completes synchronously.
        // We don't need to call GetOverlappedResult() additionally, because
        // 'numberOfBytesRead' is valid in this case.
        DWORD numberOfBytesRead;
        DWORD errorCode = ERROR_SUCCESS;
        if (!ReadFile(handle, ptr, bytesToRead, &numberOfBytesRead, &overlapped)) {
            errorCode = GetLastError();
            if (errorCode == ERROR_IO_PENDING) {
                Q_ASSERT(state == Running);
                // Operation has been queued and will complete in the future.
                readSequenceStarted = true;
                SetThreadpoolWait(waitObject, eventHandle, NULL);
                return;
            }
        }

        if (!readCompleted(errorCode, numberOfBytesRead))
            return;

        // In the 'Draining' state, we have to get all the data with one call
        // to ReadFile(). Note that message mode pipes are not supported here.
        if (state == Draining) {
            Q_ASSERT(bytesToRead == qint64(numberOfBytesRead));
            return;
        }

        // We need to loop until all pending data has been read and an
        // operation is queued for asynchronous completion.
        // If the pipe is configured to work in message mode, we read
        // the data in chunks.
        bytesToRead = qMax(checkPipeState(), minReadBufferSize);
    }
}

/*!
    Thread pool callback procedure.
 */
void QWindowsPipeReader::waitCallback(PTP_CALLBACK_INSTANCE instance, PVOID context,
                                      PTP_WAIT wait, TP_WAIT_RESULT waitResult)
{
    Q_UNUSED(instance);
    Q_UNUSED(wait);
    Q_UNUSED(waitResult);
    QWindowsPipeReader *pipeReader = reinterpret_cast<QWindowsPipeReader *>(context);

    // Get the result of the asynchronous operation.
    DWORD numberOfBytesTransfered = 0;
    DWORD errorCode = ERROR_SUCCESS;
    if (!GetOverlappedResult(pipeReader->handle, &pipeReader->overlapped,
                             &numberOfBytesTransfered, FALSE))
        errorCode = GetLastError();

    pipeReader->mutex.lock();

    pipeReader->readSequenceStarted = false;

    // Do not overwrite error code, if error has been detected by
    // checkPipeState() in waitForPipeClosed(). Also, if the reader was
    // stopped, the only reason why this function can be called is the
    // completion of a cancellation. No signals should be emitted, and
    // no new read sequence should be started in this case.
    if (pipeReader->lastError == ERROR_SUCCESS && pipeReader->state != Stopped) {
        // Ignore ERROR_OPERATION_ABORTED. We have canceled the I/O operation
        // specifically for flushing the pipe.
        if (pipeReader->state == Draining && errorCode == ERROR_OPERATION_ABORTED)
            errorCode = ERROR_SUCCESS;

        if (pipeReader->readCompleted(errorCode, numberOfBytesTransfered))
            pipeReader->startAsyncReadLocked();

        if (pipeReader->state == Running && !pipeReader->winEventActPosted) {
            pipeReader->winEventActPosted = true;
            pipeReader->mutex.unlock();
            QCoreApplication::postEvent(pipeReader, new QEvent(QEvent::WinEventAct));
        } else {
            pipeReader->mutex.unlock();
        }
    } else {
        pipeReader->mutex.unlock();
    }

    // We set the event only after unlocking to avoid additional context
    // switches due to the released thread immediately running into the lock.
    SetEvent(pipeReader->syncHandle);
}

/*!
    Will be called whenever the read operation completes. Returns \c true if
    no error occurred; otherwise returns \c false.
 */
bool QWindowsPipeReader::readCompleted(DWORD errorCode, DWORD numberOfBytesRead)
{
    // ERROR_MORE_DATA is not an error. We're connected to a message mode
    // pipe and the message didn't fit into the pipe's system
    // buffer. We will read the remaining data in the next call.
    if (errorCode == ERROR_SUCCESS || errorCode == ERROR_MORE_DATA) {
        readyReadPending = true;
        pendingReadBytes += numberOfBytesRead;
        readBuffer.truncate(actualReadBufferSize + pendingReadBytes);
        return true;
    }

    lastError = errorCode;
    return false;
}

/*!
    Receives notification that the read operation has completed.
 */
bool QWindowsPipeReader::event(QEvent *e)
{
    if (e->type() == QEvent::WinEventAct) {
        consumePendingAndEmit(true);
        return true;
    }
    return QObject::event(e);
}

/*!
    Updates the read buffer size and emits pending signals in the main thread.
    Returns \c true, if readyRead() was emitted.
 */
bool QWindowsPipeReader::consumePendingAndEmit(bool allowWinActPosting)
{
    mutex.lock();

    // Enable QEvent::WinEventAct posting.
    if (allowWinActPosting)
        winEventActPosted = false;

    const bool emitReadyRead = consumePending();
    const DWORD dwError = lastError;

    mutex.unlock();

    // Disable any further processing, if the pipe was stopped.
    // We are not allowed to emit signals in either 'Stopped'
    // or 'Draining' state.
    if (state != Running)
        return false;

    if (emitReadyRead && !inReadyRead) {
        QScopedValueRollback<bool> guard(inReadyRead, true);
        emit readyRead();
    }

    // Trigger 'pipeBroken' only once.
    if (dwError != ERROR_SUCCESS && !pipeBroken) {
        pipeBroken = true;
        if (dwError != ERROR_BROKEN_PIPE && dwError != ERROR_PIPE_NOT_CONNECTED)
            emit winError(dwError, QLatin1String("QWindowsPipeReader::consumePendingAndEmit"));
        emit pipeClosed();
    }

    return emitReadyRead;
}

/*!
    Updates the read buffer size. Returns \c true, if readyRead()
    should be emitted. Thread-safety should be ensured by the caller.
 */
bool QWindowsPipeReader::consumePending()
{
    if (readyReadPending) {
        readyReadPending = false;
        actualReadBufferSize += pendingReadBytes;
        pendingReadBytes = 0;
        return true;
    }

    return false;
}

/*!
    Returns the number of available bytes in the pipe.
 */
DWORD QWindowsPipeReader::checkPipeState()
{
    DWORD bytes;
    if (PeekNamedPipe(handle, nullptr, 0, nullptr, &bytes, nullptr))
        return bytes;

    lastError = GetLastError();
    return 0;
}

bool QWindowsPipeReader::waitForNotification(const QDeadlineTimer &deadline)
{
    do {
        DWORD waitRet = WaitForSingleObjectEx(syncHandle, deadline.remainingTime(), TRUE);
        if (waitRet == WAIT_OBJECT_0)
            return true;

        if (waitRet != WAIT_IO_COMPLETION)
            return false;

        // Some I/O completion routine was called. Wait some more.
    } while (!deadline.hasExpired());

    return false;
}

/*!
    Waits for the completion of the asynchronous read operation.
    Returns \c true, if we've emitted the readyRead signal (non-recursive case)
    or readyRead will be emitted by the event loop (recursive case).
 */
bool QWindowsPipeReader::waitForReadyRead(int msecs)
{
    QDeadlineTimer timer(msecs);

    // Make sure that 'syncHandle' was triggered by the thread pool callback.
    while (isReadOperationActive() && waitForNotification(timer)) {
        if (consumePendingAndEmit(false))
            return true;
    }

    return false;
}

/*!
    Waits until the pipe is closed.
 */
bool QWindowsPipeReader::waitForPipeClosed(int msecs)
{
    const int sleepTime = 10;
    QDeadlineTimer timer(msecs);

    while (waitForReadyRead(timer.remainingTime())) {}
    if (pipeBroken)
        return true;

    if (timer.hasExpired())
        return false;

    // When the read buffer is full, the read sequence is not running,
    // so we need to peek the pipe to detect disconnection.
    forever {
        checkPipeState();
        consumePendingAndEmit(false);
        if (pipeBroken)
            return true;

        if (timer.hasExpired())
            return false;

        Sleep(sleepTime);
    }
}

QT_END_NAMESPACE