multiplayer_peer.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*************************************************************************/
  2. /* multiplayer_peer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_djb2_one_32(
  36. (uint32_t)OS::get_singleton()->get_ticks_usec());
  37. hash = hash_djb2_one_32(
  38. (uint32_t)OS::get_singleton()->get_unix_time(), hash);
  39. hash = hash_djb2_one_32(
  40. (uint32_t)OS::get_singleton()->get_user_data_dir().hash64(), hash);
  41. hash = hash_djb2_one_32(
  42. (uint32_t)((uint64_t)this), hash); // Rely on ASLR heap
  43. hash = hash_djb2_one_32(
  44. (uint32_t)((uint64_t)&hash), hash); // Rely on ASLR stack
  45. hash = hash & 0x7FFFFFFF; // Make it compatible with unsigned, since negative ID is used for exclusion
  46. }
  47. return hash;
  48. }
  49. void MultiplayerPeer::set_transfer_channel(int p_channel) {
  50. transfer_channel = p_channel;
  51. }
  52. int MultiplayerPeer::get_transfer_channel() const {
  53. return transfer_channel;
  54. }
  55. void MultiplayerPeer::set_transfer_mode(Multiplayer::TransferMode p_mode) {
  56. transfer_mode = p_mode;
  57. }
  58. Multiplayer::TransferMode MultiplayerPeer::get_transfer_mode() const {
  59. return transfer_mode;
  60. }
  61. void MultiplayerPeer::set_refuse_new_connections(bool p_enable) {
  62. refuse_connections = p_enable;
  63. }
  64. bool MultiplayerPeer::is_refusing_new_connections() const {
  65. return refuse_connections;
  66. }
  67. void MultiplayerPeer::_bind_methods() {
  68. ClassDB::bind_method(D_METHOD("set_transfer_channel", "channel"), &MultiplayerPeer::set_transfer_channel);
  69. ClassDB::bind_method(D_METHOD("get_transfer_channel"), &MultiplayerPeer::get_transfer_channel);
  70. ClassDB::bind_method(D_METHOD("set_transfer_mode", "mode"), &MultiplayerPeer::set_transfer_mode);
  71. ClassDB::bind_method(D_METHOD("get_transfer_mode"), &MultiplayerPeer::get_transfer_mode);
  72. ClassDB::bind_method(D_METHOD("set_target_peer", "id"), &MultiplayerPeer::set_target_peer);
  73. ClassDB::bind_method(D_METHOD("get_packet_peer"), &MultiplayerPeer::get_packet_peer);
  74. ClassDB::bind_method(D_METHOD("poll"), &MultiplayerPeer::poll);
  75. ClassDB::bind_method(D_METHOD("get_connection_status"), &MultiplayerPeer::get_connection_status);
  76. ClassDB::bind_method(D_METHOD("get_unique_id"), &MultiplayerPeer::get_unique_id);
  77. ClassDB::bind_method(D_METHOD("generate_unique_id"), &MultiplayerPeer::generate_unique_id);
  78. ClassDB::bind_method(D_METHOD("set_refuse_new_connections", "enable"), &MultiplayerPeer::set_refuse_new_connections);
  79. ClassDB::bind_method(D_METHOD("is_refusing_new_connections"), &MultiplayerPeer::is_refusing_new_connections);
  80. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_connections"), "set_refuse_new_connections", "is_refusing_new_connections");
  81. ADD_PROPERTY(PropertyInfo(Variant::INT, "transfer_mode", PROPERTY_HINT_ENUM, "Unreliable,Unreliable Ordered,Reliable"), "set_transfer_mode", "get_transfer_mode");
  82. ADD_PROPERTY(PropertyInfo(Variant::INT, "transfer_channel", PROPERTY_HINT_RANGE, "0,255,1"), "set_transfer_channel", "get_transfer_channel");
  83. BIND_ENUM_CONSTANT(CONNECTION_DISCONNECTED);
  84. BIND_ENUM_CONSTANT(CONNECTION_CONNECTING);
  85. BIND_ENUM_CONSTANT(CONNECTION_CONNECTED);
  86. BIND_CONSTANT(TARGET_PEER_BROADCAST);
  87. BIND_CONSTANT(TARGET_PEER_SERVER);
  88. ADD_SIGNAL(MethodInfo("peer_connected", PropertyInfo(Variant::INT, "id")));
  89. ADD_SIGNAL(MethodInfo("peer_disconnected", PropertyInfo(Variant::INT, "id")));
  90. ADD_SIGNAL(MethodInfo("server_disconnected"));
  91. ADD_SIGNAL(MethodInfo("connection_succeeded"));
  92. ADD_SIGNAL(MethodInfo("connection_failed"));
  93. }
  94. /*************/
  95. int MultiplayerPeerExtension::get_available_packet_count() const {
  96. int count;
  97. if (GDVIRTUAL_CALL(_get_available_packet_count, count)) {
  98. return count;
  99. }
  100. WARN_PRINT_ONCE("MultiplayerPeerExtension::_get_available_packet_count is unimplemented!");
  101. return -1;
  102. }
  103. Error MultiplayerPeerExtension::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  104. int err;
  105. if (GDVIRTUAL_CALL(_get_packet, r_buffer, &r_buffer_size, err)) {
  106. return (Error)err;
  107. }
  108. WARN_PRINT_ONCE("MultiplayerPeerExtension::_get_packet_native is unimplemented!");
  109. return FAILED;
  110. }
  111. Error MultiplayerPeerExtension::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  112. int err;
  113. if (GDVIRTUAL_CALL(_put_packet, p_buffer, p_buffer_size, err)) {
  114. return (Error)err;
  115. }
  116. WARN_PRINT_ONCE("MultiplayerPeerExtension::_put_packet_native is unimplemented!");
  117. return FAILED;
  118. }
  119. int MultiplayerPeerExtension::get_max_packet_size() const {
  120. int size;
  121. if (GDVIRTUAL_CALL(_get_max_packet_size, size)) {
  122. return size;
  123. }
  124. WARN_PRINT_ONCE("MultiplayerPeerExtension::_get_max_packet_size is unimplemented!");
  125. return 0;
  126. }
  127. void MultiplayerPeerExtension::set_transfer_channel(int p_channel) {
  128. if (GDVIRTUAL_CALL(_set_transfer_channel, p_channel)) {
  129. return;
  130. }
  131. MultiplayerPeer::set_transfer_channel(p_channel);
  132. }
  133. int MultiplayerPeerExtension::get_transfer_channel() const {
  134. int channel;
  135. if (GDVIRTUAL_CALL(_get_transfer_channel, channel)) {
  136. return channel;
  137. }
  138. return MultiplayerPeer::get_transfer_channel();
  139. }
  140. void MultiplayerPeerExtension::set_transfer_mode(Multiplayer::TransferMode p_mode) {
  141. if (GDVIRTUAL_CALL(_set_transfer_mode, p_mode)) {
  142. return;
  143. }
  144. MultiplayerPeer::set_transfer_mode(p_mode);
  145. }
  146. Multiplayer::TransferMode MultiplayerPeerExtension::get_transfer_mode() const {
  147. int mode;
  148. if (GDVIRTUAL_CALL(_get_transfer_mode, mode)) {
  149. return (Multiplayer::TransferMode)mode;
  150. }
  151. return MultiplayerPeer::get_transfer_mode();
  152. }
  153. void MultiplayerPeerExtension::set_target_peer(int p_peer_id) {
  154. if (GDVIRTUAL_CALL(_set_target_peer, p_peer_id)) {
  155. return;
  156. }
  157. WARN_PRINT_ONCE("MultiplayerPeerExtension::_set_target_peer is unimplemented!");
  158. }
  159. int MultiplayerPeerExtension::get_packet_peer() const {
  160. int peer;
  161. if (GDVIRTUAL_CALL(_get_packet_peer, peer)) {
  162. return peer;
  163. }
  164. WARN_PRINT_ONCE("MultiplayerPeerExtension::_get_packet_peer is unimplemented!");
  165. return 0;
  166. }
  167. bool MultiplayerPeerExtension::is_server() const {
  168. bool server;
  169. if (GDVIRTUAL_CALL(_is_server, server)) {
  170. return server;
  171. }
  172. WARN_PRINT_ONCE("MultiplayerPeerExtension::_is_server is unimplemented!");
  173. return false;
  174. }
  175. void MultiplayerPeerExtension::poll() {
  176. int err;
  177. if (GDVIRTUAL_CALL(_poll, err)) {
  178. return;
  179. }
  180. WARN_PRINT_ONCE("MultiplayerPeerExtension::_poll is unimplemented!");
  181. }
  182. int MultiplayerPeerExtension::get_unique_id() const {
  183. int id;
  184. if (GDVIRTUAL_CALL(_get_unique_id, id)) {
  185. return id;
  186. }
  187. WARN_PRINT_ONCE("MultiplayerPeerExtension::_get_unique_id is unimplemented!");
  188. return 0;
  189. }
  190. void MultiplayerPeerExtension::set_refuse_new_connections(bool p_enable) {
  191. if (GDVIRTUAL_CALL(_set_refuse_new_connections, p_enable)) {
  192. return;
  193. }
  194. MultiplayerPeer::set_refuse_new_connections(p_enable);
  195. }
  196. bool MultiplayerPeerExtension::is_refusing_new_connections() const {
  197. bool refusing;
  198. if (GDVIRTUAL_CALL(_is_refusing_new_connections, refusing)) {
  199. return refusing;
  200. }
  201. return MultiplayerPeer::is_refusing_new_connections();
  202. }
  203. MultiplayerPeer::ConnectionStatus MultiplayerPeerExtension::get_connection_status() const {
  204. int status;
  205. if (GDVIRTUAL_CALL(_get_connection_status, status)) {
  206. return (ConnectionStatus)status;
  207. }
  208. WARN_PRINT_ONCE("MultiplayerPeerExtension::_get_connection_status is unimplemented!");
  209. return CONNECTION_DISCONNECTED;
  210. }
  211. void MultiplayerPeerExtension::_bind_methods() {
  212. GDVIRTUAL_BIND(_get_packet, "r_buffer", "r_buffer_size");
  213. GDVIRTUAL_BIND(_put_packet, "p_buffer", "p_buffer_size");
  214. GDVIRTUAL_BIND(_get_available_packet_count);
  215. GDVIRTUAL_BIND(_get_max_packet_size);
  216. GDVIRTUAL_BIND(_set_transfer_channel, "p_channel");
  217. GDVIRTUAL_BIND(_get_transfer_channel);
  218. GDVIRTUAL_BIND(_set_transfer_mode, "p_mode");
  219. GDVIRTUAL_BIND(_get_transfer_mode);
  220. GDVIRTUAL_BIND(_set_target_peer, "p_peer");
  221. GDVIRTUAL_BIND(_get_packet_peer);
  222. GDVIRTUAL_BIND(_is_server);
  223. GDVIRTUAL_BIND(_poll);
  224. GDVIRTUAL_BIND(_get_unique_id);
  225. GDVIRTUAL_BIND(_set_refuse_new_connections, "p_enable");
  226. GDVIRTUAL_BIND(_is_refusing_new_connections);
  227. GDVIRTUAL_BIND(_get_connection_status);
  228. }