wsl_server.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**************************************************************************/
  2. /* wsl_server.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 WSL_SERVER_H
  31. #define WSL_SERVER_H
  32. #ifndef JAVASCRIPT_ENABLED
  33. #include "websocket_server.h"
  34. #include "wsl_peer.h"
  35. #include "core/io/stream_peer_ssl.h"
  36. #include "core/io/stream_peer_tcp.h"
  37. #include "core/io/tcp_server.h"
  38. class WSLServer : public WebSocketServer {
  39. GDCIIMPL(WSLServer, WebSocketServer);
  40. private:
  41. class PendingPeer : public Reference {
  42. private:
  43. bool _parse_request(const Vector<String> p_protocols);
  44. public:
  45. Ref<StreamPeerTCP> tcp;
  46. Ref<StreamPeer> connection;
  47. bool use_ssl;
  48. uint64_t time;
  49. uint8_t req_buf[WSL_MAX_HEADER_SIZE];
  50. int req_pos;
  51. String key;
  52. String protocol;
  53. bool has_request;
  54. CharString response;
  55. int response_sent;
  56. PendingPeer();
  57. Error do_handshake(const Vector<String> p_protocols, uint64_t p_timeout, const Vector<String> &p_extra_headers);
  58. };
  59. int _in_buf_size;
  60. int _in_pkt_size;
  61. int _out_buf_size;
  62. int _out_pkt_size;
  63. List<Ref<PendingPeer>> _pending;
  64. Ref<TCP_Server> _server;
  65. Vector<String> _protocols;
  66. Vector<String> _extra_headers;
  67. public:
  68. Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets);
  69. void set_extra_headers(const Vector<String> &p_headers);
  70. Error listen(int p_port, const Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false);
  71. void stop();
  72. bool is_listening() const;
  73. int get_max_packet_size() const;
  74. bool has_peer(int p_id) const;
  75. Ref<WebSocketPeer> get_peer(int p_id) const;
  76. IP_Address get_peer_address(int p_peer_id) const;
  77. int get_peer_port(int p_peer_id) const;
  78. void disconnect_peer(int p_peer_id, int p_code = 1000, String p_reason = "");
  79. virtual void poll();
  80. WSLServer();
  81. ~WSLServer();
  82. };
  83. #endif // JAVASCRIPT_ENABLED
  84. #endif // WSL_SERVER_H