GekkoNet 1.0
C/C++ Peer to Peer Game Networking SDK
Loading...
Searching...
No Matches
backend.cpp
Go to the documentation of this file.
1#include "backend.h"
2
3#include <cassert>
4#include <climits>
5#include <cstring>
6
7#include "zpp/zpp_bits.h"
8
10{
11 _num_players = 0;
12 _input_size = 0;
13 _state_size = 0;
17
18 // gen magic for session
19 std::srand((unsigned int)std::time(nullptr));
20 _session_magic = std::rand();
21
23}
24
26 u8 num_players,
27 u32 input_size,
28 u32 state_size,
29 bool accept_spectator_state)
30{
31 _num_players = num_players;
32 _input_size = input_size;
33 _state_size = state_size;
34 _accept_spectator_state = accept_spectator_state;
35
36 _net_player_queue.resize(num_players);
37
38}
39
40
41bool Gekko::InputCache::IsValid(Frame current_ack, Frame current_last_input) const
42{
43 return !packets.empty()
44 && last_acked_frame == current_ack
45 && last_input_frame == current_last_input;
46}
47
49{
50 if (inputs.empty()) return;
51
52 // compute target size from ack
53 u32 target = (u32)inputs.size();
54 if (min_ack != INT_MAX) {
55 Frame oldest = last_added_input - (Frame)inputs.size() + 1;
56 Frame acked = std::max((Frame)0, min_ack - oldest + 1);
57 target = (u32)inputs.size() - std::min((u32)acked, (u32)inputs.size());
58 }
59
60 // apply safety cap
61 target = std::min(target, max_size);
62
63 while (inputs.size() > target) {
64 inputs.pop_front();
65 }
66}
67
68void Gekko::MessageSystem::AddInput(Frame input_frame, Handle player, u8 input[], bool remote)
69{
70 auto& input_q = _net_player_queue[player];
71 if (input_q.last_added_input + 1 == input_frame) {
72 input_q.last_added_input++;
73 input_q.inputs.push_back(std::make_unique<u8[]>(_input_size));
74 std::memcpy(input_q.inputs.back().get(), input, _input_size);
75 }
76
77 // discard acked inputs (local) or just cap the queue (remote)
78 Frame min_ack = remote ? (Frame)INT_MAX : GetMinLastAckedFrame(false);
80}
81
83{
84 auto& input_q = _net_spectator_queue;
85 if (input_q.last_added_input + 1 == input_frame) {
86 input_q.last_added_input++;
87 const size_t num_players = locals.size() + remotes.size();
88 input_q.inputs.push_back(std::make_unique<u8[]>(_input_size * num_players));
89 std::memcpy(input_q.inputs.back().get(), input, _input_size * num_players);
90 }
91
92 // discard acked inputs and cap the queue
94}
95
97{
98 // A late spectator must load its confirmed state before receiving inputs.
100
101 // send per-peer input packets to remotes
102 if (!remotes.empty() && !locals.empty()) {
103 for (auto& peer : remotes) {
104 SendInputsToPeer(peer.get(), host, false);
105 }
106 }
107 // check for disconnects (runs even in spectator sessions with no locals)
108 if (!remotes.empty()) {
110 }
111
112 // send per-peer input packets to spectators
113 if (!spectators.empty() && !locals.empty()) {
114 for (auto& peer : spectators) {
115 SendInputsToPeer(peer.get(), host, true);
116 }
117 }
118 // check for disconnects
119 if (!spectators.empty()) {
121 }
122
123 // notify peers about fresh disconnects
125
126 // exchange input claims for disconnected players
128
129 // drain remaining messages (acks, sync, health, etc.)
130 while (!_pending_output.empty()) {
131 auto& pkt = _pending_output.front();
132 if ((pkt->pkt.header.type == SessionHealth || pkt->pkt.header.type == NetworkHealth) && pkt->addr.GetSize() == 0) {
133 // send to remotes
134 SendDataToAll(pkt.get(), host);
135 // send to spectators
136 SendDataToAll(pkt.get(), host, true);
137 }
138 else {
139 SendDataTo(pkt.get(), host);
140 }
141 _pending_output.pop();
142 }
143}
144
146{
147 for (u32 i = 0; i < length; i++) {
148 auto res = data[i];
149 auto addr = NetAddress(res->addr.data, res->addr.size);
150
151 _bin_buffer.clear();
152 _bin_buffer.insert(_bin_buffer.begin(), (u8*)res->data, (u8*)res->data + res->data_len);
153
154 NetPacket pkt;
155 // Cap how much the archive will allocate from a length prefix. Untrusted
156 // datagrams can claim a huge size before the bytes are shown to be there,
157 // so without this a single packet can force a multi-gigabyte allocation.
158 // The default adapter receives into a 64 KiB buffer, so valid packets fit.
159 zpp::bits::in in(_bin_buffer, zpp::bits::alloc_limit<65536>{});
160
161 if (failure(in(pkt.header, pkt.body))) {
162 printf("failed to deserialize packet\n");
163 }
164 else {
165 ParsePacket(addr, pkt, res->data_len);
166 }
167
168 // cleanup :)
169 host->free_data(res->addr.data);
170 host->free_data(res->data);
171 host->free_data(res);
172 }
173}
174
176{
177 if (!addr) {
178 return;
179 }
180
181 _pending_output.push(std::make_unique<NetData>());
182 auto& message = _pending_output.back();
183
184 message->addr.Copy(addr);
185
186 message->pkt.header.type = SyncRequest;
187 message->pkt.header.magic = 0;
188
189 SyncMsg body = {};
191
192 message->pkt.body = body;
193}
194
196{
197 if (!addr || magic == 0) {
198 return;
199 }
200
201 _pending_output.push(std::make_unique<NetData>());
202 auto& message = _pending_output.back();
203
204 message->addr.Copy(addr);
205 message->pkt.header.type = SyncResponse;
206 message->pkt.header.magic = magic;
207
208 SyncMsg body = {};
210
211 message->pkt.body = body;
212}
213
215{
216 if (!addr || magic == 0) {
217 return;
218 }
219
220 _pending_output.push(std::make_unique<NetData>());
221 auto& message = _pending_output.back();
222
223 message->addr.Copy(addr);
224 message->pkt.header.type = Disconnect;
225 message->pkt.header.magic = magic;
226
227 message->pkt.body = DisconnectMsg();
228}
229
231{
232 const u64 now = TimeSinceEpoch();
233
234 std::vector<std::unique_ptr<Player>>* current = &remotes;
235 for (u32 i = 0; i < 2; i++)
236 {
237 if (i == 1) {
238 current = &spectators;
239 }
240
241 for (auto& actor : *current) {
242 if (actor->disconnect_msgs_left == 0 || actor->GetStatus() != Disconnected) {
243 continue;
244 }
245
246 // without an address or a finished handshake the message cant be delivered.
247 if (actor->address.GetSize() == 0 || actor->session_magic == 0) {
248 actor->disconnect_msgs_left = 0;
249 continue;
250 }
251
252 // dont want to spam the network with disconnect packets
253 if (actor->last_disconnect_msg_time + NetStats::DISCONNECT_MSG_DELAY > now) {
254 continue;
255 }
256
257 SendDisconnect(&actor->address, actor->session_magic);
258 actor->last_disconnect_msg_time = now;
259 actor->disconnect_msgs_left--;
260 }
261 }
262
263 for (auto iter = _pending_disconnects.begin(); iter != _pending_disconnects.end(); ) {
264 auto& pending = *iter;
265
266 if (pending->messages_left == 0 || pending->address.GetSize() == 0 ||
267 pending->session_magic == 0) {
268 iter = _pending_disconnects.erase(iter);
269 continue;
270 }
271
272 if (pending->last_message_time + NetStats::DISCONNECT_MSG_DELAY > now) {
273 ++iter;
274 continue;
275 }
276
277 SendDisconnect(&pending->address, pending->session_magic);
278 pending->last_message_time = now;
279 pending->messages_left--;
280
281 if (pending->messages_left == 0) {
282 iter = _pending_disconnects.erase(iter);
283 }
284 else {
285 ++iter;
286 }
287 }
288}
289
290void Gekko::MessageSystem::SendInputAck(Handle player, Frame frame, i8 local_advantage)
291{
292 auto plyr = GetPlayerByHandle(player);
293
294 if (!plyr) {
295 return;
296 }
297
298 _pending_output.push(std::make_unique<NetData>());
299 auto& message = _pending_output.back();
300
301 message->addr.Copy(&plyr->address);
302 message->pkt.header.magic = plyr->session_magic;
303 message->pkt.header.type = InputAck;
304
305 InputAckMsg body = {};
306 body.ack_frame = frame;
307 body.frame_advantage = local_advantage;
308
309 message->pkt.body = body;
310}
311
313{
314 auto result = std::vector<Handle>();
315 for (auto& player: remotes) {
316 if (player->address.Equals(*addr)) {
317 result.push_back(player->handle);
318 }
319 }
320 return result;
321}
322
324{
325 std::vector<std::unique_ptr<Player>>* current = &locals;
326 for (u32 i = 0; i < 2; i++)
327 {
328 if (i == 1) {
329 current = &remotes;
330 }
331
332 for (auto& player : *current) {
333 if (player->handle == handle) {
334 return player.get();
335 }
336 }
337 }
338
339 return nullptr;
340}
341
343{
344 Frame min = INT_MAX;
345 for (auto& player : spectator ? spectators : remotes) {
346 if (player->GetStatus() == Connected) {
347 min = std::min(player->stats.last_acked_frame, min);
348 }
349 }
350 return min;
351}
352
354{
355 if (spectator) return _net_spectator_queue.last_added_input;
356
357 Frame result = INT_MAX;
358 for (auto& local : locals) {
359 result = std::min(_net_player_queue[local->handle].last_added_input, result);
360 }
361
362 return result;
363}
364
366{
367 i32 result = 0;
368 u64 now = TimeSinceEpoch();
369
370 std::vector<std::unique_ptr<Player>>* current = &remotes;
371
372 for (u32 i = 0; i < 2; i++)
373 {
374 if (i == 1) {
375 current = &spectators;
376 }
377 for (auto& player : *current) {
378 if (player->GetStatus() == Initiating) {
379 if (player->stats.last_sent_sync_message + NetStats::SYNC_MSG_DELAY < now) {
380 if (player->sync_num == 0) {
381 SendSyncRequest(&player->address);
382 player->stats.last_sent_sync_message = now;
383 }
384 else if (player->sync_num < NUM_TO_SYNC) {
385 SendSyncResponse(&player->address, player->session_magic);
386 player->stats.last_sent_sync_message = now;
387 }
388 else {
389 player->SetStatus(Connected);
390 session_events.AddPlayerConnectedEvent(player->handle);
391 result++;
392 }
393 }
394 result--;
395 }
396 }
397 }
398
399 return result == 0;
400}
401
403{
404 // disconnecting a local actor means leaving the session, so drop every peer.
405 for (auto& local : locals) {
406 if (local->handle == handle) {
407 if (local->GetStatus() == Disconnected) {
408 return false;
409 }
410
411 local->SetStatus(Disconnected);
412
413 std::vector<std::unique_ptr<Player>>* current = &remotes;
414 for (u32 i = 0; i < 2; i++)
415 {
416 if (i == 1) {
417 current = &spectators;
418 }
419
420 for (auto& actor : *current) {
421 if (actor->GetStatus() != Disconnected) {
422 MarkActorDisconnected(actor.get());
423 }
424 actor->disconnect_msgs_left = NUM_DISCONNECT_MSGS;
425 }
426 }
427 return true;
428 }
429 }
430
431 // find the requested remote actor or spectator.
432 Player* target = nullptr;
433
434 std::vector<std::unique_ptr<Player>>* current = &remotes;
435 for (u32 i = 0; i < 2; i++)
436 {
437 if (i == 1) {
438 current = &spectators;
439 }
440
441 for (auto& actor : *current) {
442 if (actor->handle == handle) {
443 target = actor.get();
444 break;
445 }
446 }
447 }
448
449 if (!target || target->GetStatus() == Disconnected) {
450 return false;
451 }
452
453 // an address is a single connection, so drop every actor that shares it.
454 current = &remotes;
455 for (u32 i = 0; i < 2; i++)
456 {
457 if (i == 1) {
458 current = &spectators;
459 }
460
461 for (auto& actor : *current) {
462 const bool same_peer = actor.get() == target ||
463 (target->address.GetSize() != 0 && actor->address.Equals(target->address));
464
465 if (!same_peer) {
466 continue;
467 }
468
469 if (actor->GetStatus() != Disconnected) {
470 MarkActorDisconnected(actor.get());
471 }
473 }
474 }
475
476 return true;
477}
478
480{
481 _pending_output.push(std::make_unique<NetData>());
482 auto& message = _pending_output.back();
483
484 // the address and magic is set later so dont worry about it now
485 message->pkt.header.type = SessionHealth;
486
487 SessionHealthMsg body = {};
488 body.frame = frame;
489 body.checksum = checksum;
490
491 message->pkt.body = body;
492}
493
495{
496 u64 now = TimeSinceEpoch();
497
498 // dont want to spam the network with network health packets
500 return;
501 }
502
503 _pending_output.push(std::make_unique<NetData>());
504 auto& message = _pending_output.back();
505
506 // the address and magic is set later so dont worry about it now
507 message->pkt.header.type = NetworkHealth;
508
509 NetworkHealthMsg body = {};
510 body.send_time = now;
511 body.received = false;
512
513 message->pkt.body = body;
514
516}
517
519{
520 return _net_player_queue[player].last_added_input;
521}
522
523std::deque<std::unique_ptr<u8[]>>& Gekko::MessageSystem::GetNetPlayerQueue(Handle player)
524{
525 return _net_player_queue[player].inputs;
526}
527
529{
530 for (auto iter = spectators.begin(); iter != spectators.end(); ) {
531 auto& spectator = *iter;
532 if (spectator->GetStatus() != Disconnected) {
533 ++iter;
534 continue;
535 }
536
537 if (spectator->disconnect_msgs_left > 0 && spectator->address.GetSize() > 0 &&
538 spectator->session_magic != 0) {
539 auto pending = std::make_unique<PendingDisconnect>();
540 pending->address.Copy(&spectator->address);
541 pending->session_magic = spectator->session_magic;
542 pending->messages_left = spectator->disconnect_msgs_left;
543 pending->last_message_time = spectator->last_disconnect_msg_time;
544 _pending_disconnects.push_back(std::move(pending));
545 }
546
547 iter = spectators.erase(iter);
548 }
549}
550
552{
553 const auto& queue = _net_spectator_queue;
554 return queue.last_added_input - (Frame)queue.inputs.size() + 1;
555}
556
558 Handle spectator,
559 Frame frame,
560 const u8* state,
561 u32 state_size)
562{
563 if (!state || state_size == 0 || state_size > _state_size) {
564 return;
565 }
566
567 for (auto& player : spectators) {
568 if (player->handle != spectator || player->GetStatus() == Disconnected) {
569 continue;
570 }
571
572 player->spectator_state.assign(state, state + state_size);
573 player->spectator_state_frame = frame;
574 player->spectator_state_configured = true;
575 player->spectator_state_acked = false;
576 player->last_spectator_state_send_time = 0;
577 player->stats.last_acked_frame = frame;
578 player->input_cache.packets.clear();
579 return;
580 }
581}
582
583bool Gekko::MessageSystem::TakeSpectatorState(Frame& frame, std::vector<u8>& state)
584{
585 if (!_incoming_spectator_state.ready) {
586 return false;
587 }
588
589 frame = _incoming_spectator_state.frame;
590 state = _incoming_spectator_state.state;
591 _incoming_spectator_state.ready = false;
592 _incoming_spectator_state.taken = true;
593 return true;
594}
595
600
602{
603 // dont treat the frames just past a disconnect as confirmed right away,
604 // a peer may still claim to hold more inputs for the disconnected player.
605 Frame frame = INT32_MAX;
606 const u64 now = TimeSinceEpoch();
607
608 for (auto& actor : remotes) {
609 if (actor->GetStatus() != Disconnected || actor->handle >= _num_players) {
610 continue;
611 }
612
613 // the hold ends once every peer agreed to our claim or had enough time to.
614 if (now - actor->last_claim_raise_time >= NetStats::DISCONNECT_CLAIM_HOLD) {
615 continue;
616 }
617
618 bool settled = true;
619 for (auto& peer : remotes) {
620 if (peer->GetStatus() != Connected) {
621 continue;
622 }
623 auto iter = peer->peer_claims.find(actor->handle);
624 if (iter == peer->peer_claims.end() || iter->second != actor->disconnect_frame) {
625 settled = false;
626 break;
627 }
628 }
629
630 if (!settled) {
631 frame = std::min(actor->disconnect_frame, frame);
632 }
633 }
634
635 return frame;
636}
637
639{
640 session_events.AddPlayerDisconnectedEvent(actor->handle);
641 actor->SetStatus(Disconnected);
642 actor->sync_num = 0;
643
644 // spectators dont own an input queue so theres no frame to agree on.
645 if (actor->handle < _num_players) {
646 actor->disconnect_frame = _net_player_queue[actor->handle].last_added_input;
648 }
649}
650
652{
653 const u64 now = TimeSinceEpoch();
654
655 for (auto& actor : remotes) {
656 if (actor->GetStatus() != Disconnected || actor->handle >= _num_players) {
657 continue;
658 }
659
660 // stop claiming once the exchange had plenty of time to settle.
661 if (now - actor->last_claim_raise_time > NetStats::DISCONNECT_TIMEOUT) {
662 continue;
663 }
664
665 // dont want to spam the network with claim packets
666 if (actor->last_claim_sent_time + NetStats::DISCONNECT_MSG_DELAY > now) {
667 continue;
668 }
669
670 // find the peers which have not heard or agreed to our current claim yet.
671 std::vector<Player*> pending;
672 for (auto& peer : remotes) {
673 if (peer->GetStatus() != Connected ||
674 peer->address.GetSize() == 0 || peer->session_magic == 0) {
675 continue;
676 }
677
678 auto claimed = peer->peer_claims.find(actor->handle);
679 auto sent = peer->peer_claims_sent.find(actor->handle);
680
681 const bool agreed = claimed != peer->peer_claims.end() &&
682 claimed->second == actor->disconnect_frame;
683 const bool told = sent != peer->peer_claims_sent.end() &&
684 sent->second == actor->disconnect_frame;
685
686 if (!agreed || !told) {
687 pending.push_back(peer.get());
688 }
689 }
690
691 if (pending.empty()) {
692 continue;
693 }
694
695 // build the claim carrying the inputs we hold so any peer can catch up.
696 auto& input_q = _net_player_queue[actor->handle];
697
698 DisconnectClaimMsg body = {};
699 body.player = actor->handle;
700 body.last_frame = input_q.last_added_input;
701 body.start_frame = body.last_frame - (Frame)input_q.inputs.size() + 1;
702
703 for (auto& input : input_q.inputs) {
704 body.inputs.insert(body.inputs.end(), input.get(), input.get() + _input_size);
705 }
706
707 for (auto peer : pending) {
708 _pending_output.push(std::make_unique<NetData>());
709 auto& message = _pending_output.back();
710
711 message->addr.Copy(&peer->address);
712 message->pkt.header.type = DisconnectClaim;
713 message->pkt.header.magic = peer->session_magic;
714 message->pkt.body = body;
715
716 peer->peer_claims_sent[actor->handle] = body.last_frame;
717 }
718
719 actor->last_claim_sent_time = now;
720 }
721}
722
724{
725 // we lack inputs the session agreed on and cant obtain them anymore,
726 // drop every peer since we cant simulate in agreement any longer.
727 for (auto& peer : remotes) {
728 if (peer->GetStatus() != Disconnected) {
729 MarkActorDisconnected(peer.get());
730 }
731 }
732}
733
735{
736 // a timeout of 0 means the user handles disconnecting themselves.
737 if (_disconnect_timeout == 0) {
738 return;
739 }
740
741 const u64 now = TimeSinceEpoch();
742 for (auto& actor : spectator ? spectators : remotes) {
743 if (actor->GetStatus() == Connected) {
744 // give the actor a chance to save itself.
745 if (actor->stats.last_received_message == 0) {
746 actor->stats.last_received_message = now;
747 continue;
748 }
749 // check whether messages are being sent if not disconnect.
750 const u64 msg_diff = now - actor->stats.last_received_message;
751 if (msg_diff > _disconnect_timeout) {
752 MarkActorDisconnected(actor.get());
753 // let the actor know it has been dropped in case its still able to receive.
754 actor->disconnect_msgs_left = NUM_DISCONNECT_MSGS;
755 }
756 }
757 }
758}
759
761{
762 using namespace std::chrono;
763 return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
764}
765
766void Gekko::MessageSystem::SendDataToAll(NetData* pkt, GekkoNetAdapter* host, bool spectators_only)
767{
768 auto& actors = spectators_only ? spectators : remotes;
769
770 std::vector<u8> body_buffer;
771 zpp::bits::out body_out(body_buffer);
772
773 if (failure(body_out(pkt->pkt.body))) {
774 printf("failed to serialize packet body\n");
775 return;
776 }
777
778 for (auto& actor : actors) {
779 _bin_buffer.clear();
780 if (actor->address.GetSize() != 0 && actor->GetStatus() != Disconnected) {
781
782 pkt->addr.Copy(&actor->address);
783 pkt->pkt.header.magic = actor->session_magic;
784
785 zpp::bits::out out(_bin_buffer);
786 if (failure(out(pkt->pkt.header))) {
787 printf("failed to serialize packet header\n");
788 continue;
789 }
790
791 _bin_buffer.insert(
792 _bin_buffer.end(),
793 body_buffer.begin(),
794 body_buffer.end()
795 );
796
797 auto addr = GekkoNetAddress();
798 addr.data = actor->address.GetAddress();
799 addr.size = actor->address.GetSize();
800
801 host->send_data(&addr, (char*)_bin_buffer.data(), (int)_bin_buffer.size());
802 actor->stats.bytes_sent_accum += (u32)_bin_buffer.size();
803 }
804 }
805}
806
808{
809 _bin_buffer.clear();
810
811 zpp::bits::out out(_bin_buffer);
812 if (failure(out(pkt->pkt.header, pkt->pkt.body))) {
813 printf("failed to serialize packet\n");
814 return;
815 }
816
817 auto addr = GekkoNetAddress();
818 addr.data = pkt->addr.GetAddress();
819 addr.size = pkt->addr.GetSize();
820
821 host->send_data(&addr, (char*)_bin_buffer.data(), (int)_bin_buffer.size());
822
823 u32 sent_size = (u32)_bin_buffer.size();
824 std::vector<std::unique_ptr<Player>>* current = &remotes;
825 for (u32 i = 0; i < 2; i++)
826 {
827 if (i == 1) {
828 current = &spectators;
829 }
830
831 for (auto& actor : *current) {
832 if (actor->address.Equals(pkt->addr)) {
833 actor->stats.bytes_sent_accum += sent_size;
834 }
835 }
836 }
837}
838
840{
841 u64 now = TimeSinceEpoch();
842 // update receive timers.
843 std::vector<std::unique_ptr<Player>>* current = &remotes;
844 for (u32 i = 0; i < 2; i++)
845 {
846 if (i == 1) {
847 current = &spectators;
848 }
849
850 for (auto& player : *current) {
851 if (player->address.Equals(addr)) {
852 player->stats.last_received_message = now;
853 player->stats.bytes_received_accum += packet_size;
854 }
855 }
856 }
857
858 // handle packet.
859 if (pkt.header.magic != _session_magic) {
860 if (pkt.header.type == SyncRequest) {
861 OnSyncRequest(addr, pkt);
862 }
863 else {
864 printf("dropped packet!\n");
865 }
866 }
867 else {
868 switch (pkt.header.type)
869 {
870 case SyncResponse:
871 OnSyncResponse(addr, pkt);
872 return;
873 case Inputs:
874 case SpectatorInputs:
875 OnInputs(addr, pkt);
876 return;
877 case InputAck:
878 OnInputAck(addr, pkt);
879 return;
880 case SessionHealth:
881 OnSessionHealth(addr, pkt);
882 return;
883 case NetworkHealth:
884 OnNetworkHealth(addr, pkt);
885 return;
886 case SpectatorState:
887 OnSpectatorState(addr, pkt);
888 return;
890 OnSpectatorStateAck(addr, pkt);
891 return;
892 case Disconnect:
893 OnDisconnect(addr, pkt);
894 return;
895 case DisconnectClaim:
896 OnDisconnectClaim(addr, pkt);
897 return;
898 default:
899 assert(false && "cannot process an unknown event!");
900 return;
901 }
902 }
903}
904
906{
907 i32 should_send = 0;
908 u64 now = TimeSinceEpoch();
909 auto body = std::get_if<SyncMsg>(&pkt.body);
910
911 if (!body) {
912 return;
913 }
914
915 // handle requests and set the peer its session magic for both remotes and spectators
916 std::vector<std::unique_ptr<Player>>* current = &remotes;
917 for (u32 i = 0; i < 2; i++)
918 {
919 if (i == 1) {
920 current = &spectators;
921 }
922
923 for (auto& player : *current) {
924 if (player->address.Equals(addr)) {
925 player->session_magic = body->rng_data;
926 if (player->sync_num == 0) {
927 player->stats.last_sent_sync_message = now;
928 should_send++;
929 }
930 }
931 }
932 }
933
934 if (should_send > 0) {
935 // send a packet containing the local session magic
936 SendSyncResponse(&addr, body->rng_data);
937 }
938}
939
941{
942 i32 should_send = 0;
943 u64 now = TimeSinceEpoch();
944 auto body = std::get_if<SyncMsg>(&pkt.body);
945
946 if (!body) {
947 return;
948 }
949
950 // handle sync responses for both remotes and spectators
951 std::vector<std::unique_ptr<Player>>* current = &remotes;
952 for (u32 i = 0; i < 2; i++)
953 {
954 if (i == 1) {
955 current = &spectators;
956 }
957
958 for (auto& player : *current) {
959 if (player->GetStatus() == Connected) {
960 // connected but the remote is still asking ? maybe high packet loss? send a response again
961 should_send++;
962 continue;
963 }
964
965 if (player->address.Equals(addr)) {
966 player->session_magic = body->rng_data;
967 if (player->sync_num < NUM_TO_SYNC) {
968 player->sync_num++;
969 should_send++;
970 player->stats.last_sent_sync_message = now;
971 session_events.AddPlayerSyncingEvent(
972 player->handle,
973 player->sync_num,
975 );
976 continue;
977 }
978
979 if (player->sync_num >= NUM_TO_SYNC) {
980 player->SetStatus(Connected);
981 session_events.AddPlayerConnectedEvent(player->handle);
982 continue;
983 }
984 }
985 }
986 }
987
988 if (should_send > 0) {
989 // send a packet containing the local session magic
990 SendSyncResponse(&addr, body->rng_data);
991 }
992}
993
995{
996 auto body = std::get_if<InputMsg>(&pkt.body);
997
998 if (!body) {
999 return;
1000 }
1001
1002 const bool is_spectator = (pkt.header.type == SpectatorInputs);
1003
1004 // reverse RLE + delta if the sender compressed this packet
1005 if (body->compressed) {
1006 if (body->inputs.size() % 2 != 0) {
1007 return;
1008 }
1009
1010 auto decompressed = Compression::RLEDecode(body->inputs.data(), (u32)body->inputs.size());
1011 const u32 stride = is_spectator ? _input_size * _num_players : _input_size;
1012 body->inputs = Compression::DeltaDecode(decompressed.data(), (u32)decompressed.size(), stride);
1013 }
1014
1015 const Frame start_frame = body->start_frame;
1016 const u32 input_count = body->input_count;
1017
1018 if (is_spectator) {
1019 if ((u64)input_count * _num_players * _input_size > body->inputs.size()) {
1020 return;
1021 }
1022
1023 for (u32 frame_idx = 0; frame_idx < input_count; frame_idx++) {
1024 const Frame recv_frame = start_frame + frame_idx;
1025 const u32 frame_offset = frame_idx * _num_players * _input_size;
1026
1027 for (u32 player = 0; player < _num_players; player++) {
1028 u8* input = &body->inputs[frame_offset + player * _input_size];
1029 AddInput(recv_frame, player, input, true);
1030 }
1031 }
1032 } else {
1033 auto handles = GetRemoteHandlesForAddress(&addr);
1034 const u32 player_count = (u32)handles.size();
1035
1036 if ((u64)input_count * player_count * _input_size > body->inputs.size()) {
1037 return;
1038 }
1039
1040 for (u32 i = 0; i < player_count; i++) {
1041 const u32 player_offset = i * input_count * _input_size;
1042
1043 for (u32 frame_idx = 0; frame_idx < input_count; frame_idx++) {
1044 const Frame recv_frame = start_frame + frame_idx;
1045 u8* input = &body->inputs[player_offset + frame_idx * _input_size];
1046 AddInput(recv_frame, handles[i], input, true);
1047 }
1048
1049 auto player = GetPlayerByHandle(handles[i]);
1050 if (player) {
1051 player->stats.last_received_frame = TimeSinceEpoch();
1052 }
1053 }
1054 }
1055}
1056
1058{
1059 auto body = std::get_if<InputAckMsg>(&pkt.body);
1060
1061 if (!body) {
1062 return;
1063 }
1064
1065 const Frame ack_frame = body->ack_frame;
1066 const i8 remote_advantage = (i8)body->frame_advantage;
1067
1068 for (auto& player : remotes) {
1069 if (player->address.Equals(addr) && player->stats.last_acked_frame < ack_frame) {
1070 player->stats.last_acked_frame = ack_frame;
1071 player->adv_history.SetRemoteAdvantage(remote_advantage);
1072 }
1073 }
1074
1075 for (auto& player : spectators) {
1076 if (player->address.Equals(addr) && player->stats.last_acked_frame < ack_frame) {
1077 player->stats.last_acked_frame = ack_frame;
1078 }
1079 }
1080}
1081
1083{
1084 auto body = std::get_if<SessionHealthMsg>(&pkt.body);
1085
1086 if (!body) {
1087 return;
1088 }
1089
1090 const Frame frame = body->frame;
1091 const u32 checksum = body->checksum;
1092
1093 for (auto& player : remotes) {
1094 if (player->address.Equals(addr)) {
1095 player->SetChecksum(frame, checksum);
1096
1097 for (auto iter = player->session_health.begin();
1098 iter != player->session_health.end(); ) {
1099 if (iter->first < (_net_player_queue[player->handle].last_added_input - 128)) {
1100 iter = player->session_health.erase(iter);
1101 } else {
1102 ++iter;
1103 }
1104 }
1105 break;
1106 }
1107 }
1108}
1109
1111{
1112 auto body = std::get_if<NetworkHealthMsg>(&pkt.body);
1113
1114 if (!body) {
1115 return;
1116 }
1117
1118 // ok if its not a returned packet then update it and send it back to its specifc peer.
1119 if (!body->received) {
1120 // find the sender in remotes or spectators
1121 Player* player = nullptr;
1122 auto handles = GetRemoteHandlesForAddress(&addr);
1123 if (!handles.empty()) {
1124 player = GetPlayerByHandle(handles.at(0));
1125 }
1126
1127 // check spectators if not found in remotes
1128 if (!player) {
1129 for (auto& spec : spectators) {
1130 if (spec->address.Equals(addr)) {
1131 player = spec.get();
1132 break;
1133 }
1134 }
1135 }
1136
1137 if (!player) {
1138 return;
1139 }
1140
1141 _pending_output.push(std::make_unique<NetData>());
1142 auto& message = _pending_output.back();
1143
1144 message->pkt.header.magic = player->session_magic;
1145 message->pkt.header.type = NetworkHealth;
1146
1147 NetworkHealthMsg new_body = {};
1148 new_body.send_time = body->send_time;
1149 new_body.received = true;
1150
1151 message->pkt.body = new_body;
1152 message->addr.Copy(&addr);
1153 return;
1154 }
1155
1156 // else update network stats
1157 u16 rtt_ms = (u16)(TimeSinceEpoch() - body->send_time);
1158 std::vector<std::unique_ptr<Player>>* current = &remotes;
1159
1160 for (u32 i = 0; i < 2; i++)
1161 {
1162 if (i == 1) {
1163 current = &spectators;
1164 }
1165
1166 for (auto& actor : *current) {
1167 if (addr.Equals(actor->address)) {
1168 actor->stats.AddRTT(rtt_ms);
1169 }
1170 }
1171 }
1172}
1173
1175{
1176 auto body = std::get_if<SpectatorStateMsg>(&pkt.body);
1177
1178 if (!_accept_spectator_state || !body || _state_size == 0 || body->total_size == 0 ||
1179 body->total_size > _state_size || body->state.empty() ||
1180 body->offset % SPECTATOR_STATE_CHUNK_SIZE != 0 ||
1181 body->offset >= body->total_size ||
1182 body->state.size() > SPECTATOR_STATE_CHUNK_SIZE ||
1183 body->state.size() > body->total_size - body->offset) {
1184 return;
1185 }
1186
1187 const u32 chunk_count =
1188 1 + (body->total_size - 1) / SPECTATOR_STATE_CHUNK_SIZE;
1189 const u32 chunk = body->offset / SPECTATOR_STATE_CHUNK_SIZE;
1190 const u32 expected_size = std::min(
1192 body->total_size - body->offset
1193 );
1194
1195 if (chunk >= chunk_count || body->state.size() != expected_size) {
1196 return;
1197 }
1198
1199 auto& incoming = _incoming_spectator_state;
1200 if (incoming.active && body->frame < incoming.frame) {
1201 return;
1202 }
1203
1204 if (incoming.active && body->frame == incoming.frame &&
1205 body->total_size != incoming.total_size) {
1206 return;
1207 }
1208
1209 if (!incoming.active || body->frame > incoming.frame) {
1210 incoming.active = true;
1211 incoming.ready = false;
1212 incoming.taken = false;
1213 incoming.frame = body->frame;
1214 incoming.total_size = body->total_size;
1215 incoming.received_chunks = 0;
1216 incoming.state.resize(body->total_size);
1217 incoming.chunks.assign(chunk_count, false);
1218 }
1219
1220 if (!incoming.chunks[chunk]) {
1221 std::memcpy(incoming.state.data() + body->offset, body->state.data(), body->state.size());
1222 incoming.chunks[chunk] = true;
1223 incoming.received_chunks++;
1224 }
1225
1226 if (incoming.received_chunks != incoming.chunks.size()) {
1227 return;
1228 }
1229
1230 if (!incoming.taken) {
1231 incoming.ready = true;
1232
1233 // Inputs after the snapshot must be accepted as a new sequential stream.
1234 for (auto& queue : _net_player_queue) {
1235 queue.inputs.clear();
1236 queue.last_added_input = incoming.frame;
1237 }
1238 }
1239
1240 _pending_output.push(std::make_unique<NetData>());
1241 auto& message = _pending_output.back();
1242 message->addr.Copy(&addr);
1243 message->pkt.header.type = SpectatorStateAck;
1244 message->pkt.header.magic = 0;
1245 for (auto& player : remotes) {
1246 if (player->address.Equals(addr)) {
1247 message->pkt.header.magic = player->session_magic;
1248 break;
1249 }
1250 }
1251
1252 if (message->pkt.header.magic == 0) {
1253 _pending_output.pop();
1254 return;
1255 }
1256
1257 SpectatorStateAckMsg ack = {};
1258 ack.frame = incoming.frame;
1259 message->pkt.body = ack;
1260}
1261
1263{
1264 auto body = std::get_if<SpectatorStateAckMsg>(&pkt.body);
1265
1266 if (!body) {
1267 return;
1268 }
1269
1270 for (auto& player : spectators) {
1271 if (!player->address.Equals(addr) || !player->spectator_state_configured ||
1272 player->spectator_state_frame != body->frame) {
1273 continue;
1274 }
1275
1276 // The retained input window moved past this state while it was in
1277 // flight. Keep the peer gated so the game session sends a newer one.
1278 if (body->frame + 1 < GetOldestSpectatorInput()) {
1279 return;
1280 }
1281
1282 player->spectator_state_acked = true;
1283 player->stats.last_acked_frame = body->frame;
1284 player->spectator_state.clear();
1285 player->input_cache.packets.clear();
1286 return;
1287 }
1288}
1289
1291{
1292 auto body = std::get_if<DisconnectMsg>(&pkt.body);
1293
1294 if (!body) {
1295 return;
1296 }
1297
1298 // the peer at this address left the session, so every actor it hosts is gone.
1299 std::vector<std::unique_ptr<Player>>* current = &remotes;
1300 for (u32 i = 0; i < 2; i++)
1301 {
1302 if (i == 1) {
1303 current = &spectators;
1304 }
1305
1306 for (auto& player : *current) {
1307 if (player->address.Equals(addr) && player->GetStatus() != Disconnected) {
1308 MarkActorDisconnected(player.get());
1309 }
1310 }
1311 }
1312}
1313
1315{
1316 auto body = std::get_if<DisconnectClaimMsg>(&pkt.body);
1317
1318 if (!body || body->player < 0 || body->player >= _num_players) {
1319 return;
1320 }
1321
1322 // the carried inputs must cover the claimed frame range.
1323 const u64 expected = (u64)((i64)body->last_frame - body->start_frame + 1) * _input_size;
1324 if (body->last_frame < body->start_frame || body->inputs.size() < expected) {
1325 return;
1326 }
1327
1328 auto plyr = GetPlayerByHandle(body->player);
1329
1330 // claims about our own actors are handled by the disconnect message instead.
1331 if (!plyr || plyr->GetType() != GekkoRemotePlayer) {
1332 return;
1333 }
1334
1335 if (plyr->GetStatus() != Disconnected) {
1337 }
1338
1339 // remember what the peer claimed so the exchange can settle.
1340 for (auto& peer : remotes) {
1341 if (peer->address.Equals(addr)) {
1342 peer->peer_claims[body->player] = body->last_frame;
1343 }
1344 }
1345
1346 if (body->last_frame <= plyr->disconnect_frame) {
1347 return;
1348 }
1349
1350 // a raise this late may touch frames the session already confirmed,
1351 // failing the session beats silently drifting apart from the other peers.
1352 if (TimeSinceEpoch() - plyr->last_claim_raise_time >= NetStats::DISCONNECT_CLAIM_HOLD) {
1354 return;
1355 }
1356
1357 // the peer holds more inputs than we do, catch up using the carried inputs.
1358 const Frame next = _net_player_queue[body->player].last_added_input + 1;
1359
1360 if (next < body->start_frame) {
1361 // the gap cant be bridged, the local session cant stay in agreement.
1363 return;
1364 }
1365
1366 for (Frame frame = std::max(next, body->start_frame); frame <= body->last_frame; frame++) {
1367 u8* input = &body->inputs[(frame - body->start_frame) * _input_size];
1368 AddInput(frame, body->player, input, true);
1369 }
1370
1371 plyr->disconnect_frame = _net_player_queue[body->player].last_added_input;
1372 plyr->last_claim_raise_time = TimeSinceEpoch();
1373 // announce the raised claim right away so the exchange settles quickly.
1374 plyr->last_claim_sent_time = 0;
1375}
1376
1378{
1379 const u64 now = TimeSinceEpoch();
1380
1381 for (auto& player : spectators) {
1382 if (player->GetStatus() != Connected || !player->requires_spectator_state ||
1383 !player->spectator_state_configured || player->spectator_state_acked ||
1384 player->spectator_state.empty()) {
1385 continue;
1386 }
1387
1388 if (player->last_spectator_state_send_time + NetStats::INPUT_RETRY_INTERVAL > now) {
1389 continue;
1390 }
1391
1392 const u32 total_size = (u32)player->spectator_state.size();
1393 for (u32 offset = 0; offset < total_size; offset += SPECTATOR_STATE_CHUNK_SIZE) {
1394 const u32 chunk_size = std::min(SPECTATOR_STATE_CHUNK_SIZE, total_size - offset);
1395
1396 SpectatorStateMsg body = {};
1397 body.frame = player->spectator_state_frame;
1398 body.total_size = total_size;
1399 body.offset = offset;
1400 body.state.insert(
1401 body.state.end(),
1402 player->spectator_state.begin() + offset,
1403 player->spectator_state.begin() + offset + chunk_size
1404 );
1405
1406 NetData data;
1407 data.addr.Copy(&player->address);
1409 data.pkt.header.magic = player->session_magic;
1410 data.pkt.body = std::move(body);
1411 SendDataTo(&data, host);
1412 }
1413
1414 player->last_spectator_state_send_time = now;
1415 }
1416}
1417
1419{
1420 if (peer->address.GetSize() == 0 || peer->GetStatus() == Disconnected) {
1421 return;
1422 }
1423
1424 if (spectator && peer->requires_spectator_state && !peer->spectator_state_acked) {
1425 return;
1426 }
1427
1428 const u32 MAX_INPUT_SIZE = 1024;
1429 const auto packet_type = spectator ? SpectatorInputs : Inputs;
1430 auto& queue = spectator ? _net_spectator_queue : _net_player_queue[locals[0]->handle];
1431 const u32 num_players = spectator ? _num_players : (u32)locals.size();
1432 const u32 inputs_per_packet = MAX_INPUT_SIZE / (_input_size * num_players);
1433 const u32 q_size = (u32)queue.inputs.size();
1434
1435 if (q_size == 0) return;
1436
1437 const Frame queue_oldest_frame = queue.last_added_input - (Frame)q_size + 1;
1438 const Frame last_input = spectator ? _net_spectator_queue.last_added_input : GetLastAddedInput(false);
1439
1440 // peer is caught up, nothing to send
1441 if (peer->stats.last_acked_frame >= last_input) return;
1442
1443 // check per-peer cache
1444 if (peer->input_cache.IsValid(peer->stats.last_acked_frame, last_input)) {
1445 // cache hit: nothing new to send, this is a pure re-send — rate limit it
1446 const u64 now = TimeSinceEpoch();
1448 return;
1449 }
1450 for (const auto& cached_msg : peer->input_cache.packets) {
1451 NetData data;
1452 data.addr.Copy(&peer->address);
1453 data.pkt.header.type = packet_type;
1454 data.pkt.header.magic = peer->session_magic;
1455 data.pkt.body = cached_msg;
1456
1457 SendDataTo(&data, host);
1458 }
1459 peer->last_input_send_time = now;
1460 return;
1461 }
1462
1463 // cache miss: rebuild packets for this peer
1464 const Frame peer_start_frame = std::max(peer->stats.last_acked_frame + 1, queue_oldest_frame);
1465 const u32 peer_start_idx = (u32)(peer_start_frame - queue_oldest_frame);
1466 const u32 peer_input_count = q_size - peer_start_idx;
1467
1468 if (peer_input_count == 0) return;
1469
1470 peer->input_cache.packets.clear();
1471
1472 const u32 packet_count = (peer_input_count + inputs_per_packet - 1) / inputs_per_packet;
1473
1474 for (u32 pc = 0; pc < packet_count; pc++) {
1475 const u32 input_start_idx = peer_start_idx + pc * inputs_per_packet;
1476 const u32 input_end_idx = std::min(peer_start_idx + peer_input_count, input_start_idx + inputs_per_packet);
1477 const u32 input_count = input_end_idx - input_start_idx;
1478
1479 InputMsg msg;
1480 msg.start_frame = queue_oldest_frame + input_start_idx;
1481
1482 if (spectator) {
1483 for (u32 i = input_start_idx; i < input_end_idx; i++) {
1484 const auto& p_input = queue.inputs.at(i);
1485 msg.inputs.insert(msg.inputs.end(),
1486 p_input.get(),
1487 p_input.get() + _input_size * num_players);
1488 }
1489 }
1490 else {
1491 for (u32 player = 0; player < num_players; player++) {
1492 const auto& player_queue = _net_player_queue[locals[player]->handle];
1493 for (u32 i = input_start_idx; i < input_end_idx; i++) {
1494 const auto& p_input = player_queue.inputs.at(i);
1495 msg.inputs.insert(msg.inputs.end(),
1496 p_input.get(),
1497 p_input.get() + _input_size);
1498 }
1499 }
1500 }
1501
1502 // delta + RLE, keep only when it actually reduces size
1503 const u32 stride = spectator ? _input_size * num_players : _input_size;
1504 msg.compressed = false;
1505 auto delta = Compression::DeltaEncode(msg.inputs.data(), (u32)msg.inputs.size(), stride);
1506 auto compressed = Compression::RLEEncode(delta.data(), (u32)delta.size());
1507 if (compressed.size() < msg.inputs.size()) {
1508 msg.inputs = std::move(compressed);
1509 msg.compressed = true;
1510 }
1511
1512 msg.total_size = (u16)msg.inputs.size();
1513 msg.input_count = input_count;
1514
1515 // cache and send
1516 peer->input_cache.packets.push_back(msg);
1517
1518 NetData data;
1519 data.addr.Copy(&peer->address);
1520 data.pkt.header.type = packet_type;
1521 data.pkt.header.magic = peer->session_magic;
1522 data.pkt.body = std::move(msg);
1523
1524 SendDataTo(&data, host);
1525 }
1526
1528
1529 // update cache keys
1531 peer->input_cache.last_input_frame = last_input;
1532}
1533
1535{
1536 _local_frame_adv = 0;
1538 std::memset(_local, 0, HISTORY_SIZE * sizeof(i8));
1539 std::memset(_remote, 0, HISTORY_SIZE * sizeof(i8));
1540}
1541
1543{
1544 const u32 update_frame = std::max(frame, 0);
1545 _local[update_frame % HISTORY_SIZE] = _local_frame_adv;
1546 _remote[update_frame % HISTORY_SIZE] = _remote_frame_adv;
1547}
1548
1550{
1551 f32 sum_local = 0.f;
1552 f32 sum_remote = 0.f;
1553
1554 for (i32 i = 0; i < HISTORY_SIZE; i++) {
1555 sum_local += _local[i];
1556 sum_remote += _remote[i];
1557 }
1558
1559 f32 avg_local = sum_local / HISTORY_SIZE;
1560 f32 avg_remote = sum_remote / HISTORY_SIZE;
1561
1562 // return the frames ahead (halved: each peer corrects its share of the gap)
1563 return (avg_local - avg_remote) / 2.f;
1564}
1565
1569
1573
std::vector< std::unique_ptr< PendingDisconnect > > _pending_disconnects
Definition backend.h:302
std::queue< std::unique_ptr< NetData > > _pending_output
Definition backend.h:304
bool TakeSpectatorState(Frame &frame, std::vector< u8 > &state)
Definition backend.cpp:583
void HandleTooFarBehindActors(bool spectator=false)
Definition backend.cpp:734
void SendSyncResponse(NetAddress *addr, u16 magic)
Definition backend.cpp:195
void OnNetworkHealth(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:1110
void SendInputAck(Handle player, Frame frame, i8 local_advantage)
Definition backend.cpp:290
Frame GetOldestSpectatorInput()
Definition backend.cpp:551
void OnInputs(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:994
void SetSpectatorState(Handle spectator, Frame frame, const u8 *state, u32 state_size)
Definition backend.cpp:557
bool _accept_spectator_state
Definition backend.h:270
void SendDataTo(NetData *pkt, GekkoNetAdapter *host)
Definition backend.cpp:807
std::vector< std::unique_ptr< Player > > spectators
Definition backend.h:182
void AddInput(Frame input_frame, Handle player, u8 input[], bool remote=false)
Definition backend.cpp:68
std::vector< u8 > _bin_buffer
Definition backend.h:306
void SendPendingSpectatorStates(GekkoNetAdapter *host)
Definition backend.cpp:1377
void SetDisconnectTimeout(u32 timeout)
Definition backend.cpp:596
void OnSpectatorState(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:1174
void OnDisconnect(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:1290
std::vector< std::unique_ptr< Player > > remotes
Definition backend.h:180
std::vector< NetInputQueue > _net_player_queue
Definition backend.h:279
void SendInputsToPeer(Player *peer, GekkoNetAdapter *host, bool spectator)
Definition backend.cpp:1418
SessionEventSystem session_events
Definition backend.h:184
const u32 SPECTATOR_STATE_CHUNK_SIZE
Definition backend.h:264
Frame GetMinLastAckedFrame(bool spectator=false)
Definition backend.cpp:342
void OnSyncResponse(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:940
void OnInputAck(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:1057
void SendPendingOutput(GekkoNetAdapter *host)
Definition backend.cpp:96
Player * GetPlayerByHandle(Handle handle)
Definition backend.cpp:323
std::deque< std::unique_ptr< u8[]> > & GetNetPlayerQueue(Handle player)
Definition backend.cpp:523
void OnSpectatorStateAck(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:1262
std::vector< Handle > GetRemoteHandlesForAddress(NetAddress *addr)
Definition backend.cpp:312
void OnSyncRequest(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:905
void SendPendingDisconnects()
Definition backend.cpp:230
void OnDisconnectClaim(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:1314
void ReclaimDisconnectedSpectators()
Definition backend.cpp:528
Frame GetLastAddedInputFrom(Handle player)
Definition backend.cpp:518
void SendDisconnect(NetAddress *addr, u16 magic)
Definition backend.cpp:214
const u32 MAX_INPUT_QUEUE_SIZE
Definition backend.h:259
void HandleData(GekkoNetAdapter *host, GekkoNetResult **data, u32 length)
Definition backend.cpp:145
void SendSyncRequest(NetAddress *addr)
Definition backend.cpp:175
void SendSessionHealth(Frame frame, u32 checksum)
Definition backend.cpp:479
Frame GetDisconnectHoldFrame()
Definition backend.cpp:601
const u32 NUM_TO_SYNC
Definition backend.h:261
void MarkActorDisconnected(Player *actor)
Definition backend.cpp:638
std::vector< std::unique_ptr< Player > > locals
Definition backend.h:178
void AddSpectatorInput(Frame input_frame, u8 input[])
Definition backend.cpp:82
void Init(u8 num_players, u32 input_size, u32 state_size=0, bool accept_spectator_state=false)
Definition backend.cpp:25
Frame GetLastAddedInput(bool spectator=false)
Definition backend.cpp:353
struct Gekko::MessageSystem::IncomingSpectatorState _incoming_spectator_state
NetInputQueue _net_spectator_queue
Definition backend.h:282
const u8 NUM_DISCONNECT_MSGS
Definition backend.h:262
void SendDataToAll(NetData *pkt, GekkoNetAdapter *host, bool spectators_only=false)
Definition backend.cpp:766
void ParsePacket(NetAddress &addr, NetPacket &pkt, u32 packet_size)
Definition backend.cpp:839
void OnSessionHealth(NetAddress &addr, NetPacket &pkt)
Definition backend.cpp:1082
bool DisconnectActor(Handle handle)
Definition backend.cpp:402
void HandleUnrecoverableGap()
Definition backend.cpp:723
const u32 MAX_SPECTATOR_INPUT_QUEUE_SIZE
Definition backend.h:260
PlayerStatus GetStatus()
Definition player.cpp:25
bool requires_spectator_state
Definition backend.h:93
Handle handle
Definition backend.h:72
u64 last_claim_raise_time
Definition backend.h:120
bool spectator_state_acked
Definition backend.h:97
void SetStatus(PlayerStatus type)
Definition player.cpp:30
NetStats stats
Definition backend.h:78
Frame disconnect_frame
Definition backend.h:112
u8 disconnect_msgs_left
Definition backend.h:88
NetAddress address
Definition backend.h:80
u64 last_input_send_time
Definition backend.h:86
InputCache input_cache
Definition backend.h:84
u16 session_magic
Definition backend.h:76
uint64_t u64
Definition gekko_types.h:8
int32_t i32
Definition gekko_types.h:12
float f32
Definition gekko_types.h:15
int32_t Handle
Definition gekko_types.h:19
int32_t Frame
Definition gekko_types.h:18
int64_t i64
Definition gekko_types.h:13
uint8_t u8
Definition gekko_types.h:5
uint16_t u16
Definition gekko_types.h:6
int8_t i8
Definition gekko_types.h:10
uint32_t u32
Definition gekko_types.h:7
struct GekkoNetAddress GekkoNetAddress
@ GekkoRemotePlayer
Definition gekkonet.h:79
@ InputAck
Definition net.h:30
@ SpectatorStateAck
Definition net.h:38
@ SyncResponse
Definition net.h:32
@ SessionHealth
Definition net.h:33
@ NetworkHealth
Definition net.h:34
@ Inputs
Definition net.h:28
@ DisconnectClaim
Definition net.h:36
@ SpectatorState
Definition net.h:37
@ Disconnect
Definition net.h:35
@ SyncRequest
Definition net.h:31
@ SpectatorInputs
Definition net.h:29
@ Connected
Definition backend.h:22
@ Initiating
Definition backend.h:21
@ Disconnected
Definition backend.h:23
void(* free_data)(void *data_ptr)
Definition gekkonet.h:102
void(* send_data)(GekkoNetAddress *addr, const char *data, int length)
Definition gekkonet.h:96
static const i32 HISTORY_SIZE
Definition backend.h:47
i8 _remote[HISTORY_SIZE]
Definition backend.h:55
i8 _local[HISTORY_SIZE]
Definition backend.h:53
void SetRemoteAdvantage(i8 adv)
Definition backend.cpp:1570
void SetLocalAdvantage(i8 adv)
Definition backend.cpp:1566
void Update(Frame frame)
Definition backend.cpp:1542
static std::vector< uint8_t > DeltaDecode(const uint8_t *data, u32 length, u32 stride)
Definition compression.h:67
static std::vector< uint8_t > RLEEncode(const uint8_t *data, u32 length)
Definition compression.h:19
static std::vector< uint8_t > DeltaEncode(const uint8_t *data, u32 length, u32 stride)
Definition compression.h:59
static std::vector< uint8_t > RLEDecode(const uint8_t *data, u32 length)
Definition compression.h:38
std::vector< u8 > inputs
Definition net.h:96
i8 frame_advantage
Definition net.h:57
Frame ack_frame
Definition net.h:56
std::vector< InputMsg > packets
Definition backend.h:29
Frame last_acked_frame
Definition backend.h:27
Frame last_input_frame
Definition backend.h:28
bool IsValid(Frame current_ack, Frame current_last_input) const
Definition backend.cpp:41
Frame start_frame
Definition net.h:47
std::vector< u8 > inputs
Definition net.h:52
bool compressed
Definition net.h:50
u16 input_count
Definition net.h:48
u16 total_size
Definition net.h:49
std::deque< std::unique_ptr< u8[]> > inputs
Definition backend.h:190
void TrimToAck(Frame min_ack, u32 max_size)
Definition backend.cpp:48
PacketType type
Definition net.h:42
bool Equals(NetAddress &other)
Definition net.cpp:44
u8 * GetAddress()
Definition net.cpp:49
u32 GetSize()
Definition net.cpp:8
void Copy(NetAddress *other)
Definition net.cpp:27
NetAddress addr
Definition net.h:117
NetPacket pkt
Definition net.h:118
MsgHeader header
Definition net.h:112
MsgBody body
Definition net.h:113
static const u64 INPUT_RETRY_INTERVAL
Definition net.h:127
static const u64 DISCONNECT_TIMEOUT
Definition net.h:122
static const u64 DISCONNECT_MSG_DELAY
Definition net.h:123
static const u64 NET_CHECK_DELAY
Definition net.h:126
Frame last_acked_frame
Definition net.h:130
static const u64 SYNC_MSG_DELAY
Definition net.h:125
static const u64 DISCONNECT_CLAIM_HOLD
Definition net.h:124
std::vector< u8 > state
Definition net.h:79
u16 rng_data
Definition net.h:61