mirror of
https://github.com/QuasarApp/qthttpserver.git
synced 2025-05-07 15:29:33 +00:00
Return the server port in listen()
It allows knowing the server port in case the user chooses to use a random port. In the previous implementation listen() was returning a boolean value making it impossible to know the port number. Change-Id: I73384188b3b2eb57816eb6c6c9a7ac1a511b7456 Reviewed-by: Mikhail Svetkin <mikhail.svetkin@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
parent
cccece0a61
commit
419e7a17f6
src/httpserver
@ -156,17 +156,19 @@ QAbstractHttpServer::QAbstractHttpServer(QAbstractHttpServerPrivate &dd, QObject
|
||||
/*!
|
||||
Tries to bind a \c QTcpServer to \a address and \a port.
|
||||
|
||||
Returns \c true upon success, false otherwise.
|
||||
Returns the server port upon success, -1 otherwise.
|
||||
*/
|
||||
bool QAbstractHttpServer::listen(const QHostAddress &address, quint16 port)
|
||||
int QAbstractHttpServer::listen(const QHostAddress &address, quint16 port)
|
||||
{
|
||||
auto tcpServer = new QTcpServer(this);
|
||||
const auto listening = tcpServer->listen(address, port);
|
||||
if (listening)
|
||||
if (listening) {
|
||||
bind(tcpServer);
|
||||
else
|
||||
return tcpServer->serverPort();
|
||||
} else {
|
||||
delete tcpServer;
|
||||
return listening;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
|
@ -61,7 +61,7 @@ class Q_HTTPSERVER_EXPORT QAbstractHttpServer : public QObject
|
||||
public:
|
||||
QAbstractHttpServer(QObject *parent = nullptr);
|
||||
|
||||
bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
|
||||
int listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
|
||||
|
||||
void bind(QTcpServer *server = nullptr);
|
||||
QVector<QTcpServer *> servers() const;
|
||||
|
Loading…
x
Reference in New Issue
Block a user