2017-12-21 03:13:23 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* emws_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
|
|
|
/*************************************************************************/
|
2021-01-01 20:13:46 +01:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 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
|
|
|
#ifdef JAVASCRIPT_ENABLED
|
|
|
|
|
|
|
|
#include "emws_peer.h"
|
|
|
|
#include "core/io/ip.h"
|
|
|
|
|
2021-07-29 22:11:27 +02:00
|
|
|
void EMWSPeer::set_sock(int p_sock, unsigned int p_in_buf_size, unsigned int p_in_pkt_size, unsigned int p_out_buf_size) {
|
2017-12-21 03:13:23 +01:00
|
|
|
peer_sock = p_sock;
|
2018-11-03 05:46:38 +01:00
|
|
|
_in_buffer.resize(p_in_pkt_size, p_in_buf_size);
|
|
|
|
_packet_buffer.resize((1 << p_in_buf_size));
|
2021-07-29 22:11:27 +02:00
|
|
|
_out_buf_size = p_out_buf_size;
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void EMWSPeer::set_write_mode(WriteMode p_mode) {
|
|
|
|
write_mode = p_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
EMWSPeer::WriteMode EMWSPeer::get_write_mode() const {
|
|
|
|
return write_mode;
|
|
|
|
}
|
|
|
|
|
2020-10-23 18:33:20 +02:00
|
|
|
Error EMWSPeer::read_msg(const uint8_t *p_data, uint32_t p_size, bool p_is_string) {
|
2017-12-21 03:13:23 +01:00
|
|
|
uint8_t is_string = p_is_string ? 1 : 0;
|
2018-11-03 05:46:38 +01:00
|
|
|
return _in_buffer.write_packet(p_data, p_size, &is_string);
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Error EMWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
|
2021-08-06 10:32:02 +02:00
|
|
|
ERR_FAIL_COND_V(_out_buf_size && ((uint64_t)godot_js_websocket_buffered_amount(peer_sock) >= (1ULL << _out_buf_size)), ERR_OUT_OF_MEMORY);
|
2021-07-29 22:11:27 +02:00
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
int is_bin = write_mode == WebSocketPeer::WRITE_MODE_BINARY ? 1 : 0;
|
2021-07-29 22:11:27 +02:00
|
|
|
|
2020-10-23 18:33:20 +02:00
|
|
|
godot_js_websocket_send(peer_sock, p_buffer, p_buffer_size, is_bin);
|
2017-12-21 03:13:23 +01:00
|
|
|
return OK;
|
2020-06-17 08:14:58 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
Error EMWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
|
2018-11-03 05:46:38 +01:00
|
|
|
if (_in_buffer.packets_left() == 0)
|
2017-12-21 03:13:23 +01:00
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
|
2018-11-03 05:46:38 +01:00
|
|
|
int read = 0;
|
2020-02-25 10:15:45 +01:00
|
|
|
Error err = _in_buffer.read_packet(_packet_buffer.ptrw(), _packet_buffer.size(), &_is_string, read);
|
2018-11-03 05:46:38 +01:00
|
|
|
ERR_FAIL_COND_V(err != OK, err);
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2020-02-25 10:15:45 +01:00
|
|
|
*r_buffer = _packet_buffer.ptr();
|
2018-11-03 05:46:38 +01:00
|
|
|
r_buffer_size = read;
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
return OK;
|
2020-06-17 08:14:58 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
int EMWSPeer::get_available_packet_count() const {
|
2018-11-03 05:46:38 +01:00
|
|
|
return _in_buffer.packets_left();
|
2020-06-17 08:14:58 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2021-07-29 22:11:27 +02:00
|
|
|
int EMWSPeer::get_current_outbound_buffered_amount() const {
|
|
|
|
if (peer_sock != -1) {
|
|
|
|
return godot_js_websocket_buffered_amount(peer_sock);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
bool EMWSPeer::was_string_packet() const {
|
2018-11-03 05:46:38 +01:00
|
|
|
return _is_string;
|
2020-06-17 08:14:58 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
bool EMWSPeer::is_connected_to_host() const {
|
|
|
|
return peer_sock != -1;
|
2020-06-17 08:14:58 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2018-09-23 21:14:20 +02:00
|
|
|
void EMWSPeer::close(int p_code, String p_reason) {
|
2017-12-21 03:13:23 +01:00
|
|
|
if (peer_sock != -1) {
|
2020-10-23 18:33:20 +02:00
|
|
|
godot_js_websocket_close(peer_sock, p_code, p_reason.utf8().get_data());
|
2017-12-21 03:13:23 +01:00
|
|
|
}
|
2018-11-03 05:46:38 +01:00
|
|
|
_is_string = 0;
|
|
|
|
_in_buffer.clear();
|
2017-12-21 03:13:23 +01:00
|
|
|
peer_sock = -1;
|
2020-06-17 08:14:58 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2021-05-06 02:48:18 +02:00
|
|
|
IPAddress EMWSPeer::get_connected_host() const {
|
|
|
|
ERR_FAIL_V_MSG(IPAddress(), "Not supported in HTML5 export.");
|
2017-12-21 03:13:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
uint16_t EMWSPeer::get_connected_port() const {
|
2019-08-11 10:49:53 +02:00
|
|
|
ERR_FAIL_V_MSG(0, "Not supported in HTML5 export.");
|
2020-06-17 08:14:58 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2020-01-28 14:06:28 +01:00
|
|
|
void EMWSPeer::set_no_delay(bool p_enabled) {
|
|
|
|
ERR_FAIL_MSG("'set_no_delay' is not supported in HTML5 export.");
|
|
|
|
}
|
|
|
|
|
2017-12-21 03:13:23 +01:00
|
|
|
EMWSPeer::EMWSPeer() {
|
2018-11-03 05:46:38 +01:00
|
|
|
close();
|
2020-06-17 08:14:58 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
EMWSPeer::~EMWSPeer() {
|
|
|
|
close();
|
2020-06-17 08:14:58 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
#endif // JAVASCRIPT_ENABLED
|