Path: blob/master/modules/enet/enet_multiplayer_peer.h
10277 views
/**************************************************************************/1/* enet_multiplayer_peer.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 "enet_connection.h"3334#include "core/crypto/crypto.h"35#include "scene/main/multiplayer_peer.h"3637#include <enet/enet.h>3839class ENetMultiplayerPeer : public MultiplayerPeer {40GDCLASS(ENetMultiplayerPeer, MultiplayerPeer);4142private:43enum {44SYSMSG_ADD_PEER,45SYSMSG_REMOVE_PEER46};4748enum {49SYSCH_RELIABLE = 0,50SYSCH_UNRELIABLE = 1,51SYSCH_MAX = 252};5354enum Mode {55MODE_NONE,56MODE_SERVER,57MODE_CLIENT,58MODE_MESH,59};6061Mode active_mode = MODE_NONE;6263uint32_t unique_id = 0;6465int target_peer = 0;6667ConnectionStatus connection_status = CONNECTION_DISCONNECTED;6869HashMap<int, Ref<ENetConnection>> hosts;70HashMap<int, Ref<ENetPacketPeer>> peers;7172struct Packet {73ENetPacket *packet = nullptr;74int from = 0;75int channel = 0;76TransferMode transfer_mode = TRANSFER_MODE_RELIABLE;77};7879List<Packet> incoming_packets;8081Packet current_packet;8283void _store_packet(int32_t p_source, ENetConnection::Event &p_event);84void _pop_current_packet();85void _disconnect_inactive_peers();86void _destroy_unused(ENetPacket *p_packet);87_FORCE_INLINE_ bool _is_active() const { return active_mode != MODE_NONE; }8889IPAddress bind_ip;9091protected:92static void _bind_methods();9394public:95virtual void set_target_peer(int p_peer) override;9697virtual int get_packet_peer() const override;98virtual TransferMode get_packet_mode() const override;99virtual int get_packet_channel() const override;100101virtual void poll() override;102virtual void close() override;103virtual void disconnect_peer(int p_peer, bool p_force = false) override;104105virtual bool is_server() const override;106virtual bool is_server_relay_supported() const override;107108// Overridden so we can instrument the DTLSServer when needed.109virtual void set_refuse_new_connections(bool p_enabled) override;110111virtual ConnectionStatus get_connection_status() const override;112113virtual int get_unique_id() const override;114115virtual int get_max_packet_size() const override;116virtual int get_available_packet_count() const override;117virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;118virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;119120Error create_server(int p_port, int p_max_clients = 32, int p_max_channels = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0);121Error create_client(const String &p_address, int p_port, int p_channel_count = 0, int p_in_bandwidth = 0, int p_out_bandwidth = 0, int p_local_port = 0);122Error create_mesh(int p_id);123Error add_mesh_peer(int p_id, Ref<ENetConnection> p_host);124125void set_bind_ip(const IPAddress &p_ip);126127Ref<ENetConnection> get_host() const;128Ref<ENetPacketPeer> get_peer(int p_id) const;129130ENetMultiplayerPeer();131~ENetMultiplayerPeer();132};133134135