2013-08-17 19:20:19 +02:00
|
|
|
// project includes
|
2013-08-17 15:39:29 +02:00
|
|
|
#include "JsonClientConnection.h"
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <api/JsonAPI.h>
|
|
|
|
|
|
|
|
// qt inc
|
2017-11-20 00:06:45 +01:00
|
|
|
#include <QTcpSocket>
|
2018-12-27 23:11:32 +01:00
|
|
|
#include <QHostAddress>
|
2017-08-06 21:50:30 +02:00
|
|
|
|
2020-08-08 13:09:15 +02:00
|
|
|
JsonClientConnection::JsonClientConnection(QTcpSocket *socket, bool localConnection)
|
2016-07-31 22:21:35 +02:00
|
|
|
: QObject()
|
|
|
|
, _socket(socket)
|
|
|
|
, _receiveBuffer()
|
2016-11-26 22:46:16 +01:00
|
|
|
, _log(Logger::getInstance("JSONCLIENTCONNECTION"))
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2017-11-20 00:06:45 +01:00
|
|
|
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
|
2019-07-12 16:54:26 +02:00
|
|
|
_jsonAPI = new JsonAPI(socket->peerAddress().toString(), _log, localConnection, this);
|
2018-12-27 23:11:32 +01:00
|
|
|
// get the callback messages from JsonAPI and send it to the client
|
2019-09-17 21:33:46 +02:00
|
|
|
connect(_jsonAPI, &JsonAPI::callbackMessage, this , &JsonClientConnection::sendMessage);
|
|
|
|
connect(_jsonAPI, &JsonAPI::forceClose, this , [&](){ _socket->close(); } );
|
|
|
|
|
|
|
|
_jsonAPI->initialize();
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
2017-11-20 00:06:45 +01:00
|
|
|
void JsonClientConnection::readRequest()
|
2017-09-16 09:08:21 +02:00
|
|
|
{
|
2017-10-12 11:55:03 +02:00
|
|
|
_receiveBuffer += _socket->readAll();
|
2019-07-12 16:54:26 +02:00
|
|
|
// raw socket data, handling as usual
|
|
|
|
int bytes = _receiveBuffer.indexOf('\n') + 1;
|
|
|
|
while(bytes > 0)
|
2014-11-08 21:01:46 +01:00
|
|
|
{
|
2019-07-12 16:54:26 +02:00
|
|
|
// create message string
|
|
|
|
QString message(QByteArray(_receiveBuffer.data(), bytes));
|
2014-10-25 22:35:53 +02:00
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
// remove message data from buffer
|
|
|
|
_receiveBuffer = _receiveBuffer.mid(bytes);
|
2014-10-25 22:35:53 +02:00
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
// handle message
|
|
|
|
_jsonAPI->handleMessage(message);
|
2014-10-25 22:35:53 +02:00
|
|
|
|
2019-07-12 16:54:26 +02:00
|
|
|
// try too look up '\n' again
|
|
|
|
bytes = _receiveBuffer.indexOf('\n') + 1;
|
2014-10-25 22:35:53 +02:00
|
|
|
}
|
2013-08-17 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 21:50:30 +02:00
|
|
|
qint64 JsonClientConnection::sendMessage(QJsonObject message)
|
2013-08-17 15:39:29 +02:00
|
|
|
{
|
2016-10-09 22:22:17 +02:00
|
|
|
QJsonDocument writer(message);
|
2017-11-20 00:06:45 +01:00
|
|
|
QByteArray data = writer.toJson(QJsonDocument::Compact) + "\n";
|
2017-06-24 11:52:22 +02:00
|
|
|
|
2017-08-06 21:50:30 +02:00
|
|
|
if (!_socket || (_socket->state() != QAbstractSocket::ConnectedState)) return 0;
|
|
|
|
return _socket->write(data.data(), data.size());
|
|
|
|
}
|
|
|
|
|
2020-08-08 13:09:15 +02:00
|
|
|
void JsonClientConnection::disconnected()
|
2017-08-06 21:50:30 +02:00
|
|
|
{
|
2017-11-20 00:06:45 +01:00
|
|
|
emit connectionClosed();
|
2017-09-16 09:08:21 +02:00
|
|
|
}
|