I just realized something . . .
[aesalon.git] / gui / src / data / NetworkReceiver.cpp
blob00bc335cf643b9db1ac30276f2fadf448699cada
1 #include "NetworkReceiver.h"
2 #include "NetworkReceiver.moc"
3 #include "storage/AllocEvent.h"
4 #include "storage/FreeEvent.h"
6 NetworkReceiver::NetworkReceiver(QObject* parent, QString host, quint16 port) : DataReceiver(parent), host(host), port(port) {
9 NetworkReceiver::~NetworkReceiver() {
12 void NetworkReceiver::run() {
13 QTcpSocket socket(NULL);
14 tcp_socket = &socket;
15 tcp_socket->connectToHost(host, port);
16 connect(tcp_socket, SIGNAL(readyRead()), this, SLOT(data_received()));
17 connect(tcp_socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
18 /* Start the event loop . . . */
19 exec();
22 quint64 NetworkReceiver::pop_quint64() {
23 quint64 ret = 0;
24 ret |= unprocessed.at(0);
25 ret |= quint64(quint8(unprocessed.at(1))) << 8;
26 ret |= quint64(quint8(unprocessed.at(2))) << 16;
27 ret |= quint64(quint8(unprocessed.at(3))) << 24;
28 ret |= quint64(quint8(unprocessed.at(4))) << 32;
29 ret |= quint64(quint8(unprocessed.at(5))) << 40;
30 ret |= quint64(quint8(unprocessed.at(6))) << 48;
31 ret |= quint64(quint8(unprocessed.at(7))) << 56;
32 unprocessed.remove(0, 8);
33 return ret;
36 void NetworkReceiver::data_received() {
37 QByteArray received = tcp_socket->readAll();
38 unprocessed += received;
39 while(unprocessed.size()) {
40 quint8 type_byte = unprocessed.at(0);
41 unprocessed.remove(0, 1);
42 /* If the first bit is set, then it's a block event . . . */
43 if(type_byte & 0x01) {
44 quint64 address = pop_quint64();
45 if((type_byte & 0x06) == 0) {
46 emit event_received(new AllocEvent(address, pop_quint64()));
48 else if((type_byte & 0x06) == 1) {
49 emit event_received(new FreeEvent(address));
50 emit event_received(new AllocEvent(pop_quint64(), pop_quint64()));
52 else if((type_byte & 0x06) == 2) {
53 emit event_received(new FreeEvent(address));
59 void NetworkReceiver::disconnected() {
60 emit no_more_data();
61 quit();