2017-12-21 03:13:23 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* websocket_peer.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
2017-12-21 03:47:16 +01:00
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
2017-12-21 03:13:23 +01:00
|
|
|
/*************************************************************************/
|
2022-01-03 21:27:34 +01:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-12-21 03:13:23 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2019-01-01 12:46:36 +01:00
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
#include "websocket_peer.h"
|
|
|
|
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 22:44:44 +02:00
|
|
|
WebSocketPeer *(*WebSocketPeer::_create)() = nullptr;
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
WebSocketPeer::WebSocketPeer() {
|
|
|
|
}
|
|
|
|
|
|
|
|
WebSocketPeer::~WebSocketPeer() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketPeer::_bind_methods() {
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 22:44:44 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("connect_to_url", "url", "verify_tls", "trusted_tls_certificate"), &WebSocketPeer::connect_to_url, DEFVAL(true), DEFVAL(Ref<X509Certificate>()));
|
|
|
|
ClassDB::bind_method(D_METHOD("accept_stream", "stream"), &WebSocketPeer::accept_stream);
|
|
|
|
ClassDB::bind_method(D_METHOD("send", "message", "write_mode"), &WebSocketPeer::_send_bind, DEFVAL(WRITE_MODE_BINARY));
|
|
|
|
ClassDB::bind_method(D_METHOD("send_text", "message"), &WebSocketPeer::send_text);
|
2017-12-21 03:13:23 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("was_string_packet"), &WebSocketPeer::was_string_packet);
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 22:44:44 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("poll"), &WebSocketPeer::poll);
|
2018-09-23 21:14:20 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("close", "code", "reason"), &WebSocketPeer::close, DEFVAL(1000), DEFVAL(""));
|
2018-04-10 17:52:10 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_connected_host"), &WebSocketPeer::get_connected_host);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_connected_port"), &WebSocketPeer::get_connected_port);
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 22:44:44 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_selected_protocol"), &WebSocketPeer::get_selected_protocol);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_requested_url"), &WebSocketPeer::get_requested_url);
|
2020-01-28 14:06:28 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_no_delay", "enabled"), &WebSocketPeer::set_no_delay);
|
2021-07-29 22:11:27 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_current_outbound_buffered_amount"), &WebSocketPeer::get_current_outbound_buffered_amount);
|
2017-12-21 03:13:23 +01:00
|
|
|
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 22:44:44 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_ready_state"), &WebSocketPeer::get_ready_state);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_close_code"), &WebSocketPeer::get_close_code);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_close_reason"), &WebSocketPeer::get_close_reason);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_supported_protocols"), &WebSocketPeer::_get_supported_protocols);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_supported_protocols", "protocols"), &WebSocketPeer::set_supported_protocols);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_handshake_headers"), &WebSocketPeer::_get_handshake_headers);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_handshake_headers", "protocols"), &WebSocketPeer::set_handshake_headers);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_inbound_buffer_size"), &WebSocketPeer::get_inbound_buffer_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_inbound_buffer_size", "buffer_size"), &WebSocketPeer::set_inbound_buffer_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_outbound_buffer_size"), &WebSocketPeer::get_outbound_buffer_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_outbound_buffer_size", "buffer_size"), &WebSocketPeer::set_outbound_buffer_size);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("set_max_queued_packets", "buffer_size"), &WebSocketPeer::set_max_queued_packets);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_max_queued_packets"), &WebSocketPeer::get_max_queued_packets);
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "supported_protocols"), "set_supported_protocols", "get_supported_protocols");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "handshake_headers"), "set_handshake_headers", "get_handshake_headers");
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "inbound_buffer_size"), "set_inbound_buffer_size", "get_inbound_buffer_size");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "outbound_buffer_size"), "set_outbound_buffer_size", "get_outbound_buffer_size");
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_queued_packets"), "set_max_queued_packets", "get_max_queued_packets");
|
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
BIND_ENUM_CONSTANT(WRITE_MODE_TEXT);
|
|
|
|
BIND_ENUM_CONSTANT(WRITE_MODE_BINARY);
|
[WebSocket] Refactor websocket module.
This commit is a huge refactor of the websocket module.
The module is really old, and some design choices had to be
re-evaluated.
The WebSocketClient and WebSocketServer classes are now gone, and
WebSocketPeer can act as either client or server.
The WebSocketMultiplayerPeer class is no longer abstract, and implements
the Multiplayer API on top of the lower level WebSocketPeer.
WebSocketPeer is now a "raw" peer, like StreamPeerTCP and StreamPeerTLS,
so it emits no signal, and just needs polling to update its internal
state.
To use it as a client, simply call WebSocketPeer.coonect_to_url, then
frequently poll the peer until STATE_OPEN is reached and then you can
write or read from it, or STATE_CLOSED and then you can check the
disconnect code and reason).
To implement a server instead, a TCPServer must be created, and the
accepted connections needs to be provided to
WebSocketPeer.accept_stream (which will perform the HTTP handshake).
A full example of a WebSocketServer using TLS will be provided in the
demo repository.
2022-09-24 22:44:44 +02:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(STATE_CONNECTING);
|
|
|
|
BIND_ENUM_CONSTANT(STATE_OPEN);
|
|
|
|
BIND_ENUM_CONSTANT(STATE_CLOSING);
|
|
|
|
BIND_ENUM_CONSTANT(STATE_CLOSED);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error WebSocketPeer::_send_bind(const PackedByteArray &p_message, WriteMode p_mode) {
|
|
|
|
return send(p_message.ptr(), p_message.size(), p_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
Error WebSocketPeer::send_text(const String &p_text) {
|
|
|
|
const CharString cs = p_text.utf8();
|
|
|
|
return send((const uint8_t *)cs.ptr(), cs.length(), WRITE_MODE_TEXT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketPeer::set_supported_protocols(const Vector<String> &p_protocols) {
|
|
|
|
// Strip edges from protocols.
|
|
|
|
supported_protocols.resize(p_protocols.size());
|
|
|
|
for (int i = 0; i < p_protocols.size(); i++) {
|
|
|
|
supported_protocols.write[i] = p_protocols[i].strip_edges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vector<String> WebSocketPeer::get_supported_protocols() const {
|
|
|
|
return supported_protocols;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> WebSocketPeer::_get_supported_protocols() const {
|
|
|
|
Vector<String> out;
|
|
|
|
out.append_array(supported_protocols);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketPeer::set_handshake_headers(const Vector<String> &p_headers) {
|
|
|
|
handshake_headers = p_headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vector<String> WebSocketPeer::get_handshake_headers() const {
|
|
|
|
return handshake_headers;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<String> WebSocketPeer::_get_handshake_headers() const {
|
|
|
|
Vector<String> out;
|
|
|
|
out.append_array(handshake_headers);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketPeer::set_outbound_buffer_size(int p_buffer_size) {
|
|
|
|
outbound_buffer_size = p_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketPeer::get_outbound_buffer_size() const {
|
|
|
|
return outbound_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketPeer::set_inbound_buffer_size(int p_buffer_size) {
|
|
|
|
inbound_buffer_size = p_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketPeer::get_inbound_buffer_size() const {
|
|
|
|
return inbound_buffer_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketPeer::set_max_queued_packets(int p_max_queued_packets) {
|
|
|
|
max_queued_packets = p_max_queued_packets;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WebSocketPeer::get_max_queued_packets() const {
|
|
|
|
return max_queued_packets;
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|