forked from facebookarchive/RakNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadyEvent.cpp
572 lines (526 loc) · 17 KB
/
ReadyEvent.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
* Copyright (c) 2014, Oculus VR, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include "NativeFeatureIncludes.h"
#if _RAKNET_SUPPORT_ReadyEvent==1
#include "ReadyEvent.h"
#include "RakPeerInterface.h"
#include "BitStream.h"
#include "MessageIdentifiers.h"
#include "RakAssert.h"
#ifdef _MSC_VER
#pragma warning( push )
#endif
using namespace RakNet;
int RakNet::ReadyEvent::RemoteSystemCompByGuid( const RakNetGUID &key, const RemoteSystem &data )
{
if (key < data.rakNetGuid)
return -1;
else if (key==data.rakNetGuid)
return 0;
else
return 1;
}
int RakNet::ReadyEvent::ReadyEventNodeComp( const int &key, ReadyEvent::ReadyEventNode * const &data )
{
if (key < data->eventId)
return -1;
else if (key==data->eventId)
return 0;
else
return 1;
}
STATIC_FACTORY_DEFINITIONS(ReadyEvent,ReadyEvent);
ReadyEvent::ReadyEvent()
{
channel=0;
}
ReadyEvent::~ReadyEvent()
{
Clear();
}
bool ReadyEvent::SetEvent(int eventId, bool isReady)
{
bool objectExists;
unsigned eventIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists==false)
{
// Totally new event
CreateNewEvent(eventId, isReady);
}
else
{
return SetEventByIndex(eventIndex, isReady);
}
return true;
}
void ReadyEvent::ForceCompletion(int eventId)
{
bool objectExists;
unsigned eventIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists==false)
{
// Totally new event
CreateNewEvent(eventId, true);
eventIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
}
ReadyEventNode *ren = readyEventNodeList[eventIndex];
ren->eventStatus=ID_READY_EVENT_FORCE_ALL_SET;
UpdateReadyStatus(eventIndex);
}
bool ReadyEvent::DeleteEvent(int eventId)
{
bool objectExists;
unsigned eventIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
RakNet::OP_DELETE(readyEventNodeList[eventIndex], _FILE_AND_LINE_);
readyEventNodeList.RemoveAtIndex(eventIndex);
return true;
}
return false;
}
bool ReadyEvent::IsEventSet(int eventId)
{
bool objectExists;
unsigned eventIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
return readyEventNodeList[eventIndex]->eventStatus==ID_READY_EVENT_SET || readyEventNodeList[eventIndex]->eventStatus==ID_READY_EVENT_ALL_SET;
}
return false;
}
bool ReadyEvent::IsEventCompletionProcessing(int eventId) const
{
bool objectExists;
unsigned eventIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
bool anyAllReady=false;
bool allAllReady=true;
ReadyEventNode *ren = readyEventNodeList[eventIndex];
if (ren->eventStatus==ID_READY_EVENT_FORCE_ALL_SET)
return false;
for (unsigned i=0; i < ren->systemList.Size(); i++)
{
if (ren->systemList[i].lastReceivedStatus==ID_READY_EVENT_ALL_SET)
anyAllReady=true;
else
allAllReady=false;
}
return anyAllReady==true && allAllReady==false;
}
return false;
}
bool ReadyEvent::IsEventCompleted(int eventId) const
{
bool objectExists;
unsigned eventIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
return IsEventCompletedByIndex(eventIndex);
}
return false;
}
bool ReadyEvent::HasEvent(int eventId)
{
return readyEventNodeList.HasData(eventId);
}
unsigned ReadyEvent::GetEventListSize(void) const
{
return readyEventNodeList.Size();
}
int ReadyEvent::GetEventAtIndex(unsigned index) const
{
return readyEventNodeList[index]->eventId;
}
bool ReadyEvent::AddToWaitList(int eventId, RakNetGUID guid)
{
bool eventExists;
unsigned eventIndex = readyEventNodeList.GetIndexFromKey(eventId, &eventExists);
if (eventExists==false)
eventIndex=CreateNewEvent(eventId, false);
// Don't do this, otherwise if we are trying to start a 3 player game, it will not allow the 3rd player to hit ready if the first two players have already done so
//if (IsLocked(eventIndex))
// return false; // Not in the list, but event is already completed, or is starting to complete, and adding more waiters would fail this.
unsigned i;
unsigned numAdded=0;
if (guid==UNASSIGNED_RAKNET_GUID)
{
for (i=0; i < rakPeerInterface->GetMaximumNumberOfPeers(); i++)
{
RakNetGUID firstGuid = rakPeerInterface->GetGUIDFromIndex(i);
if (firstGuid!=UNASSIGNED_RAKNET_GUID)
{
numAdded+=AddToWaitListInternal(eventIndex, firstGuid);
}
}
}
else
{
numAdded=AddToWaitListInternal(eventIndex, guid);
}
if (numAdded>0)
UpdateReadyStatus(eventIndex);
return numAdded>0;
}
bool ReadyEvent::RemoveFromWaitList(int eventId, RakNetGUID guid)
{
bool eventExists;
unsigned eventIndex = readyEventNodeList.GetIndexFromKey(eventId, &eventExists);
if (eventExists)
{
if (guid==UNASSIGNED_RAKNET_GUID)
{
// Remove all waiters
readyEventNodeList[eventIndex]->systemList.Clear(false, _FILE_AND_LINE_);
UpdateReadyStatus(eventIndex);
}
else
{
bool systemExists;
unsigned systemIndex = readyEventNodeList[eventIndex]->systemList.GetIndexFromKey(guid, &systemExists);
if (systemExists)
{
bool isCompleted = IsEventCompletedByIndex(eventIndex);
readyEventNodeList[eventIndex]->systemList.RemoveAtIndex(systemIndex);
if (isCompleted==false && IsEventCompletedByIndex(eventIndex))
PushCompletionPacket(readyEventNodeList[eventIndex]->eventId);
UpdateReadyStatus(eventIndex);
return true;
}
}
}
return false;
}
bool ReadyEvent::IsInWaitList(int eventId, RakNetGUID guid)
{
bool objectExists;
unsigned readyIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
return readyEventNodeList[readyIndex]->systemList.HasData(guid);
}
return false;
}
unsigned ReadyEvent::GetRemoteWaitListSize(int eventId) const
{
bool objectExists;
unsigned readyIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
return readyEventNodeList[readyIndex]->systemList.Size();
}
return 0;
}
RakNetGUID ReadyEvent::GetFromWaitListAtIndex(int eventId, unsigned index) const
{
bool objectExists;
unsigned readyIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
return readyEventNodeList[readyIndex]->systemList[index].rakNetGuid;
}
return UNASSIGNED_RAKNET_GUID;
}
ReadyEventSystemStatus ReadyEvent::GetReadyStatus(int eventId, RakNetGUID guid)
{
bool objectExists;
unsigned readyIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
ReadyEventNode *ren = readyEventNodeList[readyIndex];
unsigned systemIndex = ren->systemList.GetIndexFromKey(guid, &objectExists);
if (objectExists==false)
return RES_NOT_WAITING;
if (ren->systemList[systemIndex].lastReceivedStatus==ID_READY_EVENT_SET)
return RES_READY;
if (ren->systemList[systemIndex].lastReceivedStatus==ID_READY_EVENT_UNSET)
return RES_WAITING;
if (ren->systemList[systemIndex].lastReceivedStatus==ID_READY_EVENT_ALL_SET)
return RES_ALL_READY;
}
return RES_UNKNOWN_EVENT;
}
void ReadyEvent::SetSendChannel(unsigned char newChannel)
{
channel=newChannel;
}
PluginReceiveResult ReadyEvent::OnReceive(Packet *packet)
{
unsigned char packetIdentifier;
packetIdentifier = ( unsigned char ) packet->data[ 0 ];
// bool doPrint = packet->systemAddress.GetPort()==60002 || rakPeerInterface->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS).GetPort()==60002;
switch (packetIdentifier)
{
case ID_READY_EVENT_UNSET:
case ID_READY_EVENT_SET:
case ID_READY_EVENT_ALL_SET:
// if (doPrint) {if (packet->systemAddress.GetPort()==60002) RAKNET_DEBUG_PRINTF("FROM 60002: "); else if (rakPeerInterface->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS).port==60002) RAKNET_DEBUG_PRINTF("TO 60002: "); RAKNET_DEBUG_PRINTF("ID_READY_EVENT_SET\n");}
OnReadyEventPacketUpdate(packet);
return RR_CONTINUE_PROCESSING;
case ID_READY_EVENT_FORCE_ALL_SET:
OnReadyEventForceAllSet(packet);
return RR_CONTINUE_PROCESSING;
case ID_READY_EVENT_QUERY:
// if (doPrint) {if (packet->systemAddress.GetPort()==60002) RAKNET_DEBUG_PRINTF("FROM 60002: "); else if (rakPeerInterface->GetInternalID(UNASSIGNED_SYSTEM_ADDRESS).port==60002) RAKNET_DEBUG_PRINTF("TO 60002: "); RAKNET_DEBUG_PRINTF("ID_READY_EVENT_QUERY\n");}
OnReadyEventQuery(packet);
return RR_STOP_PROCESSING_AND_DEALLOCATE;
}
return RR_CONTINUE_PROCESSING;
}
bool ReadyEvent::AddToWaitListInternal(unsigned eventIndex, RakNetGUID guid)
{
ReadyEventNode *ren = readyEventNodeList[eventIndex];
bool objectExists;
unsigned systemIndex = ren->systemList.GetIndexFromKey(guid, &objectExists);
if (objectExists==false)
{
RemoteSystem rs;
rs.lastReceivedStatus=ID_READY_EVENT_UNSET;
rs.lastSentStatus=ID_READY_EVENT_UNSET;
rs.rakNetGuid=guid;
ren->systemList.InsertAtIndex(rs,systemIndex, _FILE_AND_LINE_);
SendReadyStateQuery(ren->eventId, guid);
return true;
}
return false;
}
void ReadyEvent::OnReadyEventForceAllSet(Packet *packet)
{
RakNet::BitStream incomingBitStream(packet->data, packet->length, false);
incomingBitStream.IgnoreBits(8);
int eventId;
incomingBitStream.Read(eventId);
bool objectExists;
unsigned readyIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
ReadyEventNode *ren = readyEventNodeList[readyIndex];
if (ren->eventStatus!=ID_READY_EVENT_FORCE_ALL_SET)
{
ren->eventStatus=ID_READY_EVENT_FORCE_ALL_SET;
PushCompletionPacket(ren->eventId);
}
}
}
void ReadyEvent::OnReadyEventPacketUpdate(Packet *packet)
{
RakNet::BitStream incomingBitStream(packet->data, packet->length, false);
incomingBitStream.IgnoreBits(8);
int eventId;
incomingBitStream.Read(eventId);
bool objectExists;
unsigned readyIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
ReadyEventNode *ren = readyEventNodeList[readyIndex];
bool systemExists;
unsigned systemIndex = ren->systemList.GetIndexFromKey(packet->guid, &systemExists);
if (systemExists)
{
// Just return if no change
if (ren->systemList[systemIndex].lastReceivedStatus==packet->data[0])
return;
bool wasCompleted = IsEventCompletedByIndex(readyIndex);
ren->systemList[systemIndex].lastReceivedStatus=packet->data[0];
// If forced all set, doesn't matter what the new packet is
if (ren->eventStatus==ID_READY_EVENT_FORCE_ALL_SET)
return;
UpdateReadyStatus(readyIndex);
if (wasCompleted==false && IsEventCompletedByIndex(readyIndex))
PushCompletionPacket(readyIndex);
}
}
}
void ReadyEvent::OnReadyEventQuery(Packet *packet)
{
RakNet::BitStream incomingBitStream(packet->data, packet->length, false);
incomingBitStream.IgnoreBits(8);
int eventId;
incomingBitStream.Read(eventId);
bool objectExists;
unsigned readyIndex = readyEventNodeList.GetIndexFromKey(eventId, &objectExists);
if (objectExists)
{
unsigned systemIndex = readyEventNodeList[readyIndex]->systemList.GetIndexFromKey(packet->guid,&objectExists);
// Force the non-default send, because our initial send may have arrived at a system that didn't yet create the ready event
if (objectExists)
SendReadyUpdate(readyIndex, systemIndex, true);
}
}
void ReadyEvent::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
{
(void) systemAddress;
(void) rakNetGUID;
(void) lostConnectionReason;
RemoveFromAllLists(rakNetGUID);
}
void ReadyEvent::OnRakPeerShutdown(void)
{
Clear();
}
bool ReadyEvent::SetEventByIndex(int eventIndex, bool isReady)
{
ReadyEventNode *ren = readyEventNodeList[eventIndex];
if ((ren->eventStatus==ID_READY_EVENT_ALL_SET || ren->eventStatus==ID_READY_EVENT_SET) && isReady==true)
return false; // Success - no change
if (ren->eventStatus==ID_READY_EVENT_UNSET && isReady==false)
return false; // Success - no change
if (ren->eventStatus==ID_READY_EVENT_FORCE_ALL_SET)
return false; // Can't change
if (isReady)
ren->eventStatus=ID_READY_EVENT_SET;
else
ren->eventStatus=ID_READY_EVENT_UNSET;
UpdateReadyStatus(eventIndex);
// Check if now completed, and if so, tell the user about it
if (IsEventCompletedByIndex(eventIndex))
{
PushCompletionPacket(ren->eventId);
}
return true;
}
bool ReadyEvent::IsEventCompletedByIndex(unsigned eventIndex) const
{
ReadyEventNode *ren = readyEventNodeList[eventIndex];
unsigned i;
if (ren->eventStatus==ID_READY_EVENT_FORCE_ALL_SET)
return true;
if (ren->eventStatus!=ID_READY_EVENT_ALL_SET)
return false;
for (i=0; i < ren->systemList.Size(); i++)
if (ren->systemList[i].lastReceivedStatus!=ID_READY_EVENT_ALL_SET)
return false;
return true;
}
void ReadyEvent::Clear(void)
{
unsigned i;
for (i=0; i < readyEventNodeList.Size(); i++)
{
RakNet::OP_DELETE(readyEventNodeList[i], _FILE_AND_LINE_);
}
readyEventNodeList.Clear(false, _FILE_AND_LINE_);
}
unsigned ReadyEvent::CreateNewEvent(int eventId, bool isReady)
{
ReadyEventNode *ren = RakNet::OP_NEW<ReadyEventNode>( _FILE_AND_LINE_ );
ren->eventId=eventId;
if (isReady==false)
ren->eventStatus=ID_READY_EVENT_UNSET;
else
ren->eventStatus=ID_READY_EVENT_SET;
return readyEventNodeList.Insert(eventId, ren, true, _FILE_AND_LINE_);
}
void ReadyEvent::UpdateReadyStatus(unsigned eventIndex)
{
ReadyEventNode *ren = readyEventNodeList[eventIndex];
bool anyUnset;
unsigned i;
if (ren->eventStatus==ID_READY_EVENT_SET)
{
// If you are set, and no other systems are ID_READY_EVENT_UNSET, then change your status to ID_READY_EVENT_ALL_SET
anyUnset=false;
for (i=0; i < ren->systemList.Size(); i++)
{
if (ren->systemList[i].lastReceivedStatus==ID_READY_EVENT_UNSET)
{
anyUnset=true;
break;
}
}
if (anyUnset==false)
{
ren->eventStatus=ID_READY_EVENT_ALL_SET;
}
}
else if (ren->eventStatus==ID_READY_EVENT_ALL_SET)
{
// If you are all set, and any systems are ID_READY_EVENT_UNSET, then change your status to ID_READY_EVENT_SET
anyUnset=false;
for (i=0; i < ren->systemList.Size(); i++)
{
if (ren->systemList[i].lastReceivedStatus==ID_READY_EVENT_UNSET)
{
anyUnset=true;
break;
}
}
if (anyUnset==true)
{
ren->eventStatus=ID_READY_EVENT_SET;
}
}
BroadcastReadyUpdate(eventIndex, false);
}
void ReadyEvent::SendReadyUpdate(unsigned eventIndex, unsigned systemIndex, bool forceIfNotDefault)
{
ReadyEventNode *ren = readyEventNodeList[eventIndex];
RakNet::BitStream bs;
// I do this rather than write true or false, so users that do not use BitStreams can still read the data
if ((ren->eventStatus!=ren->systemList[systemIndex].lastSentStatus) ||
(forceIfNotDefault && ren->eventStatus!=ID_READY_EVENT_UNSET))
{
bs.Write(ren->eventStatus);
bs.Write(ren->eventId);
SendUnified(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, channel, ren->systemList[systemIndex].rakNetGuid, false);
ren->systemList[systemIndex].lastSentStatus=ren->eventStatus;
}
}
void ReadyEvent::BroadcastReadyUpdate(unsigned eventIndex, bool forceIfNotDefault)
{
ReadyEventNode *ren = readyEventNodeList[eventIndex];
unsigned systemIndex;
for (systemIndex=0; systemIndex < ren->systemList.Size(); systemIndex++)
{
SendReadyUpdate(eventIndex, systemIndex, forceIfNotDefault);
}
}
void ReadyEvent::SendReadyStateQuery(unsigned eventId, RakNetGUID guid)
{
RakNet::BitStream bs;
bs.Write((MessageID)ID_READY_EVENT_QUERY);
bs.Write(eventId);
SendUnified(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, channel, guid, false);
}
void ReadyEvent::RemoveFromAllLists(RakNetGUID guid)
{
unsigned eventIndex;
for (eventIndex=0; eventIndex < readyEventNodeList.Size(); eventIndex++)
{
bool isCompleted = IsEventCompletedByIndex(eventIndex);
bool systemExists;
unsigned systemIndex;
systemIndex = readyEventNodeList[eventIndex]->systemList.GetIndexFromKey(guid, &systemExists);
if (systemExists)
readyEventNodeList[eventIndex]->systemList.RemoveAtIndex(systemIndex);
UpdateReadyStatus(eventIndex);
if (isCompleted==false && IsEventCompletedByIndex(eventIndex))
PushCompletionPacket(readyEventNodeList[eventIndex]->eventId);
}
}
void ReadyEvent::PushCompletionPacket(unsigned eventId)
{
(void) eventId;
// Not necessary
/*
// Pass a packet to the user that we are now completed, as setting ourselves to signaled was the last thing being waited on
Packet *p = AllocatePacketUnified(sizeof(MessageID)+sizeof(int));
RakNet::BitStream bs(p->data, sizeof(MessageID)+sizeof(int), false);
bs.SetWriteOffset(0);
bs.Write((MessageID)ID_READY_EVENT_ALL_SET);
bs.Write(eventId);
rakPeerInterface->PushBackPacket(p, false);
*/
}
#ifdef _MSC_VER
#pragma warning( pop )
#endif
#endif // _RAKNET_SUPPORT_*