mirror of
https://github.com/HifiExperiments/overte.git
synced 2025-08-04 03:13:09 +02:00
Merge pull request #5609 from murillodigital/master
Better handling of socket binding
This commit is contained in:
commit
f6f189c4b8
2 changed files with 36 additions and 7 deletions
|
@ -9,6 +9,7 @@
|
||||||
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <QtCore/QCoreApplication>
|
||||||
#include <QtCore/QDebug>
|
#include <QtCore/QDebug>
|
||||||
#include <QtCore/QFile>
|
#include <QtCore/QFile>
|
||||||
#include <QtCore/QFileInfo>
|
#include <QtCore/QFileInfo>
|
||||||
|
@ -19,16 +20,19 @@
|
||||||
#include "EmbeddedWebserverLogging.h"
|
#include "EmbeddedWebserverLogging.h"
|
||||||
#include "HTTPManager.h"
|
#include "HTTPManager.h"
|
||||||
|
|
||||||
|
const int SOCKET_ERROR_EXIT_CODE = 2;
|
||||||
|
const int SOCKET_CHECK_INTERVAL_IN_MS = 30000;
|
||||||
|
|
||||||
HTTPManager::HTTPManager(quint16 port, const QString& documentRoot, HTTPRequestHandler* requestHandler, QObject* parent) :
|
HTTPManager::HTTPManager(quint16 port, const QString& documentRoot, HTTPRequestHandler* requestHandler, QObject* parent) :
|
||||||
QTcpServer(parent),
|
QTcpServer(parent),
|
||||||
_documentRoot(documentRoot),
|
_documentRoot(documentRoot),
|
||||||
_requestHandler(requestHandler)
|
_requestHandler(requestHandler),
|
||||||
|
_port(port)
|
||||||
{
|
{
|
||||||
// start listening on the passed port
|
bindSocket();
|
||||||
if (!listen(QHostAddress("0.0.0.0"), port)) {
|
_isListeningTimer = new QTimer(this);
|
||||||
qCDebug(embeddedwebserver) << "Failed to open HTTP server socket:" << errorString();
|
connect(_isListeningTimer, &QTimer::timeout, this, &HTTPManager::isTcpServerListening);
|
||||||
return;
|
_isListeningTimer->start(SOCKET_CHECK_INTERVAL_IN_MS);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTTPManager::incomingConnection(qintptr socketDescriptor) {
|
void HTTPManager::incomingConnection(qintptr socketDescriptor) {
|
||||||
|
@ -157,3 +161,19 @@ bool HTTPManager::handleHTTPRequest(HTTPConnection* connection, const QUrl& url,
|
||||||
bool HTTPManager::requestHandledByRequestHandler(HTTPConnection* connection, const QUrl& url) {
|
bool HTTPManager::requestHandledByRequestHandler(HTTPConnection* connection, const QUrl& url) {
|
||||||
return _requestHandler && _requestHandler->handleHTTPRequest(connection, url);
|
return _requestHandler && _requestHandler->handleHTTPRequest(connection, url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HTTPManager::isTcpServerListening() {
|
||||||
|
if (!isListening()) {
|
||||||
|
qCWarning(embeddedwebserver) << "Socket on port " << QString::number(_port) << " is no longer listening";
|
||||||
|
bindSocket();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool HTTPManager::bindSocket() {
|
||||||
|
qCDebug(embeddedwebserver) << "Attempting to bind TCP socket on port " << QString::number(_port);
|
||||||
|
if (!listen(QHostAddress::Any, _port)) {
|
||||||
|
qCritical() << "Failed to open HTTP server socket:" << errorString() << " can't continue";
|
||||||
|
QCoreApplication::exit(SOCKET_ERROR_EXIT_CODE);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
|
@ -17,6 +17,7 @@
|
||||||
#define hifi_HTTPManager_h
|
#define hifi_HTTPManager_h
|
||||||
|
|
||||||
#include <QtNetwork/QTcpServer>
|
#include <QtNetwork/QTcpServer>
|
||||||
|
#include <QtCore/QTimer>
|
||||||
|
|
||||||
class HTTPConnection;
|
class HTTPConnection;
|
||||||
class HTTPSConnection;
|
class HTTPSConnection;
|
||||||
|
@ -35,14 +36,22 @@ public:
|
||||||
HTTPManager(quint16 port, const QString& documentRoot, HTTPRequestHandler* requestHandler = NULL, QObject* parent = 0);
|
HTTPManager(quint16 port, const QString& documentRoot, HTTPRequestHandler* requestHandler = NULL, QObject* parent = 0);
|
||||||
|
|
||||||
bool handleHTTPRequest(HTTPConnection* connection, const QUrl& url, bool skipSubHandler = false);
|
bool handleHTTPRequest(HTTPConnection* connection, const QUrl& url, bool skipSubHandler = false);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void isTcpServerListening();
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool bindSocket();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/// Accepts all pending connections
|
/// Accepts all pending connections
|
||||||
virtual void incomingConnection(qintptr socketDescriptor);
|
virtual void incomingConnection(qintptr socketDescriptor);
|
||||||
virtual bool requestHandledByRequestHandler(HTTPConnection* connection, const QUrl& url);
|
virtual bool requestHandledByRequestHandler(HTTPConnection* connection, const QUrl& url);
|
||||||
protected:
|
|
||||||
QString _documentRoot;
|
QString _documentRoot;
|
||||||
HTTPRequestHandler* _requestHandler;
|
HTTPRequestHandler* _requestHandler;
|
||||||
|
QTimer* _isListeningTimer;
|
||||||
|
const quint16 _port;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // hifi_HTTPManager_h
|
#endif // hifi_HTTPManager_h
|
||||||
|
|
Loading…
Reference in a new issue