2
0

webrtc_multiplayer_peer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*************************************************************************/
  2. /* webrtc_multiplayer_peer.h */
  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. #ifndef WEBRTC_MULTIPLAYER_H
  31. #define WEBRTC_MULTIPLAYER_H
  32. #include "core/multiplayer/multiplayer_peer.h"
  33. #include "webrtc_peer_connection.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. class ConnectedPeer : public RefCounted {
  46. public:
  47. Ref<WebRTCPeerConnection> connection;
  48. List<Ref<WebRTCDataChannel>> channels;
  49. bool connected;
  50. ConnectedPeer() {
  51. connected = false;
  52. for (int i = 0; i < CH_RESERVED_MAX; i++) {
  53. channels.push_front(Ref<WebRTCDataChannel>());
  54. }
  55. }
  56. };
  57. uint32_t unique_id = 0;
  58. int target_peer = 0;
  59. int client_count = 0;
  60. ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
  61. int next_packet_peer = 0;
  62. bool server_compat = false;
  63. Map<int, Ref<ConnectedPeer>> peer_map;
  64. List<Dictionary> channels_config;
  65. void _peer_to_dict(Ref<ConnectedPeer> p_connected_peer, Dictionary &r_dict);
  66. void _find_next_peer();
  67. public:
  68. WebRTCMultiplayerPeer() {}
  69. ~WebRTCMultiplayerPeer();
  70. Error initialize(int p_self_id, bool p_server_compat = false, Array p_channels_config = Array());
  71. Error add_peer(Ref<WebRTCPeerConnection> p_peer, int p_peer_id, int p_unreliable_lifetime = 1);
  72. void remove_peer(int p_peer_id);
  73. bool has_peer(int p_peer_id);
  74. Dictionary get_peer(int p_peer_id);
  75. Dictionary get_peers();
  76. void close();
  77. // PacketPeer
  78. Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; ///< buffer is GONE after next get_packet
  79. Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  80. int get_available_packet_count() const override;
  81. int get_max_packet_size() const override;
  82. // MultiplayerPeer
  83. void set_target_peer(int p_peer_id) override;
  84. int get_unique_id() const override;
  85. int get_packet_peer() const override;
  86. bool is_server() const override;
  87. void poll() override;
  88. ConnectionStatus get_connection_status() const override;
  89. };
  90. #endif // WEBRTC_MULTIPLAYER_H