multiplayer_peer.cpp 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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::_bind_methods() {
  50. ClassDB::bind_method(D_METHOD("set_transfer_channel", "channel"), &MultiplayerPeer::set_transfer_channel);
  51. ClassDB::bind_method(D_METHOD("get_transfer_channel"), &MultiplayerPeer::get_transfer_channel);
  52. ClassDB::bind_method(D_METHOD("set_transfer_mode", "mode"), &MultiplayerPeer::set_transfer_mode);
  53. ClassDB::bind_method(D_METHOD("get_transfer_mode"), &MultiplayerPeer::get_transfer_mode);
  54. ClassDB::bind_method(D_METHOD("set_target_peer", "id"), &MultiplayerPeer::set_target_peer);
  55. ClassDB::bind_method(D_METHOD("get_packet_peer"), &MultiplayerPeer::get_packet_peer);
  56. ClassDB::bind_method(D_METHOD("poll"), &MultiplayerPeer::poll);
  57. ClassDB::bind_method(D_METHOD("get_connection_status"), &MultiplayerPeer::get_connection_status);
  58. ClassDB::bind_method(D_METHOD("get_unique_id"), &MultiplayerPeer::get_unique_id);
  59. ClassDB::bind_method(D_METHOD("generate_unique_id"), &MultiplayerPeer::generate_unique_id);
  60. ClassDB::bind_method(D_METHOD("set_refuse_new_connections", "enable"), &MultiplayerPeer::set_refuse_new_connections);
  61. ClassDB::bind_method(D_METHOD("is_refusing_new_connections"), &MultiplayerPeer::is_refusing_new_connections);
  62. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "refuse_new_connections"), "set_refuse_new_connections", "is_refusing_new_connections");
  63. ADD_PROPERTY(PropertyInfo(Variant::INT, "transfer_mode", PROPERTY_HINT_ENUM, "Unreliable,Unreliable Ordered,Reliable"), "set_transfer_mode", "get_transfer_mode");
  64. ADD_PROPERTY(PropertyInfo(Variant::INT, "transfer_channel", PROPERTY_HINT_RANGE, "0,255,1"), "set_transfer_channel", "get_transfer_channel");
  65. BIND_ENUM_CONSTANT(TRANSFER_MODE_UNRELIABLE);
  66. BIND_ENUM_CONSTANT(TRANSFER_MODE_UNRELIABLE_ORDERED);
  67. BIND_ENUM_CONSTANT(TRANSFER_MODE_RELIABLE);
  68. BIND_ENUM_CONSTANT(CONNECTION_DISCONNECTED);
  69. BIND_ENUM_CONSTANT(CONNECTION_CONNECTING);
  70. BIND_ENUM_CONSTANT(CONNECTION_CONNECTED);
  71. BIND_CONSTANT(TARGET_PEER_BROADCAST);
  72. BIND_CONSTANT(TARGET_PEER_SERVER);
  73. ADD_SIGNAL(MethodInfo("peer_connected", PropertyInfo(Variant::INT, "id")));
  74. ADD_SIGNAL(MethodInfo("peer_disconnected", PropertyInfo(Variant::INT, "id")));
  75. ADD_SIGNAL(MethodInfo("server_disconnected"));
  76. ADD_SIGNAL(MethodInfo("connection_succeeded"));
  77. ADD_SIGNAL(MethodInfo("connection_failed"));
  78. }