1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtCore module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include "qwindowspipewriter_p.h"
49
QWindowsPipeWriter::QWindowsPipeWriter(HANDLE pipe, QObject * parent)
51
writePipe(INVALID_HANDLE_VALUE),
55
#if !defined(Q_OS_WINCE) || (_WIN32_WCE >= 0x600)
56
DuplicateHandle(GetCurrentProcess(), pipe, GetCurrentProcess(),
57
&writePipe, 0, FALSE, DUPLICATE_SAME_ACCESS);
60
writePipe = GetCurrentProcess();
64
QWindowsPipeWriter::~QWindowsPipeWriter()
68
waitCondition.wakeOne();
72
#if !defined(Q_OS_WINCE) || (_WIN32_WCE >= 0x600)
73
CloseHandle(writePipe);
77
bool QWindowsPipeWriter::waitForWrite(int msecs)
79
QMutexLocker locker(&lock);
80
bool hadWritten = hasWritten;
84
if (!waitCondition.wait(&lock, msecs))
86
hadWritten = hasWritten;
91
qint64 QWindowsPipeWriter::write(const char *ptr, qint64 maxlen)
96
QMutexLocker locker(&lock);
97
data.append(QByteArray(ptr, maxlen));
98
waitCondition.wakeOne();
102
void QWindowsPipeWriter::run()
105
memset(&overl, 0, sizeof overl);
106
overl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
109
while(data.isEmpty() && (!quitNow)) {
110
waitCondition.wakeOne();
111
waitCondition.wait(&lock);
120
QByteArray copy = data;
124
const char *ptrData = copy.data();
125
qint64 maxlen = copy.size();
126
qint64 totalWritten = 0;
128
overl.OffsetHigh = 0;
129
while ((!quitNow) && totalWritten < maxlen) {
131
if (!WriteFile(writePipe, ptrData + totalWritten,
132
maxlen - totalWritten, &written, &overl)) {
134
if (GetLastError() == 0xE8/*NT_STATUS_INVALID_USER_BUFFER*/) {
135
// give the os a rest
140
if (GetLastError() == ERROR_IO_PENDING) {
141
if (!GetOverlappedResult(writePipe, &overl, &written, TRUE)) {
142
CloseHandle(overl.hEvent);
146
CloseHandle(overl.hEvent);
153
totalWritten += written;
154
#if defined QPIPEWRITER_DEBUG
155
qDebug("QWindowsPipeWriter::run() wrote %d %d/%d bytes",
156
written, int(totalWritten), int(maxlen));
159
data.remove(0, written);
163
emit bytesWritten(totalWritten);
166
CloseHandle(overl.hEvent);
169
#endif //QT_NO_THREAD