From 5b24f0a6bb5a670539ed6ad94a9359378203b6da Mon Sep 17 00:00:00 2001 From: Jesus Fernandez Date: Wed, 31 May 2017 15:34:43 +0200 Subject: Add QLocalServer::socketDescriptor Adds a function to return the native socket descriptor. It allows threaded or forked applications to reuse a previously created socket. [ChangeLog][QtNetwork][QLocalServer] Added a function to retrieve the socket descriptor. Task-number: QTBUG-55043 Change-Id: I556e97000d2c02ad2bdd636984de6c7564381c6a Reviewed-by: Thiago Macieira --- src/network/socket/qlocalserver.cpp | 40 +++++++++++++++++++++++++++++++++++++ src/network/socket/qlocalserver.h | 2 ++ 2 files changed, 42 insertions(+) (limited to 'src/network') 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 +#endif + QT_BEGIN_NAMESPACE #ifndef QT_NO_LOCALSERVER @@ -182,6 +186,42 @@ QLocalServer::SocketOptions QLocalServer::socketOptions() const return d->socketOptions; } +/*! + \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); -- cgit v1.2.3