Path: blob/master/modules/multiplayer/scene_replication_interface.h
20929 views
/**************************************************************************/1/* scene_replication_interface.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "multiplayer_spawner.h"33#include "multiplayer_synchronizer.h"3435#include "core/object/ref_counted.h"36#include "core/templates/rb_set.h"3738class SceneMultiplayer;39class SceneCacheInterface;4041class SceneReplicationInterface : public RefCounted {42GDCLASS(SceneReplicationInterface, RefCounted);4344private:45struct TrackedNode {46ObjectID id;47uint32_t net_id = 0;48uint32_t remote_peer = 0;49ObjectID spawner;50HashSet<ObjectID> synchronizers;5152bool operator==(const ObjectID &p_other) { return id == p_other; }5354TrackedNode() {}55TrackedNode(const ObjectID &p_id) { id = p_id; }56TrackedNode(const ObjectID &p_id, uint32_t p_net_id) {57id = p_id;58net_id = p_net_id;59}60};6162struct PeerInfo {63HashSet<ObjectID> sync_nodes;64HashSet<ObjectID> spawn_nodes;65HashMap<ObjectID, uint64_t> last_watch_usecs;66HashMap<uint32_t, ObjectID> recv_sync_ids;67HashMap<uint32_t, ObjectID> recv_nodes;68uint16_t last_sent_sync = 0;69};7071// Replication state.72HashMap<int, PeerInfo> peers_info;73uint32_t last_net_id = 0;74HashMap<ObjectID, TrackedNode> tracked_nodes;75RBSet<ObjectID> spawned_nodes;76HashSet<ObjectID> sync_nodes;7778// Pending local spawn information (handles spawning nested nodes during ready).79HashSet<ObjectID> spawn_queue;8081// Pending remote spawn information.82ObjectID pending_spawn;83int pending_spawn_remote = 0;84const uint8_t *pending_buffer = nullptr;85int pending_buffer_size = 0;86List<uint32_t> pending_sync_net_ids;8788// Replicator config.89SceneMultiplayer *multiplayer = nullptr;90SceneCacheInterface *multiplayer_cache = nullptr;91PackedByteArray packet_cache;92int sync_mtu = 1350; // Highly dependent on underlying protocol.93int delta_mtu = 65535;9495TrackedNode &_track(const ObjectID &p_id);96void _untrack(const ObjectID &p_id);97void _node_ready(const ObjectID &p_oid);9899bool _has_authority(const Node *p_node);100bool _verify_synchronizer(int p_peer, MultiplayerSynchronizer *p_sync, uint32_t &r_net_id);101MultiplayerSynchronizer *_find_synchronizer(int p_peer, uint32_t p_net_ida);102103void _send_sync(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint16_t p_sync_net_time, uint64_t p_usec);104void _send_delta(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint64_t p_usec, const HashMap<ObjectID, uint64_t> &p_last_watch_usecs);105Error _make_spawn_packet(Node *p_node, MultiplayerSpawner *p_spawner, int &r_len);106Error _make_despawn_packet(Node *p_node, int &r_len);107Error _send_raw(const uint8_t *p_buffer, int p_size, int p_peer, bool p_reliable);108109void _visibility_changed(int p_peer, ObjectID p_oid);110Error _update_sync_visibility(int p_peer, MultiplayerSynchronizer *p_sync);111Error _update_spawn_visibility(int p_peer, const ObjectID &p_oid);112void _free_remotes(const PeerInfo &p_info);113114template <typename T>115static T *get_id_as(const ObjectID &p_id) {116return p_id.is_valid() ? ObjectDB::get_instance<T>(p_id) : nullptr;117}118119#ifdef DEBUG_ENABLED120_FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);121#endif122123public:124static void make_default();125126void on_reset();127void on_peer_change(int p_id, bool p_connected);128129Error on_spawn(Object *p_obj, Variant p_config);130Error on_despawn(Object *p_obj, Variant p_config);131Error on_replication_start(Object *p_obj, Variant p_config);132Error on_replication_stop(Object *p_obj, Variant p_config);133void on_network_process();134135Error on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);136Error on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);137Error on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);138Error on_delta_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);139140bool is_rpc_visible(const ObjectID &p_oid, int p_peer) const;141142void set_max_sync_packet_size(int p_size);143int get_max_sync_packet_size() const;144145void set_max_delta_packet_size(int p_size);146int get_max_delta_packet_size() const;147148SceneReplicationInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache) {149multiplayer = p_multiplayer;150multiplayer_cache = p_cache;151}152};153154155