summaryrefslogtreecommitdiffstats
path: root/src/network/socket/qlocalsocket_win.cpp
diff options
context:
space:
mode:
authorPeter Hartmann <peter.hartmann@trolltech.com>2009-07-15 10:30:21 (GMT)
committerPeter Hartmann <peter.hartmann@trolltech.com>2009-07-15 10:30:21 (GMT)
commit97f82b2344334fa158f20e4ed059984fd3c43162 (patch)
tree05a38900c0409a904f45ad35b7447baef13623ed /src/network/socket/qlocalsocket_win.cpp
parent2567ec486d5d95dc4ca06874cf75bf03bd7502b9 (diff)
parent28d0930593c6c04a7ef538353f8bee55df00a0e8 (diff)
downloadQt-97f82b2344334fa158f20e4ed059984fd3c43162.zip
Qt-97f82b2344334fa158f20e4ed059984fd3c43162.tar.gz
Qt-97f82b2344334fa158f20e4ed059984fd3c43162.tar.bz2
Merge branch 'master' of git@scm.dev.nokia.troll.no:qt/qt
Diffstat (limited to 'src/network/socket/qlocalsocket_win.cpp')
-rw-r--r--src/network/socket/qlocalsocket_win.cpp249
1 files changed, 131 insertions, 118 deletions
diff --git a/src/network/socket/qlocalsocket_win.cpp b/src/network/socket/qlocalsocket_win.cpp
index b1b69fc..794b2b7 100644
--- a/src/network/socket/qlocalsocket_win.cpp
+++ b/src/network/socket/qlocalsocket_win.cpp
@@ -48,13 +48,13 @@
QT_BEGIN_NAMESPACE
-#define NOTIFYTIMEOUT 100
-
void QLocalSocketPrivate::init()
{
Q_Q(QLocalSocket);
- QObject::connect(&dataNotifier, SIGNAL(timeout()), q, SLOT(_q_notified()));
+ memset(&overlapped, 0, sizeof(overlapped));
overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
+ dataReadNotifier = new QWinEventNotifier(overlapped.hEvent, q);
+ q->connect(dataReadNotifier, SIGNAL(activated(HANDLE)), q, SLOT(_q_notified()));
}
void QLocalSocketPrivate::setErrorString(const QString &function)
@@ -101,8 +101,10 @@ QLocalSocketPrivate::QLocalSocketPrivate() : QIODevicePrivate(),
handle(INVALID_HANDLE_VALUE),
pipeWriter(0),
readBufferMaxSize(0),
+ actualReadBufferSize(0),
error(QLocalSocket::UnknownSocketError),
- readyReadEmitted(false),
+ readSequenceStarted(false),
+ pendingReadyRead(false),
pipeClosed(false),
state(QLocalSocket::UnconnectedState)
{
@@ -166,7 +168,7 @@ void QLocalSocket::connectToServer(const QString &name, OpenMode openMode)
// we have a valid handle
d->serverName = name;
- if (setSocketDescriptor((quintptr)localSocket), openMode) {
+ if (setSocketDescriptor((quintptr)localSocket, ConnectedState, openMode)) {
d->handle = localSocket;
emit connected();
}
@@ -176,82 +178,103 @@ void QLocalSocket::connectToServer(const QString &name, OpenMode openMode)
qint64 QLocalSocket::readData(char *data, qint64 maxSize)
{
Q_D(QLocalSocket);
- if (d->readBuffer.isEmpty()) {
- if (!d->readFromSocket()) {
- if (d->pipeClosed)
- return -1;
- return 0;
- }
- }
-
- if (!d->dataNotifier.isActive() && d->threadData->eventDispatcher)
- d->dataNotifier.start(NOTIFYTIMEOUT);
-
- if (d->readBuffer.isEmpty())
- return qint64(0);
- // If readFromSocket() read data, copy it to its destination.
- if (maxSize == 1) {
+ qint64 readSoFar;
+ // If startAsyncRead() read data, copy it to its destination.
+ if (maxSize == 1 && d->actualReadBufferSize > 0) {
*data = d->readBuffer.getChar();
- return 1;
+ d->actualReadBufferSize--;
+ readSoFar = 1;
+ } else {
+ qint64 bytesToRead = qMin(qint64(d->actualReadBufferSize), maxSize);
+ readSoFar = 0;
+ while (readSoFar < bytesToRead) {
+ const char *ptr = d->readBuffer.readPointer();
+ int bytesToReadFromThisBlock = qMin(bytesToRead - readSoFar,
+ qint64(d->readBuffer.nextDataBlockSize()));
+ memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock);
+ readSoFar += bytesToReadFromThisBlock;
+ d->readBuffer.free(bytesToReadFromThisBlock);
+ d->actualReadBufferSize -= bytesToReadFromThisBlock;
+ }
}
- qint64 bytesToRead = qMin(qint64(d->readBuffer.size()), maxSize);
- qint64 readSoFar = 0;
- while (readSoFar < bytesToRead) {
- const char *ptr = d->readBuffer.readPointer();
- int bytesToReadFromThisBlock = qMin(int(bytesToRead - readSoFar),
- d->readBuffer.nextDataBlockSize());
- memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock);
- readSoFar += bytesToReadFromThisBlock;
- d->readBuffer.free(bytesToReadFromThisBlock);
- }
+ if (!d->readSequenceStarted)
+ d->startAsyncRead();
+ d->checkReadyRead();
+
return readSoFar;
}
/*!
\internal
- read from the socket
+ Schedules or cancels a readyRead() emission depending on actual data availability
*/
-qint64 QLocalSocketPrivate::readData(char *data, qint64 maxSize)
+void QLocalSocketPrivate::checkReadyRead()
{
- DWORD bytesRead = 0;
- overlapped.Offset = 0;
- overlapped.OffsetHigh = 0;
- bool success = ReadFile(handle, data, maxSize, &bytesRead, &overlapped);
- if (!success && GetLastError() == ERROR_IO_PENDING)
- if (GetOverlappedResult(handle, &overlapped, &bytesRead, TRUE))
- success = true;
- if (!success) {
- setErrorString(QLatin1String("QLocalSocket::readData"));
- return 0;
+ if (actualReadBufferSize > 0) {
+ if (!pendingReadyRead) {
+ Q_Q(QLocalSocket);
+ QTimer::singleShot(0, q, SLOT(_q_emitReadyRead()));
+ pendingReadyRead = true;
+ }
+ } else {
+ pendingReadyRead = false;
}
- return bytesRead;
}
/*!
\internal
Reads data from the socket into the readbuffer
*/
-bool QLocalSocketPrivate::readFromSocket()
+void QLocalSocketPrivate::startAsyncRead()
{
- qint64 bytesToRead = bytesAvailable();
- if (bytesToRead == 0)
- return false;
+ do {
+ DWORD bytesToRead = bytesAvailable();
+ if (bytesToRead == 0) {
+ // There are no bytes in the pipe but we need to
+ // start the overlapped read with some buffer size.
+ bytesToRead = initialReadBufferSize;
+ }
- if (readBufferMaxSize && bytesToRead
- > (readBufferMaxSize - readBuffer.size()))
- bytesToRead = readBufferMaxSize - readBuffer.size();
+ 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);
- qint64 readBytes = readData(ptr, bytesToRead);
- if (readBytes == 0) {
- readBuffer.chop(bytesToRead);
- return false;
+ char *ptr = readBuffer.reserve(bytesToRead);
+
+ readSequenceStarted = true;
+ if (ReadFile(handle, ptr, bytesToRead, NULL, &overlapped)) {
+ completeAsyncRead();
+ } else if (GetLastError() != ERROR_IO_PENDING) {
+ setErrorString(QLatin1String("QLocalSocketPrivate::startAsyncRead"));
+ return;
+ }
+ } while (!readSequenceStarted);
+}
+
+/*!
+ \internal
+ Sets the correct size of the read buffer after a read operation.
+ */
+void QLocalSocketPrivate::completeAsyncRead()
+{
+ ResetEvent(overlapped.hEvent);
+ readSequenceStarted = false;
+
+ DWORD bytesRead;
+ if (!GetOverlappedResult(handle, &overlapped, &bytesRead, TRUE)) {
+ setErrorString(QLatin1String("QLocalSocketPrivate::completeAsyncRead"));
+ return;
}
- readyReadEmitted = false;
- readBuffer.chop(int(bytesToRead - (readBytes < 0 ? qint64(0) : readBytes)));
- return true;
+
+ actualReadBufferSize += bytesRead;
+ readBuffer.truncate(actualReadBufferSize);
}
qint64 QLocalSocket::writeData(const char *data, qint64 maxSize)
@@ -273,11 +296,9 @@ void QLocalSocket::abort()
/*!
The number of bytes available from the pipe
*/
-qint64 QLocalSocketPrivate::bytesAvailable()
+DWORD QLocalSocketPrivate::bytesAvailable()
{
Q_Q(QLocalSocket);
- if (q->state() != QLocalSocket::ConnectedState)
- return 0;
DWORD bytes;
if (PeekNamedPipe(handle, NULL, 0, NULL, &bytes, NULL)) {
return bytes;
@@ -300,7 +321,7 @@ qint64 QLocalSocket::bytesAvailable() const
{
Q_D(const QLocalSocket);
qint64 available = QIODevice::bytesAvailable();
- available += (qint64) d->readBuffer.size();
+ available += (qint64) d->actualReadBufferSize;
return available;
}
@@ -327,7 +348,6 @@ void QLocalSocket::close()
QIODevice::close();
d->state = ClosingState;
emit stateChanged(d->state);
- d->readyReadEmitted = false;
emit readChannelFinished();
d->serverName = QString();
d->fullServerName = QString();
@@ -336,10 +356,13 @@ void QLocalSocket::close()
disconnectFromServer();
return;
}
+ d->readSequenceStarted = false;
+ d->pendingReadyRead = false;
d->pipeClosed = false;
DisconnectNamedPipe(d->handle);
CloseHandle(d->handle);
d->handle = INVALID_HANDLE_VALUE;
+ ResetEvent(d->overlapped.hEvent);
d->state = UnconnectedState;
emit stateChanged(d->state);
emit disconnected();
@@ -347,7 +370,6 @@ void QLocalSocket::close()
delete d->pipeWriter;
d->pipeWriter = 0;
}
- d->dataNotifier.stop();
}
bool QLocalSocket::flush()
@@ -381,12 +403,15 @@ bool QLocalSocket::setSocketDescriptor(quintptr socketDescriptor,
{
Q_D(QLocalSocket);
d->readBuffer.clear();
+ d->actualReadBufferSize = 0;
QIODevice::open(openMode);
d->handle = (int*)socketDescriptor;
d->state = socketState;
emit stateChanged(d->state);
- if (d->threadData->eventDispatcher)
- d->dataNotifier.start(NOTIFYTIMEOUT);
+ if (d->state == ConnectedState && openMode.testFlag(QIODevice::ReadOnly)) {
+ d->startAsyncRead();
+ d->checkReadyRead();
+ }
return true;
}
@@ -400,20 +425,18 @@ void QLocalSocketPrivate::_q_canWrite()
void QLocalSocketPrivate::_q_notified()
{
Q_Q(QLocalSocket);
- if (0 != bytesAvailable()) {
- if (readBufferMaxSize == 0 || readBuffer.size() < readBufferMaxSize) {
- if (!readFromSocket()) {
- return;
- }
- // wait until buffer is cleared before starting again
- if (readBufferMaxSize && readBuffer.size() == readBufferMaxSize) {
- dataNotifier.stop();
- }
- }
- if (!readyReadEmitted) {
- readyReadEmitted = true;
- q->emit readyRead();
- }
+ completeAsyncRead();
+ startAsyncRead();
+ pendingReadyRead = false;
+ emit q->readyRead();
+}
+
+void QLocalSocketPrivate::_q_emitReadyRead()
+{
+ if (pendingReadyRead) {
+ Q_Q(QLocalSocket);
+ pendingReadyRead = false;
+ emit q->readyRead();
}
}
@@ -446,11 +469,15 @@ bool QLocalSocket::waitForDisconnected(int msecs)
Q_D(QLocalSocket);
if (state() == UnconnectedState)
return false;
+ if (!openMode().testFlag(QIODevice::ReadOnly)) {
+ qWarning("QLocalSocket::waitForDisconnected isn't supported for write only pipes.");
+ return false;
+ }
QIncrementalSleepTimer timer(msecs);
forever {
- d->_q_notified();
- if (d->pipeClosed)
- close();
+ d->bytesAvailable(); // to check if PeekNamedPipe fails
+ if (d->pipeClosed)
+ close();
if (state() == UnconnectedState)
return true;
Sleep(timer.nextSleepTime());
@@ -470,22 +497,24 @@ bool QLocalSocket::isValid() const
bool QLocalSocket::waitForReadyRead(int msecs)
{
Q_D(QLocalSocket);
- QIncrementalSleepTimer timer(msecs);
- forever {
- d->_q_notified();
- if (bytesAvailable() > 0) {
- if (!d->readyReadEmitted) {
- d->readyReadEmitted = true;
- emit readyRead();
- }
- return true;
- }
- Sleep(timer.nextSleepTime());
- if (timer.hasTimedOut())
- break;
+ if (bytesAvailable() > 0)
+ return true;
+
+ if (d->state != QLocalSocket::ConnectedState)
+ return false;
+
+ Q_ASSERT(d->readSequenceStarted);
+ DWORD result = WaitForSingleObject(d->overlapped.hEvent, msecs == -1 ? INFINITE : msecs);
+ switch (result) {
+ case WAIT_OBJECT_0:
+ d->_q_notified();
+ return true;
+ case WAIT_TIMEOUT:
+ return false;
}
+ qWarning("QLocalSocket::waitForReadyRead WaitForSingleObject failed with error code %d.", GetLastError());
return false;
}
@@ -495,27 +524,11 @@ bool QLocalSocket::waitForBytesWritten(int msecs)
if (!d->pipeWriter)
return false;
- QIncrementalSleepTimer timer(msecs);
- forever {
- if (d->pipeWriter->hadWritten())
- return true;
-
- if (d->pipeWriter->bytesToWrite() == 0)
- return false;
-
- // Wait for the pipe writer to acknowledge that it has
- // written. This will succeed if either the pipe writer has
- // already written the data, or if it manages to write data
- // within the given timeout.
- if (d->pipeWriter->waitForWrite(0))
- return true;
-
- Sleep(timer.nextSleepTime());
- if (timer.hasTimedOut())
- break;
- }
-
- return false;
+ // Wait for the pipe writer to acknowledge that it has
+ // written. This will succeed if either the pipe writer has
+ // already written the data, or if it manages to write data
+ // within the given timeout.
+ return d->pipeWriter->waitForWrite(msecs);
}
QT_END_NAMESPACE