lws_peer.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************************/
  2. /* lws_peer.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 LWSPEER_H
  31. #define LWSPEER_H
  32. #ifndef JAVASCRIPT_ENABLED
  33. #include "core/error_list.h"
  34. #include "core/io/packet_peer.h"
  35. #include "core/ring_buffer.h"
  36. #include "libwebsockets.h"
  37. #include "lws_config.h"
  38. #include "packet_buffer.h"
  39. #include "websocket_peer.h"
  40. class LWSPeer : public WebSocketPeer {
  41. GDCIIMPL(LWSPeer, WebSocketPeer);
  42. private:
  43. int _in_size;
  44. uint8_t _is_string;
  45. // Our packet info is just a boolean (is_string), using uint8_t for it.
  46. PacketBuffer<uint8_t> _in_buffer;
  47. PacketBuffer<uint8_t> _out_buffer;
  48. PoolVector<uint8_t> _packet_buffer;
  49. struct lws *wsi;
  50. WriteMode write_mode;
  51. int close_code;
  52. String close_reason;
  53. public:
  54. struct PeerData {
  55. uint32_t peer_id;
  56. bool force_close;
  57. bool clean_close;
  58. };
  59. virtual int get_available_packet_count() const;
  60. virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
  61. virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
  62. virtual int get_max_packet_size() const { return _packet_buffer.size(); };
  63. virtual void close(int p_code = 1000, String p_reason = "");
  64. virtual bool is_connected_to_host() const;
  65. virtual IP_Address get_connected_host() const;
  66. virtual uint16_t get_connected_port() const;
  67. virtual WriteMode get_write_mode() const;
  68. virtual void set_write_mode(WriteMode p_mode);
  69. virtual bool was_string_packet() const;
  70. void set_wsi(struct lws *wsi, unsigned int _in_buf_size, unsigned int _in_pkt_size, unsigned int _out_buf_size, unsigned int _out_pkt_size);
  71. Error read_wsi(void *in, size_t len);
  72. Error write_wsi();
  73. void send_close_status(struct lws *wsi);
  74. String get_close_reason(void *in, size_t len, int &r_code);
  75. LWSPeer();
  76. ~LWSPeer();
  77. };
  78. #endif // JAVASCRIPT_ENABLED
  79. #endif // LSWPEER_H