add 'network' permission
This commit is contained in:
parent
56d808e3e2
commit
dd40780334
@ -1,3 +1,6 @@
|
|||||||
# default project
|
# default project
|
||||||
name = "default"
|
name = "default"
|
||||||
base_packs = ["base"]
|
base_packs = ["base"]
|
||||||
|
permissions = [
|
||||||
|
"network"
|
||||||
|
]
|
||||||
|
|||||||
@ -182,6 +182,9 @@ local function clean(iterable, checkFun, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
network.__process_events = function()
|
network.__process_events = function()
|
||||||
|
if not network.is_available() then
|
||||||
|
return
|
||||||
|
end
|
||||||
local CLIENT_CONNECTED = 1
|
local CLIENT_CONNECTED = 1
|
||||||
local CONNECTED_TO_SERVER = 2
|
local CONNECTED_TO_SERVER = 2
|
||||||
local DATAGRAM = 3
|
local DATAGRAM = 3
|
||||||
|
|||||||
@ -94,12 +94,17 @@ bool ClientConnection::alive() const {
|
|||||||
static network::Server& create_tcp_server(
|
static network::Server& create_tcp_server(
|
||||||
DebuggingServer& dbgServer, Engine& engine, int port
|
DebuggingServer& dbgServer, Engine& engine, int port
|
||||||
) {
|
) {
|
||||||
auto& network = engine.getNetwork();
|
auto network = engine.getNetwork();
|
||||||
u64id_t serverId = network.openTcpServer(
|
if (network == nullptr) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"unable to create tcp server: project has no network permission"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
u64id_t serverId = network->openTcpServer(
|
||||||
port,
|
port,
|
||||||
[&network, &dbgServer](u64id_t sid, u64id_t id) {
|
[&network, &dbgServer](u64id_t sid, u64id_t id) {
|
||||||
auto& connection = dynamic_cast<network::ReadableConnection&>(
|
auto& connection = dynamic_cast<network::ReadableConnection&>(
|
||||||
*network.getConnection(id, true)
|
*network->getConnection(id, true)
|
||||||
);
|
);
|
||||||
connection.setPrivate(true);
|
connection.setPrivate(true);
|
||||||
logger.info() << "connected client " << id << ": "
|
logger.info() << "connected client " << id << ": "
|
||||||
@ -108,7 +113,7 @@ static network::Server& create_tcp_server(
|
|||||||
dbgServer.setClient(id);
|
dbgServer.setClient(id);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
auto& server = *network.getServer(serverId, true);
|
auto& server = *network->getServer(serverId, true);
|
||||||
server.setPrivate(true);
|
server.setPrivate(true);
|
||||||
|
|
||||||
auto& tcpServer = dynamic_cast<network::TcpServer&>(server);
|
auto& tcpServer = dynamic_cast<network::TcpServer&>(server);
|
||||||
@ -302,8 +307,10 @@ void DebuggingServer::sendValue(
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DebuggingServer::setClient(u64id_t client) {
|
void DebuggingServer::setClient(u64id_t client) {
|
||||||
|
auto network = engine.getNetwork();
|
||||||
|
assert (network != nullptr);
|
||||||
connection =
|
connection =
|
||||||
std::make_unique<ClientConnection>(engine.getNetwork(), client);
|
std::make_unique<ClientConnection>(*network, client);
|
||||||
connectionEstablished = false;
|
connectionEstablished = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,6 +14,7 @@ namespace scripting {
|
|||||||
|
|
||||||
struct Permissions {
|
struct Permissions {
|
||||||
static inline std::string WRITE_TO_USER = "write-to-user";
|
static inline std::string WRITE_TO_USER = "write-to-user";
|
||||||
|
static inline std::string NETWORK = "network";
|
||||||
|
|
||||||
std::set<std::string> permissions;
|
std::set<std::string> permissions;
|
||||||
|
|
||||||
|
|||||||
@ -138,7 +138,10 @@ void Engine::initialize(CoreParameters coreParameters) {
|
|||||||
|
|
||||||
editor = std::make_unique<devtools::Editor>(*this);
|
editor = std::make_unique<devtools::Editor>(*this);
|
||||||
cmd = std::make_unique<cmd::CommandsInterpreter>();
|
cmd = std::make_unique<cmd::CommandsInterpreter>();
|
||||||
network = network::Network::create(settings.network);
|
|
||||||
|
if (project->permissions.has(Permissions::NETWORK)) {
|
||||||
|
network = network::Network::create(settings.network);
|
||||||
|
}
|
||||||
|
|
||||||
if (!params.debugServerString.empty()) {
|
if (!params.debugServerString.empty()) {
|
||||||
try {
|
try {
|
||||||
@ -233,7 +236,9 @@ void Engine::run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::postUpdate() {
|
void Engine::postUpdate() {
|
||||||
network->update();
|
if (network) {
|
||||||
|
network->update();
|
||||||
|
}
|
||||||
postRunnables.run();
|
postRunnables.run();
|
||||||
scripting::process_post_runnables();
|
scripting::process_post_runnables();
|
||||||
|
|
||||||
@ -266,6 +271,8 @@ void Engine::nextFrame(bool waitForRefresh) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::startPauseLoop() {
|
void Engine::startPauseLoop() {
|
||||||
|
assert (network != nullptr);
|
||||||
|
|
||||||
bool initialCursorLocked = false;
|
bool initialCursorLocked = false;
|
||||||
if (!isHeadless()) {
|
if (!isHeadless()) {
|
||||||
initialCursorLocked = input->isCursorLocked();
|
initialCursorLocked = input->isCursorLocked();
|
||||||
|
|||||||
@ -3,13 +3,12 @@
|
|||||||
#include "CoreParameters.hpp"
|
#include "CoreParameters.hpp"
|
||||||
#include "PostRunnables.hpp"
|
#include "PostRunnables.hpp"
|
||||||
#include "Time.hpp"
|
#include "Time.hpp"
|
||||||
#include "delegates.hpp"
|
|
||||||
#include "settings.hpp"
|
#include "settings.hpp"
|
||||||
#include "typedefs.hpp"
|
|
||||||
#include "util/ObjectsKeeper.hpp"
|
#include "util/ObjectsKeeper.hpp"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
class Assets;
|
class Assets;
|
||||||
class ContentControl;
|
class ContentControl;
|
||||||
@ -161,8 +160,8 @@ public:
|
|||||||
return *window;
|
return *window;
|
||||||
}
|
}
|
||||||
|
|
||||||
network::Network& getNetwork() {
|
network::Network* getNetwork() {
|
||||||
return *network;
|
return network.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd::CommandsInterpreter& getCmd() {
|
cmd::CommandsInterpreter& getCmd() {
|
||||||
|
|||||||
@ -59,6 +59,7 @@ std::shared_ptr<UINode> create_debug_panel(
|
|||||||
Player& player,
|
Player& player,
|
||||||
bool allowDebugCheats
|
bool allowDebugCheats
|
||||||
) {
|
) {
|
||||||
|
auto network = engine.getNetwork();
|
||||||
auto& gui = engine.getGUI();
|
auto& gui = engine.getGUI();
|
||||||
auto panel = std::make_shared<Panel>(
|
auto panel = std::make_shared<Panel>(
|
||||||
gui, glm::vec2(300, 200), glm::vec4(5.0f), 2.0f
|
gui, glm::vec2(300, 200), glm::vec4(5.0f), 2.0f
|
||||||
@ -87,17 +88,18 @@ std::shared_ptr<UINode> create_debug_panel(
|
|||||||
fpsMax = fps;
|
fpsMax = fps;
|
||||||
});
|
});
|
||||||
|
|
||||||
panel->listenInterval(1.0f, [&engine]() {
|
if (network) {
|
||||||
const auto& network = engine.getNetwork();
|
panel->listenInterval(1.0f, [network]() {
|
||||||
size_t totalDownload = network.getTotalDownload();
|
size_t totalDownload = network->getTotalDownload();
|
||||||
size_t totalUpload = network.getTotalUpload();
|
size_t totalUpload = network->getTotalUpload();
|
||||||
netSpeedString =
|
netSpeedString =
|
||||||
L"download: " + std::to_wstring(totalDownload - lastTotalDownload) +
|
L"download: " + std::to_wstring(totalDownload - lastTotalDownload) +
|
||||||
L" B/s upload: " + std::to_wstring(totalUpload - lastTotalUpload) +
|
L" B/s upload: " + std::to_wstring(totalUpload - lastTotalUpload) +
|
||||||
L" B/s";
|
L" B/s";
|
||||||
lastTotalDownload = totalDownload;
|
lastTotalDownload = totalDownload;
|
||||||
lastTotalUpload = totalUpload;
|
lastTotalUpload = totalUpload;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
panel->add(create_label(gui, []() { return L"fps: "+fpsString;}));
|
panel->add(create_label(gui, []() { return L"fps: "+fpsString;}));
|
||||||
|
|
||||||
@ -116,7 +118,9 @@ std::shared_ptr<UINode> create_debug_panel(
|
|||||||
panel->add(create_label(gui, []() {
|
panel->add(create_label(gui, []() {
|
||||||
return L"lua-stack: " + std::to_wstring(scripting::get_values_on_stack());
|
return L"lua-stack: " + std::to_wstring(scripting::get_values_on_stack());
|
||||||
}));
|
}));
|
||||||
panel->add(create_label(gui, []() { return netSpeedString; }));
|
if (network) {
|
||||||
|
panel->add(create_label(gui, []() { return netSpeedString; }));
|
||||||
|
}
|
||||||
panel->add(create_label(gui, [&engine]() {
|
panel->add(create_label(gui, [&engine]() {
|
||||||
auto& settings = engine.getSettings();
|
auto& settings = engine.getSettings();
|
||||||
bool culling = settings.graphics.frustumCulling.get();
|
bool culling = settings.graphics.frustumCulling.get();
|
||||||
|
|||||||
@ -15,7 +15,11 @@ using namespace scripting;
|
|||||||
static int l_start_debug_instance(lua::State* L) {
|
static int l_start_debug_instance(lua::State* L) {
|
||||||
int port = lua::tointeger(L, 1);
|
int port = lua::tointeger(L, 1);
|
||||||
if (port == 0) {
|
if (port == 0) {
|
||||||
port = engine->getNetwork().findFreePort();
|
auto network = engine->getNetwork();
|
||||||
|
if (network == nullptr) {
|
||||||
|
throw std::runtime_error("project has no network permission");
|
||||||
|
}
|
||||||
|
port = network->findFreePort();
|
||||||
if (port == -1) {
|
if (port == -1) {
|
||||||
throw std::runtime_error("could not find free port");
|
throw std::runtime_error("could not find free port");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -133,7 +133,7 @@ static int l_post(lua::State* L, network::Network& network) {
|
|||||||
auto headers = read_headers(L, 3);
|
auto headers = read_headers(L, 3);
|
||||||
int currentRequestId = request_id++;
|
int currentRequestId = request_id++;
|
||||||
|
|
||||||
engine->getNetwork().post(
|
network.post(
|
||||||
url,
|
url,
|
||||||
string,
|
string,
|
||||||
[currentRequestId](std::vector<char> bytes) {
|
[currentRequestId](std::vector<char> bytes) {
|
||||||
@ -240,7 +240,7 @@ static int l_recv(lua::State* L, network::Network& network) {
|
|||||||
u64id_t id = lua::tointeger(L, 1);
|
u64id_t id = lua::tointeger(L, 1);
|
||||||
int length = lua::tointeger(L, 2);
|
int length = lua::tointeger(L, 2);
|
||||||
|
|
||||||
auto connection = engine->getNetwork().getConnection(id, false);
|
auto connection = network.getConnection(id, false);
|
||||||
|
|
||||||
if (connection == nullptr || connection->getTransportType() != network::TransportType::TCP) {
|
if (connection == nullptr || connection->getTransportType() != network::TransportType::TCP) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -519,11 +519,21 @@ static int l_pull_events(lua::State* L, network::Network& network) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int l_is_available(lua::State* L) {
|
||||||
|
return engine->getNetwork() != nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
template <int(*func)(lua::State*, network::Network&)>
|
template <int(*func)(lua::State*, network::Network&)>
|
||||||
int wrap(lua_State* L) {
|
int wrap(lua_State* L) {
|
||||||
int result = 0;
|
int result = 0;
|
||||||
try {
|
try {
|
||||||
result = func(L, engine->getNetwork());
|
auto network = engine->getNetwork();
|
||||||
|
if (network == nullptr) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"network subsystem is not available in the project"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
result = func(L, *network);
|
||||||
}
|
}
|
||||||
// transform exception with description into lua_error
|
// transform exception with description into lua_error
|
||||||
catch (std::exception& e) {
|
catch (std::exception& e) {
|
||||||
@ -543,6 +553,7 @@ const luaL_Reg networklib[] = {
|
|||||||
{"get_total_upload", wrap<l_get_total_upload>},
|
{"get_total_upload", wrap<l_get_total_upload>},
|
||||||
{"get_total_download", wrap<l_get_total_download>},
|
{"get_total_download", wrap<l_get_total_download>},
|
||||||
{"find_free_port", wrap<l_find_free_port>},
|
{"find_free_port", wrap<l_find_free_port>},
|
||||||
|
{"is_available", lua::wrap<l_is_available>},
|
||||||
{"__pull_events", wrap<l_pull_events>},
|
{"__pull_events", wrap<l_pull_events>},
|
||||||
{"__open_tcp", wrap<l_open_tcp>},
|
{"__open_tcp", wrap<l_open_tcp>},
|
||||||
{"__open_udp", wrap<l_open_udp>},
|
{"__open_udp", wrap<l_open_udp>},
|
||||||
|
|||||||
@ -194,5 +194,6 @@ void Network::update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Network> Network::create(const NetworkSettings& settings) {
|
std::unique_ptr<Network> Network::create(const NetworkSettings& settings) {
|
||||||
|
logger.info() << "initializing network";
|
||||||
return std::make_unique<Network>(network::create_curl_requests());
|
return std::make_unique<Network>(network::create_curl_requests());
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user