44 #include <private/qthread_p.h> 62 BOOL windowsError = GetLastError();
67 windowsError = ERROR_NO_DATA;
69 switch (windowsError) {
70 case ERROR_PIPE_NOT_CONNECTED:
71 case ERROR_BROKEN_PIPE:
77 case ERROR_FILE_NOT_FOUND:
82 case ERROR_ACCESS_DENIED:
90 #if defined QLOCALSOCKET_DEBUG 96 if (currentState !=
state) {
97 q->emit stateChanged(
state);
99 q->emit disconnected();
105 handle(INVALID_HANDLE_VALUE),
125 if (
handle != INVALID_HANDLE_VALUE) {
126 DisconnectNamedPipe(
handle);
134 if (
state() == ConnectedState ||
state() == ConnectingState)
139 d->state = ConnectingState;
140 emit stateChanged(
d->state);
144 d->state = UnconnectedState;
146 emit stateChanged(
d->state);
152 d->fullServerName = name;
154 d->fullServerName = pipePath + name;
160 localSocket = CreateFile((
const wchar_t *)
d->fullServerName.utf16(),
165 FILE_FLAG_OVERLAPPED,
168 if (localSocket != INVALID_HANDLE_VALUE)
170 DWORD
error = GetLastError();
172 if (ERROR_PIPE_BUSY != error) {
177 if (!WaitNamedPipe((
const wchar_t *)
d->fullServerName.utf16(), 5000))
181 if (localSocket == INVALID_HANDLE_VALUE) {
182 d->setErrorString(
QLatin1String(
"QLocalSocket::connectToServer"));
188 d->serverName =
name;
189 if (setSocketDescriptor((
quintptr)localSocket, ConnectedState, openMode)) {
190 d->handle = localSocket;
200 if (
d->pipeClosed &&
d->actualReadBufferSize == 0)
205 if (maxSize == 1 &&
d->actualReadBufferSize > 0) {
206 *data =
d->readBuffer.getChar();
207 d->actualReadBufferSize--;
212 while (readSoFar < bytesToRead) {
213 const char *
ptr =
d->readBuffer.readPointer();
214 int bytesToReadFromThisBlock =
qMin(bytesToRead - readSoFar,
215 qint64(
d->readBuffer.nextDataBlockSize()));
216 memcpy(data + readSoFar, ptr, bytesToReadFromThisBlock);
217 readSoFar += bytesToReadFromThisBlock;
218 d->readBuffer.free(bytesToReadFromThisBlock);
219 d->actualReadBufferSize -= bytesToReadFromThisBlock;
224 if (
d->actualReadBufferSize == 0)
227 if (!
d->readSequenceStarted)
263 if (bytesToRead == 0) {
271 if (bytesToRead == 0) {
284 switch (GetLastError()) {
285 case ERROR_IO_PENDING:
288 case ERROR_MORE_DATA:
294 case ERROR_PIPE_NOT_CONNECTED:
300 emit q->readChannelFinished();
326 switch (GetLastError()) {
327 case ERROR_MORE_DATA:
332 case ERROR_PIPE_NOT_CONNECTED:
348 if (!
d->pipeWriter) {
352 d->pipeWriter->start();
354 return d->pipeWriter->write(data, maxSize);
361 delete d->pipeWriter;
379 if (PeekNamedPipe(
handle, NULL, 0, NULL, &bytes, NULL)) {
384 emit q->readChannelFinished();
402 available += (
qint64)
d->actualReadBufferSize;
409 return (
d->pipeWriter) ?
d->pipeWriter->bytesToWrite() : 0;
415 if (
state() != ConnectedState)
418 ||
d->readBuffer.indexOf(
'\n',
d->actualReadBufferSize) != -1);
424 if (
state() == UnconnectedState)
428 d->state = ClosingState;
429 emit stateChanged(
d->state);
431 emit readChannelFinished();
435 if (
state() != UnconnectedState && bytesToWrite() > 0) {
436 disconnectFromServer();
439 d->readSequenceStarted =
false;
440 d->pendingReadyRead =
false;
441 d->pipeClosed =
false;
442 d->destroyPipeHandles();
443 d->handle = INVALID_HANDLE_VALUE;
444 ResetEvent(
d->overlapped.hEvent);
445 d->state = UnconnectedState;
446 emit stateChanged(
d->state);
449 delete d->pipeWriter;
458 return d->pipeWriter->waitForWrite(0);
470 delete d->pipeWriter;
475 if (
d->pipeWriter &&
d->pipeWriter->bytesToWrite() != 0) {
477 emit stateChanged(
d->state);
490 LocalSocketState socketState, OpenMode
openMode)
493 d->readBuffer.clear();
494 d->actualReadBufferSize = 0;
496 d->handle = (
int*)socketDescriptor;
497 d->state = socketState;
498 emit stateChanged(
d->state);
518 emit q->readChannelFinished();
546 return d->readBufferMaxSize;
552 d->readBufferMaxSize = size;
558 return (
state() == ConnectedState);
564 if (
state() == UnconnectedState)
567 qWarning(
"QLocalSocket::waitForDisconnected isn't supported for write only pipes.");
575 if (
state() == UnconnectedState)
588 return d->handle != INVALID_HANDLE_VALUE;
595 if (bytesAvailable() > 0)
608 DWORD result = WaitForSingleObject(
d->overlapped.hEvent, msecs == -1 ? INFINITE : msecs);
613 if (
d->pipeClosed && !bytesAvailable()) {
622 qWarning(
"QLocalSocket::waitForReadyRead WaitForSingleObject failed with error code %d.",
int(GetLastError()));
636 return d->pipeWriter->waitForWrite(msecs);
quintptr socketDescriptor() const
Returns the native socket descriptor of the QLocalSocket object if this is available; otherwise retur...
bool waitForReadyRead(int msecs=30000)
This function blocks until data is available for reading and the QIODevice::readyRead() signal has be...
QIntegerForSizeof< void * >::Unsigned quintptr
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
#define QT_END_NAMESPACE
This macro expands to.
LocalSocketState
This enum describes the different states in which a socket can be.
bool waitForConnected(int msecs=30000)
Waits until the socket is connected, up to msecs milliseconds.
QWinEventNotifier * dataReadNotifier
QIODevice::OpenMode openMode
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
QLocalSocket::LocalSocketState state
virtual void close()
Reimplemented Function
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool waitForDisconnected(int msecs=30000)
Waits until the socket has disconnected, up to msecs milliseconds.
The QString class provides a Unicode character string.
QWindowsPipeWriter * pipeWriter
virtual qint64 writeData(const char *, qint64)
Reimplemented Function
bool completeAsyncRead()
Sets the correct size of the read buffer after a read operation.
QLocalSocket::LocalSocketError error
The QLocalSocket class provides a local socket.
qint64 readBufferSize() const
Returns the size of the internal read buffer.
char * reserve(int bytes)
#define QT_BEGIN_NAMESPACE
This macro expands to.
static QIntfbScreen * connected
virtual qint64 readData(char *, qint64)
Reimplemented Function
void setReadBufferSize(qint64 size)
Sets the size of QLocalSocket's internal read buffer to be size bytes.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Q_CORE_EXPORT void qWarning(const char *,...)
bool flush()
This function writes as much as possible from the internal write buffer to the socket, without blocking.
static const char * data(const QByteArray &arr)
void connectToServer(const QString &name, OpenMode openMode=ReadWrite)
Attempts to make a connection to name.
#define FALSE
Synonym for false.
const T * ptr(const T &t)
LocalSocketError error() const
Returns the type of error that last occurred.
virtual qint64 bytesToWrite() const
Reimplemented Function
bool isValid() const
Returns true if the socket is valid and ready for use; otherwise returns false.
DWORD checkPipeState()
Returns the number of available bytes in the pipe.
#define TRUE
Synonym for true.
virtual bool canReadLine() const
Reimplemented Function
void abort()
Aborts the current connection and resets the socket.
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
static const qint64 initialReadBufferSize
bool waitForBytesWritten(int msecs=30000)
Reimplemented Function
bool singleShot
This static function calls a slot after a given time interval.
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
virtual qint64 bytesAvailable() const
Reimplemented Function
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Q_CORE_EXPORT QTextStream & flush(QTextStream &s)
void destroyPipeHandles()
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false...
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
LocalSocketError
The LocalServerError enumeration represents the errors that can occur.
bool setSocketDescriptor(quintptr socketDescriptor, LocalSocketState socketState=ConnectedState, OpenMode openMode=ReadWrite)
void setErrorString(const QString &function)
void disconnectFromServer()
Attempts to close the socket.
#define forever
This macro is provided for convenience for writing infinite loops.