websocket_multiplayer_peer.h 5.3 KB

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