1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-22 18:52:30 +01:00

Add server project

This commit is contained in:
Daniel Schukies 2018-11-17 11:49:37 +01:00
parent 368bf83660
commit e983a12aa8
5 changed files with 226 additions and 1 deletions

View File

@ -7,7 +7,8 @@ SUBDIRS = \
ScreenPlay/ThirdParty/qt-google-analytics/qt-google-analytics.pro \
ScreenPlay/ThirdParty/stomt-qt-sdk/sdk/stomt-qt-sdk.pro \
ScreenPlayWidget/ScreenPlayWidget.pro \
ScreenPlayWorkshop/ScreenPlayWorkshop.pro
ScreenPlayWorkshop/ScreenPlayWorkshop.pro \
ScreenPlayServer
ScreenPlayWindow.depends = ScreenPlaySDK
ScreenPlayWidget.depends = ScreenPlaySDK

View File

@ -0,0 +1,28 @@
QT -= gui
CONFIG += c++17 console
QT += sql websockets network
CONFIG -= app_bundle
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
src/aimberserver.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
src/aimberserver.h

31
ScreenPlayServer/main.cpp Normal file
View File

@ -0,0 +1,31 @@
#include <QCoreApplication>
#include <QScopedPointer>
#include <QSharedPointer>
#include <QDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include "src/aimberserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
AimberServer ps(16395);
/*
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("localhost");
db.setDatabaseName("ProtoLogin");
db.setUserName("CommunityUser");
//db.setPassword("windowsPW2");
qDebug() << "Open Database " << db.open();
QSqlQueryModel* model = new QSqlQueryModel();
model->setQuery("SELECT * FROM public.\"User\"");
qDebug() << model->rowCount();
*/
return a.exec();
}

View File

@ -0,0 +1,125 @@
#include "aimberserver.h"
//#include <google/protobuf/message_lite.h>
//#include <google/protobuf/any.pb.h>.
AimberServer::AimberServer(int port, QObject* parent) : QObject(parent)
{
/** --------- SSL --------- **/
/*
QSslConfiguration sslConfiguration;
// Create a cert & key
// https://developer.salesforce.com/blogs/developer-relations/2011/05/generating-valid-self-signed-certificates.html
QFile certFile(QStringLiteral("server.crt"));
QFile keyFile(QStringLiteral("server.key"));
certFile.open(QIODevice::ReadOnly);
keyFile.open(QIODevice::ReadOnly);
QSslCertificate certificate(&certFile, QSsl::Pem);
if(certificate.isNull())
{
qDebug() << "Invalid certificate!";
}
QSslKey sslKey(&keyFile, QSsl::Rsa,QSsl::Pem, QSsl::PrivateKey, QByteArray("1234"));
if(sslKey.isNull())
{
qDebug() << "Invalid key!";
}
certFile.close();
keyFile.close();
sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConfiguration.setLocalCertificate(certificate);
sslConfiguration.setPrivateKey(sslKey);
sslConfiguration.setProtocol(QSsl::SecureProtocols);
*/
/** --------- SocketServer --------- **/
m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Community Login Server"), QWebSocketServer::NonSecureMode, this);
//m_pWebSocketServer->setSslConfiguration(sslConfiguration);
if (m_pWebSocketServer->listen(QHostAddress::LocalHost, port))
{
qDebug() << "SSL Echo Server listening on port" << port;
connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &AimberServer::onNewConnection);
connect(m_pWebSocketServer, &QWebSocketServer::sslErrors, this, &AimberServer::onSslErrors);
}
//community::Login Login;
//Login.set_password("test");
}
AimberServer::~AimberServer()
{
m_pWebSocketServer->close();
qDeleteAll(m_clients.begin(), m_clients.end());
}
void AimberServer::onNewConnection()
{
QWebSocket* pSocket = m_pWebSocketServer->nextPendingConnection();
qDebug() << "Client connected:" << pSocket->peerName() << pSocket->origin();
connect(pSocket, &QWebSocket::textMessageReceived, this, &AimberServer::processTextMessage);
connect(pSocket, &QWebSocket::binaryMessageReceived, this, &AimberServer::processBinaryMessage);
connect(pSocket, &QWebSocket::disconnected, this, &AimberServer::socketDisconnected);
m_clients << pSocket;
}
void AimberServer::processTextMessage(QString message)
{
qDebug() << message;
//google::protobuf::MessageLite m;
/*
community::MessageContainer messageContainer;
messageContainer.ParseFromString(message.toStdString());
for (const google::protobuf::Any& cmessage : messageContainer.message())
{
if (cmessage.Is<community::Login>())
{
//NetworkErrorDetails network_error;
//detail.UnpackTo(&network_error);
//... processing network_error ...
}
}
*/
}
void AimberServer::processBinaryMessage(QByteArray message)
{
qDebug() << "Binary Message Received";
}
void AimberServer::socketDisconnected()
{
qDebug() << "Client disconnected";
QWebSocket* pClient = qobject_cast<QWebSocket*>(sender());
if (pClient)
{
m_clients.removeAll(pClient);
pClient->deleteLater();
}
}
void AimberServer::onSslErrors(const QList<QSslError>& errors)
{
qDebug() << "SSL errors occurred: \n";
for (int i = 0; i < errors.length(); ++i)
{
qDebug() << errors.at(i).errorString();
}
}

View File

@ -0,0 +1,40 @@
#pragma once
#include <QDebug>
#include <QFile>
#include <QObject>
#include <QString>
#include <QVector>
#include <QList>
#include <QtCore/QByteArray>
#include <QtNetwork/QSslCertificate>
#include <QtNetwork/QSslError>
#include <QtNetwork/QSslKey>
#include <QtWebSockets/QWebSocketServer>
#include <QtWebSockets/QWebSocket>
//#include "Source/ProtocolBufferSub/GenCode/container.pb.h"
//#include "Source/ProtocolBufferSub/GenCode/account.pb.h"
class AimberServer : public QObject
{
Q_OBJECT
public:
explicit AimberServer(int port, QObject* parent = nullptr);
~AimberServer();
signals:
private slots:
void onNewConnection();
void processTextMessage(QString message);
void processBinaryMessage(QByteArray message);
void socketDisconnected();
void onSslErrors(const QList<QSslError>& errors);
public slots:
private:
QWebSocketServer* m_pWebSocketServer = nullptr;
QVector<QWebSocket*> m_clients;
};