networked_multiplayer_custom.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**************************************************************************/
  2. /* networked_multiplayer_custom.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 "core/io/networked_multiplayer_custom.h"
  31. NetworkedMultiplayerCustom::NetworkedMultiplayerCustom() {
  32. self_id = 0;
  33. transfer_mode = TransferMode::TRANSFER_MODE_RELIABLE;
  34. connection_status = ConnectionStatus::CONNECTION_DISCONNECTED;
  35. refusing_new_connections = false;
  36. target_id = 0;
  37. // Default to a large value.
  38. max_packet_size = 1 << 24;
  39. }
  40. NetworkedMultiplayerCustom::~NetworkedMultiplayerCustom() {
  41. }
  42. void NetworkedMultiplayerCustom::_bind_methods() {
  43. ClassDB::bind_method(D_METHOD("initialize", "self_peer_id"), &NetworkedMultiplayerCustom::initialize);
  44. ClassDB::bind_method(D_METHOD("set_max_packet_size", "max_packet_size"), &NetworkedMultiplayerCustom::set_max_packet_size);
  45. ClassDB::bind_method(D_METHOD("set_connection_status", "connection_status"), &NetworkedMultiplayerCustom::set_connection_status);
  46. ClassDB::bind_method(D_METHOD("deliver_packet", "buffer", "from_peer_id"), &NetworkedMultiplayerCustom::deliver_packet);
  47. ADD_SIGNAL(MethodInfo("packet_generated", PropertyInfo(Variant::INT, "peer_id"), PropertyInfo(Variant::POOL_BYTE_ARRAY, "buffer"), PropertyInfo(Variant::INT, "transfer_mode")));
  48. }
  49. //
  50. // PacketPeer
  51. //
  52. Error NetworkedMultiplayerCustom::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
  53. ERR_FAIL_COND_V(incoming_packets.size() == 0, Error::ERR_UNAVAILABLE);
  54. current_packet = incoming_packets.front()->get();
  55. incoming_packets.pop_front();
  56. *r_buffer = current_packet.data.read().ptr();
  57. r_buffer_size = current_packet.data.size();
  58. return Error::OK;
  59. }
  60. Error NetworkedMultiplayerCustom::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
  61. PoolByteArray a;
  62. a.resize(p_buffer_size);
  63. PoolByteArray::Write w = a.write();
  64. memcpy(w.ptr(), p_buffer, p_buffer_size);
  65. emit_signal("packet_generated", target_id, a, transfer_mode);
  66. return Error::OK;
  67. }
  68. int NetworkedMultiplayerCustom::get_available_packet_count() const {
  69. return incoming_packets.size();
  70. }
  71. int NetworkedMultiplayerCustom::get_max_packet_size() const {
  72. return max_packet_size;
  73. }
  74. //
  75. // NetworkedMultiplayerPeer.
  76. //
  77. void NetworkedMultiplayerCustom::set_transfer_mode(NetworkedMultiplayerPeer::TransferMode p_mode) {
  78. transfer_mode = p_mode;
  79. }
  80. NetworkedMultiplayerPeer::TransferMode NetworkedMultiplayerCustom::get_transfer_mode() const {
  81. return transfer_mode;
  82. }
  83. void NetworkedMultiplayerCustom::set_target_peer(int p_target_peer) {
  84. target_id = p_target_peer;
  85. }
  86. int NetworkedMultiplayerCustom::get_packet_peer() const {
  87. ERR_FAIL_COND_V(connection_status != ConnectionStatus::CONNECTION_CONNECTED, 1);
  88. ERR_FAIL_COND_V(incoming_packets.size() == 0, 1);
  89. return incoming_packets.front()->get().from;
  90. }
  91. bool NetworkedMultiplayerCustom::is_server() const {
  92. return self_id == 1;
  93. }
  94. void NetworkedMultiplayerCustom::poll() {
  95. }
  96. int NetworkedMultiplayerCustom::get_unique_id() const {
  97. return self_id;
  98. }
  99. void NetworkedMultiplayerCustom::set_refuse_new_connections(bool p_refuse_new_connections) {
  100. refusing_new_connections = p_refuse_new_connections;
  101. }
  102. bool NetworkedMultiplayerCustom::is_refusing_new_connections() const {
  103. return refusing_new_connections;
  104. }
  105. NetworkedMultiplayerPeer::ConnectionStatus NetworkedMultiplayerCustom::get_connection_status() const {
  106. return connection_status;
  107. }
  108. //
  109. // Custom methods.
  110. //
  111. void NetworkedMultiplayerCustom::initialize(int p_self_id) {
  112. ERR_FAIL_COND_MSG(connection_status != ConnectionStatus::CONNECTION_CONNECTING,
  113. "Can only initialize if connection status is CONNECTION_CONNECTING.");
  114. ERR_FAIL_COND_MSG(p_self_id < 0 || p_self_id > ~(1 << 31),
  115. "Cannot initialize with invalid unique network id.");
  116. self_id = p_self_id;
  117. if (self_id == 1) {
  118. set_connection_status(ConnectionStatus::CONNECTION_CONNECTED);
  119. }
  120. }
  121. void NetworkedMultiplayerCustom::set_max_packet_size(int p_max_packet_size) {
  122. max_packet_size = p_max_packet_size;
  123. }
  124. void NetworkedMultiplayerCustom::set_connection_status(NetworkedMultiplayerPeer::ConnectionStatus p_connection_status) {
  125. if (connection_status == p_connection_status) {
  126. return;
  127. }
  128. ERR_FAIL_COND_MSG(p_connection_status == ConnectionStatus::CONNECTION_CONNECTING && connection_status != ConnectionStatus::CONNECTION_DISCONNECTED,
  129. "Can only change connection status to CONNECTION_CONNECTING from CONNECTION_DISCONNECTED.");
  130. ERR_FAIL_COND_MSG(p_connection_status == ConnectionStatus::CONNECTION_CONNECTED && connection_status != ConnectionStatus::CONNECTION_CONNECTING,
  131. "Can only change connection status to CONNECTION_CONNECTED from CONNECTION_CONNECTING.");
  132. if (p_connection_status == ConnectionStatus::CONNECTION_CONNECTED) {
  133. connection_status = p_connection_status;
  134. emit_signal("connection_succeeded");
  135. } else if (p_connection_status == ConnectionStatus::CONNECTION_DISCONNECTED) {
  136. ConnectionStatus old_connection_status = connection_status;
  137. connection_status = p_connection_status;
  138. if (self_id != 1) {
  139. if (old_connection_status == ConnectionStatus::CONNECTION_CONNECTING) {
  140. emit_signal("connection_failed");
  141. } else {
  142. emit_signal("server_disconnected");
  143. }
  144. }
  145. } else {
  146. connection_status = p_connection_status;
  147. }
  148. }
  149. void NetworkedMultiplayerCustom::deliver_packet(const PoolByteArray &p_data, int p_from_peer_id) {
  150. Packet p = { p_data, p_from_peer_id };
  151. incoming_packets.push_back(p);
  152. }