You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-05 12:10:55 +00:00
[HTML5] Port JavaScript inline code to libraries.
The API is implemented in javascript, and generates C functions that can be called from godot. This allows much cleaner code replacing all `EM_ASM` calls in our C++ code with plain C function calls. This also gets rid of few hacks and comes with few optimizations (e.g. custom cursor shapes should be much faster now).
This commit is contained in:
@@ -35,14 +35,13 @@
|
||||
#include "core/project_settings.h"
|
||||
#include "emscripten.h"
|
||||
|
||||
extern "C" {
|
||||
EMSCRIPTEN_KEEPALIVE void _esws_on_connect(void *obj, char *proto) {
|
||||
void EMWSClient::_esws_on_connect(void *obj, char *proto) {
|
||||
EMWSClient *client = static_cast<EMWSClient *>(obj);
|
||||
client->_is_connecting = false;
|
||||
client->_on_connect(String(proto));
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void _esws_on_message(void *obj, uint8_t *p_data, int p_data_size, int p_is_string) {
|
||||
void EMWSClient::_esws_on_message(void *obj, const uint8_t *p_data, int p_data_size, int p_is_string) {
|
||||
EMWSClient *client = static_cast<EMWSClient *>(obj);
|
||||
|
||||
Error err = static_cast<EMWSPeer *>(*client->get_peer(1))->read_msg(p_data, p_data_size, p_is_string == 1);
|
||||
@@ -50,22 +49,27 @@ EMSCRIPTEN_KEEPALIVE void _esws_on_message(void *obj, uint8_t *p_data, int p_dat
|
||||
client->_on_peer_packet();
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void _esws_on_error(void *obj) {
|
||||
void EMWSClient::_esws_on_error(void *obj) {
|
||||
EMWSClient *client = static_cast<EMWSClient *>(obj);
|
||||
client->_is_connecting = false;
|
||||
client->_on_error();
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void _esws_on_close(void *obj, int code, char *reason, int was_clean) {
|
||||
void EMWSClient::_esws_on_close(void *obj, int code, const char *reason, int was_clean) {
|
||||
EMWSClient *client = static_cast<EMWSClient *>(obj);
|
||||
client->_on_close_request(code, String(reason));
|
||||
client->_is_connecting = false;
|
||||
client->disconnect_from_host();
|
||||
client->_on_disconnect(was_clean != 0);
|
||||
}
|
||||
}
|
||||
|
||||
Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port, bool p_ssl, const Vector<String> p_protocols, const Vector<String> p_custom_headers) {
|
||||
|
||||
if (_js_id) {
|
||||
godot_js_websocket_destroy(_js_id);
|
||||
_js_id = 0;
|
||||
}
|
||||
|
||||
String proto_string;
|
||||
for (int i = 0; i < p_protocols.size(); i++) {
|
||||
if (i != 0)
|
||||
@@ -85,106 +89,17 @@ Error EMWSClient::connect_to_host(String p_host, String p_path, uint16_t p_port,
|
||||
}
|
||||
}
|
||||
str += p_host + ":" + itos(p_port) + p_path;
|
||||
|
||||
_is_connecting = true;
|
||||
/* clang-format off */
|
||||
int peer_sock = EM_ASM_INT({
|
||||
var proto_str = UTF8ToString($2);
|
||||
var socket = null;
|
||||
try {
|
||||
if (proto_str) {
|
||||
socket = new WebSocket(UTF8ToString($1), proto_str.split(","));
|
||||
} else {
|
||||
socket = new WebSocket(UTF8ToString($1));
|
||||
}
|
||||
} catch (e) {
|
||||
return -1;
|
||||
}
|
||||
var c_ptr = Module.IDHandler.get($0);
|
||||
socket.binaryType = "arraybuffer";
|
||||
|
||||
// Connection opened
|
||||
socket.addEventListener("open", function (event) {
|
||||
if (!Module.IDHandler.has($0))
|
||||
return; // Godot Object is gone!
|
||||
ccall("_esws_on_connect",
|
||||
"void",
|
||||
["number", "string"],
|
||||
[c_ptr, socket.protocol]
|
||||
);
|
||||
});
|
||||
|
||||
// Listen for messages
|
||||
socket.addEventListener("message", function (event) {
|
||||
if (!Module.IDHandler.has($0))
|
||||
return; // Godot Object is gone!
|
||||
var buffer;
|
||||
var is_string = 0;
|
||||
if (event.data instanceof ArrayBuffer) {
|
||||
|
||||
buffer = new Uint8Array(event.data);
|
||||
|
||||
} else if (event.data instanceof Blob) {
|
||||
|
||||
alert("Blob type not supported");
|
||||
return;
|
||||
|
||||
} else if (typeof event.data === "string") {
|
||||
|
||||
is_string = 1;
|
||||
var enc = new TextEncoder("utf-8");
|
||||
buffer = new Uint8Array(enc.encode(event.data));
|
||||
|
||||
} else {
|
||||
|
||||
alert("Unknown message type");
|
||||
return;
|
||||
|
||||
}
|
||||
var len = buffer.length*buffer.BYTES_PER_ELEMENT;
|
||||
var out = _malloc(len);
|
||||
HEAPU8.set(buffer, out);
|
||||
ccall("_esws_on_message",
|
||||
"void",
|
||||
["number", "number", "number", "number"],
|
||||
[c_ptr, out, len, is_string]
|
||||
);
|
||||
_free(out);
|
||||
});
|
||||
|
||||
socket.addEventListener("error", function (event) {
|
||||
if (!Module.IDHandler.has($0))
|
||||
return; // Godot Object is gone!
|
||||
ccall("_esws_on_error",
|
||||
"void",
|
||||
["number"],
|
||||
[c_ptr]
|
||||
);
|
||||
});
|
||||
|
||||
socket.addEventListener("close", function (event) {
|
||||
if (!Module.IDHandler.has($0))
|
||||
return; // Godot Object is gone!
|
||||
var was_clean = 0;
|
||||
if (event.wasClean)
|
||||
was_clean = 1;
|
||||
ccall("_esws_on_close",
|
||||
"void",
|
||||
["number", "number", "string", "number"],
|
||||
[c_ptr, event.code, event.reason, was_clean]
|
||||
);
|
||||
});
|
||||
|
||||
return Module.IDHandler.add(socket);
|
||||
}, _js_id, str.utf8().get_data(), proto_string.utf8().get_data());
|
||||
/* clang-format on */
|
||||
if (peer_sock == -1)
|
||||
_js_id = godot_js_websocket_create(this, str.utf8().get_data(), proto_string.utf8().get_data(), &_esws_on_connect, &_esws_on_message, &_esws_on_error, &_esws_on_close);
|
||||
if (!_js_id) {
|
||||
return FAILED;
|
||||
}
|
||||
|
||||
static_cast<Ref<EMWSPeer> >(_peer)->set_sock(peer_sock, _in_buf_size, _in_pkt_size);
|
||||
static_cast<Ref<EMWSPeer> >(_peer)->set_sock(_js_id, _in_buf_size, _in_pkt_size);
|
||||
|
||||
return OK;
|
||||
};
|
||||
}
|
||||
|
||||
void EMWSClient::poll() {
|
||||
}
|
||||
@@ -203,22 +118,22 @@ NetworkedMultiplayerPeer::ConnectionStatus EMWSClient::get_connection_status() c
|
||||
}
|
||||
|
||||
return CONNECTION_DISCONNECTED;
|
||||
};
|
||||
}
|
||||
|
||||
void EMWSClient::disconnect_from_host(int p_code, String p_reason) {
|
||||
|
||||
_peer->close(p_code, p_reason);
|
||||
};
|
||||
}
|
||||
|
||||
IP_Address EMWSClient::get_connected_host() const {
|
||||
|
||||
ERR_FAIL_V_MSG(IP_Address(), "Not supported in HTML5 export.");
|
||||
};
|
||||
}
|
||||
|
||||
uint16_t EMWSClient::get_connected_port() const {
|
||||
|
||||
ERR_FAIL_V_MSG(0, "Not supported in HTML5 export.");
|
||||
};
|
||||
}
|
||||
|
||||
int EMWSClient::get_max_packet_size() const {
|
||||
return (1 << _in_buf_size) - PROTO_SIZE;
|
||||
@@ -235,22 +150,16 @@ EMWSClient::EMWSClient() {
|
||||
_in_pkt_size = nearest_shift((int)GLOBAL_GET(WSC_IN_PKT) - 1);
|
||||
_is_connecting = false;
|
||||
_peer = Ref<EMWSPeer>(memnew(EMWSPeer));
|
||||
/* clang-format off */
|
||||
_js_id = EM_ASM_INT({
|
||||
return Module.IDHandler.add($0);
|
||||
}, this);
|
||||
/* clang-format on */
|
||||
};
|
||||
_js_id = 0;
|
||||
}
|
||||
|
||||
EMWSClient::~EMWSClient() {
|
||||
|
||||
disconnect_from_host();
|
||||
_peer = Ref<EMWSPeer>();
|
||||
/* clang-format off */
|
||||
EM_ASM({
|
||||
Module.IDHandler.remove($0);
|
||||
}, _js_id);
|
||||
/* clang-format on */
|
||||
};
|
||||
if (_js_id) {
|
||||
godot_js_websocket_destroy(_js_id);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // JAVASCRIPT_ENABLED
|
||||
|
||||
Reference in New Issue
Block a user