46 #ifndef QT_NO_LOCALSOCKET 48 #include <sys/types.h> 49 #include <sys/socket.h> 60 # include <selectLib.h> 63 #define QT_CONNECT_TIMEOUT 30000 71 connectingOpenMode(0),
80 q->connect(&unixSocket,
SIGNAL(aboutToClose()), q,
SIGNAL(aboutToClose()));
83 q->connect(&unixSocket,
SIGNAL(readyRead()), q,
SIGNAL(readyRead()));
86 q->connect(&unixSocket,
SIGNAL(disconnected()), q,
SIGNAL(disconnected()));
91 q->connect(&unixSocket,
SIGNAL(readChannelFinished()), q,
SIGNAL(readChannelFinished()));
92 unixSocket.setParent(q);
101 q->setErrorString(errorString);
102 emit q->error(error);
125 #if defined QLOCALSOCKET_DEBUG 126 qWarning() <<
"QLocalSocket::Unhandled socket state change:" << newState;
130 if (currentState !=
state)
209 q->setErrorString(errorString);
218 q->emit stateChanged(
state);
224 if (
state() == ConnectedState
225 ||
state() == ConnectingState)
228 d->errorString.clear();
230 d->state = ConnectingState;
231 emit stateChanged(
d->state);
234 d->errorOccurred(ServerNotFoundError,
240 if (-1 == (
d->connectingSocket =
qt_safe_socket(PF_UNIX, SOCK_STREAM, 0))) {
241 d->errorOccurred(UnsupportedSocketOperationError,
247 int flags =
fcntl(
d->connectingSocket, F_GETFL, 0);
249 || -1 == (
fcntl(
d->connectingSocket, F_SETFL, flags | O_NONBLOCK))) {
250 d->errorOccurred(UnknownSocketError,
257 d->connectingName =
name;
259 d->_q_connectToSocket();
269 void QLocalSocketPrivate::_q_connectToSocket()
276 connectingPathName = connectingName;
279 connectingPathName +=
QLatin1Char(
'/') + connectingName;
282 struct sockaddr_un name;
283 name.sun_family = PF_UNIX;
284 if (
sizeof(name.sun_path) < (
uint)connectingPathName.
toLatin1().
size() + 1) {
289 ::memcpy(name.sun_path, connectingPathName.
toLatin1().
data(),
291 if (-1 ==
qt_safe_connect(connectingSocket, (
struct sockaddr *)&name,
sizeof(name))) {
313 q->connect(delayConnect,
SIGNAL(activated(
int)), q,
SLOT(_q_connectToSocket()));
316 connectTimer =
new QTimer(q);
317 q->connect(connectTimer,
SIGNAL(timeout()),
318 q,
SLOT(_q_abortConnectionAttempt()),
322 delayConnect->setEnabled(
true);
331 cancelDelayedConnect();
335 if (unixSocket.setSocketDescriptor(connectingSocket,
337 q->QIODevice::open(connectingOpenMode);
343 connectingSocket = -1;
344 connectingName.clear();
345 connectingOpenMode = 0;
349 LocalSocketState socketState, OpenMode
openMode)
353 switch (socketState) {
354 case ConnectingState:
363 case UnconnectedState:
368 d->state = socketState;
369 return d->unixSocket.setSocketDescriptor(socketDescriptor,
370 newSocketState, openMode);
373 void QLocalSocketPrivate::_q_abortConnectionAttempt()
379 void QLocalSocketPrivate::cancelDelayedConnect()
382 delayConnect->setEnabled(
false);
385 connectTimer->stop();
394 return d->unixSocket.socketDescriptor();
400 return d->unixSocket.readData(
data, c);
406 return d->unixSocket.writeData(
data, c);
412 d->unixSocket.abort();
424 return d->unixSocket.bytesToWrite();
436 d->unixSocket.close();
437 d->cancelDelayedConnect();
438 if (
d->connectingSocket != -1)
439 ::close(
d->connectingSocket);
440 d->connectingSocket = -1;
441 d->connectingName.clear();
442 d->connectingOpenMode = 0;
443 d->serverName.clear();
444 d->fullServerName.clear();
451 return d->unixSocket.waitForBytesWritten(msecs);
457 return d->unixSocket.flush();
463 d->unixSocket.disconnectFromHost();
469 switch (
d->unixSocket.error()) {
491 #if defined QLOCALSOCKET_DEBUG 492 qWarning() <<
"QLocalSocket error not handled:" <<
d->unixSocket.error();
496 return UnknownSocketError;
502 return d->unixSocket.isValid();
508 return d->unixSocket.readBufferSize();
514 d->unixSocket.setReadBufferSize(size);
520 if (
state() != ConnectingState)
521 return (
state() == ConnectedState);
525 FD_SET(
d->connectingSocket, &fds);
528 timeout.tv_sec = msec / 1000;
529 timeout.tv_usec = (msec % 1000) * 1000;
533 timeout.tv_usec = 1000;
539 while (
state() == ConnectingState
540 && (-1 == msec || timer.
elapsed() < msec)) {
545 result =
::select(
d->connectingSocket + 1, 0, &fds, 0, &timeout);
547 result =
::select(
d->connectingSocket + 1, &fds, 0, 0, &timeout);
549 if (-1 == result &&
errno != EINTR) {
555 d->_q_connectToSocket();
558 return (
state() == ConnectedState);
564 if (
state() == UnconnectedState) {
565 qWarning() <<
"QLocalSocket::waitForDisconnected() is not allowed in UnconnectedState";
568 return (
d->unixSocket.waitForDisconnected(msecs));
576 return (
d->unixSocket.waitForReadyRead(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
#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.
char * data()
Returns a pointer to the data stored in the byte array.
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
int select(int, fd_set *, fd_set *, fd_set *, struct timeval *)
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
#define QT_CONNECT_TIMEOUT
bool waitForDisconnected(int msecs=30000)
Waits until the socket has disconnected, up to msecs milliseconds.
The QString class provides a Unicode character string.
virtual qint64 writeData(const char *, qint64)
Reimplemented Function
QLocalSocket::LocalSocketError error
The QElapsedTimer class provides a fast way to calculate elapsed times.
The QLocalSocket class provides a local socket.
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
qint64 readBufferSize() const
Returns the size of the internal read buffer.
The QSocketNotifier class provides support for monitoring activity on a file descriptor.
SocketState
This enum describes the different states in which a socket can be.
#define QT_BEGIN_NAMESPACE
This macro expands to.
static QIntfbScreen * connected
static int qt_safe_connect(int sockfd, const struct sockaddr *addr, QT_SOCKLEN_T addrlen)
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.
SocketError
This enum describes the socket errors that can occur.
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.
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
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.
static QString tempPath()
Returns the absolute path of the system's temporary directory.
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 int qt_safe_socket(int domain, int type, int protocol, int flags=0)
void clear()
Clears the contents of the string and makes it empty.
bool waitForBytesWritten(int msecs=30000)
Reimplemented Function
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
virtual qint64 bytesAvailable() const
Reimplemented Function
int size() const
Returns the number of bytes in this byte array.
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false...
The QTimer class provides repetitive and single-shot timers.
LocalSocketError
The LocalServerError enumeration represents the errors that can occur.
void start()
Starts this timer.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
bool setSocketDescriptor(quintptr socketDescriptor, LocalSocketState socketState=ConnectedState, OpenMode openMode=ReadWrite)
void disconnectFromServer()
Attempts to close the socket.