1 | /**************************************************************************/ |
2 | /* enet_multiplayer_peer.h */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #ifndef ENET_MULTIPLAYER_PEER_H |
32 | #define ENET_MULTIPLAYER_PEER_H |
33 | |
34 | #include "enet_connection.h" |
35 | |
36 | #include "core/crypto/crypto.h" |
37 | #include "scene/main/multiplayer_peer.h" |
38 | |
39 | #include <enet/enet.h> |
40 | |
41 | class ENetMultiplayerPeer : public MultiplayerPeer { |
42 | GDCLASS(ENetMultiplayerPeer, MultiplayerPeer); |
43 | |
44 | private: |
45 | enum { |
46 | SYSMSG_ADD_PEER, |
47 | SYSMSG_REMOVE_PEER |
48 | }; |
49 | |
50 | enum { |
51 | SYSCH_RELIABLE = 0, |
52 | SYSCH_UNRELIABLE = 1, |
53 | SYSCH_MAX = 2 |
54 | }; |
55 | |
56 | enum Mode { |
57 | MODE_NONE, |
58 | MODE_SERVER, |
59 | MODE_CLIENT, |
60 | MODE_MESH, |
61 | }; |
62 | |
63 | Mode active_mode = MODE_NONE; |
64 | |
65 | uint32_t unique_id = 0; |
66 | |
67 | int target_peer = 0; |
68 | |
69 | ConnectionStatus connection_status = CONNECTION_DISCONNECTED; |
70 | |
71 | HashMap<int, Ref<ENetConnection>> hosts; |
72 | HashMap<int, Ref<ENetPacketPeer>> peers; |
73 | |
74 | struct Packet { |
75 | ENetPacket *packet = nullptr; |
76 | int from = 0; |
77 | int channel = 0; |
78 | TransferMode transfer_mode = TRANSFER_MODE_RELIABLE; |
79 | }; |
80 | |
81 | List<Packet> incoming_packets; |
82 | |
83 | Packet current_packet; |
84 | |
85 | void _store_packet(int32_t p_source, ENetConnection::Event &p_event); |
86 | void _pop_current_packet(); |
87 | void _disconnect_inactive_peers(); |
88 | void _destroy_unused(ENetPacket *p_packet); |
89 | _FORCE_INLINE_ bool _is_active() const { return active_mode != MODE_NONE; } |
90 | |
91 | IPAddress bind_ip; |
92 | |
93 | protected: |
94 | static void _bind_methods(); |
95 | |
96 | public: |
97 | virtual void set_target_peer(int p_peer) override; |
98 | |
99 | virtual int get_packet_peer() const override; |
100 | virtual TransferMode get_packet_mode() const override; |
101 | virtual int get_packet_channel() const override; |
102 | |
103 | virtual void poll() override; |
104 | virtual void close() override; |
105 | virtual void disconnect_peer(int p_peer, bool p_force = false) override; |
106 | |
107 | virtual bool is_server() const override; |
108 | virtual bool is_server_relay_supported() const override; |
109 | |
110 | // Overridden so we can instrument the DTLSServer when needed. |
111 | virtual void set_refuse_new_connections(bool p_enabled) override; |
112 | |
113 | virtual ConnectionStatus get_connection_status() const override; |
114 | |
115 | virtual int get_unique_id() const override; |
116 | |
117 | virtual int get_max_packet_size() const override; |
118 | virtual int get_available_packet_count() const override; |
119 | virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; |
120 | virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; |
121 | |
122 | 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); |
123 | 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); |
124 | Error create_mesh(int p_id); |
125 | Error add_mesh_peer(int p_id, Ref<ENetConnection> p_host); |
126 | |
127 | void set_bind_ip(const IPAddress &p_ip); |
128 | |
129 | Ref<ENetConnection> get_host() const; |
130 | Ref<ENetPacketPeer> get_peer(int p_id) const; |
131 | |
132 | ENetMultiplayerPeer(); |
133 | ~ENetMultiplayerPeer(); |
134 | }; |
135 | |
136 | #endif // ENET_MULTIPLAYER_PEER_H |
137 | |