mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 10:31:50 +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_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_udp = network.__open_udp
|
||||
local connect_tcp = network.__connect_tcp
|
||||
local connect_udp = network.__connect_udp
|
||||
network.__request = nil
|
||||
network.__get = nil
|
||||
network.__get_binary = nil
|
||||
network.__post = nil
|
||||
network.__open_tcp = nil
|
||||
network.__open_udp = nil
|
||||
network.__connect_tcp = nil
|
||||
|
|
@ -130,33 +124,31 @@ end
|
|||
network.request = request
|
||||
|
||||
network.get = function(url, callback, errorCallback, headers)
|
||||
local id = http_get(url, headers)
|
||||
if callback then
|
||||
_http_response_callbacks[id] = callback
|
||||
end
|
||||
if errorCallback then
|
||||
_http_error_callbacks[id] = errorCallback
|
||||
end
|
||||
return request(url, {
|
||||
headers = headers,
|
||||
on_response = callback,
|
||||
on_error = errorCallback,
|
||||
})
|
||||
end
|
||||
|
||||
network.get_binary = function(url, callback, errorCallback, headers)
|
||||
local id = http_get_binary(url, headers)
|
||||
if callback then
|
||||
_http_response_callbacks[id] = callback
|
||||
end
|
||||
if errorCallback then
|
||||
_http_error_callbacks[id] = errorCallback
|
||||
end
|
||||
return request(url, {
|
||||
headers = headers,
|
||||
on_response = callback and (function (response) return callback(Bytearray(response)) end),
|
||||
on_error = errorCallback
|
||||
})
|
||||
end
|
||||
|
||||
network.post = function(url, data, callback, errorCallback, headers)
|
||||
local id = http_post(url, data, headers)
|
||||
if callback then
|
||||
_http_response_callbacks[id] = callback
|
||||
end
|
||||
if errorCallback then
|
||||
_http_error_callbacks[id] = errorCallback
|
||||
end
|
||||
network.post = function(url, body, callback, errorCallback, headers)
|
||||
return request(url, {
|
||||
method = "POST",
|
||||
headers = table.extend({
|
||||
"Content-Type: application/json"
|
||||
}, headers),
|
||||
body = body,
|
||||
on_response = callback,
|
||||
on_error = errorCallback,
|
||||
})
|
||||
end
|
||||
|
||||
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");
|
||||
}
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = network.getConnection(id, false)) {
|
||||
|
|
@ -598,9 +522,6 @@ int wrap(lua_State* L) {
|
|||
|
||||
const luaL_Reg networklib[] = {
|
||||
{"__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_download", wrap<l_get_total_download>},
|
||||
{"find_free_port", wrap<l_find_free_port>},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue