Refactor tcpserver into multiple files
This commit is contained in:
parent
e3b6d5c3a6
commit
59b55d1868
10 changed files with 486 additions and 446 deletions
100
src/service/tcpclient.cpp
Normal file
100
src/service/tcpclient.cpp
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
#include <QTcpSocket>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
#include <iostream>
|
||||
|
||||
#include "items/item.h"
|
||||
#include "service.h"
|
||||
#include "tcpclient.h"
|
||||
|
||||
TcpClient::TcpClient(QObject* parent):
|
||||
Service(parent),
|
||||
socket(new QTcpSocket(this))
|
||||
{
|
||||
connect(socket, &QTcpSocket::readyRead, this, &TcpClient::socketReadyRead);
|
||||
}
|
||||
|
||||
void TcpClient::sendJson(const QJsonObject& json)
|
||||
{
|
||||
QByteArray jsonData = QJsonDocument(json).toJson();
|
||||
socket->write(QString("MSG JSON LEN " + QString::number(jsonData.size()) + "\n").toLatin1() + jsonData);
|
||||
}
|
||||
|
||||
bool TcpClient::launch(const QHostAddress &address, quint16 port)
|
||||
{
|
||||
socket->connectToHost(address, port);
|
||||
return socket->waitForConnected(2000);
|
||||
}
|
||||
|
||||
void TcpClient::processIncomeingJson(const QByteArray& jsonbytes)
|
||||
{
|
||||
QJsonDocument doc = QJsonDocument::fromJson(jsonbytes);
|
||||
QJsonObject json = doc.object();
|
||||
QString type = json["MessageType"].toString();
|
||||
if(type == "ItemUpdate")
|
||||
{
|
||||
std::cout<<"Got item json:\n"<<QString::fromLatin1(jsonbytes).toStdString();
|
||||
QJsonArray data = json["Data"].toArray();
|
||||
std::vector<std::shared_ptr<Item>> items;
|
||||
for(QJsonValueRef itemjson : data)
|
||||
{
|
||||
QJsonObject jsonobject = itemjson.toObject();
|
||||
std::shared_ptr<Item> item = Item::loadItem(jsonobject);
|
||||
if(item)
|
||||
{
|
||||
item->setLoaded(false);
|
||||
items.push_back(item);
|
||||
}
|
||||
}
|
||||
if(!items.empty())
|
||||
gotItems(items, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Service::processIncomeingJson(jsonbytes);
|
||||
}
|
||||
}
|
||||
|
||||
void TcpClient::processComand(const QByteArray& command)
|
||||
{
|
||||
if(command.startsWith("MSG JSON LEN "))
|
||||
{
|
||||
state = STATE_RECV_JSON;
|
||||
recievebytes = command.mid(13).toLongLong();
|
||||
}
|
||||
}
|
||||
|
||||
void TcpClient::socketReadyRead()
|
||||
{
|
||||
buffer += socket->readAll();
|
||||
bool remianing = true;
|
||||
while(remianing)
|
||||
{
|
||||
remianing = false;
|
||||
while(state == STATE_IDLE && buffer.contains('\n'))
|
||||
{
|
||||
size_t newlineIndex = buffer.indexOf('\n');
|
||||
QByteArray command = buffer.left(newlineIndex);
|
||||
buffer.remove(0, newlineIndex+1);
|
||||
processComand(command);
|
||||
remianing = true;
|
||||
}
|
||||
if(state == STATE_RECV_JSON)
|
||||
{
|
||||
if(recievebytes <= buffer.size())
|
||||
{
|
||||
QByteArray json = buffer.left(recievebytes);
|
||||
buffer.remove(0, recievebytes);
|
||||
recievebytes = 0;
|
||||
state = STATE_IDLE;
|
||||
processIncomeingJson(json);
|
||||
remianing = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TcpClient::~TcpClient()
|
||||
{
|
||||
delete socket;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue