websocket_server.h 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**************************************************************************/
  2. /* websocket_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 WEBSOCKET_SERVER_H
  31. #define WEBSOCKET_SERVER_H
  32. #include "core/crypto/crypto.h"
  33. #include "core/reference.h"
  34. #include "websocket_multiplayer_peer.h"
  35. #include "websocket_peer.h"
  36. class WebSocketServer : public WebSocketMultiplayerPeer {
  37. GDCLASS(WebSocketServer, WebSocketMultiplayerPeer);
  38. GDCICLASS(WebSocketServer);
  39. IP_Address bind_ip;
  40. protected:
  41. static void _bind_methods();
  42. Ref<CryptoKey> private_key;
  43. Ref<X509Certificate> ssl_cert;
  44. Ref<X509Certificate> ca_chain;
  45. uint32_t handshake_timeout = 3000;
  46. public:
  47. virtual void poll() = 0;
  48. virtual void set_extra_headers(const Vector<String> &p_headers) = 0;
  49. virtual Error listen(int p_port, const Vector<String> p_protocols = Vector<String>(), bool gd_mp_api = false) = 0;
  50. virtual void stop() = 0;
  51. virtual bool is_listening() const = 0;
  52. virtual bool has_peer(int p_id) const = 0;
  53. virtual Ref<WebSocketPeer> get_peer(int p_id) const = 0;
  54. virtual bool is_server() const;
  55. ConnectionStatus get_connection_status() const;
  56. virtual IP_Address get_peer_address(int p_peer_id) const = 0;
  57. virtual int get_peer_port(int p_peer_id) const = 0;
  58. virtual void disconnect_peer(int p_peer_id, int p_code = 1000, String p_reason = "") = 0;
  59. void _on_peer_packet(int32_t p_peer_id);
  60. void _on_connect(int32_t p_peer_id, String p_protocol);
  61. void _on_disconnect(int32_t p_peer_id, bool p_was_clean);
  62. void _on_close_request(int32_t p_peer_id, int p_code, String p_reason);
  63. IP_Address get_bind_ip() const;
  64. void set_bind_ip(const IP_Address &p_bind_ip);
  65. Ref<CryptoKey> get_private_key() const;
  66. void set_private_key(Ref<CryptoKey> p_key);
  67. Ref<X509Certificate> get_ssl_certificate() const;
  68. void set_ssl_certificate(Ref<X509Certificate> p_cert);
  69. Ref<X509Certificate> get_ca_chain() const;
  70. void set_ca_chain(Ref<X509Certificate> p_ca_chain);
  71. float get_handshake_timeout() const;
  72. void set_handshake_timeout(float p_timeout);
  73. virtual Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) = 0;
  74. WebSocketServer();
  75. ~WebSocketServer();
  76. };
  77. #endif // WEBSOCKET_SERVER_H