Path: blob/master/modules/multiplayer/scene_multiplayer.h
10277 views
/**************************************************************************/1/* scene_multiplayer.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 "scene_cache_interface.h"33#include "scene_replication_interface.h"34#include "scene_rpc_interface.h"3536#include "scene/main/multiplayer_api.h"3738class OfflineMultiplayerPeer : public MultiplayerPeer {39GDCLASS(OfflineMultiplayerPeer, MultiplayerPeer);4041public:42virtual int get_available_packet_count() const override { return 0; }43virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override {44*r_buffer = nullptr;45r_buffer_size = 0;46return OK;47}48virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override { return OK; }49virtual int get_max_packet_size() const override { return 0; }5051virtual void set_target_peer(int p_peer_id) override {}52virtual int get_packet_peer() const override { return 0; }53virtual TransferMode get_packet_mode() const override { return TRANSFER_MODE_RELIABLE; }54virtual int get_packet_channel() const override { return 0; }55virtual void disconnect_peer(int p_peer, bool p_force = false) override {}56virtual bool is_server() const override { return true; }57virtual void poll() override {}58virtual void close() override {}59virtual int get_unique_id() const override { return TARGET_PEER_SERVER; }60virtual ConnectionStatus get_connection_status() const override { return CONNECTION_CONNECTED; }61};6263class SceneMultiplayer : public MultiplayerAPI {64GDCLASS(SceneMultiplayer, MultiplayerAPI);6566public:67enum NetworkCommands {68NETWORK_COMMAND_REMOTE_CALL = 0,69NETWORK_COMMAND_SIMPLIFY_PATH,70NETWORK_COMMAND_CONFIRM_PATH,71NETWORK_COMMAND_RAW,72NETWORK_COMMAND_SPAWN,73NETWORK_COMMAND_DESPAWN,74NETWORK_COMMAND_SYNC,75NETWORK_COMMAND_SYS,76};7778enum SysCommands {79SYS_COMMAND_AUTH,80SYS_COMMAND_ADD_PEER,81SYS_COMMAND_DEL_PEER,82SYS_COMMAND_RELAY,83};8485enum {86SYS_CMD_SIZE = 6, // Command + sys command + peer_id (+ optional payload).87};8889// For each command, the 4 MSB can contain custom flags, as defined by subsystems.90enum {91CMD_FLAG_0_SHIFT = 4,92CMD_FLAG_1_SHIFT = 5,93CMD_FLAG_2_SHIFT = 6,94CMD_FLAG_3_SHIFT = 7,95};9697// This is the mask that will be used to extract the command.98enum {99CMD_MASK = 7, // 0x7 -> 0b00000111100};101102private:103struct PendingPeer {104bool local = false;105bool remote = false;106uint64_t time = 0;107};108109Ref<MultiplayerPeer> multiplayer_peer;110MultiplayerPeer::ConnectionStatus last_connection_status = MultiplayerPeer::CONNECTION_DISCONNECTED;111HashMap<int, PendingPeer> pending_peers; // true if locally finalized.112Callable auth_callback;113uint64_t auth_timeout = 3000;114HashSet<int> connected_peers;115int remote_sender_id = 0;116int remote_sender_override = 0;117118Vector<uint8_t> packet_cache;119120NodePath root_path;121bool allow_object_decoding = false;122bool server_relay = true;123Ref<StreamPeerBuffer> relay_buffer;124125Ref<SceneCacheInterface> cache;126Ref<SceneReplicationInterface> replicator;127Ref<SceneRPCInterface> rpc;128129#ifdef DEBUG_ENABLED130_FORCE_INLINE_ void _profile_bandwidth(const String &p_what, int p_value);131_FORCE_INLINE_ Error _send(const uint8_t *p_packet, int p_packet_len); // Also profiles.132#else133_FORCE_INLINE_ Error _send(const uint8_t *p_packet, int p_packet_len) {134return multiplayer_peer->put_packet(p_packet, p_packet_len);135}136#endif137138protected:139static void _bind_methods();140141void _process_packet(int p_from, const uint8_t *p_packet, int p_packet_len);142void _process_raw(int p_from, const uint8_t *p_packet, int p_packet_len);143void _process_sys(int p_from, const uint8_t *p_packet, int p_packet_len, MultiplayerPeer::TransferMode p_mode, int p_channel);144145void _add_peer(int p_id);146void _admit_peer(int p_id);147void _del_peer(int p_id);148void _update_status();149150public:151virtual void set_multiplayer_peer(const Ref<MultiplayerPeer> &p_peer) override;152virtual Ref<MultiplayerPeer> get_multiplayer_peer() override;153154virtual Error poll() override;155virtual int get_unique_id() override;156virtual Vector<int> get_peer_ids() override;157virtual int get_remote_sender_id() override { return remote_sender_override ? remote_sender_override : remote_sender_id; }158159virtual Error rpcp(Object *p_obj, int p_peer_id, const StringName &p_method, const Variant **p_arg, int p_argcount) override;160161virtual Error object_configuration_add(Object *p_obj, Variant p_config) override;162virtual Error object_configuration_remove(Object *p_obj, Variant p_config) override;163164void clear();165166// Usually from object_configuration_add/remove167void set_root_path(const NodePath &p_path);168NodePath get_root_path() const;169170void disconnect_peer(int p_id);171172Error send_auth(int p_to, Vector<uint8_t> p_bytes);173Error complete_auth(int p_peer);174void set_auth_callback(Callable p_callback);175Callable get_auth_callback() const;176void set_auth_timeout(double p_timeout);177double get_auth_timeout() const;178Vector<int> get_authenticating_peer_ids();179180Error send_command(int p_to, const uint8_t *p_packet, int p_packet_len); // Used internally to relay packets when needed.181Error send_bytes(Vector<uint8_t> p_data, int p_to = MultiplayerPeer::TARGET_PEER_BROADCAST, MultiplayerPeer::TransferMode p_mode = MultiplayerPeer::TRANSFER_MODE_RELIABLE, int p_channel = 0);182String get_rpc_md5(const Object *p_obj);183184const HashSet<int> get_connected_peers() const { return connected_peers; }185186void set_remote_sender_override(int p_id) { remote_sender_override = p_id; }187void set_refuse_new_connections(bool p_refuse);188bool is_refusing_new_connections() const;189190void set_allow_object_decoding(bool p_enable);191bool is_object_decoding_allowed() const;192193void set_server_relay_enabled(bool p_enabled);194bool is_server_relay_enabled() const;195196void set_max_sync_packet_size(int p_size);197int get_max_sync_packet_size() const;198199void set_max_delta_packet_size(int p_size);200int get_max_delta_packet_size() const;201202SceneMultiplayer();203~SceneMultiplayer();204};205206207