From a03c633e4f13f17a7876d74248205bde539f2785 Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Fri, 3 Apr 2015 19:21:54 +0300 Subject: [PATCH] Virtualize options setting on TCP server socket Currently, socket options setting is hardcoded in QTcpServer::listen() function after the engine initialization and before a binding to the address. This disallows a socket tuning in further QTcpServer inheritance. Add a private virtual configureCreatedSocket() method that gives an ability to set the socket options before listening. Change-Id: Ice9b477e64f21daee96c0ec6d27a8408f9e1aa93 Reviewed-by: Thiago Macieira --- src/network/socket/qtcpserver.cpp | 29 ++++++++++++++++++----------- src/network/socket/qtcpserver_p.h | 2 ++ 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/network/socket/qtcpserver.cpp b/src/network/socket/qtcpserver.cpp index b41d207947..50cf812491 100644 --- a/src/network/socket/qtcpserver.cpp +++ b/src/network/socket/qtcpserver.cpp @@ -158,6 +158,23 @@ QNetworkProxy QTcpServerPrivate::resolveProxy(const QHostAddress &address, quint } #endif +/*! \internal +*/ +void QTcpServerPrivate::configureCreatedSocket() +{ +#if defined(Q_OS_UNIX) + // Under Unix, we want to be able to bind to the port, even if a socket on + // the same address-port is in TIME_WAIT. Under Windows this is possible + // anyway -- furthermore, the meaning of reusable on Windows is different: + // it means that you can use the same address-port for multiple listening + // sockets. + // Don't abort though if we can't set that option. For example the socks + // engine doesn't support that option, but that shouldn't prevent us from + // trying to bind/listen. + socketEngine->setOption(QAbstractSocketEngine::AddressReusable, 1); +#endif +} + /*! \internal */ void QTcpServerPrivate::readNotification() @@ -275,17 +292,7 @@ bool QTcpServer::listen(const QHostAddress &address, quint16 port) if (addr.protocol() == QAbstractSocket::AnyIPProtocol && proto == QAbstractSocket::IPv4Protocol) addr = QHostAddress::AnyIPv4; -#if defined(Q_OS_UNIX) - // Under Unix, we want to be able to bind to the port, even if a socket on - // the same address-port is in TIME_WAIT. Under Windows this is possible - // anyway -- furthermore, the meaning of reusable on Windows is different: - // it means that you can use the same address-port for multiple listening - // sockets. - // Don't abort though if we can't set that option. For example the socks - // engine doesn't support that option, but that shouldn't prevent us from - // trying to bind/listen. - d->socketEngine->setOption(QAbstractSocketEngine::AddressReusable, 1); -#endif + d->configureCreatedSocket(); if (!d->socketEngine->bind(addr, port)) { d->serverSocketError = d->socketEngine->error(); diff --git a/src/network/socket/qtcpserver_p.h b/src/network/socket/qtcpserver_p.h index 415a0e190a..47505e7e91 100644 --- a/src/network/socket/qtcpserver_p.h +++ b/src/network/socket/qtcpserver_p.h @@ -80,6 +80,8 @@ public: QNetworkProxy resolveProxy(const QHostAddress &address, quint16 port); #endif + virtual void configureCreatedSocket(); + // from QAbstractSocketEngineReceiver void readNotification() Q_DECL_OVERRIDE; void closeNotification() Q_DECL_OVERRIDE { readNotification(); }