/
force.cpp
260 lines (206 loc) · 4.99 KB
/
force.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "force.h"
#include <QHostInfo>
#include <QDebug>
#include <QNetworkInterface>
Force::Force(QObject *parent) :
advTimer(),
op(NULL),
state(WaitingForAction),
QObject(parent)
{
selfName = QHostInfo::localHostName();
// Create initial Jedi
Jedi *j = new Jedi(0, selfName, QHostAddress(), this);
jediIndex.append(j);
jediMap[j->host()] = j;
// Create UDP listening socket
foreach(QHostAddress ia, QNetworkInterface::allAddresses()) {
if(ia != ia.LocalHost && ia.protocol() == QAbstractSocket::IPv4Protocol) {
s_out.bind(ia, udpPort, QUdpSocket::ShareAddress);
break;
}
}
s_in.bind(udpPort, QUdpSocket::ShareAddress);
connect(&s_in, SIGNAL(readyRead()), this, SLOT(processDatagramsIn()));
connect(&s_out, SIGNAL(readyRead()), this, SLOT(processDatagramsOut()));
// Advertisment timer
advTimer.setInterval(100);
connect(&advTimer, SIGNAL(timeout()), this, SLOT(advertise()));
advTimer.start();
setState(WaitingForAction);
}
Force::~Force() {
retreat();
}
void Force::processDatagramsIn() {
processDatagrams(&s_in);
}
void Force::processDatagramsOut() {
processDatagrams(&s_out);
}
void Force::processDatagrams(QUdpSocket *s) {
while(s->hasPendingDatagrams()) {
QHostAddress from;
QByteArray ba;
MessageType t;
quint8 ver, t_uint;
ba.resize(s->pendingDatagramSize());
s->readDatagram(ba.data(), ba.size(), &from);
QDataStream ds(ba);
ds.setByteOrder(QDataStream::LittleEndian);
ds >> ver >> t_uint;
t = (MessageType) t_uint;
if(ver != FORCE_VERSION) {
// Version mismatch
return;
}
if(isSelfAddress(from)) {
// From self, not interesting
return;
}
switch(t) {
case AnnouncePresence:
processAnnounce(&ds, &from);
break;
case DefyYou:
processDefy(&ds, &from);
break;
case PissedOnMyself:
processCoward(&ds, &from);
break;
case SaberMoved:
processSaber(&ds, &from);
break;
}
}
}
void Force::processAnnounce(QDataStream *ds, QHostAddress *from) {
QString name;
Jedi *j = 0;
*ds >> name;
j = jediMap[*from];
if(!j) {
j = new Jedi(jediIndex.count(), name, *from, this);
jediIndex.append(j);
jediMap[j->host()] = j;
}
emit gotJedi(j);
}
void Force::processCoward(QDataStream *ds, QHostAddress *from) {
processAnnounce(ds, from);
Jedi *j = jediMap[*from];
if(j && (state & FightBit)) {
setState(WaitingForAction);
emit gotCoward(j);
}
}
void Force::processDefy(QDataStream *ds, QHostAddress *from) {
if(state == WaitingForAction) {
Jedi *j = jediMap[*from];
if(j) {
op = j;
setState(FightingClient);
emit gotEngagedWith(j);
}
}
}
void Force::processSaber(QDataStream *ds, QHostAddress *from) {
Jedi *j;
j = jediMap[*from];
if(j) {
double x, y, z;
bool original;
ForceMessageSaber msg;
if(state & FightBit) {
*ds >> original >> x >> y >> z;
if(state == FightingServer) {
msg.x = x;
msg.y = y;
msg.z = z;
msg.original = false;
QByteArray ba = makeMessage(SaberMoved, &msg);
s_out.writeDatagram(ba, op->host(), udpPort);
}
emit gotSaberUpdate(original ? j : jediIndex[0], x, y, z);
}
}
}
bool Force::isSelfAddress(const QHostAddress a) {
foreach(QHostAddress ia, QNetworkInterface::allAddresses()) {
if(ia == a) {
return true;
}
}
return false;
}
void Force::engageWith(int id) {
if(state == WaitingForAction) {
ForceMessageDefy defy;
defy.name = selfName;
op = jediIndex[id];
if(op) {
QByteArray ba = makeMessage(DefyYou, &defy);
s_out.writeDatagram(ba, op->host(), udpPort);
setState(FightingServer);
emit gotEngagedWith(op);
}
}
}
void Force::retreat() {
if((state & FightBit) && op) {
QByteArray ba = makeMessage(PissedOnMyself);
s_out.writeDatagram(ba, op->host(), udpPort);
setState(WaitingForAction);
emit gotCoward(op);
}
}
void Force::updateSaber(double x, double y, double z) {
if(op) {
if(state & FightBit) {
ForceMessageSaber msg;
msg.x = x;
msg.y = y;
msg.z = z;
msg.original = true;
QByteArray ba = makeMessage(SaberMoved, &msg);
s_out.writeDatagram(ba, op->host(), udpPort);
}
if(state == FightingServer) {
emit gotSaberUpdate(jediIndex[0], x, y, z);
}
}
}
void Force::advertise() {
if(state == WaitingForAction) {
ForceMessagePresence msg;
msg.name = selfName;
QByteArray ba = makeMessage(AnnouncePresence, &msg);
s_out.writeDatagram(ba, QHostAddress(QHostAddress::Broadcast), udpPort);
}
}
QByteArray Force::makeMessage(const MessageType t, const ForceMessage *msg) {
ForceMessagePresence *pre_msg = 0;
ForceMessageSaber *sa_msg = 0;
QByteArray ret;
QDataStream st(&ret, QIODevice::WriteOnly);
st.setByteOrder(QDataStream::LittleEndian);
st << (quint8) FORCE_VERSION << (quint8) t;
switch(t) {
case AnnouncePresence:
case DefyYou:
pre_msg = (ForceMessagePresence*) msg;
st << pre_msg->name;
break;
case SaberMoved:
sa_msg = (ForceMessageSaber*) msg;
st << sa_msg->original << sa_msg->x << sa_msg->y << sa_msg->z;
break;
default:
break;
}
return ret;
}
void Force::setState(Force::FightState s) {
state = s;
emit stateChanged(s);
}