mirror of
https://github.com/QuasarApp/Snake.git
synced 2025-04-28 18:54:40 +00:00
57 lines
1.3 KiB
C++
57 lines
1.3 KiB
C++
#include "client.h"
|
|
#include <QTcpSocket>
|
|
|
|
namespace ClientProtocol {
|
|
|
|
void Client::incommingData() {
|
|
auto array = _destination->readAll();
|
|
|
|
if (_downloadPackage.hdr.isValid()) {
|
|
_downloadPackage.data.append(array);
|
|
|
|
} else {
|
|
memcpy(&_downloadPackage.hdr,
|
|
array.data(), sizeof(unsigned char));
|
|
_downloadPackage.data.append(array.mid(1));
|
|
}
|
|
|
|
if (_downloadPackage.isValid()) {
|
|
emit sigIncommingData(_downloadPackage.parse());
|
|
_downloadPackage.reset();
|
|
return;
|
|
}
|
|
}
|
|
|
|
Client::Client(QObject *ptr):
|
|
QObject (ptr) {
|
|
|
|
_destination = new QTcpSocket(this);
|
|
|
|
_destination->connectToHost(DEFAULT_SNAKE_SERVER, DEFAULT_SNAKE_PORT);
|
|
|
|
connect(_destination, &QTcpSocket::readyRead,
|
|
this, &Client::incommingData);
|
|
}
|
|
|
|
bool Client::sendPackage(const Package &pkg) {
|
|
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;
|
|
}
|
|
|
|
auto bytes = pkg.toBytes();
|
|
|
|
return bytes.size() == _destination->write(bytes);
|
|
}
|
|
|
|
}
|