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