Path: blob/master/modules/multiplayer/scene_rpc_interface.h
10277 views
/**************************************************************************/1/* scene_rpc_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 "core/object/ref_counted.h"33#include "scene/main/multiplayer_api.h"3435class SceneMultiplayer;36class SceneCacheInterface;37class SceneReplicationInterface;38class Node;3940class SceneRPCInterface : public RefCounted {41GDCLASS(SceneRPCInterface, RefCounted);4243private:44struct RPCConfig {45StringName name;46MultiplayerAPI::RPCMode rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;47bool call_local = false;48MultiplayerPeer::TransferMode transfer_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE;49int channel = 0;5051bool operator==(RPCConfig const &p_other) const {52return name == p_other.name;53}54};5556struct RPCConfigCache {57HashMap<uint16_t, RPCConfig> configs;58HashMap<StringName, uint16_t> ids;59};6061struct SortRPCConfig {62StringName::AlphCompare compare;63bool operator()(const RPCConfig &p_a, const RPCConfig &p_b) const {64return compare(p_a.name, p_b.name);65}66};6768enum NetworkNodeIdCompression {69NETWORK_NODE_ID_COMPRESSION_8 = 0,70NETWORK_NODE_ID_COMPRESSION_16,71NETWORK_NODE_ID_COMPRESSION_32,72};7374enum NetworkNameIdCompression {75NETWORK_NAME_ID_COMPRESSION_8 = 0,76NETWORK_NAME_ID_COMPRESSION_16,77};7879SceneMultiplayer *multiplayer = nullptr;80SceneCacheInterface *multiplayer_cache = nullptr;81SceneReplicationInterface *multiplayer_replicator = nullptr;8283Vector<uint8_t> packet_cache;8485HashMap<ObjectID, RPCConfigCache> rpc_cache;8687#ifdef DEBUG_ENABLED88_FORCE_INLINE_ void _profile_node_data(const String &p_what, ObjectID p_id, int p_size);89#endif9091protected:92void _process_rpc(Node *p_node, const uint16_t p_rpc_method_id, int p_from, const uint8_t *p_packet, int p_packet_len, int p_offset);9394void _send_rpc(Node *p_from, int p_to, uint16_t p_rpc_id, const RPCConfig &p_config, const StringName &p_name, const Variant **p_arg, int p_argcount);95Node *_process_get_node(int p_from, const uint8_t *p_packet, uint32_t p_node_target, int p_packet_len);9697void _parse_rpc_config(const Variant &p_config, bool p_for_node, RPCConfigCache &r_cache);98const RPCConfigCache &_get_node_config(const Node *p_node);99100public:101Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount);102void process_rpc(int p_from, const uint8_t *p_packet, int p_packet_len);103String get_rpc_md5(const Object *p_obj);104105SceneRPCInterface(SceneMultiplayer *p_multiplayer, SceneCacheInterface *p_cache, SceneReplicationInterface *p_replicator) {106multiplayer = p_multiplayer;107multiplayer_cache = p_cache;108multiplayer_replicator = p_replicator;109}110};111112113