webrtc_multiplayer_peer.h 4.9 KB

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