wsl_server.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*************************************************************************/
  2. /* wsl_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 WSLSERVER_H
  31. #define WSLSERVER_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. #define WSL_SERVER_TIMEOUT 1000
  39. class WSLServer : public WebSocketServer {
  40. GDCIIMPL(WSLServer, WebSocketServer);
  41. private:
  42. class PendingPeer : public Reference {
  43. private:
  44. bool _parse_request(const Vector<String> p_protocols);
  45. public:
  46. Ref<StreamPeerTCP> tcp;
  47. Ref<StreamPeer> connection;
  48. bool use_ssl;
  49. int time;
  50. uint8_t req_buf[WSL_MAX_HEADER_SIZE];
  51. int req_pos;
  52. String key;
  53. String protocol;
  54. bool has_request;
  55. CharString response;
  56. int response_sent;
  57. PendingPeer();
  58. Error do_handshake(const Vector<String> p_protocols);
  59. };
  60. int _in_buf_size;
  61. int _in_pkt_size;
  62. int _out_buf_size;
  63. int _out_pkt_size;
  64. List<Ref<PendingPeer> > _pending;
  65. Ref<TCP_Server> _server;
  66. Vector<String> _protocols;
  67. public:
  68. Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets);
  69. Error listen(int p_port, const Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false);
  70. void stop();
  71. bool is_listening() const;
  72. int get_max_packet_size() const;
  73. bool has_peer(int p_id) const;
  74. Ref<WebSocketPeer> get_peer(int p_id) const;
  75. IP_Address get_peer_address(int p_peer_id) const;
  76. int get_peer_port(int p_peer_id) const;
  77. void disconnect_peer(int p_peer_id, int p_code = 1000, String p_reason = "");
  78. virtual void poll();
  79. WSLServer();
  80. ~WSLServer();
  81. };
  82. #endif // JAVASCRIPT_ENABLED
  83. #endif // WSLSERVER_H