webrtc_multiplayer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*************************************************************************/
  2. /* webrtc_multiplayer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/io/networked_multiplayer_peer.h"
  33. #include "webrtc_peer_connection.h"
  34. class WebRTCMultiplayer : public NetworkedMultiplayerPeer {
  35. GDCLASS(WebRTCMultiplayer, NetworkedMultiplayerPeer);
  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 Reference {
  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;
  58. int target_peer;
  59. int client_count;
  60. bool refuse_connections;
  61. ConnectionStatus connection_status;
  62. TransferMode transfer_mode;
  63. int next_packet_peer;
  64. bool server_compat;
  65. Map<int, Ref<ConnectedPeer>> peer_map;
  66. void _peer_to_dict(Ref<ConnectedPeer> p_connected_peer, Dictionary &r_dict);
  67. void _find_next_peer();
  68. public:
  69. WebRTCMultiplayer();
  70. ~WebRTCMultiplayer();
  71. Error initialize(int p_self_id, bool p_server_compat = false);
  72. Error add_peer(Ref<WebRTCPeerConnection> p_peer, int p_peer_id, int p_unreliable_lifetime = 1);
  73. void remove_peer(int p_peer_id);
  74. bool has_peer(int p_peer_id);
  75. Dictionary get_peer(int p_peer_id);
  76. Dictionary get_peers();
  77. void close();
  78. // PacketPeer
  79. Error get_packet(const uint8_t **r_buffer, int &r_buffer_size); ///< buffer is GONE after next get_packet
  80. Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
  81. int get_available_packet_count() const;
  82. int get_max_packet_size() const;
  83. // NetworkedMultiplayerPeer
  84. void set_transfer_mode(TransferMode p_mode);
  85. TransferMode get_transfer_mode() const;
  86. void set_target_peer(int p_peer_id);
  87. int get_unique_id() const;
  88. int get_packet_peer() const;
  89. bool is_server() const;
  90. void poll();
  91. void set_refuse_new_connections(bool p_enable);
  92. bool is_refusing_new_connections() const;
  93. ConnectionStatus get_connection_status() const;
  94. };
  95. #endif