-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserversocket.cpp
130 lines (120 loc) · 4.12 KB
/
serversocket.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "serversocket.h"
#include "packet.h"
#include "messagepacket.h"
#include "screenpacket.h"
#include "resolution.h"
#include "util.h"
#include <QList>
#include <QDebug>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include "scenematrix.h"
#include "confirmpassworddialog.h"
#include <QCryptographicHash>
#include "mouseeventspacket.h"
#include <iostream>
#include <QApplication>
using namespace DG;
ServerSocket::ServerSocket(QGraphicsScene* scene, QObject* parent):CommonSocket(parent), _scene(scene), dlg(0x0){
connect(this, SIGNAL(msgWaiting()), this, SLOT(msgReceived()));
_matrix = new SceneMatrix(_scene);
}
ServerSocket::~ServerSocket(){
}
void ServerSocket::clientConnected(){
DG::MessagePacket* m = new DG::MessagePacket(0);
m->setMessage("hi! HellaRexer");
send(m);
state = Connected;
}
void ServerSocket::msgReceived(){
DG::Packet* p = rcv();
switch(currentState()){
case Connected:{
DG::MessagePacket* m = dynamic_cast<DG::MessagePacket*>(p);
if(m->message().startsWith("hallo")){
DG::MessagePacket* res = new DG::MessagePacket((int)Connected);
res->setMessage("welcome HellaRex 1.0.0.0");
send(res);
state = Welcome;
}
}break;
case Welcome:{
DG::MessagePacket* m = dynamic_cast<DG::MessagePacket*>(p);
if(m->message().startsWith("challange")){
dlg = new DG::ConfirmPasswordDialog;
connect(dlg, SIGNAL(confirmed(QString)), this, SLOT(confirmed(QString)));
dlg->show();
}
}break;
case Password:{
DG::MessagePacket* m = dynamic_cast<DG::MessagePacket*>(p);
if(m->message().startsWith("res")){
QList<QByteArray> parts = m->message().trimmed().split('|');
QList<QByteArray> _currentParts = parts[0].split(' ');
DG::Resolution* resolution = new DG::Resolution;
resolution->unpack(_currentParts[1]);
QList<DG::Resolution*> supportedResolutions = Resolution::parseSupportedResolutions(parts[1], ',');
prepare(resolution);
DG::MessagePacket* res = new DG::MessagePacket((int)Password);
res->setMessage("res "+resolution->pack());
send(res);
state = ResolutionAccepted;
}else if(m->message().startsWith("denied")){
if(dlg != 0x0){
dlg->show();
}
}
}break;
case ResolutionAccepted:{
DG::MessagePacket* m = dynamic_cast<DG::MessagePacket*>(p);
if(m->message().startsWith("prepared")){
QList<QByteArray> parts = m->message().trimmed().split(' ');
QList<QByteArray> _dimParts = parts[1].split('|');
quint16 rows = _dimParts[0].toInt();
quint16 cols = _dimParts[1].toInt();
_matrix->setGridDimension(rows, cols);
DG::MessagePacket* res = new DG::MessagePacket((int)ResolutionAccepted);
res->setMessage("start");
send(res);
state = Start;
}
}break;
case Start:{
state = Working;
}
case Working:{
if(lastHeaderType() == DG::Packet::ScreenPacket){
DG::ScreenPacket* s = dynamic_cast<DG::ScreenPacket*>(p);
QGraphicsPixmapItem* item = s->graphicsPixmapItem();
if(!s->dumpPixmap()){
//QApplication::beep();
}
//_scene->addItem(item);
_matrix->addItem(s->row(), s->col(), item);
DG::MessagePacket* m = new DG::MessagePacket((int)Working);
m->setMessage("ACK");
send(m);
}else if(lastHeaderType() == DG::Packet::MessagePacket){
}
}break;
}
delete p;
}
void ServerSocket::prepare(const DG::Resolution* resolution){
_scene->setSceneRect(0, 0, resolution->x(), resolution->y());
}
void ServerSocket::confirmed(const QString& pass){
DG::MessagePacket* res = new DG::MessagePacket((int)Welcome);
QString passwordChecksum = QCryptographicHash::hash(pass.toAscii(), QCryptographicHash::Md5);
QString passStr = "password|"+passwordChecksum;
res->setMessage(passStr.toAscii());
send(res);
state = Password;
}
void ServerSocket::mouseEventPacketsWaiting(DG::MouseEventsPacket* packet){
send(packet);
/*DG::MessagePacket* m = new DG::MessagePacket((int)Working);
m->setMessage("MOUSE");
send(m);*/
}