| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "enet_connection.h" |
| 34 |
|
| 35 |
#include "core/crypto/crypto.h" |
| 36 |
#include "scene/main/multiplayer_peer.h" |
| 37 |
|
| 38 |
#include <enet/enet.h> |
| 39 |
|
| 40 |
class ENetMultiplayerPeer : public MultiplayerPeer { |
| 41 |
GDCLASS(ENetMultiplayerPeer, MultiplayerPeer); |
| 42 |
|
| 43 |
private: |
| 44 |
enum { |
| 45 |
SYSMSG_ADD_PEER, |
| 46 |
SYSMSG_REMOVE_PEER |
| 47 |
}; |
| 48 |
|
| 49 |
enum { |
| 50 |
SYSCH_RELIABLE = 0, |
| 51 |
SYSCH_UNRELIABLE = 1, |
| 52 |
SYSCH_MAX = 2 |
| 53 |
}; |
| 54 |
|
| 55 |
enum Mode { |
| 56 |
MODE_NONE, |
| 57 |
MODE_SERVER, |
| 58 |
MODE_CLIENT, |
| 59 |
MODE_MESH, |
| 60 |
}; |
| 61 |
|
| 62 |
Mode active_mode = MODE_NONE; |
| 63 |
|
| 64 |
uint32_t unique_id = 0; |
| 65 |
|
| 66 |
int target_peer = 0; |
| 67 |
|
| 68 |
ConnectionStatus connection_status = CONNECTION_DISCONNECTED; |
| 69 |
|
| 70 |
HashMap<int, Ref<ENetConnection>> hosts; |
| 71 |
HashMap<int, Ref<ENetPacketPeer>> peers; |
| 72 |
|
| 73 |
struct Packet { |
| 74 |
ENetPacket *packet = nullptr; |
| 75 |
int from = 0; |
| 76 |
int channel = 0; |
| 77 |
TransferMode transfer_mode = TRANSFER_MODE_RELIABLE; |
| 78 |
}; |
| 79 |
|
| 80 |
List<Packet> incoming_packets; |
| 81 |
|
| 82 |
Packet current_packet; |
| 83 |
|
| 84 |
void _store_packet(int32_t p_source, ENetConnection::Event &p_event); |
| 85 |
void _pop_current_packet(); |
| 86 |
void _disconnect_inactive_peers(); |
| 87 |
void _destroy_unused(ENetPacket *p_packet); |
| 88 |
_FORCE_INLINE_ bool _is_active() const { return active_mode != MODE_NONE; } |
| 89 |
|
| 90 |
IPAddress bind_ip; |
| 91 |
|
| 92 |
protected: |
| 93 |
static void _bind_methods(); |
| 94 |
|
| 95 |
public: |
| 96 |
virtual void set_target_peer(int p_peer) override; |
| 97 |
|
| 98 |
virtual int get_packet_peer() const override; |
| 99 |
virtual TransferMode get_packet_mode() const override; |
| 100 |
virtual int get_packet_channel() const override; |
| 101 |
|
| 102 |
virtual void poll() override; |
| 103 |
virtual void close() override; |
| 104 |
virtual void disconnect_peer(int p_peer, bool p_force = false) override; |
| 105 |
|
| 106 |
virtual bool is_server() const override; |
| 107 |
virtual bool is_server_relay_supported() const override; |
| 108 |
|
| 109 |
|
| 110 |
virtual void set_refuse_new_connections(bool p_enabled) override; |
| 111 |
|
| 112 |
virtual ConnectionStatus get_connection_status() const override; |
| 113 |
|
| 114 |
virtual int get_unique_id() const override; |
| 115 |
|
| 116 |
virtual int get_max_packet_size() const override; |
| 117 |
virtual int get_available_packet_count() const override; |
| 118 |
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; |
| 119 |
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; |
| 120 |
|
| 121 |
Error 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); |
| 122 |
Error 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); |
| 123 |
Error create_mesh(int p_id); |
| 124 |
Error add_mesh_peer(int p_id, Ref<ENetConnection> p_host); |
| 125 |
|
| 126 |
void set_bind_ip(const IPAddress &p_ip); |
| 127 |
|
| 128 |
Ref<ENetConnection> get_host() const; |
| 129 |
Ref<ENetPacketPeer> get_peer(int p_id) const; |
| 130 |
|
| 131 |
ENetMultiplayerPeer(); |
| 132 |
~ENetMultiplayerPeer(); |
| 133 |
}; |