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 <thiago.macieira@intel.com>
This commit is contained in:
Jesus Fernandez 2017-05-31 15:34:43 +02:00 committed by Jesus Fernandez
parent 3a072f354f
commit 5b24f0a6bb
3 changed files with 44 additions and 0 deletions

View File

@ -41,6 +41,10 @@
#include "qlocalserver_p.h" #include "qlocalserver_p.h"
#include "qlocalsocket.h" #include "qlocalsocket.h"
#if defined(Q_OS_WIN) && !defined(QT_LOCALSOCKET_TCP)
#include <QtCore/qt_windows.h>
#endif
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
#ifndef QT_NO_LOCALSERVER #ifndef QT_NO_LOCALSERVER
@ -182,6 +186,42 @@ QLocalServer::SocketOptions QLocalServer::socketOptions() const
return d->socketOptions; 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 Stop listening for incoming connections. Existing connections are not
affected, but any new connections will be refused. affected, but any new connections will be refused.

View File

@ -93,6 +93,8 @@ public:
void setSocketOptions(SocketOptions options); void setSocketOptions(SocketOptions options);
SocketOptions socketOptions() const; SocketOptions socketOptions() const;
qintptr socketDescriptor() const;
protected: protected:
virtual void incomingConnection(quintptr socketDescriptor); virtual void incomingConnection(quintptr socketDescriptor);

View File

@ -306,9 +306,11 @@ void tst_QLocalSocket::listen()
// already isListening // already isListening
QTest::ignoreMessage(QtWarningMsg, "QLocalServer::listen() called when already listening"); QTest::ignoreMessage(QtWarningMsg, "QLocalServer::listen() called when already listening");
QVERIFY(!server.listen(name)); QVERIFY(!server.listen(name));
QVERIFY(server.socketDescriptor() != -1);
} else { } else {
QVERIFY(!server.errorString().isEmpty()); QVERIFY(!server.errorString().isEmpty());
QCOMPARE(server.serverError(), QAbstractSocket::HostNotFoundError); QCOMPARE(server.serverError(), QAbstractSocket::HostNotFoundError);
QCOMPARE(server.socketDescriptor(), -1);
} }
QCOMPARE(server.maxPendingConnections(), 30); QCOMPARE(server.maxPendingConnections(), 30);
bool timedOut = false; bool timedOut = false;