1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qwindowspipewriter_p.h"
48 QWindowsPipeWriter::QWindowsPipeWriter(HANDLE pipe
, QObject
* parent
)
50 writePipe(INVALID_HANDLE_VALUE
),
54 #if !defined(Q_OS_WINCE) || (_WIN32_WCE >= 0x600)
55 DuplicateHandle(GetCurrentProcess(), pipe
, GetCurrentProcess(),
56 &writePipe
, 0, FALSE
, DUPLICATE_SAME_ACCESS
);
59 writePipe
= GetCurrentProcess();
63 QWindowsPipeWriter::~QWindowsPipeWriter()
67 waitCondition
.wakeOne();
71 #if !defined(Q_OS_WINCE) || (_WIN32_WCE >= 0x600)
72 CloseHandle(writePipe
);
76 bool QWindowsPipeWriter::waitForWrite(int msecs
)
78 QMutexLocker
locker(&lock
);
79 bool hadWritten
= hasWritten
;
83 if (!waitCondition
.wait(&lock
, msecs
))
85 hadWritten
= hasWritten
;
90 qint64
QWindowsPipeWriter::write(const char *ptr
, qint64 maxlen
)
95 QMutexLocker
locker(&lock
);
96 data
.append(QByteArray(ptr
, maxlen
));
97 waitCondition
.wakeOne();
101 void QWindowsPipeWriter::run()
103 OVERLAPPED overl
= {0, 0, 0, 0, NULL
};
104 overl
.hEvent
= CreateEvent(NULL
, TRUE
, FALSE
, NULL
);
107 while(data
.isEmpty() && (!quitNow
)) {
108 waitCondition
.wakeOne();
109 waitCondition
.wait(&lock
);
118 QByteArray copy
= data
;
122 const char *ptrData
= copy
.data();
123 qint64 maxlen
= copy
.size();
124 qint64 totalWritten
= 0;
126 overl
.OffsetHigh
= 0;
127 while ((!quitNow
) && totalWritten
< maxlen
) {
129 // Write 2k at a time to prevent flooding the pipe. If you
130 // write too much (4k-8k), the pipe can close
132 if (!WriteFile(writePipe
, ptrData
+ totalWritten
,
133 qMin
<int>(2048, 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