Path: blob/master/modules/multiplayer/scene_replication_interface.h
10277 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"3637class SceneMultiplayer;38class SceneCacheInterface;3940class SceneReplicationInterface : public RefCounted {41GDCLASS(SceneReplicationInterface, RefCounted);4243private:44struct TrackedNode {45ObjectID id;46uint32_t net_id = 0;47uint32_t remote_peer = 0;48ObjectID spawner;49HashSet<ObjectID> synchronizers;5051bool operator==(const ObjectID &p_other) { return id == p_other; }5253TrackedNode() {}54TrackedNode(const ObjectID &p_id) { id = p_id; }55TrackedNode(const ObjectID &p_id, uint32_t p_net_id) {56id = p_id;57net_id = p_net_id;58}59};6061struct PeerInfo {62HashSet<ObjectID> sync_nodes;63HashSet<ObjectID> spawn_nodes;64HashMap<ObjectID, uint64_t> last_watch_usecs;65HashMap<uint32_t, ObjectID> recv_sync_ids;66HashMap<uint32_t, ObjectID> recv_nodes;67uint16_t last_sent_sync = 0;68};6970// Replication state.71HashMap<int, PeerInfo> peers_info;72uint32_t last_net_id = 0;73HashMap<ObjectID, TrackedNode> tracked_nodes;74RBSet<ObjectID> spawned_nodes;75HashSet<ObjectID> sync_nodes;7677// Pending local spawn information (handles spawning nested nodes during ready).78HashSet<ObjectID> spawn_queue;7980// Pending remote spawn information.81ObjectID pending_spawn;82int pending_spawn_remote = 0;83const uint8_t *pending_buffer = nullptr;84int pending_buffer_size = 0;85List<uint32_t> pending_sync_net_ids;8687// Replicator config.88SceneMultiplayer *multiplayer = nullptr;89SceneCacheInterface *multiplayer_cache = nullptr;90PackedByteArray packet_cache;91int sync_mtu = 1350; // Highly dependent on underlying protocol.92int delta_mtu = 65535;9394TrackedNode &_track(const ObjectID &p_id);95void _untrack(const ObjectID &p_id);96void _node_ready(const ObjectID &p_oid);9798bool _has_authority(const Node *p_node);99bool _verify_synchronizer(int p_peer, MultiplayerSynchronizer *p_sync, uint32_t &r_net_id);100MultiplayerSynchronizer *_find_synchronizer(int p_peer, uint32_t p_net_ida);101102void _send_sync(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint16_t p_sync_net_time, uint64_t p_usec);103void _send_delta(int p_peer, const HashSet<ObjectID> &p_synchronizers, uint64_t p_usec, const HashMap<ObjectID, uint64_t> &p_last_watch_usecs);104Error _make_spawn_packet(Node *p_node, MultiplayerSpawner *p_spawner, int &r_len);105Error _make_despawn_packet(Node *p_node, int &r_len);106Error _send_raw(const uint8_t *p_buffer, int p_size, int p_peer, bool p_reliable);107108void _visibility_changed(int p_peer, ObjectID p_oid);109Error _update_sync_visibility(int p_peer, MultiplayerSynchronizer *p_sync);110Error _update_spawn_visibility(int p_peer, const ObjectID &p_oid);111void _free_remotes(const PeerInfo &p_info);112113template <typename T>114static T *get_id_as(const ObjectID &p_id) {115return p_id.is_valid() ? ObjectDB::get_instance<T>(p_id) : nullptr;116}117118#ifdef DEBUG_ENABLED119_FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);120#endif121122public:123static void make_default();124125void on_reset();126void on_peer_change(int p_id, bool p_connected);127128Error on_spawn(Object *p_obj, Variant p_config);129Error on_despawn(Object *p_obj, Variant p_config);130Error on_replication_start(Object *p_obj, Variant p_config);131Error on_replication_stop(Object *p_obj, Variant p_config);132void on_network_process();133134Error on_spawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);135Error on_despawn_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);136Error on_sync_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);137Error on_delta_receive(int p_from, const uint8_t *p_buffer, int p_buffer_len);138139bool is_rpc_visible(const ObjectID &p_oid, int p_peer) const;140141void set_max_sync_packet_size(int p_size);142int get_max_sync_packet_size() const;143144void set_max_delta_packet_size(int p_size);145int get_max_delta_packet_size() const;146147SceneReplicationInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache) {148multiplayer = p_multiplayer;149multiplayer_cache = p_cache;150}151};152153154