webrtc_multiplayer_peer.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**************************************************************************/
  2. /* webrtc_multiplayer_peer.h */
  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. #pragma once
  31. #include "webrtc_peer_connection.h"
  32. #include "scene/main/multiplayer_peer.h"
  33. class WebRTCMultiplayerPeer : public MultiplayerPeer {
  34. GDCLASS(WebRTCMultiplayerPeer, MultiplayerPeer);
  35. protected:
  36. static void _bind_methods();
  37. private:
  38. enum {
  39. CH_RELIABLE = 0,
  40. CH_ORDERED = 1,
  41. CH_UNRELIABLE = 2,
  42. CH_RESERVED_MAX = 3
  43. };
  44. enum NetworkMode {
  45. MODE_NONE,
  46. MODE_SERVER,
  47. MODE_CLIENT,
  48. MODE_MESH,
  49. };
  50. class ConnectedPeer : public RefCounted {
  51. public:
  52. Ref<WebRTCPeerConnection> connection;
  53. List<Ref<WebRTCDataChannel>> channels;
  54. bool connected;
  55. ConnectedPeer() {
  56. connected = false;
  57. for (int i = 0; i < CH_RESERVED_MAX; i++) {
  58. channels.push_front(Ref<WebRTCDataChannel>());
  59. }
  60. }
  61. };
  62. uint32_t unique_id = 0;
  63. int target_peer = 0;
  64. int client_count = 0;
  65. ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
  66. int next_packet_peer = 0;
  67. int next_packet_channel = 0;
  68. NetworkMode network_mode = MODE_NONE;
  69. HashMap<int, Ref<ConnectedPeer>> peer_map;
  70. List<TransferMode> channels_modes;
  71. List<Dictionary> channels_config;
  72. void _peer_to_dict(Ref<ConnectedPeer> p_connected_peer, Dictionary &r_dict);
  73. void _find_next_peer();
  74. Ref<ConnectedPeer> _get_next_peer();
  75. Error _initialize(int p_self_id, NetworkMode p_mode, Array p_channels_config = Array());
  76. public:
  77. WebRTCMultiplayerPeer() {}
  78. ~WebRTCMultiplayerPeer();
  79. Error create_server(Array p_channels_config = Array());
  80. Error create_client(int p_self_id, Array p_channels_config = Array());
  81. Error create_mesh(int p_self_id, Array p_channels_config = Array());
  82. Error add_peer(Ref<WebRTCPeerConnection> p_peer, int p_peer_id, int p_unreliable_lifetime = 1);
  83. void remove_peer(int p_peer_id);
  84. bool has_peer(int p_peer_id);
  85. Dictionary get_peer(int p_peer_id);
  86. Dictionary get_peers();
  87. // PacketPeer
  88. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
  89. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  90. virtual int get_available_packet_count() const override;
  91. virtual int get_max_packet_size() const override;
  92. // MultiplayerPeer
  93. virtual void set_target_peer(int p_peer_id) override;
  94. virtual int get_unique_id() const override;
  95. virtual int get_packet_peer() const override;
  96. virtual int get_packet_channel() const override;
  97. virtual TransferMode get_packet_mode() const override;
  98. virtual bool is_server() const override;
  99. virtual bool is_server_relay_supported() const override;
  100. virtual void poll() override;
  101. virtual void close() override;
  102. virtual void disconnect_peer(int p_peer_id, bool p_force = false) override;
  103. virtual ConnectionStatus get_connection_status() const override;
  104. };