-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.cpp
93 lines (77 loc) · 2.85 KB
/
webserver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "webserver.h"
#include <QBuffer>
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtNetwork/QSslCertificate>
#include <QtNetwork/QSslKey>
#include <QtWebSockets/QWebSocket>
#include <QtWebSockets/QWebSocketServer>
#include <qdebug.h>
QT_USE_NAMESPACE
Webserver::Webserver(quint16 port, QObject *parent)
: QObject(parent), m_pWebSocketServer(nullptr) {
m_pWebSocketServer = new QWebSocketServer(QStringLiteral("Camera Server"),
QWebSocketServer::SecureMode, this);
QSslConfiguration sslConfiguration;
QFile certFile(QStringLiteral("minica.pem"));
QFile keyFile(QStringLiteral("minica-key.pem"));
certFile.open(QIODevice::ReadOnly);
keyFile.open(QIODevice::ReadOnly);
QSslCertificate certificate(&certFile, QSsl::Pem);
QSslKey sslKey(&keyFile, QSsl::Rsa, QSsl::Pem);
certFile.close();
keyFile.close();
sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyNone);
sslConfiguration.setLocalCertificate(certificate);
sslConfiguration.setPrivateKey(sslKey);
sslConfiguration.setProtocol(QSsl::TlsV1SslV3);
m_pWebSocketServer->setSslConfiguration(sslConfiguration);
connect(m_pWebSocketServer, &QWebSocketServer::sslErrors, this,
&Webserver::onSslErrors);
connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this,
&Webserver::onNewConnection);
if (m_pWebSocketServer->listen(QHostAddress::Any, port)) {
qDebug() << "SSL Server listening on port" << port;
}
}
Webserver::~Webserver() {
m_pWebSocketServer->close();
qDeleteAll(m_clients.begin(), m_clients.end());
}
void Webserver::onCameraImageAvailable(const QImage &image) {
QByteArray ba;
QBuffer buffer;
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "JPG");
QString message = QString(buffer.data().toBase64());
lock.lockForRead();
foreach (QWebSocket *client, m_clients) { client->sendTextMessage(message); }
lock.unlock();
}
void Webserver::onNewConnection() {
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
qDebug() << "Client connected:" << pSocket->peerName() << pSocket->origin();
connect(pSocket, &QWebSocket::disconnected, this,
&Webserver::socketDisconnected);
connect(pSocket, &QWebSocket::textMessageReceived, this,
&Webserver::onTextMessageReceived);
pSocket->ignoreSslErrors();
m_clients << pSocket;
}
void Webserver::socketDisconnected() {
qDebug() << "Client disconnected";
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
if (pClient) {
lock.lockForWrite();
m_clients.removeAll(pClient);
pClient->deleteLater();
lock.unlock();
}
}
void Webserver::onTextMessageReceived(const QString &message) {
qDebug() << "Message received: " << message;
emit commandReceived(message);
}
void Webserver::onSslErrors(const QList<QSslError> &errors) {
qDebug() << "Ssl errors occurred: " << errors;
}