wsl_peer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*************************************************************************/
  2. /* wsl_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 WSLPEER_H
  31. #define WSLPEER_H
  32. #ifndef JAVASCRIPT_ENABLED
  33. #include "core/error_list.h"
  34. #include "core/io/packet_peer.h"
  35. #include "core/io/stream_peer_tcp.h"
  36. #include "core/ring_buffer.h"
  37. #include "packet_buffer.h"
  38. #include "websocket_peer.h"
  39. #include "wslay/wslay.h"
  40. #define WSL_MAX_HEADER_SIZE 4096
  41. class WSLPeer : public WebSocketPeer {
  42. GDCIIMPL(WSLPeer, WebSocketPeer);
  43. public:
  44. struct PeerData {
  45. bool polling;
  46. bool destroy;
  47. bool valid;
  48. bool is_server;
  49. bool closing;
  50. void *obj;
  51. void *peer;
  52. Ref<StreamPeer> conn;
  53. Ref<StreamPeerTCP> tcp;
  54. int id;
  55. wslay_event_context_ptr ctx;
  56. PeerData() {
  57. polling = false;
  58. destroy = false;
  59. valid = false;
  60. is_server = false;
  61. id = 1;
  62. ctx = NULL;
  63. obj = NULL;
  64. closing = false;
  65. peer = NULL;
  66. }
  67. };
  68. static String compute_key_response(String p_key);
  69. static String generate_key();
  70. private:
  71. static bool _wsl_poll(struct PeerData *p_data);
  72. static void _wsl_destroy(struct PeerData **p_data);
  73. struct PeerData *_data;
  74. uint8_t _is_string;
  75. // Our packet info is just a boolean (is_string), using uint8_t for it.
  76. PacketBuffer<uint8_t> _in_buffer;
  77. PoolVector<uint8_t> _packet_buffer;
  78. WriteMode write_mode;
  79. int _out_buf_size;
  80. int _out_pkt_size;
  81. public:
  82. int close_code;
  83. String close_reason;
  84. void poll(); // Used by client and server.
  85. virtual int get_available_packet_count() const;
  86. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
  87. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
  88. virtual int get_max_packet_size() const { return _packet_buffer.size(); };
  89. virtual int get_current_outbound_buffered_amount() const;
  90. virtual void close_now();
  91. virtual void close(int p_code = 1000, String p_reason = "");
  92. virtual bool is_connected_to_host() const;
  93. virtual IP_Address get_connected_host() const;
  94. virtual uint16_t get_connected_port() const;
  95. virtual WriteMode get_write_mode() const;
  96. virtual void set_write_mode(WriteMode p_mode);
  97. virtual bool was_string_packet() const;
  98. virtual void set_no_delay(bool p_enabled);
  99. void make_context(PeerData *p_data, unsigned int p_in_buf_size, unsigned int p_in_pkt_size, unsigned int p_out_buf_size, unsigned int p_out_pkt_size);
  100. Error parse_message(const wslay_event_on_msg_recv_arg *arg);
  101. void invalidate();
  102. WSLPeer();
  103. ~WSLPeer();
  104. };
  105. #endif // JAVASCRIPT_ENABLED
  106. #endif // LSWPEER_H