mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-13 23:11:49 +00:00
cleanup network.* functions
This commit is contained in:
parent
03efd8c669
commit
9226f362a7
2 changed files with 21 additions and 108 deletions
|
|
@ -101,17 +101,11 @@ local _http_response_callbacks = {}
|
||||||
local _http_error_callbacks = {}
|
local _http_error_callbacks = {}
|
||||||
|
|
||||||
local http_request = network.__request
|
local http_request = network.__request
|
||||||
local http_get = network.__get
|
|
||||||
local http_get_binary = network.__get_binary
|
|
||||||
local http_post = network.__post
|
|
||||||
local open_tcp = network.__open_tcp
|
local open_tcp = network.__open_tcp
|
||||||
local open_udp = network.__open_udp
|
local open_udp = network.__open_udp
|
||||||
local connect_tcp = network.__connect_tcp
|
local connect_tcp = network.__connect_tcp
|
||||||
local connect_udp = network.__connect_udp
|
local connect_udp = network.__connect_udp
|
||||||
network.__request = nil
|
network.__request = nil
|
||||||
network.__get = nil
|
|
||||||
network.__get_binary = nil
|
|
||||||
network.__post = nil
|
|
||||||
network.__open_tcp = nil
|
network.__open_tcp = nil
|
||||||
network.__open_udp = nil
|
network.__open_udp = nil
|
||||||
network.__connect_tcp = nil
|
network.__connect_tcp = nil
|
||||||
|
|
@ -130,33 +124,31 @@ end
|
||||||
network.request = request
|
network.request = request
|
||||||
|
|
||||||
network.get = function(url, callback, errorCallback, headers)
|
network.get = function(url, callback, errorCallback, headers)
|
||||||
local id = http_get(url, headers)
|
return request(url, {
|
||||||
if callback then
|
headers = headers,
|
||||||
_http_response_callbacks[id] = callback
|
on_response = callback,
|
||||||
end
|
on_error = errorCallback,
|
||||||
if errorCallback then
|
})
|
||||||
_http_error_callbacks[id] = errorCallback
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
network.get_binary = function(url, callback, errorCallback, headers)
|
network.get_binary = function(url, callback, errorCallback, headers)
|
||||||
local id = http_get_binary(url, headers)
|
return request(url, {
|
||||||
if callback then
|
headers = headers,
|
||||||
_http_response_callbacks[id] = callback
|
on_response = callback and (function (response) return callback(Bytearray(response)) end),
|
||||||
end
|
on_error = errorCallback
|
||||||
if errorCallback then
|
})
|
||||||
_http_error_callbacks[id] = errorCallback
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
network.post = function(url, data, callback, errorCallback, headers)
|
network.post = function(url, body, callback, errorCallback, headers)
|
||||||
local id = http_post(url, data, headers)
|
return request(url, {
|
||||||
if callback then
|
method = "POST",
|
||||||
_http_response_callbacks[id] = callback
|
headers = table.extend({
|
||||||
end
|
"Content-Type: application/json"
|
||||||
if errorCallback then
|
}, headers),
|
||||||
_http_error_callbacks[id] = errorCallback
|
body = body,
|
||||||
end
|
on_response = callback,
|
||||||
|
on_error = errorCallback,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
network.tcp_open = function (port, handler)
|
network.tcp_open = function (port, handler)
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,7 @@ static int l_request(lua::State* L, network::Network& network) {
|
||||||
throw std::runtime_error("table expected as argument #2");
|
throw std::runtime_error("table expected as argument #2");
|
||||||
}
|
}
|
||||||
if (lua::getfield(L, "method", 2)) {
|
if (lua::getfield(L, "method", 2)) {
|
||||||
request.method = util::upper_case(lua::require_string(L, -1));
|
request.method = lua::require_string(L, -1);
|
||||||
lua::pop(L);
|
lua::pop(L);
|
||||||
}
|
}
|
||||||
if (lua::getfield(L, "headers", 2)) {
|
if (lua::getfield(L, "headers", 2)) {
|
||||||
|
|
@ -115,82 +115,6 @@ static int l_request(lua::State* L, network::Network& network) {
|
||||||
return lua::pushinteger(L, currentRequestId);
|
return lua::pushinteger(L, currentRequestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int perform_get(lua::State* L, network::Network& network, bool binary) {
|
|
||||||
std::string url(lua::require_lstring(L, 1));
|
|
||||||
auto headers = read_headers(L, 2);
|
|
||||||
|
|
||||||
int currentRequestId = request_id++;
|
|
||||||
|
|
||||||
network::HttpRequest request {};
|
|
||||||
request.url = std::move(url);
|
|
||||||
request.onResponse =
|
|
||||||
[currentRequestId, binary](std::vector<char> bytes) {
|
|
||||||
push_event(NetworkEvent(
|
|
||||||
RESPONSE,
|
|
||||||
ResponseEventDto {
|
|
||||||
200, binary, currentRequestId, std::move(bytes)}
|
|
||||||
));
|
|
||||||
};
|
|
||||||
request.onReject =
|
|
||||||
[currentRequestId, binary](int code, std::vector<char> bytes) {
|
|
||||||
push_event(NetworkEvent(
|
|
||||||
RESPONSE,
|
|
||||||
ResponseEventDto {
|
|
||||||
code, binary, currentRequestId, std::move(bytes)}
|
|
||||||
));
|
|
||||||
};
|
|
||||||
request.headers = std::move(headers);
|
|
||||||
network.request(std::move(request));
|
|
||||||
return lua::pushinteger(L, currentRequestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_get(lua::State* L, network::Network& network) {
|
|
||||||
return perform_get(L, network, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_get_binary(lua::State* L, network::Network& network) {
|
|
||||||
return perform_get(L, network, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_post(lua::State* L, network::Network& network) {
|
|
||||||
std::string url(lua::require_lstring(L, 1));
|
|
||||||
auto data = lua::tovalue(L, 2);
|
|
||||||
|
|
||||||
std::string string;
|
|
||||||
if (data.isString()) {
|
|
||||||
string = data.asString();
|
|
||||||
} else {
|
|
||||||
string = json::stringify(data, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto headers = read_headers(L, 3);
|
|
||||||
int currentRequestId = request_id++;
|
|
||||||
|
|
||||||
network::HttpRequest request {};
|
|
||||||
request.url = std::move(url);
|
|
||||||
request.body = string;
|
|
||||||
request.headers = std::move(headers);
|
|
||||||
request.headers.emplace_back("Content-Type: application/json");
|
|
||||||
request.onResponse =
|
|
||||||
[currentRequestId](std::vector<char> bytes) {
|
|
||||||
push_event(NetworkEvent(
|
|
||||||
RESPONSE,
|
|
||||||
ResponseEventDto {
|
|
||||||
200, false, currentRequestId, std::move(bytes)}
|
|
||||||
));
|
|
||||||
};
|
|
||||||
request.onReject =
|
|
||||||
[currentRequestId](int code, std::vector<char> bytes) {
|
|
||||||
push_event(NetworkEvent(
|
|
||||||
RESPONSE,
|
|
||||||
ResponseEventDto {
|
|
||||||
code, false, currentRequestId, std::move(bytes)}
|
|
||||||
));
|
|
||||||
};
|
|
||||||
network.request(std::move(request));
|
|
||||||
return lua::pushinteger(L, currentRequestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_close(lua::State* L, network::Network& network) {
|
static int l_close(lua::State* L, network::Network& network) {
|
||||||
u64id_t id = lua::tointeger(L, 1);
|
u64id_t id = lua::tointeger(L, 1);
|
||||||
if (auto connection = network.getConnection(id, false)) {
|
if (auto connection = network.getConnection(id, false)) {
|
||||||
|
|
@ -598,9 +522,6 @@ int wrap(lua_State* L) {
|
||||||
|
|
||||||
const luaL_Reg networklib[] = {
|
const luaL_Reg networklib[] = {
|
||||||
{"__request", wrap<l_request>},
|
{"__request", wrap<l_request>},
|
||||||
{"__get", wrap<l_get>},
|
|
||||||
{"__get_binary", wrap<l_get_binary>},
|
|
||||||
{"__post", wrap<l_post>},
|
|
||||||
{"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>},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue