diff options
-rw-r--r-- | src/network/socket/qlocalserver.cpp | 40 | ||||
-rw-r--r-- | src/network/socket/qlocalserver.h | 2 | ||||
-rw-r--r-- | tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp | 2 |
3 files changed, 44 insertions, 0 deletions
diff --git a/src/network/socket/qlocalserver.cpp b/src/network/socket/qlocalserver.cpp index 94143c2dc0..219a19f8b1 100644 --- a/src/network/socket/qlocalserver.cpp +++ b/src/network/socket/qlocalserver.cpp @@ -41,6 +41,10 @@ #include "qlocalserver_p.h" #include "qlocalsocket.h" +#if defined(Q_OS_WIN) && !defined(QT_LOCALSOCKET_TCP) +#include <QtCore/qt_windows.h> +#endif + QT_BEGIN_NAMESPACE #ifndef QT_NO_LOCALSERVER @@ -183,6 +187,42 @@ QLocalServer::SocketOptions QLocalServer::socketOptions() const } /*! + \since 5.10 + Returns the native socket descriptor the server uses to listen + for incoming instructions, or -1 if the server is not listening. + + The type of the descriptor depends on the platform: + \list + \li On Windows, the returned value is a + \l{https://msdn.microsoft.com/en-us/library/windows/desktop/ms740522(v=vs.85).aspx} + {Winsock 2 Socket Handle}. + + \li With WinRT and on INTEGRITY, the returned value is the + QTcpServer socket descriptor and the type is defined by + \l{QTcpServer::socketDescriptor}{socketDescriptor}. + + \li On all other UNIX-like operating systems, the type is + a file descriptor representing a listening socket. + \endlist + + \sa listen() +*/ +qintptr QLocalServer::socketDescriptor() const +{ + Q_D(const QLocalServer); + if (!isListening()) + return -1; +#if defined(QT_LOCALSOCKET_TCP) + return d->tcpServer.socketDescriptor(); +#elif defined(Q_OS_WIN) + const auto handle = d->connectionEventNotifier->handle(); + return handle != INVALID_HANDLE_VALUE ? qintptr(handle) : -1; +#else + return d->socketNotifier->socket(); +#endif +} + +/*! Stop listening for incoming connections. Existing connections are not affected, but any new connections will be refused. diff --git a/src/network/socket/qlocalserver.h b/src/network/socket/qlocalserver.h index 52c533141f..2341fd40d4 100644 --- a/src/network/socket/qlocalserver.h +++ b/src/network/socket/qlocalserver.h @@ -93,6 +93,8 @@ public: void setSocketOptions(SocketOptions options); SocketOptions socketOptions() const; + qintptr socketDescriptor() const; + protected: virtual void incomingConnection(quintptr socketDescriptor); diff --git a/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp b/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp index 60ee4eb471..7760138067 100644 --- a/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp +++ b/tests/auto/network/socket/qlocalsocket/tst_qlocalsocket.cpp @@ -306,9 +306,11 @@ void tst_QLocalSocket::listen() // already isListening QTest::ignoreMessage(QtWarningMsg, "QLocalServer::listen() called when already listening"); QVERIFY(!server.listen(name)); + QVERIFY(server.socketDescriptor() != -1); } else { QVERIFY(!server.errorString().isEmpty()); QCOMPARE(server.serverError(), QAbstractSocket::HostNotFoundError); + QCOMPARE(server.socketDescriptor(), -1); } QCOMPARE(server.maxPendingConnections(), 30); bool timedOut = false; |