283 lines
5.6 KiB
C++
Raw Normal View History

2019-02-13 13:03:40 +03:00
#include "client.h"
2019-02-15 23:36:59 +03:00
#include <QRegularExpression>
2019-02-13 13:03:40 +03:00
#include <QTcpSocket>
2019-02-15 23:36:59 +03:00
#include <QVariantMap>
2019-03-01 07:56:15 +03:00
#include <QDateTime>
2019-03-01 15:26:45 +03:00
#include <quasarapp.h>
2019-06-13 12:53:35 +03:00
#include <qrsaencryption.h>
#include <pubkey.h>
2019-03-05 00:48:32 +03:00
#include "factorynetobjects.h"
2019-04-29 14:39:48 +03:00
#include "gamedata.h"
#include "getitem.h"
#include "login.h"
#include "updateplayerdata.h"
2019-02-13 13:03:40 +03:00
namespace ClientProtocol {
2019-04-29 14:39:48 +03:00
bool Client::receiveData(const QByteArray &obj, Header hdr) {
2019-04-29 14:39:48 +03:00
auto command = static_cast<Command>(hdr.command);
auto type = static_cast<Type>(hdr.type);
int index = hdr.sig;
2019-06-13 12:53:35 +03:00
if (command == Command::PubKey && !_rsaKey.size()) {
2019-06-14 10:54:37 +03:00
PubKey data;
2019-06-13 12:53:35 +03:00
data.fromBytes(obj);
return setRSAKey(data.getKey());;
}
2019-03-01 07:56:15 +03:00
if (index < 0 || index > 255)
2019-03-01 15:26:45 +03:00
return false;
2019-03-01 07:56:15 +03:00
2019-03-02 13:29:21 +03:00
#define idx static_cast<quint8>(index)
2019-03-01 15:26:45 +03:00
2019-04-29 14:39:48 +03:00
auto expectedCommand = static_cast<Command>(
_requestsMap[idx].value("expected",
static_cast<quint8>(Command::Undefined)).toInt());
if (expectedCommand == Command::Undefined ||
(command != expectedCommand) ||
type != Type::Responke) {
2019-03-05 00:48:32 +03:00
2019-03-01 15:26:45 +03:00
QuasarAppUtils::Params::verboseLog("wrong sig of package");
return false;
}
2019-04-29 14:39:48 +03:00
_requestsMap[idx]["time"] = QDateTime::currentMSecsSinceEpoch();
2019-03-01 07:56:15 +03:00
2019-04-29 14:39:48 +03:00
if (expectedCommand != Command::Undefined &&
(command == expectedCommand) && type == Type::Responke) {
2019-03-01 07:56:15 +03:00
2019-06-13 12:53:35 +03:00
setLoginStatus(expectedCommand == Command::Login ||
2019-04-29 14:39:48 +03:00
expectedCommand == Command::GetItem ||
expectedCommand == Command::GameData);
2019-03-01 07:56:15 +03:00
}
2019-04-29 14:39:48 +03:00
emit sigIncommingData(static_cast<Command>(hdr.command), obj);
2019-03-01 15:26:45 +03:00
return true;
2019-03-01 07:56:15 +03:00
}
2019-06-13 12:53:35 +03:00
bool Client::setRSAKey(const QByteArray& key) {
bool newStatus = QRSAEncryption::isValidRsaKey(key);
2019-03-01 07:56:15 +03:00
if (newStatus != _online) {
2019-06-13 12:53:35 +03:00
_rsaKey = key;
2019-03-01 07:56:15 +03:00
_online = newStatus;
emit onlineChanged(_online);
}
2019-06-13 12:53:35 +03:00
return newStatus;
}
void Client::setLoginStatus(bool newStatus) {
if (newStatus != _logined) {
_logined = newStatus;
emit loginChanged(_logined);
}
}
2019-02-13 13:03:40 +03:00
void Client::incommingData() {
auto array = _destination->readAll();
if (_downloadPackage.hdr.isValid()) {
_downloadPackage.data.append(array);
} else {
memcpy(&_downloadPackage.hdr,
2019-02-14 14:09:17 +03:00
array.data(), sizeof(Header));
_downloadPackage.data.append(array.mid(sizeof(Header)));
2019-02-13 13:03:40 +03:00
}
if (_downloadPackage.isValid()) {
2019-06-14 10:54:37 +03:00
if (!receiveData(_downloadPackage.data, _downloadPackage.hdr)) {
2019-03-01 15:26:45 +03:00
// ban
2019-02-15 23:36:59 +03:00
}
2019-02-13 13:03:40 +03:00
_downloadPackage.reset();
return;
}
}
2019-05-26 13:52:16 +03:00
Client::Client(const QString &addrress, unsigned short port, QObject *ptr):
2019-02-13 13:03:40 +03:00
QObject (ptr) {
_destination = new QTcpSocket(this);
2019-05-26 13:52:16 +03:00
_destination->connectToHost(addrress, port);
2019-02-13 13:03:40 +03:00
connect(_destination, &QTcpSocket::readyRead,
this, &Client::incommingData);
}
2019-03-02 13:29:21 +03:00
bool Client::sendPackage(Package &pkg) {
2019-02-13 13:03:40 +03:00
if (!pkg.isValid()) {
return false;
}
if (!_destination->isValid()) {
qCritical() << "destination server not valid!";
return false;
}
if (!_destination->waitForConnected()) {
qCritical() << "no connected to server! " << _destination->errorString();
return false;
}
2019-03-02 13:29:21 +03:00
auto index = nextIndex();
2019-03-05 00:48:32 +03:00
_requestsMap[index] = {{"expected", static_cast<const unsigned short>(pkg.hdr.command)}};
2019-03-02 13:29:21 +03:00
pkg.hdr.sig = index;
2019-02-13 13:03:40 +03:00
2019-04-29 14:39:48 +03:00
QByteArray bytes = pkg.toBytes();
2019-03-01 15:26:45 +03:00
bool sendet = bytes.size() == _destination->write(bytes);
return sendet;
2019-02-13 13:03:40 +03:00
}
2019-03-02 13:29:21 +03:00
unsigned char Client::nextIndex() {
return static_cast<unsigned char>((currentIndex++) % 256);
}
2019-03-05 00:48:32 +03:00
bool Client::ping() {
Package pcg;
2019-04-29 14:39:48 +03:00
if (!pcg.create(Command::Ping, Type::Request)) {
2019-03-05 00:48:32 +03:00
return false;
};
if (!sendPackage(pcg)) {
return false;
}
return true;
}
2019-02-15 23:36:59 +03:00
bool Client::login(const QString &gmail, const QByteArray &pass) {
if (!pass.size()) {
return false;
}
2019-02-13 14:28:42 +03:00
2019-02-15 23:36:59 +03:00
if (!gmail.size()) {
return false;
}
Package pcg;
2019-03-05 00:48:32 +03:00
2019-04-29 14:39:48 +03:00
Login login;
login.setHashPass(pass);
login.setGmail(gmail);
2019-04-29 17:28:01 +03:00
login.setId(0);
if (!login.isValid()) {
return false;
}
2019-04-29 14:39:48 +03:00
if (!pcg.create(&login, Type::Request)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
return true;
}
bool Client::updateData() {
2019-06-14 10:54:37 +03:00
if (!isLogin()) {
return false;
}
Package pcg;
2019-03-05 00:48:32 +03:00
2019-04-29 14:39:48 +03:00
UpdatePlayerData rec;
rec.setToken(_token);
2019-04-29 17:28:01 +03:00
rec.setId(0);
if (!rec.isValid()) {
return false;
}
2019-04-29 14:39:48 +03:00
if (!pcg.create(&rec, Type::Request)) {
2019-02-15 23:36:59 +03:00
return false;
};
if (!sendPackage(pcg)) {
return false;
}
2019-02-13 14:28:42 +03:00
2019-02-15 23:36:59 +03:00
return true;
}
2019-02-13 14:28:42 +03:00
2019-03-05 00:48:32 +03:00
bool Client::savaData(const QList<int>& gameData) {
2019-06-14 10:54:37 +03:00
if (!isLogin()) {
return false;
}
Package pcg;
2019-04-29 14:39:48 +03:00
GameData rec;
rec.setToken(_token);
rec.setTimeClick(gameData);
2019-04-29 17:28:01 +03:00
rec.setId(0);
if (!rec.isValid()) {
return false;
}
2019-03-05 00:48:32 +03:00
2019-04-29 14:39:48 +03:00
if (!pcg.create(&rec, Type::Request)) {
return false;
};
if (!sendPackage(pcg)) {
return false;
}
return true;
2019-02-13 14:28:42 +03:00
}
bool Client::getItem(int id) {
2019-06-14 10:54:37 +03:00
if (!isLogin()) {
2019-02-15 23:36:59 +03:00
return false;
}
if (!id) {
return false;
}
Package pcg;
2019-03-05 00:48:32 +03:00
2019-04-29 14:39:48 +03:00
GetItem rec;
rec.setToken(_token);
rec.setId(id);
2019-04-29 17:28:01 +03:00
if (!rec.isValid()) {
return false;
}
2019-04-29 14:39:48 +03:00
if (!pcg.create(&rec, Type::Request)) {
2019-02-15 23:36:59 +03:00
return false;
};
if (!sendPackage(pcg)) {
return false;
}
2019-02-13 14:28:42 +03:00
2019-02-15 23:36:59 +03:00
return true;
2019-02-13 14:28:42 +03:00
}
2019-06-13 12:53:35 +03:00
const bool& Client::isOnline() const {
return _online;
}
2019-06-13 12:53:35 +03:00
const bool& Client::isLogin() const {
return _logined;
}
2019-02-13 13:03:40 +03:00
}