2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* editor_file_server.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 12:46:12 +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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "editor_file_server.h"
|
2017-01-16 07:04:19 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#include "../editor_settings.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/io/marshalls.h"
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//#define DEBUG_PRINT(m_p) print_line(m_p)
|
2018-08-24 07:35:07 +00:00
|
|
|
//#define DEBUG_TIME(m_what) printf("MS: %s - %lu\n", m_what, OS::get_singleton()->get_ticks_usec());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-08-24 07:35:07 +00:00
|
|
|
#define DEBUG_PRINT(m_what)
|
|
|
|
#define DEBUG_TIME(m_what)
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void EditorFileServer::_close_client(ClientData *cd) {
|
2017-01-14 14:07:57 +00:00
|
|
|
cd->connection->disconnect_from_host();
|
2020-02-26 10:28:13 +00:00
|
|
|
{
|
|
|
|
MutexLock lock(cd->efs->wait_mutex);
|
2021-03-06 12:26:16 +00:00
|
|
|
cd->efs->to_wait.insert(cd->thread);
|
2020-02-26 10:28:13 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
while (cd->files.size()) {
|
2022-05-13 13:04:37 +00:00
|
|
|
cd->files.remove(cd->files.begin());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
memdelete(cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFileServer::_subthread_start(void *s) {
|
2022-04-05 10:40:26 +00:00
|
|
|
ClientData *cd = static_cast<ClientData *>(s);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-01-30 15:22:15 +00:00
|
|
|
cd->connection->set_no_delay(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
uint8_t buf4[8];
|
|
|
|
Error err = cd->connection->get_data(buf4, 4);
|
|
|
|
if (err != OK) {
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int passlen = decode_uint32(buf4);
|
|
|
|
|
|
|
|
if (passlen > 512) {
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_FAIL_COND(passlen > 512);
|
|
|
|
} else if (passlen > 0) {
|
|
|
|
Vector<char> passutf8;
|
|
|
|
passutf8.resize(passlen + 1);
|
|
|
|
err = cd->connection->get_data((uint8_t *)passutf8.ptr(), passlen);
|
|
|
|
if (err != OK) {
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
2018-07-25 01:11:03 +00:00
|
|
|
passutf8.write[passlen] = 0;
|
2019-02-12 20:10:08 +00:00
|
|
|
String s2;
|
|
|
|
s2.parse_utf8(passutf8.ptr());
|
|
|
|
if (s2 != cd->efs->password) {
|
2014-02-10 01:10:30 +00:00
|
|
|
encode_uint32(ERR_INVALID_DATA, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
OS::get_singleton()->delay_usec(1000000);
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_PRINT("CLIENT PASSWORD MISMATCH");
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-09 09:42:46 +00:00
|
|
|
if (!cd->efs->password.is_empty()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
encode_uint32(ERR_INVALID_DATA, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
OS::get_singleton()->delay_usec(1000000);
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_PRINT("CLIENT PASSWORD MISMATCH (should be empty!)");
|
|
|
|
ERR_FAIL();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
encode_uint32(OK, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
|
|
|
|
while (!cd->quit) {
|
|
|
|
//wait for ID
|
|
|
|
err = cd->connection->get_data(buf4, 4);
|
|
|
|
DEBUG_TIME("get_data")
|
|
|
|
|
|
|
|
if (err != OK) {
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
|
|
|
int id = decode_uint32(buf4);
|
|
|
|
|
|
|
|
//wait for command
|
|
|
|
err = cd->connection->get_data(buf4, 4);
|
|
|
|
if (err != OK) {
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
|
|
|
int cmd = decode_uint32(buf4);
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FileAccessNetwork::COMMAND_FILE_EXISTS:
|
|
|
|
case FileAccessNetwork::COMMAND_GET_MODTIME:
|
|
|
|
case FileAccessNetwork::COMMAND_OPEN_FILE: {
|
|
|
|
DEBUG_TIME("open_file")
|
|
|
|
err = cd->connection->get_data(buf4, 4);
|
|
|
|
if (err != OK) {
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int namelen = decode_uint32(buf4);
|
|
|
|
Vector<char> fileutf8;
|
|
|
|
fileutf8.resize(namelen + 1);
|
|
|
|
err = cd->connection->get_data((uint8_t *)fileutf8.ptr(), namelen);
|
|
|
|
if (err != OK) {
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
2018-07-25 01:11:03 +00:00
|
|
|
fileutf8.write[namelen] = 0;
|
2019-02-12 20:10:08 +00:00
|
|
|
String s2;
|
|
|
|
s2.parse_utf8(fileutf8.ptr());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (cmd == FileAccessNetwork::COMMAND_FILE_EXISTS) {
|
2019-02-12 20:10:08 +00:00
|
|
|
print_verbose("FILE EXISTS: " + s2);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
if (cmd == FileAccessNetwork::COMMAND_GET_MODTIME) {
|
2019-02-12 20:10:08 +00:00
|
|
|
print_verbose("MOD TIME: " + s2);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
if (cmd == FileAccessNetwork::COMMAND_OPEN_FILE) {
|
2019-02-12 20:10:08 +00:00
|
|
|
print_verbose("OPEN: " + s2);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-12 20:10:08 +00:00
|
|
|
if (!s2.begins_with("res://")) {
|
2014-02-10 01:10:30 +00:00
|
|
|
_close_client(cd);
|
2019-02-12 20:10:08 +00:00
|
|
|
ERR_FAIL_COND(!s2.begins_with("res://"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
ERR_CONTINUE(cd->files.has(id));
|
|
|
|
|
|
|
|
if (cmd == FileAccessNetwork::COMMAND_FILE_EXISTS) {
|
|
|
|
encode_uint32(id, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
encode_uint32(FileAccessNetwork::RESPONSE_FILE_EXISTS, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
2019-02-12 20:10:08 +00:00
|
|
|
encode_uint32(FileAccess::exists(s2), buf4);
|
2014-02-10 01:10:30 +00:00
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
DEBUG_TIME("open_file_end")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd == FileAccessNetwork::COMMAND_GET_MODTIME) {
|
|
|
|
encode_uint32(id, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
encode_uint32(FileAccessNetwork::RESPONSE_GET_MODTIME, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
2019-02-12 20:10:08 +00:00
|
|
|
encode_uint64(FileAccess::get_modified_time(s2), buf4);
|
2014-02-10 01:10:30 +00:00
|
|
|
cd->connection->put_data(buf4, 8);
|
|
|
|
DEBUG_TIME("open_file_end")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-03-23 09:08:58 +00:00
|
|
|
Ref<FileAccess> fa = FileAccess::open(s2, FileAccess::READ);
|
|
|
|
if (fa.is_null()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
//not found, continue
|
|
|
|
encode_uint32(id, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
encode_uint32(FileAccessNetwork::RESPONSE_OPEN, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
encode_uint32(ERR_FILE_NOT_FOUND, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
DEBUG_TIME("open_file_end")
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
encode_uint32(id, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
encode_uint32(FileAccessNetwork::RESPONSE_OPEN, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
encode_uint32(OK, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
2021-05-25 06:58:49 +00:00
|
|
|
encode_uint64(fa->get_length(), buf4);
|
2014-02-10 01:10:30 +00:00
|
|
|
cd->connection->put_data(buf4, 8);
|
|
|
|
|
|
|
|
cd->files[id] = fa;
|
|
|
|
DEBUG_TIME("open_file_end")
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case FileAccessNetwork::COMMAND_READ_BLOCK: {
|
|
|
|
err = cd->connection->get_data(buf4, 8);
|
|
|
|
if (err != OK) {
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
ERR_CONTINUE(!cd->files.has(id));
|
|
|
|
|
|
|
|
uint64_t offset = decode_uint64(buf4);
|
|
|
|
|
|
|
|
err = cd->connection->get_data(buf4, 4);
|
|
|
|
if (err != OK) {
|
|
|
|
_close_client(cd);
|
|
|
|
ERR_FAIL_COND(err != OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int blocklen = decode_uint32(buf4);
|
|
|
|
ERR_CONTINUE(blocklen > (16 * 1024 * 1024));
|
|
|
|
|
|
|
|
cd->files[id]->seek(offset);
|
|
|
|
Vector<uint8_t> buf;
|
|
|
|
buf.resize(blocklen);
|
2019-03-26 17:51:13 +00:00
|
|
|
uint32_t read = cd->files[id]->get_buffer(buf.ptrw(), blocklen);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2018-11-08 17:42:41 +00:00
|
|
|
print_verbose("GET BLOCK - offset: " + itos(offset) + ", blocklen: " + itos(blocklen));
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//not found, continue
|
|
|
|
encode_uint32(id, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
encode_uint32(FileAccessNetwork::RESPONSE_DATA, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
encode_uint64(offset, buf4);
|
|
|
|
cd->connection->put_data(buf4, 8);
|
|
|
|
encode_uint32(read, buf4);
|
|
|
|
cd->connection->put_data(buf4, 4);
|
|
|
|
cd->connection->put_data(buf.ptr(), read);
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case FileAccessNetwork::COMMAND_CLOSE: {
|
2018-11-08 17:42:41 +00:00
|
|
|
print_verbose("CLOSED");
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_CONTINUE(!cd->files.has(id));
|
|
|
|
cd->files.erase(id);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_close_client(cd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFileServer::_thread_start(void *s) {
|
2022-04-05 10:40:26 +00:00
|
|
|
EditorFileServer *self = static_cast<EditorFileServer *>(s);
|
2014-02-10 01:10:30 +00:00
|
|
|
while (!self->quit) {
|
|
|
|
if (self->cmd == CMD_ACTIVATE) {
|
|
|
|
self->server->listen(self->port);
|
|
|
|
self->active = true;
|
|
|
|
self->cmd = CMD_NONE;
|
|
|
|
} else if (self->cmd == CMD_STOP) {
|
|
|
|
self->server->stop();
|
|
|
|
self->active = false;
|
|
|
|
self->cmd = CMD_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->active) {
|
|
|
|
if (self->server->is_connection_available()) {
|
|
|
|
ClientData *cd = memnew(ClientData);
|
|
|
|
cd->connection = self->server->take_connection();
|
|
|
|
cd->efs = self;
|
|
|
|
cd->quit = false;
|
2021-03-06 12:26:16 +00:00
|
|
|
cd->thread = memnew(Thread);
|
|
|
|
cd->thread->start(_subthread_start, cd);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:28:13 +00:00
|
|
|
self->wait_mutex.lock();
|
2014-02-10 01:10:30 +00:00
|
|
|
while (self->to_wait.size()) {
|
2022-05-19 15:00:06 +00:00
|
|
|
Thread *w = *self->to_wait.begin();
|
2014-02-10 01:10:30 +00:00
|
|
|
self->to_wait.erase(w);
|
2020-02-26 10:28:13 +00:00
|
|
|
self->wait_mutex.unlock();
|
2021-01-19 12:29:41 +00:00
|
|
|
w->wait_to_finish();
|
2020-02-26 10:28:13 +00:00
|
|
|
self->wait_mutex.lock();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2020-02-26 10:28:13 +00:00
|
|
|
self->wait_mutex.unlock();
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
OS::get_singleton()->delay_usec(100000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFileServer::start() {
|
|
|
|
stop();
|
2022-03-06 20:39:19 +00:00
|
|
|
port = EDITOR_GET("filesystem/file_server/port");
|
|
|
|
password = EDITOR_GET("filesystem/file_server/password");
|
2014-02-10 01:10:30 +00:00
|
|
|
cmd = CMD_ACTIVATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EditorFileServer::is_active() const {
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditorFileServer::stop() {
|
|
|
|
cmd = CMD_STOP;
|
|
|
|
}
|
|
|
|
|
|
|
|
EditorFileServer::EditorFileServer() {
|
2021-06-17 22:03:09 +00:00
|
|
|
server.instantiate();
|
2021-01-19 12:29:41 +00:00
|
|
|
thread.start(_thread_start, this);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-05 22:41:36 +00:00
|
|
|
EDITOR_DEF("filesystem/file_server/port", 6010);
|
|
|
|
EDITOR_DEF("filesystem/file_server/password", "");
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EditorFileServer::~EditorFileServer() {
|
|
|
|
quit = true;
|
2021-01-19 12:29:41 +00:00
|
|
|
thread.wait_to_finish();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|