websocket_multiplayer_peer.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**************************************************************************/
  2. /* websocket_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 "websocket_peer.h"
  32. #include "core/io/tcp_server.h"
  33. #include "core/templates/list.h"
  34. #include "scene/main/multiplayer_peer.h"
  35. class WebSocketMultiplayerPeer : public MultiplayerPeer {
  36. GDCLASS(WebSocketMultiplayerPeer, MultiplayerPeer);
  37. private:
  38. Ref<WebSocketPeer> _create_peer();
  39. protected:
  40. enum {
  41. SYS_NONE = 0,
  42. SYS_ADD = 1,
  43. SYS_DEL = 2,
  44. SYS_ID = 3,
  45. PROTO_SIZE = 9
  46. };
  47. struct Packet {
  48. int source = 0;
  49. uint8_t *data = nullptr;
  50. uint32_t size = 0;
  51. };
  52. struct PendingPeer {
  53. uint64_t time = 0;
  54. Ref<StreamPeerTCP> tcp;
  55. Ref<StreamPeer> connection;
  56. Ref<WebSocketPeer> ws;
  57. };
  58. uint64_t handshake_timeout = 3000;
  59. Ref<WebSocketPeer> peer_config;
  60. HashMap<int, PendingPeer> pending_peers;
  61. Ref<TCPServer> tcp_server;
  62. Ref<TLSOptions> tls_server_options;
  63. ConnectionStatus connection_status = CONNECTION_DISCONNECTED;
  64. List<Packet> incoming_packets;
  65. HashMap<int, Ref<WebSocketPeer>> peers_map;
  66. Packet current_packet;
  67. int target_peer = 0;
  68. int unique_id = 0;
  69. static void _bind_methods();
  70. void _poll_client();
  71. void _poll_server();
  72. void _clear();
  73. public:
  74. /* MultiplayerPeer */
  75. virtual void set_target_peer(int p_target_peer) override;
  76. virtual int get_packet_peer() const override;
  77. virtual int get_packet_channel() const override { return 0; }
  78. virtual TransferMode get_packet_mode() const override { return TRANSFER_MODE_RELIABLE; }
  79. virtual int get_unique_id() const override;
  80. virtual bool is_server_relay_supported() const override { return true; }
  81. virtual int get_max_packet_size() const override;
  82. virtual bool is_server() const override;
  83. virtual void poll() override;
  84. virtual void close() override;
  85. virtual void disconnect_peer(int p_peer_id, bool p_force = false) override;
  86. virtual ConnectionStatus get_connection_status() const override;
  87. /* PacketPeer */
  88. virtual int get_available_packet_count() const override;
  89. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override;
  90. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override;
  91. /* WebSocketPeer */
  92. virtual Ref<WebSocketPeer> get_peer(int p_peer_id) const;
  93. Error create_client(const String &p_url, Ref<TLSOptions> p_options);
  94. Error create_server(int p_port, IPAddress p_bind_ip, Ref<TLSOptions> p_options);
  95. void set_supported_protocols(const Vector<String> &p_protocols);
  96. Vector<String> get_supported_protocols() const;
  97. void set_handshake_headers(const Vector<String> &p_headers);
  98. Vector<String> get_handshake_headers() const;
  99. void set_outbound_buffer_size(int p_buffer_size);
  100. int get_outbound_buffer_size() const;
  101. void set_inbound_buffer_size(int p_buffer_size);
  102. int get_inbound_buffer_size() const;
  103. float get_handshake_timeout() const;
  104. void set_handshake_timeout(float p_timeout);
  105. IPAddress get_peer_address(int p_peer_id) const;
  106. int get_peer_port(int p_peer_id) const;
  107. void set_max_queued_packets(int p_max_queued_packets);
  108. int get_max_queued_packets() const;
  109. WebSocketMultiplayerPeer();
  110. ~WebSocketMultiplayerPeer();
  111. };