diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2015-04-03 19:21:54 +0300 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2015-04-05 15:00:01 +0000 |
commit | a03c633e4f13f17a7876d74248205bde539f2785 (patch) | |
tree | a25b634bd5112231e5cccc93d1a4d9e5475321a1 | |
parent | 70d4b35b937d8a98be156b159bafbc07bfb03097 (diff) |
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 <thiago.macieira@intel.com>
-rw-r--r-- | src/network/socket/qtcpserver.cpp | 29 | ||||
-rw-r--r-- | 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 @@ -160,6 +160,23 @@ QNetworkProxy QTcpServerPrivate::resolveProxy(const QHostAddress &address, quint /*! \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() { Q_Q(QTcpServer); @@ -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(); } |