21# include <selectLib.h>
24#define QT_CONNECT_TIMEOUT 30000
34 QLocalSocket::SocketOptions options)
38 return connectingName;
44static QLocalSocket::SocketOptions optionsForPlatform(QLocalSocket::SocketOptions srcOptions)
111#if defined QLOCALSOCKET_DEBUG
116 if (currentState !=
state)
125 errorString = QLocalSocket::tr(
"%1: Connection refused").
arg(function);
128 errorString = QLocalSocket::tr(
"%1: Remote closed").
arg(function);
134 errorString = QLocalSocket::tr(
"%1: Socket access error").
arg(function);
137 errorString = QLocalSocket::tr(
"%1: Socket resource error").
arg(function);
140 errorString = QLocalSocket::tr(
"%1: Socket operation timed out").
arg(function);
143 errorString = QLocalSocket::tr(
"%1: Datagram too large").
arg(function);
146 errorString = QLocalSocket::tr(
"%1: Connection error").
arg(function);
149 errorString = QLocalSocket::tr(
"%1: The socket operation is not supported").
arg(function);
152 errorString = QLocalSocket::tr(
"%1: Operation not permitted when socket is in this state").
arg(function);
207 q->emit stateChanged(
state);
220 d->errorString.clear();
225 if (
d->serverName.isEmpty()) {
231 if (-1 == (
d->connectingSocket =
qt_safe_socket(PF_UNIX, SOCK_STREAM, 0, O_NONBLOCK))) {
237 d->connectingName =
d->serverName;
239 d->_q_connectToSocket();
254 QLocalSocket::SocketOptions options = optionsForPlatform(socketOptions);
257 struct ::sockaddr_un
addr;
258 addr.sun_family = PF_UNIX;
259 memset(
addr.sun_path, 0,
sizeof(
addr.sun_path));
264 if (
sizeof(
addr.sun_path) <
static_cast<size_t>(encodedConnectingPathName.
size() + extraCharacters)) {
270 QT_SOCKLEN_T addrSize =
sizeof(::sockaddr_un);
272 ::memcpy(
addr.sun_path + 1, encodedConnectingPathName.
constData(),
273 encodedConnectingPathName.
size() + 1);
274 addrSize = offsetof(::sockaddr_un, sun_path) + encodedConnectingPathName.
size() + 1;
276 ::memcpy(
addr.sun_path, encodedConnectingPathName.
constData(),
277 encodedConnectingPathName.
size() + 1);
337 LocalSocketState socketState, OpenMode openMode)
341 switch (socketState) {
356 d->state = socketState;
364 bool abstractAddress =
false;
366 struct ::sockaddr_un
addr;
367 QT_SOCKLEN_T
len =
sizeof(
addr);
369 const int getpeernameStatus = ::getpeername(socketDescriptor, (sockaddr *)&
addr, &
len);
370 if (getpeernameStatus != 0 ||
len == offsetof(sockaddr_un, sun_path)) {
373 if (::getsockname(socketDescriptor, (sockaddr *)&
addr, &
len) != 0)
377 QLocalSocket::SocketOptions options = socketOptions.value();
386 bool &abstractNamespace)
388 if (
len <= offsetof(::sockaddr_un, sun_path))
390 len -= offsetof(::sockaddr_un, sun_path);
393 && (
addr.sun_family == PF_UNIX &&
addr.sun_path[0] == 0);
399 len - (abstractNamespace ? 1 : 0));
405 int truncPos =
name.size() - 1;
408 name.truncate(truncPos);
441 return d->unixSocket.socketDescriptor();
447 return d->unixSocket.read(
data,
c);
457 return d_func()->unixSocket.readLine(
data, maxSize + 1);
462 return d_func()->unixSocket.skip(maxSize);
468 return d->unixSocket.writeData(
data,
c);
474 d->unixSocket.abort();
487 return d->unixSocket.bytesToWrite();
501 d->unixSocket.close();
502 d->cancelDelayedConnect();
503 if (
d->connectingSocket != -1)
505 d->connectingSocket = -1;
506 d->connectingName.clear();
507 d->connectingOpenMode = { };
508 d->serverName.clear();
509 d->fullServerName.clear();
515 return d->unixSocket.waitForBytesWritten(msecs);
521 return d->unixSocket.flush();
527 d->unixSocket.disconnectFromHost();
533 switch (
d->unixSocket.error()) {
555#if defined QLOCALSOCKET_DEBUG
556 qWarning() <<
"QLocalSocket error not handled:" <<
d->unixSocket.error();
566 return d->unixSocket.isValid();
572 return d->unixSocket.readBufferSize();
578 d->unixSocket.setReadBufferSize(
size);
599 "QLocalSocket::waitForConnected"_L1);
601 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.
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
const char * constData() const noexcept
Returns a pointer to the const data stored in the byte array.
static constexpr QChar fromLatin1(char c) noexcept
Converts the Latin-1 character c to its equivalent QChar.
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.
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
QString mid(qsizetype position, qsizetype n=-1) const
Returns a string that contains n characters of this string, starting at the specified position index.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
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)
Combined button and popup list for selecting options.
static struct pollfd qt_make_pollfd(int fd, short events)
static int qt_poll_msecs(struct pollfd *fds, nfds_t nfds, int timeout)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction function
DBusConnection const char DBusError * error
#define QT_CONNECT_TIMEOUT
constexpr const T & qMax(const T &a, const T &b)
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