21# include <selectLib.h>
24using namespace std::chrono_literals;
26#define QT_CONNECT_TIMEOUT 30000
35 QLocalSocket::SocketOptions options)
39 return connectingName;
45static QLocalSocket::SocketOptions optionsForPlatform(QLocalSocket::SocketOptions srcOptions)
85 QString function =
"QLocalSocket"_L1;
112#if defined QLOCALSOCKET_DEBUG
117 if (currentState !=
state)
126 errorString = QLocalSocket::tr(
"%1: Connection refused").
arg(function);
129 errorString = QLocalSocket::tr(
"%1: Remote closed").
arg(function);
135 errorString = QLocalSocket::tr(
"%1: Socket access error").
arg(function);
138 errorString = QLocalSocket::tr(
"%1: Socket resource error").
arg(function);
141 errorString = QLocalSocket::tr(
"%1: Socket operation timed out").
arg(function);
144 errorString = QLocalSocket::tr(
"%1: Datagram too large").
arg(function);
147 errorString = QLocalSocket::tr(
"%1: Connection error").
arg(function);
150 errorString = QLocalSocket::tr(
"%1: The socket operation is not supported").
arg(function);
153 errorString = QLocalSocket::tr(
"%1: Operation not permitted when socket is in this state").
arg(function);
208 q->emit stateChanged(
state);
221 d->errorString.clear();
226 if (
d->serverName.isEmpty()) {
232 if (-1 == (
d->connectingSocket =
qt_safe_socket(PF_UNIX, SOCK_STREAM, 0, O_NONBLOCK))) {
238 d->connectingName =
d->serverName;
240 d->_q_connectToSocket();
255 QLocalSocket::SocketOptions options = optionsForPlatform(socketOptions);
258 struct ::sockaddr_un
addr;
259 addr.sun_family = PF_UNIX;
260 memset(
addr.sun_path, 0,
sizeof(
addr.sun_path));
265 if (
sizeof(
addr.sun_path) <
static_cast<size_t>(encodedConnectingPathName.size() + extraCharacters)) {
266 QString function =
"QLocalSocket::connectToServer"_L1;
271 QT_SOCKLEN_T addrSize =
sizeof(::sockaddr_un);
273 ::memcpy(
addr.sun_path + 1, encodedConnectingPathName.constData(),
274 encodedConnectingPathName.size() + 1);
275 addrSize = offsetof(::sockaddr_un, sun_path) + encodedConnectingPathName.size() + 1;
277 ::memcpy(
addr.sun_path, encodedConnectingPathName.constData(),
278 encodedConnectingPathName.size() + 1);
281 QString function =
"QLocalSocket::connectToServer"_L1;
329 QString function =
"QLocalSocket::connectToServer"_L1;
338 LocalSocketState socketState, OpenMode openMode)
342 switch (socketState) {
365 bool abstractAddress =
false;
367 struct ::sockaddr_un
addr;
368 QT_SOCKLEN_T
len =
sizeof(
addr);
370 const int getpeernameStatus = ::getpeername(socketDescriptor, (sockaddr *)&
addr, &
len);
371 if (getpeernameStatus != 0 ||
len == offsetof(sockaddr_un, sun_path)) {
374 if (::getsockname(socketDescriptor, (sockaddr *)&
addr, &
len) != 0)
378 QLocalSocket::SocketOptions options = socketOptions.value();
387 bool &abstractNamespace)
389 if (
len <= offsetof(::sockaddr_un, sun_path))
391 len -= offsetof(::sockaddr_un, sun_path);
394 && (
addr.sun_family == PF_UNIX &&
addr.sun_path[0] == 0);
400 len - (abstractNamespace ? 1 : 0));
405 if (!abstractNamespace && (
name.at(
name.size() - 1) == QChar::fromLatin1(
'\0'))) {
406 int truncPos =
name.size() - 1;
407 while (truncPos > 0 &&
name.at(truncPos - 1) == QChar::fromLatin1(
'\0'))
409 name.truncate(truncPos);
442 return d->unixSocket.socketDescriptor();
448 return d->unixSocket.read(
data,
c);
458 return d_func()->unixSocket.readLine(
data, maxSize + 1);
463 return d_func()->unixSocket.skip(maxSize);
469 return d->unixSocket.writeData(
data,
c);
475 d->unixSocket.abort();
488 return d->unixSocket.bytesToWrite();
502 d->unixSocket.close();
503 d->cancelDelayedConnect();
504 if (
d->connectingSocket != -1)
506 d->connectingSocket = -1;
507 d->connectingName.clear();
508 d->connectingOpenMode = { };
509 d->serverName.clear();
510 d->fullServerName.clear();
516 return d->unixSocket.waitForBytesWritten(msecs);
522 return d->unixSocket.flush();
528 d->unixSocket.disconnectFromHost();
534 switch (
d->unixSocket.error()) {
556#if defined QLOCALSOCKET_DEBUG
557 qWarning() <<
"QLocalSocket error not handled:" <<
d->unixSocket.error();
567 return d->unixSocket.isValid();
573 return d->unixSocket.readBufferSize();
579 d->unixSocket.setReadBufferSize(
size);
598 "QLocalSocket::waitForConnected"_L1);
600 d->_q_connectToSocket();
611 qWarning(
"QLocalSocket::waitForDisconnected() is not allowed in UnconnectedState");
614 return (
d->unixSocket.waitForDisconnected(msecs));
622 return (
d->unixSocket.waitForReadyRead(msecs));
SocketState
This enum describes the different states in which a socket can be.
SocketError
This enum describes the socket errors that can occur.
@ UnsupportedSocketOperationError
virtual bool setSocketDescriptor(qintptr socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite)
Initializes QAbstractSocket with the native socket descriptor socketDescriptor.
std::chrono::nanoseconds remainingTimeAsDuration() const noexcept
Returns the time remaining before the deadline.
static QString tempPath()
Returns the absolute canonical path of the system's temporary directory.
static QByteArray encodeName(const QString &fileName)
Converts fileName to an 8-bit encoding that you can use in native APIs.
virtual bool open(QIODeviceBase::OpenMode mode)
Opens the device and sets its OpenMode to mode.
void setErrorString(const QString &errorString)
Sets the human readable description of the last device error that occurred to str.
QIODeviceBase::OpenMode openMode() const
Returns the mode in which the device has been opened; i.e.
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
virtual void close()
First emits aboutToClose(), then closes the device and sets its OpenMode to NotOpen.
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false.
static bool parseSockaddr(const sockaddr_un &addr, uint len, QString &fullServerName, QString &serverName, bool &abstractNamespace)
void _q_stateChanged(QAbstractSocket::SocketState newState)
void describeSocket(qintptr socketDescriptor)
QLocalUnixSocket unixSocket
void _q_abortConnectionAttempt()
void cancelDelayedConnect()
QLocalSocket::LocalSocketState state
void _q_errorOccurred(QAbstractSocket::SocketError newError)
void setErrorAndEmit(QLocalSocket::LocalSocketError, const QString &function)
QIODevice::OpenMode connectingOpenMode
void _q_connectToSocket()
QSocketNotifier * delayConnect
QString generateErrorString(QLocalSocket::LocalSocketError, const QString &function) const
The QLocalSocket class provides a local socket.
bool setSocketDescriptor(qintptr socketDescriptor, LocalSocketState socketState=ConnectedState, OpenMode openMode=ReadWrite)
Initializes QLocalSocket with the native socket descriptor socketDescriptor.
LocalSocketState state() const
Returns the state of the socket.
virtual qint64 readData(char *, qint64) override
\reimp
void abort()
Aborts the current connection and resets the socket.
@ AbstractNamespaceOption
virtual qint64 bytesToWrite() const override
\reimp
virtual bool canReadLine() const override
\reimp
bool waitForBytesWritten(int msecs=30000) override
\reimp
LocalSocketState
This enum describes the different states in which a socket can be.
bool isValid() const
Returns true if the socket is valid and ready for use; otherwise returns false.
bool flush()
This function writes as much as possible from the internal write buffer to the socket,...
LocalSocketError error() const
Returns the type of error that last occurred.
qint64 readBufferSize() const
Returns the size of the internal read buffer.
bool waitForDisconnected(int msecs=30000)
Waits until the socket has disconnected, up to msecs milliseconds.
void disconnectFromServer()
Attempts to close the socket.
qint64 readLineData(char *data, qint64 maxSize) override
\reimp
bool waitForConnected(int msecs=30000)
Waits until the socket is connected, up to msecs milliseconds.
bool waitForReadyRead(int msecs=30000) override
This function blocks until data is available for reading and the \l{QIODevice::}{readyRead()} signal ...
void connectToServer(OpenMode openMode=ReadWrite)
void setReadBufferSize(qint64 size)
Sets the size of QLocalSocket's internal read buffer to be size bytes.
qintptr socketDescriptor() const
Returns the native socket descriptor of the QLocalSocket object if this is available; otherwise retur...
virtual void close() override
Closes the I/O device for the socket and calls disconnectFromServer() to close the socket's connectio...
qint64 skipData(qint64 maxSize) override
\reimp
virtual qint64 bytesAvailable() const override
\reimp
void stateChanged(QLocalSocket::LocalSocketState socketState)
This signal is emitted whenever QLocalSocket's state changes.
virtual qint64 writeData(const char *, qint64) override
\reimp
LocalSocketError
The LocalServerError enumeration represents the errors that can occur.
@ UnsupportedSocketOperationError
void errorOccurred(QLocalSocket::LocalSocketError socketError)
void setSocketState(QAbstractSocket::SocketState state)
void setSocketError(QAbstractSocket::SocketError error)
void setParent(QObject *parent)
Makes the object a child of parent.
void setEnabled(bool)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.
\macro QT_RESTRICTED_CAST_FROM_ASCII
qsizetype lastIndexOf(QChar c, Qt::CaseSensitivity cs=Qt::CaseSensitive) const noexcept
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
QString mid(qsizetype position, qsizetype n=-1) const &
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
void clear()
Clears the contents of the string and makes it null.
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
void start(int msec)
Starts or restarts the timer with a timeout interval of msec milliseconds.
void stop()
Stops the timer.
void newState(QList< State > &states, const char *token, const char *lexem, bool pre)
QAbstractSocket::SocketState socketState(QIODevice *device)
Combined button and popup list for selecting options.
int qt_safe_poll(struct pollfd *fds, nfds_t nfds, QDeadlineTimer deadline)
static struct pollfd qt_make_pollfd(int fd, short events)
DBusConnection const char DBusError * error
#define QT_CONNECT_TIMEOUT
static int qt_safe_socket(int domain, int type, int protocol, int flags=0)
static int qt_safe_connect(int sockfd, const struct sockaddr *addr, QT_SOCKLEN_T addrlen)
GLenum GLsizei GLuint GLint * bytesWritten
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLbitfield GLuint64 timeout
[4]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLdouble GLdouble GLdouble GLdouble q
@ PlatformSupportsAbstractNamespace
QDeadlineTimer deadline(30s)