hyperion.ng/libsrc/jsonserver/JsonClientConnection.cpp

77 lines
2.3 KiB
C++
Raw Normal View History

// project includes
#include "JsonClientConnection.h"
2018-12-27 23:11:32 +01:00
#include <api/JsonAPI.h>
// qt inc
#include <QTcpSocket>
2018-12-27 23:11:32 +01:00
#include <QHostAddress>
2019-01-27 13:41:21 +01:00
// websocket includes
#include "webserver/WebSocketClient.h"
JsonClientConnection::JsonClientConnection(QTcpSocket *socket)
: QObject()
, _socket(socket)
2019-01-27 13:41:21 +01:00
, _websocketClient(nullptr)
, _receiveBuffer()
, _log(Logger::getInstance("JSONCLIENTCONNECTION"))
{
connect(_socket, &QTcpSocket::disconnected, this, &JsonClientConnection::disconnected);
connect(_socket, &QTcpSocket::readyRead, this, &JsonClientConnection::readRequest);
2018-12-27 23:11:32 +01:00
// create a new instance of JsonAPI
_jsonAPI = new JsonAPI(socket->peerAddress().toString(), _log, this);
// get the callback messages from JsonAPI and send it to the client
connect(_jsonAPI,SIGNAL(callbackMessage(QJsonObject)),this,SLOT(sendMessage(QJsonObject)));
}
void JsonClientConnection::readRequest()
{
_receiveBuffer += _socket->readAll();
2019-01-27 13:41:21 +01:00
// might be an old hyperion classic handshake request or raw socket data
if(_receiveBuffer.contains("Upgrade: websocket"))
{
if(_websocketClient == Q_NULLPTR)
{
// disconnect this slot from socket for further requests
disconnect(_socket, &QTcpSocket::readyRead, this, &JsonClientConnection::readRequest);
int start = _receiveBuffer.indexOf("Sec-WebSocket-Key") + 19;
QByteArray header(_receiveBuffer.mid(start, _receiveBuffer.indexOf("\r\n", start) - start).data());
_websocketClient = new WebSocketClient(header, _socket, this);
}
}
else
{
2019-01-27 13:41:21 +01:00
// raw socket data, handling as usual
int bytes = _receiveBuffer.indexOf('\n') + 1;
while(bytes > 0)
{
// create message string
QString message(QByteArray(_receiveBuffer.data(), bytes));
2019-01-27 13:41:21 +01:00
// remove message data from buffer
_receiveBuffer = _receiveBuffer.mid(bytes);
2019-01-27 13:41:21 +01:00
// handle message
_jsonAPI->handleMessage(message);
2019-01-27 13:41:21 +01:00
// try too look up '\n' again
bytes = _receiveBuffer.indexOf('\n') + 1;
}
}
}
qint64 JsonClientConnection::sendMessage(QJsonObject message)
{
QJsonDocument writer(message);
QByteArray data = writer.toJson(QJsonDocument::Compact) + "\n";
if (!_socket || (_socket->state() != QAbstractSocket::ConnectedState)) return 0;
return _socket->write(data.data(), data.size());
}
void JsonClientConnection::disconnected(void)
{
emit connectionClosed();
}