Skip to content

Commit a3c99c3

Browse files
committed
Fixed all the issues with SyncGroups.
1 parent 3c13a27 commit a3c99c3

5 files changed

Lines changed: 181 additions & 82 deletions

File tree

net_utilities.cpp

Lines changed: 86 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,21 @@ void NS::SyncGroup::remove_listening_peer(int p_peer) {
7272
}
7373

7474
uint32_t NS::SyncGroup::add_new_sync_object(ObjectData *p_object_data, bool p_is_simulated) {
75-
if (p_object_data->get_controlled_by_peer() != -1) {
75+
if (p_is_simulated) {
76+
// Make sure the node is not contained into the trickled sync.
77+
const int tso_index = trickled_sync_objects.find(p_object_data);
78+
if (tso_index >= 0) {
79+
remove_sync_object(tso_index, false);
80+
}
81+
} else {
82+
// Make sure the node is not contained into the realtime sync.
83+
const int rsn_index = simulated_sync_objects.find(p_object_data);
84+
if (rsn_index >= 0) {
85+
remove_sync_object(rsn_index, true);
86+
}
87+
}
88+
89+
if (p_object_data->get_controlled_by_peer() > 0) {
7690
// This is a controller with an associated peer, update the networked_peer list.
7791
// Regardless if it's simulated or not.
7892
const int peer = p_object_data->get_controlled_by_peer();
@@ -82,12 +96,6 @@ uint32_t NS::SyncGroup::add_new_sync_object(ObjectData *p_object_data, bool p_is
8296
}
8397

8498
if (p_is_simulated) {
85-
// Make sure the node is not contained into the trickled sync.
86-
const int tso_index = trickled_sync_objects.find(p_object_data);
87-
if (tso_index >= 0) {
88-
remove_sync_object(tso_index, false);
89-
}
90-
9199
// Add it into the realtime sync nodes
92100
int index = simulated_sync_objects.find(p_object_data);
93101

@@ -112,12 +120,6 @@ uint32_t NS::SyncGroup::add_new_sync_object(ObjectData *p_object_data, bool p_is
112120

113121
return index;
114122
} else {
115-
// Make sure the node is not contained into the realtime sync.
116-
const int rsn_index = simulated_sync_objects.find(p_object_data);
117-
if (rsn_index >= 0) {
118-
remove_sync_object(rsn_index, true);
119-
}
120-
121123
// Add it into the trickled sync nodes
122124
int index = trickled_sync_objects.find(p_object_data);
123125

@@ -153,45 +155,7 @@ void NS::SyncGroup::remove_sync_object(std::size_t p_index, bool p_is_simulated)
153155
trickled_sync_objects_list_changed = true;
154156
}
155157

156-
if (associted_peer > 0) {
157-
// If no other simulated objects controlled by `associated_peer` remove it from
158-
bool is_simulating = false;
159-
bool is_networking = false;
160-
161-
for (auto &soi : simulated_sync_objects) {
162-
if (soi.od->get_controlled_by_peer() == associted_peer) {
163-
is_networking = true;
164-
is_simulating = true;
165-
break;
166-
}
167-
}
168-
169-
if (!is_networking) {
170-
if (NS::VecFunc::has(listening_peers, associted_peer)) {
171-
is_networking = true;
172-
}
173-
}
174-
175-
if (!is_networking) {
176-
for (auto &toi : trickled_sync_objects) {
177-
if (toi.od->get_controlled_by_peer() == associted_peer) {
178-
is_networking = true;
179-
break;
180-
}
181-
}
182-
}
183-
184-
if (!is_simulating) {
185-
// No other objects associated to this peer are simulated, remove from simulating peers.
186-
VecFunc::remove_unordered(simulating_peers, associted_peer);
187-
update_listeners_to_simulating_peer(associted_peer, false);
188-
}
189-
190-
if (!is_networking) {
191-
NS::VecFunc::remove_unordered(networked_peers, associted_peer);
192-
NS::VecFunc::remove_unordered(peers_with_newly_calculated_latency, associted_peer);
193-
}
194-
}
158+
validate_peer_association(associted_peer);
195159
}
196160

197161
void NS::SyncGroup::remove_sync_object(const ObjectData &p_object_data) {
@@ -324,6 +288,38 @@ void NS::SyncGroup::notify_peer_has_newly_calculated_latency(int p_peer) {
324288
}
325289
}
326290

291+
void NS::SyncGroup::notify_controller_changed(NS::ObjectData *p_object_data, int p_previous_controlling_peer) {
292+
if (p_object_data->get_controlled_by_peer() == p_previous_controlling_peer) {
293+
return;
294+
}
295+
296+
bool is_in_this_sync_group = false;
297+
bool is_simulated = false;
298+
if (simulated_sync_objects.find(p_object_data) != -1) {
299+
is_in_this_sync_group = true;
300+
is_simulated = true;
301+
} else if (trickled_sync_objects.find(p_object_data) != -1) {
302+
is_in_this_sync_group = true;
303+
}
304+
305+
if (is_in_this_sync_group) {
306+
validate_peer_association(p_previous_controlling_peer);
307+
308+
if (p_object_data->get_controlled_by_peer() > 0) {
309+
const int peer = p_object_data->get_controlled_by_peer();
310+
311+
if (is_simulated) {
312+
VecFunc::insert_unique(simulating_peers, peer);
313+
update_listeners_to_simulating_peer(peer, true);
314+
}
315+
316+
if (NS::VecFunc::insert_unique(networked_peers, peer)) {
317+
NS::VecFunc::insert_unique(peers_with_newly_calculated_latency, peer);
318+
}
319+
}
320+
}
321+
}
322+
327323
void NS::SyncGroup::notify_simulating_peers_about_listener_status(int p_peer_listener, bool p_simulating) {
328324
for (int peer : simulating_peers) {
329325
NetworkedControllerBase *controller = scene_sync->get_controller_for_peer(peer);
@@ -338,6 +334,44 @@ void NS::SyncGroup::update_listeners_to_simulating_peer(int p_simulating_peer, b
338334
}
339335
}
340336

337+
void NS::SyncGroup::validate_peer_association(int p_peer) {
338+
if (p_peer <= 0) {
339+
return;
340+
}
341+
342+
// If no other simulated objects controlled by `associated_peer` remove it from
343+
bool is_simulating = false;
344+
bool is_networking = false;
345+
346+
for (auto &soi : simulated_sync_objects) {
347+
if (soi.od->get_controlled_by_peer() == p_peer) {
348+
is_networking = true;
349+
is_simulating = true;
350+
break;
351+
}
352+
}
353+
354+
if (!is_networking) {
355+
for (auto &toi : trickled_sync_objects) {
356+
if (toi.od->get_controlled_by_peer() == p_peer) {
357+
is_networking = true;
358+
break;
359+
}
360+
}
361+
}
362+
363+
if (!is_simulating) {
364+
// No other objects associated to this peer are simulated, remove from simulating peers.
365+
VecFunc::remove_unordered(simulating_peers, p_peer);
366+
update_listeners_to_simulating_peer(p_peer, false);
367+
}
368+
369+
if (!is_networking) {
370+
NS::VecFunc::remove_unordered(networked_peers, p_peer);
371+
NS::VecFunc::remove_unordered(peers_with_newly_calculated_latency, p_peer);
372+
}
373+
}
374+
341375
std::size_t NS::SyncGroup::find_simulated(const struct ObjectData &p_object_data) const {
342376
std::size_t i = 0;
343377
for (auto sso : simulated_sync_objects) {

net_utilities.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ struct SyncGroup {
566566
void remove_listening_peer(int p_peer);
567567
const std::vector<int> &get_listening_peers() const { return listening_peers; };
568568

569+
const std::vector<int> &get_networked_peers() const { return networked_peers; }
569570
const std::vector<int> &get_simulating_peers() const { return simulating_peers; }
570571

571572
/// Returns the `index` or `UINT32_MAX` on error.
@@ -585,12 +586,18 @@ struct SyncGroup {
585586

586587
void notify_peer_has_newly_calculated_latency(int p_peer);
587588

589+
void notify_controller_changed(NS::ObjectData *p_object_data, int p_previous_controlling_peer);
590+
588591
// Used when a new listener is added or removed.
589592
void notify_simulating_peers_about_listener_status(int p_peer_listener, bool p_simulating);
590593
// Used when a new simulated object (which is controlled) is added, to notify
591594
// such controller about the listeners.
592595
void update_listeners_to_simulating_peer(int p_simulating_peer, bool p_simulating);
593596

597+
// Removes the peer from this sync group if it's not associated to any object
598+
// data into this group.
599+
void validate_peer_association(int p_peer);
600+
594601
private:
595602
std::size_t find_simulated(const struct ObjectData &p_object_data) const;
596603
std::size_t find_trickled(const struct ObjectData &p_object_data) const;

scene_synchronizer.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,16 @@ void SceneSynchronizerBase::setup_controller(
384384
NS::ObjectData *object_data = get_object_data(p_id);
385385
ENSURE(object_data != nullptr);
386386

387+
const int previous_controlling_peer = object_data->get_controlled_by_peer();
387388
object_data->set_controlled_by_peer(p_peer, p_collect_input_func, p_count_input_size_func, p_are_inputs_different_func, p_process_func);
388389

389390
NetworkedControllerBase *controller = get_controller_for_peer(p_peer);
390391
if (controller) {
391392
controller->notify_controllable_objects_changed();
392393
}
393394

394-
if (is_server()) {
395-
static_cast<ServerSynchronizer *>(synchronizer)->notify_need_full_snapshot(p_peer, true);
395+
if (synchronizer) {
396+
synchronizer->on_object_data_controller_changed(object_data, previous_controlling_peer);
396397
}
397398
}
398399

@@ -1760,6 +1761,20 @@ void ServerSynchronizer::on_object_data_removed(NS::ObjectData &p_object_data) {
17601761
}
17611762
}
17621763

1764+
void ServerSynchronizer::on_object_data_controller_changed(NS::ObjectData *p_object_data, int p_previous_controlling_peer) {
1765+
if (p_object_data->get_controlled_by_peer() == p_previous_controlling_peer) {
1766+
return;
1767+
}
1768+
1769+
if (p_object_data->get_controlled_by_peer() > 0) {
1770+
notify_need_full_snapshot(p_object_data->get_controlled_by_peer(), true);
1771+
}
1772+
1773+
for (SyncGroup &sync_group : sync_groups) {
1774+
sync_group.notify_controller_changed(p_object_data, p_previous_controlling_peer);
1775+
}
1776+
}
1777+
17631778
void ServerSynchronizer::on_variable_added(NS::ObjectData *p_object_data, const std::string &p_var_name) {
17641779
#ifdef DEBUG_ENABLED
17651780
// Can't happen on server

scene_synchronizer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,7 @@ class Synchronizer {
529529
virtual void on_peer_disconnected(int p_peer_id) {}
530530
virtual void on_object_data_added(NS::ObjectData *p_object_data) {}
531531
virtual void on_object_data_removed(NS::ObjectData &p_object_data) {}
532+
virtual void on_object_data_controller_changed(NS::ObjectData *p_object_data, int p_previous_controlling_peer) {}
532533
virtual void on_variable_added(NS::ObjectData *p_object_data, const std::string &p_var_name) {}
533534
virtual void on_variable_changed(NS::ObjectData *p_object_data, VarId p_var_id, const VarData &p_old_value, int p_flag) {}
534535
virtual void on_controller_reset(NetworkedControllerBase &p_controller) {}
@@ -588,6 +589,7 @@ class ServerSynchronizer final : public Synchronizer {
588589
virtual void on_peer_disconnected(int p_peer_id) override;
589590
virtual void on_object_data_added(NS::ObjectData *p_object_data) override;
590591
virtual void on_object_data_removed(NS::ObjectData &p_object_data) override;
592+
virtual void on_object_data_controller_changed(NS::ObjectData *p_object_data, int p_previous_controlling_peer) override;
591593
virtual void on_variable_added(NS::ObjectData *p_object_data, const std::string &p_var_name) override;
592594
virtual void on_variable_changed(NS::ObjectData *p_object_data, VarId p_var_id, const VarData &p_old_value, int p_flag) override;
593595
virtual const std::vector<ObjectData *> &get_active_objects() const override { return active_objects; }

0 commit comments

Comments
 (0)