1 /*************************************************************************/
2 /*  wsl_client.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 
31 #ifndef WSLCLIENT_H
32 #define WSLCLIENT_H
33 
34 #ifndef JAVASCRIPT_ENABLED
35 
36 #include "core/error_list.h"
37 #include "core/io/stream_peer_ssl.h"
38 #include "core/io/stream_peer_tcp.h"
39 #include "websocket_client.h"
40 #include "wsl_peer.h"
41 #include "wslay/wslay.h"
42 
43 class WSLClient : public WebSocketClient {
44 
45 	GDCIIMPL(WSLClient, WebSocketClient);
46 
47 private:
48 	int _in_buf_size;
49 	int _in_pkt_size;
50 	int _out_buf_size;
51 	int _out_pkt_size;
52 
53 	Ref<WSLPeer> _peer;
54 	Ref<StreamPeerTCP> _tcp;
55 	Ref<StreamPeer> _connection;
56 
57 	CharString _request;
58 	int _requested;
59 
60 	uint8_t _resp_buf[WSL_MAX_HEADER_SIZE];
61 	int _resp_pos;
62 
63 	String _response;
64 
65 	String _key;
66 	String _host;
67 	Vector<String> _protocols;
68 	bool _use_ssl;
69 
70 	void _do_handshake();
71 	bool _verify_headers(String &r_protocol);
72 
73 public:
74 	Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets);
75 	Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocol = Vector<String>(), const Vector<String> p_custom_headers = Vector<String>());
76 	int get_max_packet_size() const;
77 	Ref<WebSocketPeer> get_peer(int p_peer_id) const;
78 	void disconnect_from_host(int p_code = 1000, String p_reason = "");
79 	IP_Address get_connected_host() const;
80 	uint16_t get_connected_port() const;
81 	virtual ConnectionStatus get_connection_status() const;
82 	virtual void poll();
83 
84 	WSLClient();
85 	~WSLClient();
86 };
87 
88 #endif // JAVASCRIPT_ENABLED
89 
90 #endif // WSLCLIENT_H
91