| 1 | /**************************************************************************/ |
| 2 | /* multiplayer_peer.cpp */ |
| 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 | #include "multiplayer_peer.h" |
| 32 | |
| 33 | #include "core/os/os.h" |
| 34 | |
| 35 | uint32_t MultiplayerPeer::generate_unique_id() const { |
| 36 | uint32_t hash = 0; |
| 37 | |
| 38 | while (hash == 0 || hash == 1) { |
| 39 | hash = hash_murmur3_one_32( |
| 40 | (uint32_t)OS::get_singleton()->get_ticks_usec()); |
| 41 | hash = hash_murmur3_one_32( |
| 42 | (uint32_t)OS::get_singleton()->get_unix_time(), hash); |
| 43 | hash = hash_murmur3_one_32( |
| 44 | (uint32_t)OS::get_singleton()->get_user_data_dir().hash64(), hash); |
| 45 | hash = hash_murmur3_one_32( |
| 46 | (uint32_t)((uint64_t)this), hash); // Rely on ASLR heap |
| 47 | hash = hash_murmur3_one_32( |
| 48 | (uint32_t)((uint64_t)&hash), hash); // Rely on ASLR stack |
| 49 | |
| 50 | hash = hash_fmix32(hash); |
| 51 | hash = hash & 0x7FFFFFFF; // Make it compatible with unsigned, since negative ID is used for exclusion |
| 52 | } |
| 53 | |
| 54 | return hash; |
| 55 | } |
| 56 | |
| 57 | void MultiplayerPeer::set_transfer_channel(int p_channel) { |
| 58 | transfer_channel = p_channel; |
| 59 | } |
| 60 | |
| 61 | int MultiplayerPeer::get_transfer_channel() const { |
| 62 | return transfer_channel; |
| 63 | } |
| 64 | |
| 65 | void MultiplayerPeer::set_transfer_mode(TransferMode p_mode) { |
| 66 | transfer_mode = p_mode; |
| 67 | } |
| 68 | |
| 69 | MultiplayerPeer::TransferMode MultiplayerPeer::get_transfer_mode() const { |
| 70 | return transfer_mode; |
| 71 | } |
| 72 | |
| 73 | void MultiplayerPeer::set_refuse_new_connections(bool p_enable) { |
| 74 | refuse_connections = p_enable; |
| 75 | } |
| 76 | |
| 77 | bool MultiplayerPeer::is_refusing_new_connections() const { |
| 78 | return refuse_connections; |
| 79 | } |
| 80 | |
| 81 | bool MultiplayerPeer::is_server_relay_supported() const { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | void MultiplayerPeer::_bind_methods() { |
| 86 | ClassDB::bind_method(D_METHOD("set_transfer_channel" , "channel" ), &MultiplayerPeer::set_transfer_channel); |
| 87 | ClassDB::bind_method(D_METHOD("get_transfer_channel" ), &MultiplayerPeer::get_transfer_channel); |
| 88 | ClassDB::bind_method(D_METHOD("set_transfer_mode" , "mode" ), &MultiplayerPeer::set_transfer_mode); |
| 89 | ClassDB::bind_method(D_METHOD("get_transfer_mode" ), &MultiplayerPeer::get_transfer_mode); |
| 90 | ClassDB::bind_method(D_METHOD("set_target_peer" , "id" ), &MultiplayerPeer::set_target_peer); |
| 91 | |
| 92 | ClassDB::bind_method(D_METHOD("get_packet_peer" ), &MultiplayerPeer::get_packet_peer); |
| 93 | ClassDB::bind_method(D_METHOD("get_packet_channel" ), &MultiplayerPeer::get_packet_channel); |
| 94 | ClassDB::bind_method(D_METHOD("get_packet_mode" ), &MultiplayerPeer::get_packet_mode); |
| 95 | |
| 96 | ClassDB::bind_method(D_METHOD("poll" ), &MultiplayerPeer::poll); |
| 97 | ClassDB::bind_method(D_METHOD("close" ), &MultiplayerPeer::close); |
| 98 | ClassDB::bind_method(D_METHOD("disconnect_peer" , "peer" , "force" ), &MultiplayerPeer::disconnect_peer, DEFVAL(false)); |
| 99 | |
| 100 | ClassDB::bind_method(D_METHOD("get_connection_status" ), &MultiplayerPeer::get_connection_status); |
| 101 | ClassDB::bind_method(D_METHOD("get_unique_id" ), &MultiplayerPeer::get_unique_id); |
| 102 | ClassDB::bind_method(D_METHOD("generate_unique_id" ), &MultiplayerPeer::generate_unique_id); |
| 103 | |
| 104 | ClassDB::bind_method(D_METHOD("set_refuse_new_connections" , "enable" ), &MultiplayerPeer::set_refuse_new_connections); |
| 105 | ClassDB::bind_method(D_METHOD("is_refusing_new_connections" ), &MultiplayerPeer::is_refusing_new_connections); |
| 106 | |
| 107 | ClassDB::bind_method(D_METHOD("is_server_relay_supported" ), &MultiplayerPeer::is_server_relay_supported); |
| 108 | |
| 109 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_connections" ), "set_refuse_new_connections" , "is_refusing_new_connections" ); |
| 110 | ADD_PROPERTY(PropertyInfo(Variant::INT, "transfer_mode" , PROPERTY_HINT_ENUM, "Unreliable,Unreliable Ordered,Reliable" ), "set_transfer_mode" , "get_transfer_mode" ); |
| 111 | ADD_PROPERTY(PropertyInfo(Variant::INT, "transfer_channel" , PROPERTY_HINT_RANGE, "0,255,1" ), "set_transfer_channel" , "get_transfer_channel" ); |
| 112 | |
| 113 | BIND_ENUM_CONSTANT(CONNECTION_DISCONNECTED); |
| 114 | BIND_ENUM_CONSTANT(CONNECTION_CONNECTING); |
| 115 | BIND_ENUM_CONSTANT(CONNECTION_CONNECTED); |
| 116 | |
| 117 | BIND_CONSTANT(TARGET_PEER_BROADCAST); |
| 118 | BIND_CONSTANT(TARGET_PEER_SERVER); |
| 119 | |
| 120 | BIND_ENUM_CONSTANT(TRANSFER_MODE_UNRELIABLE); |
| 121 | BIND_ENUM_CONSTANT(TRANSFER_MODE_UNRELIABLE_ORDERED); |
| 122 | BIND_ENUM_CONSTANT(TRANSFER_MODE_RELIABLE); |
| 123 | |
| 124 | ADD_SIGNAL(MethodInfo("peer_connected" , PropertyInfo(Variant::INT, "id" ))); |
| 125 | ADD_SIGNAL(MethodInfo("peer_disconnected" , PropertyInfo(Variant::INT, "id" ))); |
| 126 | } |
| 127 | |
| 128 | /*************/ |
| 129 | |
| 130 | Error MultiplayerPeerExtension::get_packet(const uint8_t **r_buffer, int &r_buffer_size) { |
| 131 | Error err; |
| 132 | if (GDVIRTUAL_CALL(_get_packet, r_buffer, &r_buffer_size, err)) { |
| 133 | return err; |
| 134 | } |
| 135 | if (GDVIRTUAL_IS_OVERRIDDEN(_get_packet_script)) { |
| 136 | if (!GDVIRTUAL_CALL(_get_packet_script, script_buffer)) { |
| 137 | return FAILED; |
| 138 | } |
| 139 | |
| 140 | if (script_buffer.size() == 0) { |
| 141 | return Error::ERR_UNAVAILABLE; |
| 142 | } |
| 143 | |
| 144 | *r_buffer = script_buffer.ptr(); |
| 145 | r_buffer_size = script_buffer.size(); |
| 146 | |
| 147 | return Error::OK; |
| 148 | } |
| 149 | WARN_PRINT_ONCE("MultiplayerPeerExtension::_get_packet_native is unimplemented!" ); |
| 150 | return FAILED; |
| 151 | } |
| 152 | |
| 153 | Error MultiplayerPeerExtension::put_packet(const uint8_t *p_buffer, int p_buffer_size) { |
| 154 | Error err; |
| 155 | if (GDVIRTUAL_CALL(_put_packet, p_buffer, p_buffer_size, err)) { |
| 156 | return err; |
| 157 | } |
| 158 | if (GDVIRTUAL_IS_OVERRIDDEN(_put_packet_script)) { |
| 159 | PackedByteArray a; |
| 160 | a.resize(p_buffer_size); |
| 161 | memcpy(a.ptrw(), p_buffer, p_buffer_size); |
| 162 | |
| 163 | if (!GDVIRTUAL_CALL(_put_packet_script, a, err)) { |
| 164 | return FAILED; |
| 165 | } |
| 166 | return err; |
| 167 | } |
| 168 | WARN_PRINT_ONCE("MultiplayerPeerExtension::_put_packet_native is unimplemented!" ); |
| 169 | return FAILED; |
| 170 | } |
| 171 | |
| 172 | void MultiplayerPeerExtension::set_refuse_new_connections(bool p_enable) { |
| 173 | if (GDVIRTUAL_CALL(_set_refuse_new_connections, p_enable)) { |
| 174 | return; |
| 175 | } |
| 176 | MultiplayerPeer::set_refuse_new_connections(p_enable); |
| 177 | } |
| 178 | |
| 179 | bool MultiplayerPeerExtension::is_refusing_new_connections() const { |
| 180 | bool refusing; |
| 181 | if (GDVIRTUAL_CALL(_is_refusing_new_connections, refusing)) { |
| 182 | return refusing; |
| 183 | } |
| 184 | return MultiplayerPeer::is_refusing_new_connections(); |
| 185 | } |
| 186 | |
| 187 | bool MultiplayerPeerExtension::is_server_relay_supported() const { |
| 188 | bool can_relay; |
| 189 | if (GDVIRTUAL_CALL(_is_server_relay_supported, can_relay)) { |
| 190 | return can_relay; |
| 191 | } |
| 192 | return MultiplayerPeer::is_server_relay_supported(); |
| 193 | } |
| 194 | |
| 195 | void MultiplayerPeerExtension::_bind_methods() { |
| 196 | GDVIRTUAL_BIND(_get_packet, "r_buffer" , "r_buffer_size" ); |
| 197 | GDVIRTUAL_BIND(_put_packet, "p_buffer" , "p_buffer_size" ); |
| 198 | GDVIRTUAL_BIND(_get_available_packet_count); |
| 199 | GDVIRTUAL_BIND(_get_max_packet_size); |
| 200 | |
| 201 | GDVIRTUAL_BIND(_get_packet_script) |
| 202 | GDVIRTUAL_BIND(_put_packet_script, "p_buffer" ); |
| 203 | |
| 204 | GDVIRTUAL_BIND(_get_packet_channel); |
| 205 | GDVIRTUAL_BIND(_get_packet_mode); |
| 206 | |
| 207 | GDVIRTUAL_BIND(_set_transfer_channel, "p_channel" ); |
| 208 | GDVIRTUAL_BIND(_get_transfer_channel); |
| 209 | |
| 210 | GDVIRTUAL_BIND(_set_transfer_mode, "p_mode" ); |
| 211 | GDVIRTUAL_BIND(_get_transfer_mode); |
| 212 | |
| 213 | GDVIRTUAL_BIND(_set_target_peer, "p_peer" ); |
| 214 | |
| 215 | GDVIRTUAL_BIND(_get_packet_peer); |
| 216 | GDVIRTUAL_BIND(_is_server); |
| 217 | GDVIRTUAL_BIND(_poll); |
| 218 | GDVIRTUAL_BIND(_close); |
| 219 | GDVIRTUAL_BIND(_disconnect_peer, "p_peer" , "p_force" ); |
| 220 | GDVIRTUAL_BIND(_get_unique_id); |
| 221 | GDVIRTUAL_BIND(_set_refuse_new_connections, "p_enable" ); |
| 222 | GDVIRTUAL_BIND(_is_refusing_new_connections); |
| 223 | GDVIRTUAL_BIND(_is_server_relay_supported); |
| 224 | GDVIRTUAL_BIND(_get_connection_status); |
| 225 | |
| 226 | ADD_PROPERTY_DEFAULT("transfer_mode" , TRANSFER_MODE_RELIABLE); |
| 227 | ADD_PROPERTY_DEFAULT("transfer_channel" , 0); |
| 228 | } |
| 229 | |