multiplayer_peer.cpp 9.0 KB

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