author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 29 Apr 2010 15:15:16 +0300 | |
branch | RCL_3 |
changeset 17 | 4b6ee5efea19 |
parent 14 | c0432d11811c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtNetwork module of the Qt Toolkit. |
|
8 |
** |
|
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 |
|
14 |
** this package. |
|
15 |
** |
|
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. |
|
23 |
** |
|
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. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qlocalsocket_p.h" |
|
43 |
||
44 |
#include <private/qthread_p.h> |
|
45 |
#include <qcoreapplication.h> |
|
46 |
#include <qdebug.h> |
|
47 |
||
48 |
QT_BEGIN_NAMESPACE |
|
49 |
||
50 |
void QLocalSocketPrivate::init() |
|
51 |
{ |
|
52 |
Q_Q(QLocalSocket); |
|
53 |
memset(&overlapped, 0, sizeof(overlapped)); |
|
54 |
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
|
55 |
dataReadNotifier = new QWinEventNotifier(overlapped.hEvent, q); |
|
56 |
q->connect(dataReadNotifier, SIGNAL(activated(HANDLE)), q, SLOT(_q_notified())); |
|
57 |
} |
|
58 |
||
59 |
void QLocalSocketPrivate::setErrorString(const QString &function) |
|
60 |
{ |
|
61 |
Q_Q(QLocalSocket); |
|
62 |
BOOL windowsError = GetLastError(); |
|
63 |
QLocalSocket::LocalSocketState currentState = state; |
|
64 |
||
65 |
// If the connectToServer fails due to WaitNamedPipe() time-out, assume ConnectionError |
|
66 |
if (state == QLocalSocket::ConnectingState && windowsError == ERROR_SEM_TIMEOUT) |
|
67 |
windowsError = ERROR_NO_DATA; |
|
68 |
||
69 |
switch (windowsError) { |
|
70 |
case ERROR_PIPE_NOT_CONNECTED: |
|
71 |
case ERROR_BROKEN_PIPE: |
|
72 |
case ERROR_NO_DATA: |
|
73 |
error = QLocalSocket::ConnectionError; |
|
74 |
errorString = QLocalSocket::tr("%1: Connection error").arg(function); |
|
75 |
state = QLocalSocket::UnconnectedState; |
|
76 |
break; |
|
77 |
case ERROR_FILE_NOT_FOUND: |
|
78 |
error = QLocalSocket::ServerNotFoundError; |
|
79 |
errorString = QLocalSocket::tr("%1: Invalid name").arg(function); |
|
80 |
state = QLocalSocket::UnconnectedState; |
|
81 |
break; |
|
82 |
default: |
|
83 |
error = QLocalSocket::UnknownSocketError; |
|
84 |
errorString = QLocalSocket::tr("%1: Unknown error %2").arg(function).arg(windowsError); |
|
85 |
#if defined QLOCALSOCKET_DEBUG |
|
86 |
qWarning() << "QLocalSocket error not handled:" << errorString; |
|
87 |
#endif |
|
88 |
state = QLocalSocket::UnconnectedState; |
|
89 |
} |
|
90 |
||
91 |
if (currentState != state) { |
|
92 |
q->emit stateChanged(state); |
|
93 |
if (state == QLocalSocket::UnconnectedState) |
|
94 |
q->emit disconnected(); |
|
95 |
} |
|
96 |
emit q->error(error); |
|
97 |
} |
|
98 |
||
99 |
QLocalSocketPrivate::QLocalSocketPrivate() : QIODevicePrivate(), |
|
100 |
handle(INVALID_HANDLE_VALUE), |
|
101 |
pipeWriter(0), |
|
102 |
readBufferMaxSize(0), |
|
103 |
actualReadBufferSize(0), |
|
104 |
error(QLocalSocket::UnknownSocketError), |
|
105 |
readSequenceStarted(false), |
|
106 |
pendingReadyRead(false), |
|
107 |
pipeClosed(false), |
|
108 |
state(QLocalSocket::UnconnectedState) |
|
109 |
{ |
|
110 |
} |
|
111 |
||
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
112 |
QLocalSocketPrivate::~QLocalSocketPrivate() |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
113 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
114 |
destroyPipeHandles(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
115 |
CloseHandle(overlapped.hEvent); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
116 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
117 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
118 |
void QLocalSocketPrivate::destroyPipeHandles() |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
119 |
{ |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
120 |
if (handle != INVALID_HANDLE_VALUE) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
121 |
DisconnectNamedPipe(handle); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
122 |
CloseHandle(handle); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
123 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
124 |
} |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
125 |
|
0 | 126 |
void QLocalSocket::connectToServer(const QString &name, OpenMode openMode) |
127 |
{ |
|
128 |
Q_D(QLocalSocket); |
|
129 |
if (state() == ConnectedState || state() == ConnectingState) |
|
130 |
return; |
|
131 |
||
132 |
d->error = QLocalSocket::UnknownSocketError; |
|
133 |
d->errorString = QString(); |
|
134 |
d->state = ConnectingState; |
|
135 |
emit stateChanged(d->state); |
|
136 |
if (name.isEmpty()) { |
|
137 |
d->error = QLocalSocket::ServerNotFoundError; |
|
138 |
setErrorString(QLocalSocket::tr("%1: Invalid name").arg(QLatin1String("QLocalSocket::connectToServer"))); |
|
139 |
d->state = UnconnectedState; |
|
140 |
emit error(d->error); |
|
141 |
emit stateChanged(d->state); |
|
142 |
return; |
|
143 |
} |
|
144 |
||
145 |
QString pipePath = QLatin1String("\\\\.\\pipe\\"); |
|
146 |
if (name.startsWith(pipePath)) |
|
147 |
d->fullServerName = name; |
|
148 |
else |
|
149 |
d->fullServerName = pipePath + name; |
|
150 |
// Try to open a named pipe |
|
151 |
HANDLE localSocket; |
|
152 |
forever { |
|
153 |
DWORD permissions = (openMode & QIODevice::ReadOnly) ? GENERIC_READ : 0; |
|
154 |
permissions |= (openMode & QIODevice::WriteOnly) ? GENERIC_WRITE : 0; |
|
155 |
localSocket = CreateFile((const wchar_t *)d->fullServerName.utf16(), // pipe name |
|
156 |
permissions, |
|
157 |
0, // no sharing |
|
158 |
NULL, // default security attributes |
|
159 |
OPEN_EXISTING, // opens existing pipe |
|
160 |
FILE_FLAG_OVERLAPPED, |
|
161 |
NULL); // no template file |
|
162 |
||
163 |
if (localSocket != INVALID_HANDLE_VALUE) |
|
164 |
break; |
|
165 |
DWORD error = GetLastError(); |
|
166 |
// It is really an error only if it is not ERROR_PIPE_BUSY |
|
167 |
if (ERROR_PIPE_BUSY != error) { |
|
168 |
break; |
|
169 |
} |
|
170 |
||
171 |
// All pipe instances are busy, so wait until connected or up to 5 seconds. |
|
172 |
if (!WaitNamedPipe((const wchar_t *)d->fullServerName.utf16(), 5000)) |
|
173 |
break; |
|
174 |
} |
|
175 |
||
176 |
if (localSocket == INVALID_HANDLE_VALUE) { |
|
177 |
d->setErrorString(QLatin1String("QLocalSocket::connectToServer")); |
|
178 |
d->fullServerName = QString(); |
|
179 |
return; |
|
180 |
} |
|
181 |
||
182 |
// we have a valid handle |
|
183 |
d->serverName = name; |
|
184 |
if (setSocketDescriptor((quintptr)localSocket, ConnectedState, openMode)) { |
|
185 |
d->handle = localSocket; |
|
186 |
emit connected(); |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
// This is reading from the buffer |
|
191 |
qint64 QLocalSocket::readData(char *data, qint64 maxSize) |
|
192 |
{ |
|
193 |
Q_D(QLocalSocket); |
|
194 |
||
195 |
qint64 readSoFar; |
|
196 |
// If startAsyncRead() read data, copy it to its destination. |
|
197 |
if (maxSize == 1 && d->actualReadBufferSize > 0) { |
|
198 |
*data = d->readBuffer.getChar(); |
|
199 |
d->actualReadBufferSize--; |
|
200 |
readSoFar = 1; |
|
201 |
} else { |
|
202 |
qint64 bytesToRead = qMin(qint64(d->actualReadBufferSize), maxSize); |
|
203 |
readSoFar = 0; |
|
204 |
while (readSoFar < bytesToRead) { |
|
205 |
const char *ptr = d->readBuffer.readPointer(); |
|
206 |
int bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar, |
|
207 |
qint64(d->readBuffer.nextDataBlockSize())); |
|
208 |
memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock); |
|
209 |
readSoFar += bytesToReadFromThisBlock; |
|
210 |
d->readBuffer.free(bytesToReadFromThisBlock); |
|
211 |
d->actualReadBufferSize -= bytesToReadFromThisBlock; |
|
212 |
} |
|
213 |
} |
|
214 |
||
215 |
if (d->pipeClosed) { |
|
216 |
if (readSoFar == 0) { |
|
217 |
QTimer::singleShot(0, this, SLOT(_q_pipeClosed())); |
|
218 |
return -1; // signal EOF |
|
219 |
} |
|
220 |
} else { |
|
221 |
if (!d->readSequenceStarted) |
|
222 |
d->startAsyncRead(); |
|
223 |
d->checkReadyRead(); |
|
224 |
} |
|
225 |
||
226 |
return readSoFar; |
|
227 |
} |
|
228 |
||
229 |
/*! |
|
230 |
\internal |
|
231 |
Schedules or cancels a readyRead() emission depending on actual data availability |
|
232 |
*/ |
|
233 |
void QLocalSocketPrivate::checkReadyRead() |
|
234 |
{ |
|
235 |
if (actualReadBufferSize > 0) { |
|
236 |
if (!pendingReadyRead) { |
|
237 |
Q_Q(QLocalSocket); |
|
238 |
QTimer::singleShot(0, q, SLOT(_q_emitReadyRead())); |
|
239 |
pendingReadyRead = true; |
|
240 |
} |
|
241 |
} else { |
|
242 |
pendingReadyRead = false; |
|
243 |
} |
|
244 |
} |
|
245 |
||
246 |
/*! |
|
247 |
\internal |
|
248 |
Reads data from the socket into the readbuffer |
|
249 |
*/ |
|
250 |
void QLocalSocketPrivate::startAsyncRead() |
|
251 |
{ |
|
252 |
do { |
|
253 |
DWORD bytesToRead = bytesAvailable(); |
|
254 |
if (bytesToRead == 0) { |
|
255 |
// There are no bytes in the pipe but we need to |
|
256 |
// start the overlapped read with some buffer size. |
|
257 |
bytesToRead = initialReadBufferSize; |
|
258 |
} |
|
259 |
||
260 |
if (readBufferMaxSize && bytesToRead > (readBufferMaxSize - readBuffer.size())) { |
|
261 |
bytesToRead = readBufferMaxSize - readBuffer.size(); |
|
262 |
if (bytesToRead == 0) { |
|
263 |
// Buffer is full. User must read data from the buffer |
|
264 |
// before we can read more from the pipe. |
|
265 |
return; |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
char *ptr = readBuffer.reserve(bytesToRead); |
|
270 |
||
271 |
readSequenceStarted = true; |
|
272 |
if (ReadFile(handle, ptr, bytesToRead, NULL, &overlapped)) { |
|
273 |
completeAsyncRead(); |
|
274 |
} else { |
|
275 |
switch (GetLastError()) { |
|
276 |
case ERROR_IO_PENDING: |
|
277 |
// This is not an error. We're getting notified, when data arrives. |
|
278 |
return; |
|
279 |
case ERROR_PIPE_NOT_CONNECTED: |
|
280 |
{ |
|
281 |
// It may happen, that the other side closes the connection directly |
|
282 |
// after writing data. Then we must set the appropriate socket state. |
|
283 |
pipeClosed = true; |
|
284 |
Q_Q(QLocalSocket); |
|
285 |
emit q->readChannelFinished(); |
|
286 |
return; |
|
287 |
} |
|
288 |
default: |
|
289 |
setErrorString(QLatin1String("QLocalSocketPrivate::startAsyncRead")); |
|
290 |
return; |
|
291 |
} |
|
292 |
} |
|
293 |
} while (!readSequenceStarted); |
|
294 |
} |
|
295 |
||
296 |
/*! |
|
297 |
\internal |
|
298 |
Sets the correct size of the read buffer after a read operation. |
|
299 |
Returns false, if an error occured or the connection dropped. |
|
300 |
*/ |
|
301 |
bool QLocalSocketPrivate::completeAsyncRead() |
|
302 |
{ |
|
303 |
ResetEvent(overlapped.hEvent); |
|
304 |
readSequenceStarted = false; |
|
305 |
||
306 |
DWORD bytesRead; |
|
307 |
if (!GetOverlappedResult(handle, &overlapped, &bytesRead, TRUE)) { |
|
308 |
if (GetLastError() != ERROR_PIPE_NOT_CONNECTED) |
|
309 |
setErrorString(QLatin1String("QLocalSocketPrivate::completeAsyncRead")); |
|
310 |
return false; |
|
311 |
} |
|
312 |
||
313 |
actualReadBufferSize += bytesRead; |
|
314 |
readBuffer.truncate(actualReadBufferSize); |
|
315 |
return true; |
|
316 |
} |
|
317 |
||
318 |
qint64 QLocalSocket::writeData(const char *data, qint64 maxSize) |
|
319 |
{ |
|
320 |
Q_D(QLocalSocket); |
|
321 |
if (!d->pipeWriter) { |
|
322 |
d->pipeWriter = new QWindowsPipeWriter(d->handle, this); |
|
323 |
connect(d->pipeWriter, SIGNAL(canWrite()), this, SLOT(_q_canWrite())); |
|
324 |
connect(d->pipeWriter, SIGNAL(bytesWritten(qint64)), this, SIGNAL(bytesWritten(qint64))); |
|
325 |
d->pipeWriter->start(); |
|
326 |
} |
|
327 |
return d->pipeWriter->write(data, maxSize); |
|
328 |
} |
|
329 |
||
330 |
void QLocalSocket::abort() |
|
331 |
{ |
|
332 |
close(); |
|
333 |
} |
|
334 |
||
335 |
/*! |
|
336 |
The number of bytes available from the pipe |
|
337 |
*/ |
|
338 |
DWORD QLocalSocketPrivate::bytesAvailable() |
|
339 |
{ |
|
340 |
Q_Q(QLocalSocket); |
|
341 |
DWORD bytes; |
|
342 |
if (PeekNamedPipe(handle, NULL, 0, NULL, &bytes, NULL)) { |
|
343 |
return bytes; |
|
344 |
} else { |
|
345 |
if (!pipeClosed) { |
|
346 |
pipeClosed = true; |
|
347 |
emit q->readChannelFinished(); |
|
348 |
QTimer::singleShot(0, q, SLOT(_q_pipeClosed())); |
|
349 |
} |
|
350 |
} |
|
351 |
return 0; |
|
352 |
} |
|
353 |
||
354 |
void QLocalSocketPrivate::_q_pipeClosed() |
|
355 |
{ |
|
356 |
Q_Q(QLocalSocket); |
|
357 |
q->close(); |
|
358 |
} |
|
359 |
||
360 |
qint64 QLocalSocket::bytesAvailable() const |
|
361 |
{ |
|
362 |
Q_D(const QLocalSocket); |
|
363 |
qint64 available = QIODevice::bytesAvailable(); |
|
364 |
available += (qint64) d->actualReadBufferSize; |
|
365 |
return available; |
|
366 |
} |
|
367 |
||
368 |
qint64 QLocalSocket::bytesToWrite() const |
|
369 |
{ |
|
370 |
Q_D(const QLocalSocket); |
|
371 |
return (d->pipeWriter) ? d->pipeWriter->bytesToWrite() : 0; |
|
372 |
} |
|
373 |
||
374 |
bool QLocalSocket::canReadLine() const |
|
375 |
{ |
|
376 |
Q_D(const QLocalSocket); |
|
377 |
if (state() != ConnectedState) |
|
378 |
return false; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
379 |
return (QIODevice::canReadLine() |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
380 |
|| d->readBuffer.indexOf('\n', d->actualReadBufferSize) != -1); |
0 | 381 |
} |
382 |
||
383 |
void QLocalSocket::close() |
|
384 |
{ |
|
385 |
Q_D(QLocalSocket); |
|
386 |
if (state() == UnconnectedState) |
|
387 |
return; |
|
388 |
||
389 |
QIODevice::close(); |
|
390 |
d->state = ClosingState; |
|
391 |
emit stateChanged(d->state); |
|
392 |
if (!d->pipeClosed) |
|
393 |
emit readChannelFinished(); |
|
394 |
d->serverName = QString(); |
|
395 |
d->fullServerName = QString(); |
|
396 |
||
397 |
if (state() != UnconnectedState && bytesToWrite() > 0) { |
|
398 |
disconnectFromServer(); |
|
399 |
return; |
|
400 |
} |
|
401 |
d->readSequenceStarted = false; |
|
402 |
d->pendingReadyRead = false; |
|
403 |
d->pipeClosed = false; |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
404 |
d->destroyPipeHandles(); |
0 | 405 |
d->handle = INVALID_HANDLE_VALUE; |
406 |
ResetEvent(d->overlapped.hEvent); |
|
407 |
d->state = UnconnectedState; |
|
408 |
emit stateChanged(d->state); |
|
409 |
emit disconnected(); |
|
410 |
if (d->pipeWriter) { |
|
411 |
delete d->pipeWriter; |
|
412 |
d->pipeWriter = 0; |
|
413 |
} |
|
414 |
} |
|
415 |
||
416 |
bool QLocalSocket::flush() |
|
417 |
{ |
|
418 |
Q_D(QLocalSocket); |
|
419 |
if (d->pipeWriter) |
|
420 |
return d->pipeWriter->waitForWrite(0); |
|
421 |
return false; |
|
422 |
} |
|
423 |
||
424 |
void QLocalSocket::disconnectFromServer() |
|
425 |
{ |
|
426 |
Q_D(QLocalSocket); |
|
14
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
427 |
|
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
428 |
// Are we still connected? |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
429 |
if (!isValid()) { |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
430 |
// If we have unwritten data, the pipeWriter is still present. |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
431 |
// It must be destroyed before close() to prevent an infinite loop. |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
432 |
delete d->pipeWriter; |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
433 |
d->pipeWriter = 0; |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
434 |
} |
c0432d11811c
eb175c3290cd7ea85da4a590db9461504a4904bc
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
8
diff
changeset
|
435 |
|
0 | 436 |
flush(); |
437 |
if (d->pipeWriter && d->pipeWriter->bytesToWrite() != 0) { |
|
438 |
d->state = QLocalSocket::ClosingState; |
|
439 |
emit stateChanged(d->state); |
|
440 |
} else { |
|
441 |
close(); |
|
442 |
} |
|
443 |
} |
|
444 |
||
445 |
QLocalSocket::LocalSocketError QLocalSocket::error() const |
|
446 |
{ |
|
447 |
Q_D(const QLocalSocket); |
|
448 |
return d->error; |
|
449 |
} |
|
450 |
||
451 |
bool QLocalSocket::setSocketDescriptor(quintptr socketDescriptor, |
|
452 |
LocalSocketState socketState, OpenMode openMode) |
|
453 |
{ |
|
454 |
Q_D(QLocalSocket); |
|
455 |
d->readBuffer.clear(); |
|
456 |
d->actualReadBufferSize = 0; |
|
457 |
QIODevice::open(openMode); |
|
458 |
d->handle = (int*)socketDescriptor; |
|
459 |
d->state = socketState; |
|
460 |
emit stateChanged(d->state); |
|
461 |
if (d->state == ConnectedState && openMode.testFlag(QIODevice::ReadOnly)) { |
|
462 |
d->startAsyncRead(); |
|
463 |
d->checkReadyRead(); |
|
464 |
} |
|
465 |
return true; |
|
466 |
} |
|
467 |
||
468 |
void QLocalSocketPrivate::_q_canWrite() |
|
469 |
{ |
|
470 |
Q_Q(QLocalSocket); |
|
471 |
if (state == QLocalSocket::ClosingState) |
|
472 |
q->close(); |
|
473 |
} |
|
474 |
||
475 |
void QLocalSocketPrivate::_q_notified() |
|
476 |
{ |
|
477 |
Q_Q(QLocalSocket); |
|
478 |
if (!completeAsyncRead()) { |
|
479 |
pipeClosed = true; |
|
480 |
emit q->readChannelFinished(); |
|
481 |
return; |
|
482 |
} |
|
483 |
startAsyncRead(); |
|
484 |
pendingReadyRead = false; |
|
485 |
emit q->readyRead(); |
|
486 |
} |
|
487 |
||
488 |
void QLocalSocketPrivate::_q_emitReadyRead() |
|
489 |
{ |
|
490 |
if (pendingReadyRead) { |
|
491 |
Q_Q(QLocalSocket); |
|
492 |
pendingReadyRead = false; |
|
493 |
emit q->readyRead(); |
|
494 |
} |
|
495 |
} |
|
496 |
||
497 |
quintptr QLocalSocket::socketDescriptor() const |
|
498 |
{ |
|
499 |
Q_D(const QLocalSocket); |
|
500 |
return (quintptr)d->handle; |
|
501 |
} |
|
502 |
||
503 |
qint64 QLocalSocket::readBufferSize() const |
|
504 |
{ |
|
505 |
Q_D(const QLocalSocket); |
|
506 |
return d->readBufferMaxSize; |
|
507 |
} |
|
508 |
||
509 |
void QLocalSocket::setReadBufferSize(qint64 size) |
|
510 |
{ |
|
511 |
Q_D(QLocalSocket); |
|
512 |
d->readBufferMaxSize = size; |
|
513 |
} |
|
514 |
||
515 |
bool QLocalSocket::waitForConnected(int msecs) |
|
516 |
{ |
|
517 |
Q_UNUSED(msecs); |
|
518 |
return (state() == ConnectedState); |
|
519 |
} |
|
520 |
||
521 |
bool QLocalSocket::waitForDisconnected(int msecs) |
|
522 |
{ |
|
523 |
Q_D(QLocalSocket); |
|
524 |
if (state() == UnconnectedState) |
|
525 |
return false; |
|
526 |
if (!openMode().testFlag(QIODevice::ReadOnly)) { |
|
527 |
qWarning("QLocalSocket::waitForDisconnected isn't supported for write only pipes."); |
|
528 |
return false; |
|
529 |
} |
|
530 |
QIncrementalSleepTimer timer(msecs); |
|
531 |
forever { |
|
532 |
d->bytesAvailable(); // to check if PeekNamedPipe fails |
|
533 |
if (d->pipeClosed) |
|
534 |
close(); |
|
535 |
if (state() == UnconnectedState) |
|
536 |
return true; |
|
537 |
Sleep(timer.nextSleepTime()); |
|
538 |
if (timer.hasTimedOut()) |
|
539 |
break; |
|
540 |
} |
|
541 |
||
542 |
return false; |
|
543 |
} |
|
544 |
||
545 |
bool QLocalSocket::isValid() const |
|
546 |
{ |
|
547 |
Q_D(const QLocalSocket); |
|
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
548 |
if (d->handle == INVALID_HANDLE_VALUE) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
549 |
return false; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
550 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
551 |
return PeekNamedPipe(d->handle, NULL, 0, NULL, NULL, NULL); |
0 | 552 |
} |
553 |
||
554 |
bool QLocalSocket::waitForReadyRead(int msecs) |
|
555 |
{ |
|
556 |
Q_D(QLocalSocket); |
|
557 |
||
558 |
if (bytesAvailable() > 0) |
|
559 |
return true; |
|
560 |
||
561 |
if (d->state != QLocalSocket::ConnectedState) |
|
562 |
return false; |
|
563 |
||
564 |
Q_ASSERT(d->readSequenceStarted); |
|
565 |
DWORD result = WaitForSingleObject(d->overlapped.hEvent, msecs == -1 ? INFINITE : msecs); |
|
566 |
switch (result) { |
|
567 |
case WAIT_OBJECT_0: |
|
568 |
d->_q_notified(); |
|
569 |
return true; |
|
570 |
case WAIT_TIMEOUT: |
|
571 |
return false; |
|
572 |
} |
|
573 |
||
574 |
qWarning("QLocalSocket::waitForReadyRead WaitForSingleObject failed with error code %d.", int(GetLastError())); |
|
575 |
return false; |
|
576 |
} |
|
577 |
||
578 |
bool QLocalSocket::waitForBytesWritten(int msecs) |
|
579 |
{ |
|
580 |
Q_D(const QLocalSocket); |
|
581 |
if (!d->pipeWriter) |
|
582 |
return false; |
|
583 |
||
584 |
// Wait for the pipe writer to acknowledge that it has |
|
585 |
// written. This will succeed if either the pipe writer has |
|
586 |
// already written the data, or if it manages to write data |
|
587 |
// within the given timeout. |
|
588 |
return d->pipeWriter->waitForWrite(msecs); |
|
589 |
} |
|
590 |
||
591 |
QT_END_NAMESPACE |