/****************************************************************************
**
** Copyright (C) 2020 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Safe Renderer module
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QTcpSocket>#include <QtCore>#include <QtSafeRenderer/messagesenderinterface_p.h>#include <QtSafeRenderer/qsafeevent.h>#include "server.h"
Server::Server(QSafeMessageInterface*messageSender,constquint16 port,QObject*parent)
: QObject(parent), m_messageSender(messageSender)
{
runServer(port);
}
void Server::runServer(constquint16 port)
{
m_tcpServer =newQTcpServer(this);
if (!m_tcpServer->listen(QHostAddress::Any, port)) {
qCritical() <<"Unable to start the server: "<< m_tcpServer->errorString();
return;
}
connect(m_tcpServer,&QTcpServer::newConnection,this,&Server::newConnection);
QString ipAddress;
QList<QHostAddress> ipAddressesList =QNetworkInterface::allAddresses();
// use the first non-localhost IPv4 addressfor (int i =0U; i < ipAddressesList.size(); ++i) {
if (ipAddressesList.at(i) !=QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address()) {
ipAddress = ipAddressesList.at(i).toString();
break;
}
}
// if we did not find one, use IPv4 localhostif (ipAddress.isEmpty())
ipAddress =QHostAddress(QHostAddress::LocalHost).toString();
qDebug() <<"The server is running on: "<< ipAddress <<":"<< m_tcpServer->serverPort();
}
void Server::newConnection()
{
QTcpSocket*clientConnection = m_tcpServer->nextPendingConnection();
connect(clientConnection,&QAbstractSocket::disconnected,
clientConnection,&QObject::deleteLater);
connect(clientConnection,&QAbstractSocket::readyRead,this,&Server::readData);
}
void Server::readData()
{
QTcpSocket*clientConnection = qobject_cast<QTcpSocket*>(QObject::sender());
if (clientConnection) {
unsignedchar dataBuffer[SafeRenderer::QSafeEvent::messageLength];
quint32 datalength =0U;
do {
QByteArray data = clientConnection->read(SafeRenderer::QSafeEvent::messageLength);
datalength = data.length();
if (datalength == SafeRenderer::QSafeEvent::messageLength) {
memcpy (&dataBuffer[0], data.data(), SafeRenderer::QSafeEvent::messageLength);
SafeRenderer::QSafeEvent event(dataBuffer);
m_messageSender->sendMessage(event);
}
} while (datalength >0U);
}
}