QT_BEGIN_NAMESPACE void QLocalSocketPrivate::init() { Q_Q(QLocalSocket); pipeReader = new QWindowsPipeReader(q); q->connect(pipeReader, SIGNAL(readyRead()), SIGNAL(readyRead())); q->connect(pipeReader, SIGNAL(pipeClosed()), SLOT(_q_pipeClosed()), Qt::QueuedConnection); q->connect(pipeReader, SIGNAL(winError(ulong,QString)), SLOT(_q_winError(ulong,QString))); }
void QLocalSocketPrivate::_q_notified() { Q_Q(QLocalSocket); if (!completeAsyncRead()) { pipeClosed = true; emit q->readChannelFinished(); if (actualReadBufferSize == 0) QTimer::singleShot(0, q, SLOT(_q_pipeClosed())); return; } startAsyncRead(); pendingReadyRead = false; emit q->readyRead(); }
/*! The number of bytes available from the pipe */ DWORD QLocalSocketPrivate::bytesAvailable() { Q_Q(QLocalSocket); DWORD bytes; if (PeekNamedPipe(handle, NULL, 0, NULL, &bytes, NULL)) { return bytes; } else { if (!pipeClosed) { pipeClosed = true; emit q->readChannelFinished(); QTimer::singleShot(0, q, SLOT(_q_pipeClosed())); } } return 0; }
/*! \internal Returns the number of available bytes in the pipe. Sets QLocalSocketPrivate::pipeClosed to true if the connection is broken. */ DWORD QLocalSocketPrivate::checkPipeState() { Q_Q(QLocalSocket); DWORD bytes; if (PeekNamedPipe(handle, NULL, 0, NULL, &bytes, NULL)) { return bytes; } else { if (!pipeClosed) { pipeClosed = true; emit q->readChannelFinished(); if (actualReadBufferSize == 0) QTimer::singleShot(0, q, SLOT(_q_pipeClosed())); } } return 0; }
// This is reading from the buffer qint64 QLocalSocket::readData(char *data, qint64 maxSize) { Q_D(QLocalSocket); if (d->pipeClosed && d->actualReadBufferSize == 0) return -1; // signal EOF qint64 readSoFar; // If startAsyncRead() read data, copy it to its destination. if (maxSize == 1 && d->actualReadBufferSize > 0) { *data = d->readBuffer.getChar(); 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; } } if (d->pipeClosed) { if (d->actualReadBufferSize == 0) QTimer::singleShot(0, this, SLOT(_q_pipeClosed())); } else { if (!d->readSequenceStarted) d->startAsyncRead(); d->checkReadyRead(); } return readSoFar; }