2017-12-21 03:13:23 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* lws_client.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
|
|
|
/*************************************************************************/
|
2018-03-21 10:07:51 +01:00
|
|
|
/* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2018 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. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#ifndef JAVASCRIPT_ENABLED
|
|
|
|
|
|
|
|
#include "lws_client.h"
|
|
|
|
#include "core/io/ip.h"
|
2018-03-18 20:05:42 +01:00
|
|
|
#include "core/io/stream_peer_ssl.h"
|
2018-06-07 13:33:24 +02:00
|
|
|
#include "tls/mbedtls/wrapper/include/openssl/ssl.h"
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-08-30 20:23:16 +02:00
|
|
|
// Prepare protocols
|
2017-12-21 03:13:23 +01:00
|
|
|
_lws_make_protocols(this, &LWSClient::_lws_gd_callback, p_protocols, &_lws_ref);
|
|
|
|
|
2018-08-30 20:23:16 +02:00
|
|
|
// Init lws client
|
2017-12-21 03:13:23 +01:00
|
|
|
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;
|
2018-03-18 20:05:42 +01:00
|
|
|
#if defined(LWS_OPENSSL_SUPPORT)
|
|
|
|
info.options |= LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
|
|
|
|
#endif
|
2017-12-21 03:13:23 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
char abuf[1024];
|
|
|
|
char hbuf[1024];
|
|
|
|
char pbuf[2048];
|
|
|
|
String addr_str = (String)addr;
|
|
|
|
strncpy(abuf, addr_str.ascii().get_data(), 1024);
|
|
|
|
strncpy(hbuf, p_host.utf8().get_data(), 1024);
|
|
|
|
strncpy(pbuf, p_path.utf8().get_data(), 2048);
|
|
|
|
|
|
|
|
i.context = context;
|
2018-08-30 20:23:16 +02:00
|
|
|
if (p_protocols.size() > 0)
|
|
|
|
i.protocol = _lws_ref->lws_names;
|
|
|
|
else
|
|
|
|
i.protocol = NULL;
|
2017-12-21 03:13:23 +01:00
|
|
|
i.address = abuf;
|
|
|
|
i.host = hbuf;
|
|
|
|
i.path = pbuf;
|
|
|
|
i.port = p_port;
|
2018-03-18 20:05:42 +01:00
|
|
|
|
|
|
|
if (p_ssl) {
|
|
|
|
i.ssl_connection = LCCSCF_USE_SSL;
|
|
|
|
if (!verify_ssl)
|
|
|
|
i.ssl_connection |= LCCSCF_ALLOW_SELFSIGNED;
|
|
|
|
} else {
|
|
|
|
i.ssl_connection = 0;
|
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
lws_client_connect_via_info(&i);
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2018-03-18 20:05:42 +01:00
|
|
|
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;
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_ESTABLISHED:
|
|
|
|
peer->set_wsi(wsi);
|
|
|
|
peer_data->peer_id = 0;
|
|
|
|
peer_data->force_close = false;
|
2018-09-24 00:58:28 +02:00
|
|
|
peer_data->clean_close = false;
|
2017-12-21 03:13:23 +01:00
|
|
|
_on_connect(lws_get_protocol(wsi)->name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
|
|
|
_on_error();
|
|
|
|
destroy_context();
|
2018-08-30 20:23:16 +02:00
|
|
|
return -1; // We should close the connection (would probably happen anyway)
|
2017-12-21 03:13:23 +01:00
|
|
|
|
2018-09-23 21:14:20 +02:00
|
|
|
case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: {
|
|
|
|
int code;
|
|
|
|
String reason = peer->get_close_reason(in, len, code);
|
2018-09-24 00:58:28 +02:00
|
|
|
peer_data->clean_close = true;
|
2018-09-23 21:14:20 +02:00
|
|
|
_on_close_request(code, reason);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-06-07 13:33:24 +02:00
|
|
|
case LWS_CALLBACK_CLIENT_CLOSED:
|
2017-12-21 03:13:23 +01:00
|
|
|
peer->close();
|
|
|
|
destroy_context();
|
2018-09-24 00:58:28 +02:00
|
|
|
_on_disconnect(peer_data->clean_close);
|
2018-08-30 20:23:16 +02:00
|
|
|
return 0; // We can end here
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
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:
|
2018-09-23 21:14:20 +02:00
|
|
|
if (peer_data->force_close) {
|
|
|
|
peer->send_close_status(wsi);
|
2017-12-21 03:13:23 +01:00
|
|
|
return -1;
|
2018-09-23 21:14:20 +02:00
|
|
|
}
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-23 21:14:20 +02:00
|
|
|
void LWSClient::disconnect_from_host(int p_code, String p_reason) {
|
2017-12-21 03:13:23 +01:00
|
|
|
|
|
|
|
if (context == NULL)
|
|
|
|
return;
|
|
|
|
|
2018-09-23 21:14:20 +02:00
|
|
|
_peer->close(p_code, p_reason);
|
2017-12-21 03:13:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
IP_Address LWSClient::get_connected_host() const {
|
|
|
|
|
|
|
|
return IP_Address();
|
|
|
|
};
|
|
|
|
|
|
|
|
uint16_t LWSClient::get_connected_port() const {
|
|
|
|
|
|
|
|
return 1025;
|
|
|
|
};
|
|
|
|
|
|
|
|
LWSClient::LWSClient() {
|
|
|
|
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();
|
2018-09-23 21:14:20 +02:00
|
|
|
destroy_context();
|
2017-12-21 03:13:23 +01:00
|
|
|
_peer = Ref<LWSPeer>();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // JAVASCRIPT_ENABLED
|