make tcp server dynamic to corresponding settings file
This commit is contained in:
parent
40847fd153
commit
8d6424483a
@ -34,6 +34,7 @@ TcpServer::TcpServer(QObject *parent) :
|
|||||||
// Timer for scanning network interfaces ever 5 seconds
|
// Timer for scanning network interfaces ever 5 seconds
|
||||||
// Note: QNetworkConfigurationManager does not work on embedded platforms
|
// Note: QNetworkConfigurationManager does not work on embedded platforms
|
||||||
m_timer = new QTimer(this);
|
m_timer = new QTimer(this);
|
||||||
|
m_timer->setSingleShot(false);
|
||||||
m_timer->setInterval(5000);
|
m_timer->setInterval(5000);
|
||||||
connect (m_timer, &QTimer::timeout, this, &TcpServer::onTimeout);
|
connect (m_timer, &QTimer::timeout, this, &TcpServer::onTimeout);
|
||||||
|
|
||||||
@ -69,6 +70,27 @@ void TcpServer::sendData(const QList<QUuid> &clients, const QByteArray &data)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TcpServer::reloadNetworkInterfaces()
|
||||||
|
{
|
||||||
|
GuhSettings settings(GuhSettings::SettingsRoleGlobal);
|
||||||
|
settings.beginGroup("JSONRPC");
|
||||||
|
|
||||||
|
// reload network interfaces
|
||||||
|
m_networkInterfaces.clear();
|
||||||
|
|
||||||
|
QStringList interfaceList = settings.value("interfaces", QStringList("all")).toStringList();
|
||||||
|
if (interfaceList.contains("all")) {
|
||||||
|
m_networkInterfaces = QNetworkInterface::allInterfaces();
|
||||||
|
} else {
|
||||||
|
foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
|
||||||
|
if (interfaceList.contains(interface.name())) {
|
||||||
|
m_networkInterfaces.append(interface);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
settings.endGroup();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void TcpServer::sendData(const QUuid &clientId, const QByteArray &data)
|
void TcpServer::sendData(const QUuid &clientId, const QByteArray &data)
|
||||||
{
|
{
|
||||||
@ -91,7 +113,7 @@ void TcpServer::newClientConnected()
|
|||||||
m_clientList.insert(clientId, newConnection);
|
m_clientList.insert(clientId, newConnection);
|
||||||
|
|
||||||
connect(newConnection, SIGNAL(readyRead()),this,SLOT(readPackage()));
|
connect(newConnection, SIGNAL(readyRead()),this,SLOT(readPackage()));
|
||||||
connect(newConnection,SIGNAL(disconnected()),this,SLOT(slotClientDisconnected()));
|
connect(newConnection,SIGNAL(disconnected()),this,SLOT(onClientDisconnected()));
|
||||||
|
|
||||||
emit clientConnected(clientId);
|
emit clientConnected(clientId);
|
||||||
}
|
}
|
||||||
@ -113,7 +135,7 @@ void TcpServer::readPackage()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpServer::slotClientDisconnected()
|
void TcpServer::onClientDisconnected()
|
||||||
{
|
{
|
||||||
QTcpSocket *client = qobject_cast<QTcpSocket*>(sender());
|
QTcpSocket *client = qobject_cast<QTcpSocket*>(sender());
|
||||||
qCDebug(dcConnection) << "client disconnected:" << client->peerAddress().toString();
|
qCDebug(dcConnection) << "client disconnected:" << client->peerAddress().toString();
|
||||||
@ -121,50 +143,93 @@ void TcpServer::slotClientDisconnected()
|
|||||||
m_clientList.take(clientId)->deleteLater();
|
m_clientList.take(clientId)->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TcpServer::onError(const QAbstractSocket::SocketError &error)
|
||||||
|
{
|
||||||
|
QTcpServer *server = qobject_cast<QTcpServer *>(sender());
|
||||||
|
QUuid uuid = m_serverList.key(server);
|
||||||
|
qCWarning(dcConnection) << "TCP server" << server->serverAddress().toString() << "error:" << error << server->errorString();
|
||||||
|
qCWarning(dcConnection) << "shutting down TCP server" << server->serverAddress().toString();
|
||||||
|
|
||||||
|
server->close();
|
||||||
|
m_serverList.take(uuid)->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
void TcpServer::onTimeout()
|
void TcpServer::onTimeout()
|
||||||
{
|
{
|
||||||
// // check all networkinterfaces
|
bool ipV4 = m_ipVersions.contains("IPv4");
|
||||||
// bool ipV4 = m_ipVersions.contains("IPv4");
|
bool ipV6 = m_ipVersions.contains("IPv6");
|
||||||
// bool ipV6 = m_ipVersions.contains("IPv6");
|
if (m_ipVersions.contains("any")) {
|
||||||
// if (m_ipVersions.contains("any")) {
|
ipV4 = ipV6 = true;
|
||||||
// ipV4 = ipV6 = true;
|
}
|
||||||
// }
|
|
||||||
|
|
||||||
// QList<QHostAddress *> m_serversToCreate;
|
reloadNetworkInterfaces();
|
||||||
// QList<QTcpServer *> m_serversToDelete;
|
|
||||||
|
|
||||||
// // check all available interfaces
|
// get all available host addresses
|
||||||
// foreach (const QNetworkInterface &interface, m_networkInterfaces) {
|
QList<QHostAddress> allAddresses;
|
||||||
// QList<QNetworkAddressEntry> addresseEntries = interface.addressEntries();
|
foreach (const QNetworkInterface &interface, m_networkInterfaces) {
|
||||||
// QList<QHostAddress> addresses;
|
QList<QNetworkAddressEntry> addresseEntries = interface.addressEntries();
|
||||||
|
|
||||||
// foreach (QNetworkAddressEntry entry, addresseEntries) {
|
foreach (QNetworkAddressEntry entry, addresseEntries) {
|
||||||
// if (ipV4 && entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
|
if (ipV4 && entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {
|
||||||
// addresses.append(entry.ip());
|
allAddresses.append(entry.ip());
|
||||||
// }
|
}
|
||||||
// if (ipV6 && entry.ip().protocol() == QAbstractSocket::IPv6Protocol) {
|
if (ipV6 && entry.ip().protocol() == QAbstractSocket::IPv6Protocol) {
|
||||||
// addresses.append(entry.ip());
|
allAddresses.append(entry.ip());
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// // check each host address of this interface
|
// check if a new server should be created
|
||||||
|
QList<QHostAddress> serversToCreate;
|
||||||
|
QList<QTcpServer *> serversToDelete;
|
||||||
|
|
||||||
// foreach (QTcpServer *s, m_serverList) {
|
foreach (const QHostAddress &address, allAddresses) {
|
||||||
// if (!addresses.contains(s->serverAddress()))
|
// check if there is a server for this host address
|
||||||
// return;
|
bool found = false;
|
||||||
|
foreach (QTcpServer *s, m_serverList) {
|
||||||
|
if (s->serverAddress() == address) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!found)
|
||||||
|
serversToCreate.append(address);
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete every server which has no longer an interface
|
||||||
|
foreach (QTcpServer *s, m_serverList) {
|
||||||
|
if (!allAddresses.contains(s->serverAddress())) {
|
||||||
|
qCDebug(dcConnection) << "Closing Tcp server on" << s->serverAddress().toString() << s->serverPort();
|
||||||
|
QUuid uuid = m_serverList.key(s);
|
||||||
|
s->close();
|
||||||
|
m_serverList.take(uuid)->deleteLater();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(const QHostAddress &address, serversToCreate){
|
||||||
|
QTcpServer *server = new QTcpServer(this);
|
||||||
|
if(server->listen(address, m_port)) {
|
||||||
|
qCDebug(dcConnection) << "Started TCP server on" << address.toString() << m_port;
|
||||||
|
connect(server, SIGNAL(newConnection()), SLOT(newClientConnected()));
|
||||||
|
m_serverList.insert(QUuid::createUuid(), server);
|
||||||
|
} else {
|
||||||
|
qCWarning(dcConnection) << "Tcp server error: can not listen on" << address.toString() << m_port;
|
||||||
|
delete server;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!serversToCreate.isEmpty() || !serversToDelete.isEmpty()) {
|
||||||
|
qCDebug(dcConnection) << "current server list:";
|
||||||
|
foreach (QTcpServer *s, m_serverList) {
|
||||||
|
qCDebug(dcConnection) << " - Tcp server on" << s->serverAddress().toString() << s->serverPort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TcpServer::startServer()
|
bool TcpServer::startServer()
|
||||||
{
|
{
|
||||||
qCDebug(dcConnection) << "----------------------------";
|
|
||||||
qCDebug(dcConnection) << "JSON-RPC server listening on:";
|
|
||||||
qCDebug(dcConnection) << "----------------------------";
|
|
||||||
|
|
||||||
bool ipV4 = m_ipVersions.contains("IPv4");
|
bool ipV4 = m_ipVersions.contains("IPv4");
|
||||||
bool ipV6 = m_ipVersions.contains("IPv6");
|
bool ipV6 = m_ipVersions.contains("IPv6");
|
||||||
@ -185,24 +250,22 @@ bool TcpServer::startServer()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create a tcp server for each address found for the corresponding settings
|
||||||
foreach(const QHostAddress &address, addresses){
|
foreach(const QHostAddress &address, addresses){
|
||||||
QTcpServer *server = new QTcpServer(this);
|
QTcpServer *server = new QTcpServer(this);
|
||||||
if(server->listen(address, m_port)) {
|
if(server->listen(address, m_port)) {
|
||||||
qCDebug(dcConnection) << "\tname |" << interface.name();
|
qCDebug(dcConnection) << "Started Tcp server on" << server->serverAddress().toString() << server->serverPort();
|
||||||
qCDebug(dcConnection) << "\tip |" << address.toString();
|
|
||||||
qCDebug(dcConnection) << "\tport |" << m_port;
|
|
||||||
qCDebug(dcConnection) << "\tmac |" << interface.hardwareAddress();
|
|
||||||
qCDebug(dcConnection) << "\t-----+-------------------";
|
|
||||||
|
|
||||||
connect(server, SIGNAL(newConnection()), SLOT(newClientConnected()));
|
connect(server, SIGNAL(newConnection()), SLOT(newClientConnected()));
|
||||||
m_serverList.insert(QUuid::createUuid(), server);
|
m_serverList.insert(QUuid::createUuid(), server);
|
||||||
} else {
|
} else {
|
||||||
qCWarning(dcConnection) << "ERROR: can not listen to" << interface.name() << address.toString() << m_port;
|
qCWarning(dcConnection) << "Tcp server error: can not listen on" << interface.name() << address.toString() << m_port;
|
||||||
delete server;
|
delete server;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_timer->start();
|
||||||
|
|
||||||
if (m_serverList.empty())
|
if (m_serverList.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
@ -212,10 +275,11 @@ bool TcpServer::startServer()
|
|||||||
bool TcpServer::stopServer()
|
bool TcpServer::stopServer()
|
||||||
{
|
{
|
||||||
// Listen on all Networkinterfaces
|
// Listen on all Networkinterfaces
|
||||||
foreach (QTcpServer *server, m_serverList) {
|
foreach (QTcpServer *s, m_serverList) {
|
||||||
qCDebug(dcConnection) << "close server " << server->serverAddress().toString();
|
qCDebug(dcConnection) << "Closing Tcp server on" << s->serverAddress().toString() << s->serverPort();
|
||||||
server->close();
|
QUuid uuid = m_serverList.key(s);
|
||||||
delete server;
|
s->close();
|
||||||
|
m_serverList.take(uuid)->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_serverList.empty())
|
if (!m_serverList.empty())
|
||||||
|
|||||||
@ -50,9 +50,7 @@ private:
|
|||||||
QList<QNetworkInterface> m_networkInterfaces;
|
QList<QNetworkInterface> m_networkInterfaces;
|
||||||
QStringList m_ipVersions;
|
QStringList m_ipVersions;
|
||||||
|
|
||||||
// QList<QHostAddress> serversToCreate();
|
void reloadNetworkInterfaces();
|
||||||
// QList<QHostAddress> serversToRemove();
|
|
||||||
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void clientConnected(const QUuid &clientId);
|
void clientConnected(const QUuid &clientId);
|
||||||
@ -62,7 +60,8 @@ signals:
|
|||||||
private slots:
|
private slots:
|
||||||
void newClientConnected();
|
void newClientConnected();
|
||||||
void readPackage();
|
void readPackage();
|
||||||
void slotClientDisconnected();
|
void onClientDisconnected();
|
||||||
|
void onError(const QAbstractSocket::SocketError &error);
|
||||||
void onTimeout();
|
void onTimeout();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|||||||
Reference in New Issue
Block a user