1 | /**************************************************************************/ |
2 | /* emws_peer.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 | |
31 | #ifndef EMWS_PEER_H |
32 | #define EMWS_PEER_H |
33 | |
34 | #ifdef WEB_ENABLED |
35 | |
36 | #include "packet_buffer.h" |
37 | #include "websocket_peer.h" |
38 | |
39 | #include "core/error/error_list.h" |
40 | #include "core/io/packet_peer.h" |
41 | #include "core/templates/ring_buffer.h" |
42 | |
43 | #include <emscripten.h> |
44 | |
45 | extern "C" { |
46 | typedef void (*WSOnOpen)(void *p_ref, char *p_protocol); |
47 | typedef void (*WSOnMessage)(void *p_ref, const uint8_t *p_buf, int p_buf_len, int p_is_string); |
48 | typedef void (*WSOnClose)(void *p_ref, int p_code, const char *p_reason, int p_is_clean); |
49 | typedef void (*WSOnError)(void *p_ref); |
50 | |
51 | extern int godot_js_websocket_create(void *p_ref, const char *p_url, const char *p_proto, WSOnOpen p_on_open, WSOnMessage p_on_message, WSOnError p_on_error, WSOnClose p_on_close); |
52 | extern int godot_js_websocket_send(int p_id, const uint8_t *p_buf, int p_buf_len, int p_raw); |
53 | extern int godot_js_websocket_buffered_amount(int p_id); |
54 | extern void godot_js_websocket_close(int p_id, int p_code, const char *p_reason); |
55 | extern void godot_js_websocket_destroy(int p_id); |
56 | } |
57 | |
58 | class EMWSPeer : public WebSocketPeer { |
59 | private: |
60 | int peer_sock = -1; |
61 | |
62 | State ready_state = STATE_CLOSED; |
63 | Vector<uint8_t> packet_buffer; |
64 | PacketBuffer<uint8_t> in_buffer; |
65 | uint8_t was_string = 0; |
66 | int close_code = -1; |
67 | String close_reason; |
68 | String selected_protocol; |
69 | String requested_url; |
70 | |
71 | static WebSocketPeer *_create() { return memnew(EMWSPeer); } |
72 | static void _esws_on_connect(void *obj, char *proto); |
73 | static void _esws_on_message(void *obj, const uint8_t *p_data, int p_data_size, int p_is_string); |
74 | static void _esws_on_error(void *obj); |
75 | static void _esws_on_close(void *obj, int code, const char *reason, int was_clean); |
76 | |
77 | void _clear(); |
78 | Error _send(const uint8_t *p_buffer, int p_buffer_size, bool p_binary); |
79 | |
80 | public: |
81 | static void initialize() { WebSocketPeer::_create = EMWSPeer::_create; } |
82 | |
83 | // PacketPeer |
84 | virtual int get_available_packet_count() const override; |
85 | virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size) override; |
86 | virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size) override; |
87 | virtual int get_max_packet_size() const override { return packet_buffer.size(); }; |
88 | |
89 | // WebSocketPeer |
90 | virtual Error send(const uint8_t *p_buffer, int p_buffer_size, WriteMode p_mode) override; |
91 | virtual Error connect_to_url(const String &p_url, Ref<TLSOptions> p_tls_client_options) override; |
92 | virtual Error accept_stream(Ref<StreamPeer> p_stream) override; |
93 | virtual void close(int p_code = 1000, String p_reason = "" ) override; |
94 | virtual void poll() override; |
95 | |
96 | virtual State get_ready_state() const override; |
97 | virtual int get_close_code() const override; |
98 | virtual String get_close_reason() const override; |
99 | virtual int get_current_outbound_buffered_amount() const override; |
100 | |
101 | virtual IPAddress get_connected_host() const override; |
102 | virtual uint16_t get_connected_port() const override; |
103 | virtual String get_selected_protocol() const override; |
104 | virtual String get_requested_url() const override; |
105 | |
106 | virtual bool was_string_packet() const override; |
107 | virtual void set_no_delay(bool p_enabled) override; |
108 | |
109 | EMWSPeer(); |
110 | ~EMWSPeer(); |
111 | }; |
112 | |
113 | #endif // WEB_ENABLED |
114 | |
115 | #endif // EMWS_PEER_H |
116 | |