2016-10-10 19:50:51 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* networked_multiplayer_enet.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 14:16:55 +02:00
|
|
|
/* https://godotengine.org */
|
2016-10-10 19:50:51 +02:00
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-10-10 19:50:51 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2016-08-14 18:29:25 +02:00
|
|
|
#ifndef NETWORKED_MULTIPLAYER_ENET_H
|
|
|
|
#define NETWORKED_MULTIPLAYER_ENET_H
|
|
|
|
|
2020-01-13 16:51:17 +01:00
|
|
|
#include "core/crypto/crypto.h"
|
2018-09-11 18:13:45 +02:00
|
|
|
#include "core/io/compression.h"
|
|
|
|
#include "core/io/networked_multiplayer_peer.h"
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2017-03-05 19:12:22 +01:00
|
|
|
#include <enet/enet.h>
|
|
|
|
|
2016-08-14 18:29:25 +02:00
|
|
|
class NetworkedMultiplayerENet : public NetworkedMultiplayerPeer {
|
2019-03-19 19:35:57 +01:00
|
|
|
GDCLASS(NetworkedMultiplayerENet, NetworkedMultiplayerPeer);
|
|
|
|
|
2016-08-22 06:14:08 +02:00
|
|
|
public:
|
|
|
|
enum CompressionMode {
|
|
|
|
COMPRESS_NONE,
|
|
|
|
COMPRESS_RANGE_CODER,
|
|
|
|
COMPRESS_FASTLZ,
|
2017-06-09 03:43:56 +02:00
|
|
|
COMPRESS_ZLIB,
|
|
|
|
COMPRESS_ZSTD
|
2016-08-22 06:14:08 +02:00
|
|
|
};
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
private:
|
2016-08-19 21:48:08 +02:00
|
|
|
enum {
|
|
|
|
SYSMSG_ADD_PEER,
|
|
|
|
SYSMSG_REMOVE_PEER
|
|
|
|
};
|
|
|
|
|
2016-09-30 01:23:54 +02:00
|
|
|
enum {
|
|
|
|
SYSCH_CONFIG,
|
|
|
|
SYSCH_RELIABLE,
|
|
|
|
SYSCH_UNRELIABLE,
|
|
|
|
SYSCH_MAX
|
|
|
|
};
|
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
bool active = false;
|
|
|
|
bool server = false;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
uint32_t unique_id = 0;
|
2016-08-19 21:48:08 +02:00
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
int target_peer = 0;
|
|
|
|
TransferMode transfer_mode = TRANSFER_MODE_RELIABLE;
|
|
|
|
int transfer_channel = -1;
|
|
|
|
int channel_count = SYSCH_MAX;
|
|
|
|
bool always_ordered = false;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
|
|
|
ENetEvent event;
|
2021-02-08 10:57:18 +01:00
|
|
|
ENetPeer *peer = nullptr;
|
|
|
|
ENetHost *host = nullptr;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
bool refuse_connections = false;
|
|
|
|
bool server_relay = true;
|
2016-08-19 21:48:08 +02:00
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
|
2016-08-14 19:06:51 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Map<int, ENetPeer *> peer_map;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
|
|
|
struct Packet {
|
2021-02-08 10:57:18 +01:00
|
|
|
ENetPacket *packet = nullptr;
|
|
|
|
int from = 0;
|
|
|
|
int channel = 0;
|
2016-08-14 18:29:25 +02:00
|
|
|
};
|
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
CompressionMode compression_mode = COMPRESS_NONE;
|
2016-08-22 06:14:08 +02:00
|
|
|
|
2017-12-15 16:05:42 +01:00
|
|
|
List<Packet> incoming_packets;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2017-12-15 16:05:42 +01:00
|
|
|
Packet current_packet;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2016-08-19 21:48:08 +02:00
|
|
|
uint32_t _gen_unique_id() const;
|
2017-12-15 16:05:42 +01:00
|
|
|
void _pop_current_packet();
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2016-08-22 06:14:08 +02:00
|
|
|
Vector<uint8_t> src_compressor_mem;
|
|
|
|
Vector<uint8_t> dst_compressor_mem;
|
|
|
|
|
|
|
|
ENetCompressor enet_compressor;
|
2017-03-05 16:44:50 +01:00
|
|
|
static size_t enet_compress(void *context, const ENetBuffer *inBuffers, size_t inBufferCount, size_t inLimit, enet_uint8 *outData, size_t outLimit);
|
|
|
|
static size_t enet_decompress(void *context, const enet_uint8 *inData, size_t inLimit, enet_uint8 *outData, size_t outLimit);
|
|
|
|
static void enet_compressor_destroy(void *context);
|
2016-08-22 06:14:08 +02:00
|
|
|
void _setup_compressor();
|
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
IPAddress bind_ip;
|
2017-03-05 16:44:50 +01:00
|
|
|
|
2021-02-08 10:57:18 +01:00
|
|
|
bool dtls_enabled = false;
|
2020-01-13 16:51:17 +01:00
|
|
|
Ref<CryptoKey> dtls_key;
|
|
|
|
Ref<X509Certificate> dtls_cert;
|
2021-02-08 10:57:18 +01:00
|
|
|
bool dtls_verify = true;
|
2020-01-13 16:51:17 +01:00
|
|
|
|
2016-08-14 18:29:25 +02:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual void set_transfer_mode(TransferMode p_mode) override;
|
|
|
|
virtual TransferMode get_transfer_mode() const override;
|
|
|
|
virtual void set_target_peer(int p_peer) override;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual int get_packet_peer() const override;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
virtual IPAddress get_peer_address(int p_peer_id) const;
|
2018-04-10 17:52:10 +02:00
|
|
|
virtual int get_peer_port(int p_peer_id) const;
|
2021-03-29 18:14:50 +02:00
|
|
|
virtual int get_local_port() const;
|
2020-01-20 07:43:12 +01:00
|
|
|
void set_peer_timeout(int p_peer_id, int p_timeout_limit, int p_timeout_min, int p_timeout_max);
|
2018-04-10 17:52:10 +02:00
|
|
|
|
2017-08-12 18:52:50 +02:00
|
|
|
Error create_server(int p_port, int p_max_clients = 32, int p_in_bandwidth = 0, int p_out_bandwidth = 0);
|
2021-03-29 18:14:50 +02:00
|
|
|
Error create_client(const String &p_address, int p_port, int p_in_bandwidth = 0, int p_out_bandwidth = 0, int p_local_port = 0);
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2018-05-07 00:28:59 +02:00
|
|
|
void close_connection(uint32_t wait_usec = 100);
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2018-04-08 22:45:14 +02:00
|
|
|
void disconnect_peer(int p_peer, bool now = false);
|
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual void poll() override;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual bool is_server() const override;
|
2016-08-14 23:49:50 +02:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual int get_available_packet_count() const override;
|
|
|
|
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
|
|
|
|
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual int get_max_packet_size() const override;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual ConnectionStatus get_connection_status() const override;
|
2016-08-14 18:29:25 +02:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual void set_refuse_new_connections(bool p_enable) override;
|
|
|
|
virtual bool is_refusing_new_connections() const override;
|
2016-08-19 21:48:08 +02:00
|
|
|
|
2020-07-10 12:34:39 +02:00
|
|
|
virtual int get_unique_id() const override;
|
2016-08-19 21:48:08 +02:00
|
|
|
|
2016-08-22 06:14:08 +02:00
|
|
|
void set_compression_mode(CompressionMode p_mode);
|
|
|
|
CompressionMode get_compression_mode() const;
|
|
|
|
|
2018-05-12 19:42:00 +02:00
|
|
|
int get_packet_channel() const;
|
|
|
|
int get_last_packet_channel() const;
|
|
|
|
void set_transfer_channel(int p_channel);
|
|
|
|
int get_transfer_channel() const;
|
|
|
|
void set_channel_count(int p_channel);
|
|
|
|
int get_channel_count() const;
|
|
|
|
void set_always_ordered(bool p_ordered);
|
|
|
|
bool is_always_ordered() const;
|
2019-11-26 17:21:19 +01:00
|
|
|
void set_server_relay_enabled(bool p_enabled);
|
|
|
|
bool is_server_relay_enabled() const;
|
2018-05-12 19:42:00 +02:00
|
|
|
|
2016-08-14 18:29:25 +02:00
|
|
|
NetworkedMultiplayerENet();
|
|
|
|
~NetworkedMultiplayerENet();
|
2016-08-27 13:41:34 +02:00
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
void set_bind_ip(const IPAddress &p_ip);
|
2020-01-13 16:51:17 +01:00
|
|
|
void set_dtls_enabled(bool p_enabled);
|
|
|
|
bool is_dtls_enabled() const;
|
|
|
|
void set_dtls_verify_enabled(bool p_enabled);
|
|
|
|
bool is_dtls_verify_enabled() const;
|
|
|
|
void set_dtls_key(Ref<CryptoKey> p_key);
|
|
|
|
void set_dtls_certificate(Ref<X509Certificate> p_cert);
|
2016-08-14 18:29:25 +02:00
|
|
|
};
|
|
|
|
|
2016-08-22 06:14:08 +02:00
|
|
|
VARIANT_ENUM_CAST(NetworkedMultiplayerENet::CompressionMode);
|
|
|
|
|
2016-08-14 18:29:25 +02:00
|
|
|
#endif // NETWORKED_MULTIPLAYER_ENET_H
|