Remove libwebsocket. No longer used, yay!
This commit is contained in:
@@ -7,89 +7,7 @@ Import('env_modules')
|
||||
|
||||
env_ws = env_modules.Clone()
|
||||
|
||||
if env['builtin_libwebsockets'] and not env["platform"] == "javascript": # already builtin for javascript
|
||||
thirdparty_dir = "#thirdparty/libwebsockets/"
|
||||
helper_dir = "win32helpers/"
|
||||
thirdparty_sources = [
|
||||
|
||||
"core/alloc.c",
|
||||
"core/context.c",
|
||||
"core/libwebsockets.c",
|
||||
"core/output.c",
|
||||
"core/pollfd.c",
|
||||
"core/service.c",
|
||||
|
||||
"event-libs/poll/poll.c",
|
||||
|
||||
"misc/base64-decode.c",
|
||||
"misc/lejp.c",
|
||||
"misc/sha-1.c",
|
||||
|
||||
"roles/h1/ops-h1.c",
|
||||
"roles/http/header.c",
|
||||
"roles/http/client/client.c",
|
||||
"roles/http/client/client-handshake.c",
|
||||
"roles/http/server/fops-zip.c",
|
||||
"roles/http/server/lejp-conf.c",
|
||||
"roles/http/server/parsers.c",
|
||||
"roles/http/server/server.c",
|
||||
"roles/listen/ops-listen.c",
|
||||
"roles/pipe/ops-pipe.c",
|
||||
"roles/raw/ops-raw.c",
|
||||
|
||||
"roles/ws/client-ws.c",
|
||||
"roles/ws/client-parser-ws.c",
|
||||
"roles/ws/ops-ws.c",
|
||||
"roles/ws/server-ws.c",
|
||||
|
||||
"tls/tls.c",
|
||||
"tls/tls-client.c",
|
||||
"tls/tls-server.c",
|
||||
|
||||
"tls/mbedtls/wrapper/library/ssl_cert.c",
|
||||
"tls/mbedtls/wrapper/library/ssl_pkey.c",
|
||||
"tls/mbedtls/wrapper/library/ssl_stack.c",
|
||||
"tls/mbedtls/wrapper/library/ssl_methods.c",
|
||||
"tls/mbedtls/wrapper/library/ssl_lib.c",
|
||||
"tls/mbedtls/wrapper/library/ssl_x509.c",
|
||||
"tls/mbedtls/wrapper/platform/ssl_port.c",
|
||||
"tls/mbedtls/wrapper/platform/ssl_pm.c",
|
||||
"tls/mbedtls/lws-genhash.c",
|
||||
"tls/mbedtls/mbedtls-client.c",
|
||||
"tls/mbedtls/lws-genrsa.c",
|
||||
"tls/mbedtls/ssl.c",
|
||||
"tls/mbedtls/mbedtls-server.c"
|
||||
]
|
||||
|
||||
if env["platform"] == "android": # Builtin getifaddrs
|
||||
thirdparty_sources += ["misc/getifaddrs.c"]
|
||||
|
||||
if env["platform"] == "windows" or env["platform"] == "uwp": # Winsock
|
||||
thirdparty_sources += ["plat/lws-plat-win.c", helper_dir + "getopt.c", helper_dir + "getopt_long.c", helper_dir + "gettimeofday.c"]
|
||||
else: # Unix socket
|
||||
thirdparty_sources += ["plat/lws-plat-unix.c"]
|
||||
|
||||
thirdparty_sources = [thirdparty_dir + file for file in thirdparty_sources]
|
||||
|
||||
env_lws.Prepend(CPPPATH=[thirdparty_dir])
|
||||
|
||||
if env['builtin_mbedtls']:
|
||||
mbedtls_includes = "#thirdparty/mbedtls/include"
|
||||
env_lws.Prepend(CPPPATH=[mbedtls_includes])
|
||||
|
||||
wrapper_includes = ["#thirdparty/libwebsockets/tls/mbedtls/wrapper/include/" + inc for inc in ["internal", "openssl", "platform", ""]]
|
||||
env_lws.Prepend(CPPPATH=wrapper_includes)
|
||||
|
||||
if env["platform"] == "windows" or env["platform"] == "uwp":
|
||||
env_lws.Prepend(CPPPATH=[thirdparty_dir + helper_dir])
|
||||
|
||||
if env["platform"] == "uwp":
|
||||
env_lws.Append(CPPDEFINES=["LWS_MINGW_SUPPORT"])
|
||||
|
||||
env_thirdparty = env_lws.Clone()
|
||||
env_thirdparty.disable_warnings()
|
||||
env_thirdparty.add_source_files(env.modules_sources, thirdparty_sources)
|
||||
|
||||
if env['builtin_wslay'] and not env["platform"] == "javascript": # already builtin for javascript
|
||||
wslay_dir = "#thirdparty/wslay/"
|
||||
wslay_sources = [
|
||||
"wslay_net.c",
|
||||
@@ -100,11 +18,11 @@ if env['builtin_libwebsockets'] and not env["platform"] == "javascript": # alrea
|
||||
]
|
||||
wslay_sources = [wslay_dir + s for s in wslay_sources]
|
||||
env_ws.Prepend(CPPPATH=[wslay_dir + "includes/"])
|
||||
env_ws.Append(CPPFLAGS=["-DHAVE_CONFIG_H"])
|
||||
env_ws.Append(CPPDEFINES=["HAVE_CONFIG_H"])
|
||||
if env["platform"] == "windows" or env["platform"] == "uwp":
|
||||
env_ws.Append(CPPFLAGS=["-DHAVE_WINSOCK2_H"])
|
||||
env_ws.Append(CPPDEFINES=["HAVE_WINSOCK2_H"])
|
||||
else:
|
||||
env_ws.Append(CPPFLAGS=["-DHAVE_NETINET_IN_H"])
|
||||
env_ws.Append(CPPDEFINES=["HAVE_NETINET_IN_H"])
|
||||
env_wslay = env_ws.Clone()
|
||||
env_wslay.disable_warnings()
|
||||
env_wslay.add_source_files(env.modules_sources, wslay_sources)
|
||||
|
||||
@@ -1,248 +0,0 @@
|
||||
/*************************************************************************/
|
||||
/* lws_client.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 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_client.h"
|
||||
#include "core/io/ip.h"
|
||||
#include "core/project_settings.h"
|
||||
#if defined(LWS_OPENSSL_SUPPORT)
|
||||
#include "core/io/stream_peer_ssl.h"
|
||||
#include "tls/mbedtls/wrapper/include/openssl/ssl.h"
|
||||
#endif
|
||||
|
||||
Error LWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocols) {
|
||||
|
||||
ERR_FAIL_COND_V(context != NULL, FAILED);
|
||||
|
||||
IP_Address addr;
|
||||
|
||||
if (!p_host.is_valid_ip_address()) {
|
||||
addr = IP::get_singleton()->resolve_hostname(p_host);
|
||||
} else {
|
||||
addr = p_host;
|
||||
}
|
||||
|
||||
ERR_FAIL_COND_V(!addr.is_valid(), ERR_INVALID_PARAMETER);
|
||||
|
||||
// Prepare protocols
|
||||
_lws_make_protocols(this, &LWSClient::_lws_gd_callback, p_protocols, &_lws_ref);
|
||||
|
||||
// Init lws client
|
||||
struct lws_context_creation_info info;
|
||||
struct lws_client_connect_info i;
|
||||
|
||||
memset(&i, 0, sizeof i);
|
||||
memset(&info, 0, sizeof info);
|
||||
|
||||
info.port = CONTEXT_PORT_NO_LISTEN;
|
||||
info.protocols = _lws_ref->lws_structs;
|
||||
info.gid = -1;
|
||||
info.uid = -1;
|
||||
//info.ws_ping_pong_interval = 5;
|
||||
info.user = _lws_ref;
|
||||
#if defined(LWS_OPENSSL_SUPPORT)
|
||||
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
||||
#endif
|
||||
context = lws_create_context(&info);
|
||||
|
||||
if (context == NULL) {
|
||||
_lws_free_ref(_lws_ref);
|
||||
_lws_ref = NULL;
|
||||
ERR_EXPLAIN("Unable to create lws context");
|
||||
ERR_FAIL_V(FAILED);
|
||||
}
|
||||
|
||||
i.context = context;
|
||||
if (p_protocols.size() > 0)
|
||||
i.protocol = _lws_ref->lws_names;
|
||||
else
|
||||
i.protocol = NULL;
|
||||
|
||||
if (p_ssl) {
|
||||
i.ssl_connection = LCCSCF_USE_SSL;
|
||||
if (!verify_ssl)
|
||||
i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
|
||||
} else {
|
||||
i.ssl_connection = 0;
|
||||
}
|
||||
|
||||
// These CharStrings needs to survive till we call lws_client_connect_via_info
|
||||
CharString addr_ch = ((String)addr).ascii();
|
||||
CharString host_ch = p_host.utf8();
|
||||
CharString path_ch = p_path.utf8();
|
||||
i.address = addr_ch.get_data();
|
||||
i.host = host_ch.get_data();
|
||||
i.path = path_ch.get_data();
|
||||
i.port = p_port;
|
||||
|
||||
lws_client_connect_via_info(&i);
|
||||
|
||||
return OK;
|
||||
};
|
||||
|
||||
int LWSClient::get_max_packet_size() const {
|
||||
return (1 << _out_buf_size) - PROTO_SIZE;
|
||||
}
|
||||
|
||||
void LWSClient::poll() {
|
||||
|
||||
_lws_poll();
|
||||
}
|
||||
|
||||
int LWSClient::_handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
|
||||
|
||||
Ref<LWSPeer> peer = static_cast<Ref<LWSPeer> >(_peer);
|
||||
LWSPeer::PeerData *peer_data = (LWSPeer::PeerData *)user;
|
||||
|
||||
switch (reason) {
|
||||
#if defined(LWS_OPENSSL_SUPPORT)
|
||||
case LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: {
|
||||
PoolByteArray arr = StreamPeerSSL::get_project_cert_array();
|
||||
if (arr.size() > 0)
|
||||
SSL_CTX_add_client_CA((SSL_CTX *)user, d2i_X509(NULL, &arr.read()[0], arr.size()));
|
||||
else if (verify_ssl)
|
||||
WARN_PRINTS("No CA cert specified in project settings, SSL will not work");
|
||||
} break;
|
||||
#endif
|
||||
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
||||
peer->set_wsi(wsi, _in_buf_size, _in_pkt_size, _out_buf_size, _out_pkt_size);
|
||||
peer_data->peer_id = 0;
|
||||
peer_data->force_close = false;
|
||||
peer_data->clean_close = false;
|
||||
_on_connect(lws_get_protocol(wsi)->name);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
||||
_on_error();
|
||||
destroy_context();
|
||||
return -1; // We should close the connection (would probably happen anyway)
|
||||
|
||||
case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: {
|
||||
int code;
|
||||
String reason2 = peer->get_close_reason(in, len, code);
|
||||
peer_data->clean_close = true;
|
||||
_on_close_request(code, reason2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
case LWS_CALLBACK_CLIENT_CLOSED:
|
||||
peer->close();
|
||||
destroy_context();
|
||||
_on_disconnect(peer_data->clean_close);
|
||||
return 0; // We can end here
|
||||
|
||||
case LWS_CALLBACK_CLIENT_RECEIVE:
|
||||
peer->read_wsi(in, len);
|
||||
if (peer->get_available_packet_count() > 0)
|
||||
_on_peer_packet();
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_CLIENT_WRITEABLE:
|
||||
if (peer_data->force_close) {
|
||||
peer->send_close_status(wsi);
|
||||
return -1;
|
||||
}
|
||||
|
||||
peer->write_wsi();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Ref<WebSocketPeer> LWSClient::get_peer(int p_peer_id) const {
|
||||
|
||||
return _peer;
|
||||
}
|
||||
|
||||
NetworkedMultiplayerPeer::ConnectionStatus LWSClient::get_connection_status() const {
|
||||
|
||||
if (context == NULL)
|
||||
return CONNECTION_DISCONNECTED;
|
||||
|
||||
if (_peer->is_connected_to_host())
|
||||
return CONNECTION_CONNECTED;
|
||||
|
||||
return CONNECTION_CONNECTING;
|
||||
}
|
||||
|
||||
void LWSClient::disconnect_from_host(int p_code, String p_reason) {
|
||||
|
||||
if (context == NULL)
|
||||
return;
|
||||
|
||||
_peer->close(p_code, p_reason);
|
||||
};
|
||||
|
||||
IP_Address LWSClient::get_connected_host() const {
|
||||
|
||||
return IP_Address();
|
||||
};
|
||||
|
||||
uint16_t LWSClient::get_connected_port() const {
|
||||
|
||||
return 1025;
|
||||
};
|
||||
|
||||
Error LWSClient::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) {
|
||||
ERR_EXPLAIN("Buffers sizes can only be set before listening or connecting");
|
||||
ERR_FAIL_COND_V(context != NULL, FAILED);
|
||||
|
||||
_in_buf_size = nearest_shift(p_in_buffer - 1) + 10;
|
||||
_in_pkt_size = nearest_shift(p_in_packets - 1);
|
||||
_out_buf_size = nearest_shift(p_out_buffer - 1) + 10;
|
||||
_out_pkt_size = nearest_shift(p_out_packets - 1);
|
||||
return OK;
|
||||
}
|
||||
|
||||
LWSClient::LWSClient() {
|
||||
_in_buf_size = nearest_shift((int)GLOBAL_GET(WSC_IN_BUF) - 1) + 10;
|
||||
_in_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_IN_PKT) - 1);
|
||||
_out_buf_size = nearest_shift((int)GLOBAL_GET(WSC_OUT_BUF) - 1) + 10;
|
||||
_out_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_OUT_PKT) - 1);
|
||||
|
||||
context = NULL;
|
||||
_lws_ref = NULL;
|
||||
_peer = Ref<LWSPeer>(memnew(LWSPeer));
|
||||
};
|
||||
|
||||
LWSClient::~LWSClient() {
|
||||
|
||||
invalidate_lws_ref(); // We do not want any more callback
|
||||
disconnect_from_host();
|
||||
destroy_context();
|
||||
_peer = Ref<LWSPeer>();
|
||||
};
|
||||
|
||||
#endif // JAVASCRIPT_ENABLED
|
||||
@@ -1,70 +0,0 @@
|
||||
/*************************************************************************/
|
||||
/* lws_client.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 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 LWSCLIENT_H
|
||||
#define LWSCLIENT_H
|
||||
|
||||
#ifndef JAVASCRIPT_ENABLED
|
||||
|
||||
#include "core/error_list.h"
|
||||
#include "lws_helper.h"
|
||||
#include "lws_peer.h"
|
||||
#include "websocket_client.h"
|
||||
|
||||
class LWSClient : public WebSocketClient {
|
||||
|
||||
GDCIIMPL(LWSClient, WebSocketClient);
|
||||
|
||||
LWS_HELPER(LWSClient);
|
||||
|
||||
private:
|
||||
int _in_buf_size;
|
||||
int _in_pkt_size;
|
||||
int _out_buf_size;
|
||||
int _out_pkt_size;
|
||||
|
||||
public:
|
||||
Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets);
|
||||
Error connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, PoolVector<String> p_protocol = PoolVector<String>());
|
||||
int get_max_packet_size() const;
|
||||
Ref<WebSocketPeer> get_peer(int p_peer_id) const;
|
||||
void disconnect_from_host(int p_code = 1000, String p_reason = "");
|
||||
IP_Address get_connected_host() const;
|
||||
uint16_t get_connected_port() const;
|
||||
virtual ConnectionStatus get_connection_status() const;
|
||||
virtual void poll();
|
||||
|
||||
LWSClient();
|
||||
~LWSClient();
|
||||
};
|
||||
|
||||
#endif // JAVASCRIPT_ENABLED
|
||||
|
||||
#endif // LWSCLIENT_H
|
||||
@@ -1,157 +0,0 @@
|
||||
/*************************************************************************/
|
||||
/* lws_helper.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 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. */
|
||||
/*************************************************************************/
|
||||
|
||||
#if !defined(JAVASCRIPT_ENABLED)
|
||||
|
||||
#include "lws_helper.h"
|
||||
|
||||
_LWSRef *_lws_create_ref(void *obj) {
|
||||
|
||||
_LWSRef *out = (_LWSRef *)memalloc(sizeof(_LWSRef));
|
||||
out->is_destroying = false;
|
||||
out->free_context = false;
|
||||
out->is_polling = false;
|
||||
out->obj = obj;
|
||||
out->is_valid = true;
|
||||
out->lws_structs = NULL;
|
||||
out->lws_names = NULL;
|
||||
return out;
|
||||
}
|
||||
|
||||
void _lws_free_ref(_LWSRef *ref) {
|
||||
// Free strings and structs
|
||||
memfree(ref->lws_structs);
|
||||
memfree(ref->lws_names);
|
||||
// Free ref
|
||||
memfree(ref);
|
||||
}
|
||||
|
||||
bool _lws_destroy(struct lws_context *context, _LWSRef *ref) {
|
||||
if (context == NULL || ref->is_destroying)
|
||||
return false;
|
||||
|
||||
if (ref->is_polling) {
|
||||
ref->free_context = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
ref->is_destroying = true;
|
||||
lws_context_destroy(context);
|
||||
_lws_free_ref(ref);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _lws_poll(struct lws_context *context, _LWSRef *ref) {
|
||||
|
||||
ERR_FAIL_COND_V(context == NULL, false);
|
||||
ERR_FAIL_COND_V(ref == NULL, false);
|
||||
|
||||
ref->is_polling = true;
|
||||
lws_service(context, 0);
|
||||
ref->is_polling = false;
|
||||
|
||||
if (!ref->free_context)
|
||||
return false; // Nothing to do
|
||||
|
||||
bool is_valid = ref->is_valid; // Might have been destroyed by poll
|
||||
|
||||
_lws_destroy(context, ref); // Will destroy context and ref
|
||||
|
||||
return is_valid; // If the object should NULL its context and ref
|
||||
}
|
||||
|
||||
/*
|
||||
* Prepare the protocol_structs to be fed to context.
|
||||
* Also prepare the protocol string used by the client.
|
||||
*/
|
||||
void _lws_make_protocols(void *p_obj, lws_callback_function *p_callback, PoolVector<String> p_names, _LWSRef **r_lws_ref) {
|
||||
// The input strings might go away after this call, we need to copy them.
|
||||
// We will clear them when destroying the context.
|
||||
int i;
|
||||
int len = p_names.size();
|
||||
size_t data_size = sizeof(struct LWSPeer::PeerData);
|
||||
PoolVector<String>::Read pnr = p_names.read();
|
||||
|
||||
// This is a reference connecting the object with lws keep track of status, mallocs, etc.
|
||||
// Must survive as long the context.
|
||||
// Must be freed manually when context creation fails.
|
||||
_LWSRef *ref = _lws_create_ref(p_obj);
|
||||
|
||||
// LWS protocol structs.
|
||||
ref->lws_structs = (struct lws_protocols *)memalloc(sizeof(struct lws_protocols) * (len + 2));
|
||||
memset(ref->lws_structs, 0, sizeof(struct lws_protocols) * (len + 2));
|
||||
|
||||
CharString strings = p_names.join(",").ascii();
|
||||
int str_len = strings.length();
|
||||
|
||||
// Joined string of protocols, double the size: comma separated first, NULL separated last
|
||||
ref->lws_names = (char *)memalloc((str_len + 1) * 2); // Plus the terminator
|
||||
|
||||
char *names_ptr = ref->lws_names;
|
||||
struct lws_protocols *structs_ptr = ref->lws_structs;
|
||||
|
||||
// Comma separated protocols string to be used in client Sec-WebSocket-Protocol header
|
||||
if (str_len > 0)
|
||||
copymem(names_ptr, strings.get_data(), str_len);
|
||||
names_ptr[str_len] = '\0'; // NULL terminator
|
||||
|
||||
// NULL terminated protocol strings to be used in protocol structs
|
||||
if (str_len > 0)
|
||||
copymem(&names_ptr[str_len + 1], strings.get_data(), str_len);
|
||||
names_ptr[(str_len * 2) + 1] = '\0'; // NULL terminator
|
||||
int pos = str_len + 1;
|
||||
|
||||
// The first protocol is the default for any http request (before upgrade).
|
||||
// It is also used as the websocket protocol when no subprotocol is specified.
|
||||
structs_ptr[0].name = "default";
|
||||
structs_ptr[0].callback = p_callback;
|
||||
structs_ptr[0].per_session_data_size = data_size;
|
||||
structs_ptr[0].rx_buffer_size = LWS_BUF_SIZE;
|
||||
structs_ptr[0].tx_packet_size = LWS_PACKET_SIZE;
|
||||
// Add user defined protocols
|
||||
for (i = 0; i < len; i++) {
|
||||
structs_ptr[i + 1].name = (const char *)&names_ptr[pos];
|
||||
structs_ptr[i + 1].callback = p_callback;
|
||||
structs_ptr[i + 1].per_session_data_size = data_size;
|
||||
structs_ptr[i + 1].rx_buffer_size = LWS_BUF_SIZE;
|
||||
structs_ptr[i + 1].tx_packet_size = LWS_PACKET_SIZE;
|
||||
pos += pnr[i].ascii().length() + 1;
|
||||
names_ptr[pos - 1] = '\0';
|
||||
}
|
||||
// Add protocols terminator
|
||||
structs_ptr[len + 1].name = NULL;
|
||||
structs_ptr[len + 1].callback = NULL;
|
||||
structs_ptr[len + 1].per_session_data_size = 0;
|
||||
structs_ptr[len + 1].rx_buffer_size = 0;
|
||||
|
||||
*r_lws_ref = ref;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,111 +0,0 @@
|
||||
/*************************************************************************/
|
||||
/* lws_helper.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 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 LWS_HELPER_H
|
||||
#define LWS_HELPER_H
|
||||
|
||||
#define LWS_BUF_SIZE 65536
|
||||
#define LWS_PACKET_SIZE LWS_BUF_SIZE
|
||||
|
||||
#include "core/io/stream_peer.h"
|
||||
#include "core/os/os.h"
|
||||
#include "core/reference.h"
|
||||
#include "core/ring_buffer.h"
|
||||
#include "lws_peer.h"
|
||||
|
||||
struct _LWSRef {
|
||||
bool free_context;
|
||||
bool is_polling;
|
||||
bool is_valid;
|
||||
bool is_destroying;
|
||||
void *obj;
|
||||
struct lws_protocols *lws_structs;
|
||||
char *lws_names;
|
||||
};
|
||||
|
||||
_LWSRef *_lws_create_ref(void *obj);
|
||||
void _lws_free_ref(_LWSRef *ref);
|
||||
bool _lws_destroy(struct lws_context *context, _LWSRef *ref);
|
||||
bool _lws_poll(struct lws_context *context, _LWSRef *ref);
|
||||
void _lws_make_protocols(void *p_obj, lws_callback_function *p_callback, PoolVector<String> p_names, _LWSRef **r_lws_ref);
|
||||
|
||||
/* clang-format off */
|
||||
#define LWS_HELPER(CNAME) \
|
||||
protected: \
|
||||
struct _LWSRef *_lws_ref; \
|
||||
struct lws_context *context; \
|
||||
bool _keep_servicing; \
|
||||
\
|
||||
static int _lws_gd_callback(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) { \
|
||||
\
|
||||
if (wsi == NULL) { \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
struct _LWSRef *ref = (struct _LWSRef *)lws_context_user(lws_get_context(wsi)); \
|
||||
if (!ref->is_valid) \
|
||||
return 0; \
|
||||
CNAME *helper = (CNAME *)ref->obj; \
|
||||
helper->_keep_servicing = true; \
|
||||
return helper->_handle_cb(wsi, reason, user, in, len); \
|
||||
} \
|
||||
\
|
||||
void invalidate_lws_ref() { \
|
||||
if (_lws_ref != NULL) \
|
||||
_lws_ref->is_valid = false; \
|
||||
} \
|
||||
\
|
||||
void destroy_context() { \
|
||||
if (_lws_destroy(context, _lws_ref)) { \
|
||||
context = NULL; \
|
||||
_lws_ref = NULL; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
public: \
|
||||
virtual int _handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len); \
|
||||
\
|
||||
void _lws_poll() { \
|
||||
ERR_FAIL_COND(context == NULL); \
|
||||
do { \
|
||||
_keep_servicing = false; \
|
||||
if (::_lws_poll(context, _lws_ref)) { \
|
||||
context = NULL; \
|
||||
_lws_ref = NULL; \
|
||||
break; \
|
||||
} \
|
||||
} while (_keep_servicing); \
|
||||
} \
|
||||
\
|
||||
protected:
|
||||
|
||||
/* clang-format on */
|
||||
|
||||
#endif // LWS_HELPER_H
|
||||
@@ -1,270 +0,0 @@
|
||||
/*************************************************************************/
|
||||
/* lws_peer.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 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"
|
||||
|
||||
#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, unsigned int p_in_buf_size, unsigned int p_in_pkt_size, unsigned int p_out_buf_size, unsigned int p_out_pkt_size) {
|
||||
ERR_FAIL_COND(wsi != NULL);
|
||||
|
||||
_in_buffer.resize(p_in_pkt_size, p_in_buf_size);
|
||||
_out_buffer.resize(p_out_pkt_size, p_out_buf_size);
|
||||
_packet_buffer.resize((1 << MAX(p_in_buf_size, p_out_buf_size)) + LWS_PRE);
|
||||
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);
|
||||
|
||||
if (lws_is_first_fragment(wsi))
|
||||
_in_size = 0;
|
||||
else if (_in_size == -1) // Trash this frame
|
||||
return ERR_FILE_CORRUPT;
|
||||
|
||||
Error err = _in_buffer.write_packet((const uint8_t *)in, len, NULL);
|
||||
|
||||
if (err != OK) {
|
||||
_in_buffer.discard_payload(_in_size);
|
||||
_in_size = -1;
|
||||
ERR_FAIL_V(err);
|
||||
}
|
||||
|
||||
_in_size += len;
|
||||
|
||||
if (lws_is_final_fragment(wsi)) {
|
||||
uint8_t is_string = lws_frame_is_binary(wsi) ? 0 : 1;
|
||||
err = _in_buffer.write_packet(NULL, _in_size, &is_string);
|
||||
if (err != OK) {
|
||||
_in_buffer.discard_payload(_in_size);
|
||||
_in_size = -1;
|
||||
ERR_FAIL_V(err);
|
||||
}
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
Error LWSPeer::write_wsi() {
|
||||
|
||||
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
||||
|
||||
PoolVector<uint8_t> tmp;
|
||||
int count = _out_buffer.packets_left();
|
||||
|
||||
if (count == 0)
|
||||
return OK;
|
||||
|
||||
int read = 0;
|
||||
uint8_t is_string = 0;
|
||||
PoolVector<uint8_t>::Write rw = _packet_buffer.write();
|
||||
_out_buffer.read_packet(&(rw[LWS_PRE]), _packet_buffer.size() - LWS_PRE, &is_string, read);
|
||||
|
||||
enum lws_write_protocol mode = is_string ? LWS_WRITE_TEXT : LWS_WRITE_BINARY;
|
||||
lws_write(wsi, &(rw[LWS_PRE]), read, mode);
|
||||
|
||||
if (count > 1)
|
||||
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);
|
||||
|
||||
uint8_t is_string = write_mode == WRITE_MODE_TEXT;
|
||||
_out_buffer.write_packet(p_buffer, p_buffer_size, &is_string);
|
||||
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) {
|
||||
|
||||
r_buffer_size = 0;
|
||||
|
||||
ERR_FAIL_COND_V(!is_connected_to_host(), FAILED);
|
||||
|
||||
if (_in_buffer.packets_left() == 0)
|
||||
return ERR_UNAVAILABLE;
|
||||
|
||||
int read = 0;
|
||||
PoolVector<uint8_t>::Write rw = _packet_buffer.write();
|
||||
_in_buffer.read_packet(rw.ptr(), _packet_buffer.size(), &_is_string, read);
|
||||
|
||||
*r_buffer = rw.ptr();
|
||||
r_buffer_size = read;
|
||||
|
||||
return OK;
|
||||
};
|
||||
|
||||
int LWSPeer::get_available_packet_count() const {
|
||||
|
||||
if (!is_connected_to_host())
|
||||
return 0;
|
||||
|
||||
return _in_buffer.packets_left();
|
||||
};
|
||||
|
||||
bool LWSPeer::was_string_packet() const {
|
||||
|
||||
return _is_string;
|
||||
};
|
||||
|
||||
bool LWSPeer::is_connected_to_host() const {
|
||||
|
||||
return wsi != NULL;
|
||||
};
|
||||
|
||||
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) {
|
||||
close_code = p_code;
|
||||
close_reason = p_reason;
|
||||
PeerData *data = ((PeerData *)lws_wsi_user(wsi));
|
||||
data->force_close = true;
|
||||
data->clean_close = true;
|
||||
lws_callback_on_writable(wsi); // Notify that we want to disconnect
|
||||
} else {
|
||||
close_code = -1;
|
||||
close_reason = "";
|
||||
}
|
||||
wsi = NULL;
|
||||
_in_buffer.clear();
|
||||
_out_buffer.clear();
|
||||
_in_size = 0;
|
||||
_is_string = 0;
|
||||
_packet_buffer.resize(0);
|
||||
};
|
||||
|
||||
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;
|
||||
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;
|
||||
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
|
||||
@@ -1,96 +0,0 @@
|
||||
/*************************************************************************/
|
||||
/* lws_peer.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 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 LWSPEER_H
|
||||
#define LWSPEER_H
|
||||
|
||||
#ifndef JAVASCRIPT_ENABLED
|
||||
|
||||
#include "core/error_list.h"
|
||||
#include "core/io/packet_peer.h"
|
||||
#include "core/ring_buffer.h"
|
||||
#include "libwebsockets.h"
|
||||
#include "lws_config.h"
|
||||
#include "packet_buffer.h"
|
||||
#include "websocket_peer.h"
|
||||
|
||||
class LWSPeer : public WebSocketPeer {
|
||||
|
||||
GDCIIMPL(LWSPeer, WebSocketPeer);
|
||||
|
||||
private:
|
||||
int _in_size;
|
||||
uint8_t _is_string;
|
||||
// Our packet info is just a boolean (is_string), using uint8_t for it.
|
||||
PacketBuffer<uint8_t> _in_buffer;
|
||||
PacketBuffer<uint8_t> _out_buffer;
|
||||
|
||||
PoolVector<uint8_t> _packet_buffer;
|
||||
|
||||
struct lws *wsi;
|
||||
WriteMode write_mode;
|
||||
|
||||
int close_code;
|
||||
String close_reason;
|
||||
|
||||
public:
|
||||
struct PeerData {
|
||||
uint32_t peer_id;
|
||||
bool force_close;
|
||||
bool clean_close;
|
||||
};
|
||||
|
||||
virtual int get_available_packet_count() const;
|
||||
virtual Error get_packet(const uint8_t **r_buffer, int &r_buffer_size);
|
||||
virtual Error put_packet(const uint8_t *p_buffer, int p_buffer_size);
|
||||
virtual int get_max_packet_size() const { return _packet_buffer.size(); };
|
||||
|
||||
virtual void close(int p_code = 1000, String p_reason = "");
|
||||
virtual bool is_connected_to_host() const;
|
||||
virtual IP_Address get_connected_host() const;
|
||||
virtual uint16_t get_connected_port() const;
|
||||
|
||||
virtual WriteMode get_write_mode() const;
|
||||
virtual void set_write_mode(WriteMode p_mode);
|
||||
virtual bool was_string_packet() const;
|
||||
|
||||
void set_wsi(struct lws *wsi, unsigned int _in_buf_size, unsigned int _in_pkt_size, unsigned int _out_buf_size, unsigned int _out_pkt_size);
|
||||
Error read_wsi(void *in, size_t len);
|
||||
Error write_wsi();
|
||||
void send_close_status(struct lws *wsi);
|
||||
String get_close_reason(void *in, size_t len, int &r_code);
|
||||
|
||||
LWSPeer();
|
||||
~LWSPeer();
|
||||
};
|
||||
|
||||
#endif // JAVASCRIPT_ENABLED
|
||||
|
||||
#endif // LSWPEER_H
|
||||
@@ -1,225 +0,0 @@
|
||||
/*************************************************************************/
|
||||
/* lws_server.cpp */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 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_server.h"
|
||||
#include "core/os/os.h"
|
||||
#include "core/project_settings.h"
|
||||
|
||||
Error LWSServer::listen(int p_port, PoolVector<String> p_protocols, bool gd_mp_api) {
|
||||
|
||||
ERR_FAIL_COND_V(context != NULL, FAILED);
|
||||
|
||||
_is_multiplayer = gd_mp_api;
|
||||
|
||||
struct lws_context_creation_info info;
|
||||
memset(&info, 0, sizeof info);
|
||||
|
||||
// Prepare lws protocol structs
|
||||
_lws_make_protocols(this, &LWSServer::_lws_gd_callback, p_protocols, &_lws_ref);
|
||||
|
||||
info.port = p_port;
|
||||
info.user = _lws_ref;
|
||||
info.protocols = _lws_ref->lws_structs;
|
||||
info.gid = -1;
|
||||
info.uid = -1;
|
||||
//info.ws_ping_pong_interval = 5;
|
||||
|
||||
context = lws_create_context(&info);
|
||||
|
||||
if (context == NULL) {
|
||||
_lws_free_ref(_lws_ref);
|
||||
_lws_ref = NULL;
|
||||
ERR_EXPLAIN("Unable to create LWS context");
|
||||
ERR_FAIL_V(FAILED);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
bool LWSServer::is_listening() const {
|
||||
return context != NULL;
|
||||
}
|
||||
|
||||
int LWSServer::get_max_packet_size() const {
|
||||
return (1 << _out_buf_size) - PROTO_SIZE;
|
||||
}
|
||||
|
||||
int LWSServer::_handle_cb(struct lws *wsi, enum lws_callback_reasons reason, void *user, void *in, size_t len) {
|
||||
|
||||
LWSPeer::PeerData *peer_data = (LWSPeer::PeerData *)user;
|
||||
|
||||
switch (reason) {
|
||||
case LWS_CALLBACK_HTTP:
|
||||
// no http for now
|
||||
// closing immediately returning -1;
|
||||
return -1;
|
||||
|
||||
case LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
|
||||
// check header here?
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_ESTABLISHED: {
|
||||
int32_t id = _gen_unique_id();
|
||||
|
||||
Ref<LWSPeer> peer = Ref<LWSPeer>(memnew(LWSPeer));
|
||||
peer->set_wsi(wsi, _in_buf_size, _in_pkt_size, _out_buf_size, _out_pkt_size);
|
||||
_peer_map[id] = peer;
|
||||
|
||||
peer_data->peer_id = id;
|
||||
peer_data->force_close = false;
|
||||
peer_data->clean_close = false;
|
||||
_on_connect(id, lws_get_protocol(wsi)->name);
|
||||
break;
|
||||
}
|
||||
|
||||
case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: {
|
||||
if (peer_data == NULL)
|
||||
return 0;
|
||||
|
||||
int32_t id = peer_data->peer_id;
|
||||
if (_peer_map.has(id)) {
|
||||
int code;
|
||||
Ref<LWSPeer> peer = _peer_map[id];
|
||||
String reason2 = peer->get_close_reason(in, len, code);
|
||||
peer_data->clean_close = true;
|
||||
_on_close_request(id, code, reason2);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
case LWS_CALLBACK_CLOSED: {
|
||||
if (peer_data == NULL)
|
||||
return 0;
|
||||
int32_t id = peer_data->peer_id;
|
||||
bool clean = peer_data->clean_close;
|
||||
if (_peer_map.has(id)) {
|
||||
_peer_map[id]->close();
|
||||
_peer_map.erase(id);
|
||||
}
|
||||
_on_disconnect(id, clean);
|
||||
return 0; // we can end here
|
||||
}
|
||||
|
||||
case LWS_CALLBACK_RECEIVE: {
|
||||
int32_t id = peer_data->peer_id;
|
||||
if (_peer_map.has(id)) {
|
||||
static_cast<Ref<LWSPeer> >(_peer_map[id])->read_wsi(in, len);
|
||||
if (_peer_map[id]->get_available_packet_count() > 0)
|
||||
_on_peer_packet(id);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case LWS_CALLBACK_SERVER_WRITEABLE: {
|
||||
int id = peer_data->peer_id;
|
||||
if (peer_data->force_close) {
|
||||
if (_peer_map.has(id)) {
|
||||
Ref<LWSPeer> peer = _peer_map[id];
|
||||
peer->send_close_status(wsi);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (_peer_map.has(id))
|
||||
static_cast<Ref<LWSPeer> >(_peer_map[id])->write_wsi();
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LWSServer::stop() {
|
||||
if (context == NULL)
|
||||
return;
|
||||
|
||||
_peer_map.clear();
|
||||
destroy_context();
|
||||
context = NULL;
|
||||
}
|
||||
|
||||
bool LWSServer::has_peer(int p_id) const {
|
||||
return _peer_map.has(p_id);
|
||||
}
|
||||
|
||||
Ref<WebSocketPeer> LWSServer::get_peer(int p_id) const {
|
||||
ERR_FAIL_COND_V(!has_peer(p_id), NULL);
|
||||
return _peer_map[p_id];
|
||||
}
|
||||
|
||||
IP_Address LWSServer::get_peer_address(int p_peer_id) const {
|
||||
ERR_FAIL_COND_V(!has_peer(p_peer_id), IP_Address());
|
||||
|
||||
return _peer_map[p_peer_id]->get_connected_host();
|
||||
}
|
||||
|
||||
int LWSServer::get_peer_port(int p_peer_id) const {
|
||||
ERR_FAIL_COND_V(!has_peer(p_peer_id), 0);
|
||||
|
||||
return _peer_map[p_peer_id]->get_connected_port();
|
||||
}
|
||||
|
||||
void LWSServer::disconnect_peer(int p_peer_id, int p_code, String p_reason) {
|
||||
ERR_FAIL_COND(!has_peer(p_peer_id));
|
||||
|
||||
get_peer(p_peer_id)->close(p_code, p_reason);
|
||||
}
|
||||
|
||||
Error LWSServer::set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets) {
|
||||
ERR_EXPLAIN("Buffers sizes can only be set before listening or connecting");
|
||||
ERR_FAIL_COND_V(context != NULL, FAILED);
|
||||
|
||||
_in_buf_size = nearest_shift(p_in_buffer - 1) + 10;
|
||||
_in_pkt_size = nearest_shift(p_in_packets - 1);
|
||||
_out_buf_size = nearest_shift(p_out_buffer - 1) + 10;
|
||||
_out_pkt_size = nearest_shift(p_out_packets - 1);
|
||||
return OK;
|
||||
}
|
||||
|
||||
LWSServer::LWSServer() {
|
||||
_in_buf_size = nearest_shift((int)GLOBAL_GET(WSS_IN_BUF) - 1) + 10;
|
||||
_in_pkt_size = nearest_shift((int)GLOBAL_GET(WSS_IN_PKT) - 1);
|
||||
_out_buf_size = nearest_shift((int)GLOBAL_GET(WSS_OUT_BUF) - 1) + 10;
|
||||
_out_pkt_size = nearest_shift((int)GLOBAL_GET(WSS_OUT_PKT) - 1);
|
||||
context = NULL;
|
||||
_lws_ref = NULL;
|
||||
}
|
||||
|
||||
LWSServer::~LWSServer() {
|
||||
invalidate_lws_ref(); // we do not want any more callbacks
|
||||
stop();
|
||||
}
|
||||
|
||||
#endif // JAVASCRIPT_ENABLED
|
||||
@@ -1,73 +0,0 @@
|
||||
/*************************************************************************/
|
||||
/* lws_server.h */
|
||||
/*************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/*************************************************************************/
|
||||
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
||||
/* Copyright (c) 2014-2019 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 LWSSERVER_H
|
||||
#define LWSSERVER_H
|
||||
|
||||
#ifndef JAVASCRIPT_ENABLED
|
||||
|
||||
#include "core/reference.h"
|
||||
#include "lws_helper.h"
|
||||
#include "lws_peer.h"
|
||||
#include "websocket_server.h"
|
||||
|
||||
class LWSServer : public WebSocketServer {
|
||||
|
||||
GDCIIMPL(LWSServer, WebSocketServer);
|
||||
|
||||
LWS_HELPER(LWSServer);
|
||||
|
||||
private:
|
||||
Map<int, Ref<LWSPeer> > peer_map;
|
||||
int _in_buf_size;
|
||||
int _in_pkt_size;
|
||||
int _out_buf_size;
|
||||
int _out_pkt_size;
|
||||
|
||||
public:
|
||||
Error set_buffers(int p_in_buffer, int p_in_packets, int p_out_buffer, int p_out_packets);
|
||||
Error listen(int p_port, PoolVector<String> p_protocols = PoolVector<String>(), bool gd_mp_api = false);
|
||||
void stop();
|
||||
bool is_listening() const;
|
||||
int get_max_packet_size() const;
|
||||
bool has_peer(int p_id) const;
|
||||
Ref<WebSocketPeer> get_peer(int p_id) const;
|
||||
IP_Address get_peer_address(int p_peer_id) const;
|
||||
int get_peer_port(int p_peer_id) const;
|
||||
void disconnect_peer(int p_peer_id, int p_code = 1000, String p_reason = "");
|
||||
virtual void poll() { _lws_poll(); }
|
||||
|
||||
LWSServer();
|
||||
~LWSServer();
|
||||
};
|
||||
|
||||
#endif // JAVASCRIPT_ENABLED
|
||||
|
||||
#endif // LWSSERVER_H
|
||||
@@ -37,9 +37,6 @@
|
||||
#include "emws_peer.h"
|
||||
#include "emws_server.h"
|
||||
#else
|
||||
#include "lws_client.h"
|
||||
#include "lws_peer.h"
|
||||
#include "lws_server.h"
|
||||
#include "wsl_client.h"
|
||||
#include "wsl_server.h"
|
||||
#endif
|
||||
@@ -66,9 +63,7 @@ void register_websocket_types() {
|
||||
EMWSClient::make_default();
|
||||
EMWSServer::make_default();
|
||||
#else
|
||||
LWSPeer::make_default();
|
||||
LWSClient::make_default();
|
||||
LWSServer::make_default();
|
||||
WSLPeer::make_default();
|
||||
WSLClient::make_default();
|
||||
WSLServer::make_default();
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user