godot/modules/websocket/lws_peer.cpp

284 lines
7.2 KiB
C++
Raw Normal View History

/*************************************************************************/
/* lws_peer.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
/* */
/* 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"
Fix warnings about unused variables [-Wunused-variable] Fixes the following GCC 5 warnings: ``` drivers/gles2/rasterizer_scene_gles2.cpp:1139:15: warning: unused variable 'offset' [-Wunused-variable] drivers/gles2/rasterizer_scene_gles2.cpp:1205:39: warning: unused variable 'multi_mesh' [-Wunused-variable] drivers/gles2/rasterizer_storage_gles2.cpp:359:7: warning: unused variable 'srgb' [-Wunused-variable] drivers/gles2/shader_gles2.cpp:1016:45: warning: unused variable 'texture_hints' [-Wunused-variable] editor/animation_track_editor.cpp:776:9: warning: unused variable 'keys_to' [-Wunused-variable] editor/editor_inspector.cpp:273:7: warning: unused variable 'vs_height' [-Wunused-variable] editor/editor_themes.cpp:202:10: warning: unused variable 'begin_time' [-Wunused-variable] editor/editor_themes.cpp:239:10: warning: unused variable 'end_time' [-Wunused-variable] editor/plugins/animation_blend_tree_editor_plugin.cpp:726:17: warning: unused variable 'an' [-Wunused-variable] editor/plugins/script_text_editor.cpp:1278:8: warning: unused variable 'fold_state' [-Wunused-variable] main/main.cpp:132:13: warning: 'use_vsync' defined but not used [-Wunused-variable] modules/cvtt/image_compress_cvtt.cpp:231:8: warning: unused variable 'y_end' [-Wunused-variable] modules/cvtt/image_compress_cvtt.cpp:311:6: warning: unused variable 'shift' [-Wunused-variable] modules/gdscript/gdscript_editor.cpp:58:7: warning: unused variable 'th' [-Wunused-variable] modules/gridmap/grid_map.cpp:1084:6: warning: unused variable 'ofs' [-Wunused-variable] modules/theora/video_stream_theora.cpp:442:9: warning: unused variable 'tr' [-Wunused-variable] modules/visual_script/visual_script_editor.cpp:2606:6: warning: unused variable 'count' [-Wunused-variable] modules/visual_script/visual_script_editor.cpp:2829:6: warning: unused variable 'seq_count' [-Wunused-variable] modules/visual_script/visual_script_editor.cpp:2844:24: warning: unused variable 'vnode_function' [-Wunused-variable] modules/websocket/lws_peer.cpp:122:12: warning: unused variable 'peer_data' [-Wunused-variable] modules/websocket/lws_peer.cpp:135:12: warning: unused variable 'peer_data' [-Wunused-variable] modules/websocket/lws_peer.cpp:63:12: warning: unused variable 'peer_data' [-Wunused-variable] modules/websocket/lws_peer.cpp:91:12: warning: unused variable 'peer_data' [-Wunused-variable] platform/android/export/export.cpp:763:16: warning: unused variable 'node_size' [-Wunused-variable] scene/gui/rich_text_label.cpp:850:10: warning: unused variable 'x_ofs' [-Wunused-variable] scene/gui/text_edit.cpp:653:8: warning: unused variable 'tab_w' [-Wunused-variable] scene/resources/bit_mask.cpp:186:6: warning: unused variable 'i' [-Wunused-variable] scene/resources/mesh.cpp:549:20: warning: '_array_name' defined but not used [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:107:10: warning: unused variable 'v2' [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:108:10: warning: unused variable 'v3' [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:109:10: warning: unused variable 'v4' [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:110:10: warning: unused variable 'v5' [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:111:10: warning: unused variable 'v0n' [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:112:10: warning: unused variable 'v1n' [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:113:10: warning: unused variable 'v2n' [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:114:10: warning: unused variable 'v3n' [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:115:10: warning: unused variable 'v4n' [-Wunused-variable] servers/audio/audio_rb_resampler.cpp:116:10: warning: unused variable 'v5n' [-Wunused-variable] servers/visual/default_mouse_cursor.xpm:2:21: warning: 'default_mouse_cursor_xpm' defined but not used [-Wunused-variable] ```
2018-09-27 09:24:41 +00:00
#include "core/io/ip.h"
// Needed for socket_helpers on Android at least. UNIXes has it, just include if not windows
#if !defined(WINDOWS_ENABLED)
#include <netinet/in.h>
#include <sys/socket.h>
#endif
#include "drivers/unix/net_socket_posix.h"
void LWSPeer::set_wsi(struct lws *p_wsi) {
ERR_FAIL_COND(wsi != NULL);
rbw.resize(16);
rbr.resize(16);
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);
uint32_t size = in_size;
uint8_t is_string = lws_frame_is_binary(wsi) ? 0 : 1;
if (rbr.space_left() < len + 5) {
ERR_EXPLAIN("Buffer full! Dropping data");
ERR_FAIL_V(FAILED);
}
copymem(&(input_buffer[size]), in, len);
size += len;
in_size = size;
if (lws_is_final_fragment(wsi)) {
rbr.write((uint8_t *)&size, 4);
rbr.write((uint8_t *)&is_string, 1);
rbr.write(input_buffer, size);
in_count++;
in_size = 0;
}
return OK;
}
Error LWSPeer::write_wsi() {
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
PoolVector<uint8_t> tmp;
int left = rbw.data_left();
uint32_t to_write = 0;
if (left == 0 || out_count == 0)
return OK;
rbw.read((uint8_t *)&to_write, 4);
out_count--;
if (left < to_write) {
rbw.advance_read(left);
return FAILED;
}
tmp.resize(LWS_PRE + to_write);
rbw.read(&(tmp.write()[LWS_PRE]), to_write);
lws_write(wsi, &(tmp.write()[LWS_PRE]), to_write, (enum lws_write_protocol)write_mode);
tmp.resize(0);
if (out_count > 0)
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);
rbw.write((uint8_t *)&p_buffer_size, 4);
rbw.write(p_buffer, MIN(p_buffer_size, rbw.space_left()));
out_count++;
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);
if (in_count == 0)
return ERR_UNAVAILABLE;
uint32_t to_read = 0;
uint32_t left = 0;
uint8_t is_string = 0;
r_buffer_size = 0;
rbr.read((uint8_t *)&to_read, 4);
in_count--;
left = rbr.data_left();
if (left < to_read + 1) {
rbr.advance_read(left);
return FAILED;
}
rbr.read(&is_string, 1);
rbr.read(packet_buffer, to_read);
*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;
return in_count;
};
bool LWSPeer::was_string_packet() const {
return _was_string;
};
bool LWSPeer::is_connected_to_host() const {
return wsi != NULL;
};
2018-09-23 19:14:20 +00:00
String LWSPeer::get_close_reason(void *in, size_t len, int &r_code) {
String s;
r_code = 0;
if (len < 2) // From docs this should not happen
return s;
const uint8_t *b = (const uint8_t *)in;
r_code = b[0] << 8 | b[1];
if (len > 2) {
const char *utf8 = (const char *)&b[2];
s.parse_utf8(utf8, len - 2);
}
return s;
}
void LWSPeer::send_close_status(struct lws *p_wsi) {
if (close_code == -1)
return;
int len = close_reason.size();
ERR_FAIL_COND(len > 123); // Maximum allowed reason size in bytes
lws_close_status code = (lws_close_status)close_code;
unsigned char *reason = len > 0 ? (unsigned char *)close_reason.utf8().ptrw() : NULL;
lws_close_reason(p_wsi, code, reason, len);
close_code = -1;
close_reason = "";
}
void LWSPeer::close(int p_code, String p_reason) {
if (wsi != NULL) {
2018-09-23 19:14:20 +00:00
close_code = p_code;
close_reason = p_reason;
PeerData *data = ((PeerData *)lws_wsi_user(wsi));
data->force_close = true;
data->clean_close = true;
2018-09-23 19:14:20 +00:00
lws_callback_on_writable(wsi); // Notify that we want to disconnect
} else {
close_code = -1;
close_reason = "";
}
wsi = NULL;
rbw.resize(0);
rbr.resize(0);
in_count = 0;
in_size = 0;
out_count = 0;
_was_string = false;
};
IP_Address LWSPeer::get_connected_host() const {
ERR_FAIL_COND_V(!is_connected_to_host(), IP_Address());
IP_Address ip;
uint16_t port = 0;
struct sockaddr_storage addr;
2018-06-06 16:34:08 +00:00
socklen_t len = sizeof(addr);
int fd = lws_get_socket_fd(wsi);
ERR_FAIL_COND_V(fd == -1, IP_Address());
int ret = getpeername(fd, (struct sockaddr *)&addr, &len);
ERR_FAIL_COND_V(ret != 0, IP_Address());
NetSocketPosix::_set_ip_port(&addr, ip, port);
return ip;
};
uint16_t LWSPeer::get_connected_port() const {
ERR_FAIL_COND_V(!is_connected_to_host(), 0);
IP_Address ip;
uint16_t port = 0;
struct sockaddr_storage addr;
2018-06-06 16:34:08 +00:00
socklen_t len = sizeof(addr);
int fd = lws_get_socket_fd(wsi);
ERR_FAIL_COND_V(fd == -1, 0);
int ret = getpeername(fd, (struct sockaddr *)&addr, &len);
ERR_FAIL_COND_V(ret != 0, 0);
NetSocketPosix::_set_ip_port(&addr, ip, port);
return port;
};
LWSPeer::LWSPeer() {
wsi = NULL;
write_mode = WRITE_MODE_BINARY;
close();
};
LWSPeer::~LWSPeer() {
close();
};
#endif // JAVASCRIPT_ENABLED