2017-12-21 02:13:23 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* lws_peer.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
2017-12-21 02:47:16 +00:00
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
2017-12-21 02:13:23 +00:00
|
|
|
/*************************************************************************/
|
2018-03-21 09:07:51 +00:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
|
2017-12-21 02:13:23 +00: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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifndef JAVASCRIPT_ENABLED
|
|
|
|
|
|
|
|
#include "lws_peer.h"
|
|
|
|
#include "core/io/ip.h"
|
|
|
|
|
2018-04-10 15:52:10 +00:00
|
|
|
// Needed for socket_helpers on Android at least. UNIXes has it, just include if not windows
|
|
|
|
#if !defined(WINDOWS_ENABLED)
|
|
|
|
#include <netinet/in.h>
|
2018-04-19 21:20:26 +00:00
|
|
|
#include <sys/socket.h>
|
2018-04-10 15:52:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "drivers/unix/socket_helpers.h"
|
|
|
|
|
2017-12-21 02:13:23 +00:00
|
|
|
void LWSPeer::set_wsi(struct lws *p_wsi) {
|
2018-08-06 01:40:26 +00:00
|
|
|
ERR_FAIL_COND(wsi != NULL);
|
|
|
|
|
|
|
|
rbw.resize(16);
|
|
|
|
rbr.resize(16);
|
2017-12-21 02:13:23 +00:00
|
|
|
wsi = p_wsi;
|
|
|
|
};
|
|
|
|
|
|
|
|
void LWSPeer::set_write_mode(WriteMode p_mode) {
|
|
|
|
write_mode = p_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWSPeer::WriteMode LWSPeer::get_write_mode() const {
|
|
|
|
return write_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error LWSPeer::read_wsi(void *in, size_t len) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
|
|
|
|
|
|
|
PeerData *peer_data = (PeerData *)(lws_wsi_user(wsi));
|
2018-08-06 01:40:26 +00:00
|
|
|
uint32_t size = in_size;
|
2017-12-21 02:13:23 +00:00
|
|
|
uint8_t is_string = lws_frame_is_binary(wsi) ? 0 : 1;
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
if (rbr.space_left() < len + 5) {
|
2017-12-21 02:13:23 +00:00
|
|
|
ERR_EXPLAIN("Buffer full! Dropping data");
|
|
|
|
ERR_FAIL_V(FAILED);
|
|
|
|
}
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
copymem(&(input_buffer[size]), in, len);
|
2017-12-21 02:13:23 +00:00
|
|
|
size += len;
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
in_size = size;
|
2017-12-21 02:13:23 +00:00
|
|
|
if (lws_is_final_fragment(wsi)) {
|
2018-08-06 01:40:26 +00:00
|
|
|
rbr.write((uint8_t *)&size, 4);
|
|
|
|
rbr.write((uint8_t *)&is_string, 1);
|
|
|
|
rbr.write(input_buffer, size);
|
|
|
|
in_count++;
|
|
|
|
in_size = 0;
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error LWSPeer::write_wsi() {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
|
|
|
|
|
|
|
PeerData *peer_data = (PeerData *)(lws_wsi_user(wsi));
|
|
|
|
PoolVector<uint8_t> tmp;
|
2018-08-06 01:40:26 +00:00
|
|
|
int left = rbw.data_left();
|
2017-12-21 02:13:23 +00:00
|
|
|
uint32_t to_write = 0;
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
if (left == 0 || out_count == 0)
|
2017-12-21 02:13:23 +00:00
|
|
|
return OK;
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
rbw.read((uint8_t *)&to_write, 4);
|
|
|
|
out_count--;
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
if (left < to_write) {
|
2018-08-06 01:40:26 +00:00
|
|
|
rbw.advance_read(left);
|
2017-12-21 02:13:23 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp.resize(LWS_PRE + to_write);
|
2018-08-06 01:40:26 +00:00
|
|
|
rbw.read(&(tmp.write()[LWS_PRE]), to_write);
|
2017-12-21 02:13:23 +00:00
|
|
|
lws_write(wsi, &(tmp.write()[LWS_PRE]), to_write, (enum lws_write_protocol)write_mode);
|
|
|
|
tmp.resize(0);
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
if (out_count > 0)
|
2017-12-21 02:13:23 +00:00
|
|
|
lws_callback_on_writable(wsi); // we want to write more!
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error LWSPeer::put_packet(const uint8_t *p_buffer, int p_buffer_size) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
|
|
|
|
|
|
|
PeerData *peer_data = (PeerData *)lws_wsi_user(wsi);
|
2018-08-06 01:40:26 +00:00
|
|
|
rbw.write((uint8_t *)&p_buffer_size, 4);
|
|
|
|
rbw.write(p_buffer, MIN(p_buffer_size, rbw.space_left()));
|
|
|
|
out_count++;
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
lws_callback_on_writable(wsi); // notify that we want to write
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
Error LWSPeer::get_packet(const uint8_t **r_buffer, int &r_buffer_size) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
|
|
|
|
|
|
|
PeerData *peer_data = (PeerData *)lws_wsi_user(wsi);
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
if (in_count == 0)
|
2017-12-21 02:13:23 +00:00
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
|
|
|
|
uint32_t to_read = 0;
|
|
|
|
uint32_t left = 0;
|
|
|
|
uint8_t is_string = 0;
|
|
|
|
r_buffer_size = 0;
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
rbr.read((uint8_t *)&to_read, 4);
|
|
|
|
in_count--;
|
|
|
|
left = rbr.data_left();
|
2017-12-21 02:13:23 +00:00
|
|
|
|
|
|
|
if (left < to_read + 1) {
|
2018-08-06 01:40:26 +00:00
|
|
|
rbr.advance_read(left);
|
2017-12-21 02:13:23 +00:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
rbr.read(&is_string, 1);
|
|
|
|
rbr.read(packet_buffer, to_read);
|
2017-12-21 02:13:23 +00:00
|
|
|
*r_buffer = packet_buffer;
|
|
|
|
r_buffer_size = to_read;
|
|
|
|
_was_string = is_string;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
int LWSPeer::get_available_packet_count() const {
|
|
|
|
|
|
|
|
if (!is_connected_to_host())
|
|
|
|
return 0;
|
|
|
|
|
2018-08-06 01:40:26 +00:00
|
|
|
return in_count;
|
2017-12-21 02:13:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool LWSPeer::was_string_packet() const {
|
|
|
|
|
|
|
|
return _was_string;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool LWSPeer::is_connected_to_host() const {
|
|
|
|
|
|
|
|
return wsi != NULL;
|
|
|
|
};
|
|
|
|
|
|
|
|
void LWSPeer::close() {
|
|
|
|
if (wsi != NULL) {
|
|
|
|
PeerData *data = ((PeerData *)lws_wsi_user(wsi));
|
|
|
|
data->force_close = true;
|
2018-08-06 01:40:26 +00:00
|
|
|
lws_callback_on_writable(wsi); // notify that we want to disconnect
|
2017-12-21 02:13:23 +00:00
|
|
|
}
|
2018-08-06 01:40:26 +00:00
|
|
|
wsi = NULL;
|
|
|
|
rbw.resize(0);
|
|
|
|
rbr.resize(0);
|
|
|
|
in_count = 0;
|
|
|
|
in_size = 0;
|
|
|
|
out_count = 0;
|
|
|
|
_was_string = false;
|
2017-12-21 02:13:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
IP_Address LWSPeer::get_connected_host() const {
|
|
|
|
|
2018-04-10 15:52:10 +00:00
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), IP_Address());
|
|
|
|
|
|
|
|
IP_Address ip;
|
|
|
|
int port = 0;
|
|
|
|
|
|
|
|
struct sockaddr_storage addr;
|
2018-06-06 16:34:08 +00:00
|
|
|
socklen_t len = sizeof(addr);
|
2018-04-19 21:20:26 +00:00
|
|
|
|
2018-04-10 15:52:10 +00:00
|
|
|
int fd = lws_get_socket_fd(wsi);
|
2018-04-19 21:20:26 +00:00
|
|
|
ERR_FAIL_COND_V(fd == -1, IP_Address());
|
2018-04-10 15:52:10 +00:00
|
|
|
|
|
|
|
int ret = getpeername(fd, (struct sockaddr *)&addr, &len);
|
|
|
|
ERR_FAIL_COND_V(ret != 0, IP_Address());
|
|
|
|
|
|
|
|
_set_ip_addr_port(ip, port, &addr);
|
|
|
|
|
|
|
|
return ip;
|
2017-12-21 02:13:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
uint16_t LWSPeer::get_connected_port() const {
|
|
|
|
|
2018-04-10 15:52:10 +00:00
|
|
|
ERR_FAIL_COND_V(!is_connected_to_host(), 0);
|
|
|
|
|
|
|
|
IP_Address ip;
|
|
|
|
int port = 0;
|
|
|
|
|
|
|
|
struct sockaddr_storage addr;
|
2018-06-06 16:34:08 +00:00
|
|
|
socklen_t len = sizeof(addr);
|
2018-04-19 21:20:26 +00:00
|
|
|
|
2018-04-10 15:52:10 +00:00
|
|
|
int fd = lws_get_socket_fd(wsi);
|
2018-04-19 21:20:26 +00:00
|
|
|
ERR_FAIL_COND_V(fd == -1, 0);
|
2018-04-10 15:52:10 +00:00
|
|
|
|
|
|
|
int ret = getpeername(fd, (struct sockaddr *)&addr, &len);
|
|
|
|
ERR_FAIL_COND_V(ret != 0, 0);
|
|
|
|
|
|
|
|
_set_ip_addr_port(ip, port, &addr);
|
|
|
|
|
|
|
|
return port;
|
2017-12-21 02:13:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LWSPeer::LWSPeer() {
|
|
|
|
wsi = NULL;
|
|
|
|
write_mode = WRITE_MODE_BINARY;
|
2018-08-06 01:40:26 +00:00
|
|
|
close();
|
2017-12-21 02:13:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
LWSPeer::~LWSPeer() {
|
|
|
|
|
|
|
|
close();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // JAVASCRIPT_ENABLED
|