Merge branch 'dev' into pathfinding
This commit is contained in:
commit
88afb48a1c
@ -68,6 +68,9 @@ block.get_variant(x: int, y: int, z: int) -> int
|
|||||||
|
|
||||||
-- Sets the block variant by index
|
-- Sets the block variant by index
|
||||||
block.set_variant(x: int, y: int, z: int, index: int) -> int
|
block.set_variant(x: int, y: int, z: int, index: int) -> int
|
||||||
|
|
||||||
|
-- Checks if an block has specified tag
|
||||||
|
block.has_tag(id: int, tag: str) -> bool
|
||||||
```
|
```
|
||||||
|
|
||||||
## Rotation
|
## Rotation
|
||||||
|
|||||||
@ -33,4 +33,7 @@ item.emission(itemid: int) -> str
|
|||||||
|
|
||||||
-- Returns the value of the `uses` property
|
-- Returns the value of the `uses` property
|
||||||
item.uses(itemid: int) -> int
|
item.uses(itemid: int) -> int
|
||||||
|
|
||||||
|
-- Checks if an item has specified tag
|
||||||
|
item.has_tag(itemid: int, tag: str) -> bool
|
||||||
```
|
```
|
||||||
|
|||||||
@ -67,6 +67,9 @@ block.get_variant(x: int, y: int, z: int) -> int
|
|||||||
|
|
||||||
-- Устанавливает вариант блока по индексу
|
-- Устанавливает вариант блока по индексу
|
||||||
block.set_variant(x: int, y: int, z: int, index: int) -> int
|
block.set_variant(x: int, y: int, z: int, index: int) -> int
|
||||||
|
|
||||||
|
-- Проверяет наличие тега у блока
|
||||||
|
block.has_tag(id: int, tag: str) -> bool
|
||||||
```
|
```
|
||||||
|
|
||||||
### Raycast
|
### Raycast
|
||||||
|
|||||||
@ -33,6 +33,9 @@ item.emission(itemid: int) -> str
|
|||||||
|
|
||||||
-- Возвращает значение свойства `uses`
|
-- Возвращает значение свойства `uses`
|
||||||
item.uses(itemid: int) -> int
|
item.uses(itemid: int) -> int
|
||||||
|
|
||||||
|
-- Проверяет наличие тега у предмета
|
||||||
|
item.has_tag(itemid: int, tag: str) -> bool
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Библиотека для работы с сетью.
|
Библиотека для работы с сетью.
|
||||||
|
|
||||||
## HTTP-запросы
|
## HTTP-Запросы
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
-- Выполняет GET запрос к указанному URL.
|
-- Выполняет GET запрос к указанному URL.
|
||||||
@ -98,6 +98,65 @@ server:is_open() --> bool
|
|||||||
server:get_port() --> int
|
server:get_port() --> int
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## UDP-Датаграммы
|
||||||
|
|
||||||
|
```lua
|
||||||
|
network.udp_connect(
|
||||||
|
address: str,
|
||||||
|
port: int,
|
||||||
|
-- Функция, вызываемая при получении датаграммы с указанного при открытии сокета адреса и порта
|
||||||
|
datagramHandler: function(Bytearray),
|
||||||
|
-- Функция, вызываемая после открытия сокета
|
||||||
|
-- Опциональна, так как в UDP нет handshake
|
||||||
|
[опционально] openCallback: function(WriteableSocket),
|
||||||
|
) --> WriteableSocket
|
||||||
|
```
|
||||||
|
|
||||||
|
Открывает UDP-сокет с привязкой к удалённому адресу и порту
|
||||||
|
|
||||||
|
Класс WriteableSocket имеет следующие методы:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
-- Отправляет датаграмму на адрес и порт, заданные при открытии сокета
|
||||||
|
socket:send(table|Bytearray|str)
|
||||||
|
|
||||||
|
-- Закрывает сокет
|
||||||
|
socket:close()
|
||||||
|
|
||||||
|
-- Проверяет открыт ли сокет
|
||||||
|
socket:is_open() --> bool
|
||||||
|
|
||||||
|
-- Возвращает адрес и порт, на которые привязан сокет
|
||||||
|
socket:get_address() --> str, int
|
||||||
|
```
|
||||||
|
|
||||||
|
```lua
|
||||||
|
network.udp_open(
|
||||||
|
port: int,
|
||||||
|
-- Функция, вызываемая при получении датаграмы
|
||||||
|
-- В параметры передаётся адрес и порт отправителя, а также сами данные
|
||||||
|
datagramHandler: function(address: str, port: int, data: Bytearray, server: DatagramServerSocket)
|
||||||
|
) --> DatagramServerSocket
|
||||||
|
```
|
||||||
|
|
||||||
|
Открывает UDP-сервер на указанном порту
|
||||||
|
|
||||||
|
Класс DatagramServerSocket имеет следующие методы:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
-- Отправляет датаграмму на переданный адрес и порт
|
||||||
|
server:send(address: str, port: int, data: table|Bytearray|str)
|
||||||
|
|
||||||
|
-- Завершает принятие датаграмм
|
||||||
|
server:stop()
|
||||||
|
|
||||||
|
-- Проверяет возможность принятия датаграмм
|
||||||
|
server:is_open() --> bool
|
||||||
|
|
||||||
|
-- Возвращает порт, который слушает сервер
|
||||||
|
server:get_port() --> int
|
||||||
|
```
|
||||||
|
|
||||||
## Аналитика
|
## Аналитика
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"texture": "coal_ore",
|
"texture": "coal_ore",
|
||||||
|
"tags": ["base:ore"],
|
||||||
"base:durability": 16.0
|
"base:durability": 16.0
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,5 +7,6 @@
|
|||||||
"obstacle": false,
|
"obstacle": false,
|
||||||
"selectable": false,
|
"selectable": false,
|
||||||
"replaceable": true,
|
"replaceable": true,
|
||||||
"translucent": true
|
"translucent": true,
|
||||||
|
"tags": ["base:liquid"]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,18 +46,35 @@ local Socket = {__index={
|
|||||||
get_address=function(self) return network.__get_address(self.id) end,
|
get_address=function(self) return network.__get_address(self.id) end,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
local WriteableSocket = {__index={
|
||||||
|
send=function(self, ...) return network.__send(self.id, ...) end,
|
||||||
|
close=function(self) return network.__close(self.id) end,
|
||||||
|
is_open=function(self) return network.__is_alive(self.id) end,
|
||||||
|
get_address=function(self) return network.__get_address(self.id) end,
|
||||||
|
}}
|
||||||
|
|
||||||
local ServerSocket = {__index={
|
local ServerSocket = {__index={
|
||||||
close=function(self) return network.__closeserver(self.id) end,
|
close=function(self) return network.__closeserver(self.id) end,
|
||||||
is_open=function(self) return network.__is_serveropen(self.id) end,
|
is_open=function(self) return network.__is_serveropen(self.id) end,
|
||||||
get_port=function(self) return network.__get_serverport(self.id) end,
|
get_port=function(self) return network.__get_serverport(self.id) end,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
local DatagramServerSocket = {__index={
|
||||||
|
close=function(self) return network.__closeserver(self.id) end,
|
||||||
|
is_open=function(self) return network.__is_serveropen(self.id) end,
|
||||||
|
get_port=function(self) return network.__get_serverport(self.id) end,
|
||||||
|
send=function(self, ...) return network.__udp_server_send_to(self.id, ...) end
|
||||||
|
}}
|
||||||
|
|
||||||
local _tcp_server_callbacks = {}
|
local _tcp_server_callbacks = {}
|
||||||
local _tcp_client_callbacks = {}
|
local _tcp_client_callbacks = {}
|
||||||
|
|
||||||
|
local _udp_server_callbacks = {}
|
||||||
|
local _udp_client_datagram_callbacks = {}
|
||||||
|
local _udp_client_open_callbacks = {}
|
||||||
|
|
||||||
network.tcp_open = function (port, handler)
|
network.tcp_open = function (port, handler)
|
||||||
local socket = setmetatable({id=network.__open(port)}, ServerSocket)
|
local socket = setmetatable({id=network.__open_tcp(port)}, ServerSocket)
|
||||||
|
|
||||||
_tcp_server_callbacks[socket.id] = function(id)
|
_tcp_server_callbacks[socket.id] = function(id)
|
||||||
handler(setmetatable({id=id}, Socket))
|
handler(setmetatable({id=id}, Socket))
|
||||||
@ -67,19 +84,63 @@ end
|
|||||||
|
|
||||||
network.tcp_connect = function(address, port, callback)
|
network.tcp_connect = function(address, port, callback)
|
||||||
local socket = setmetatable({id=0}, Socket)
|
local socket = setmetatable({id=0}, Socket)
|
||||||
socket.id = network.__connect(address, port)
|
socket.id = network.__connect_tcp(address, port)
|
||||||
_tcp_client_callbacks[socket.id] = function() callback(socket) end
|
_tcp_client_callbacks[socket.id] = function() callback(socket) end
|
||||||
return socket
|
return socket
|
||||||
end
|
end
|
||||||
|
|
||||||
|
network.udp_open = function (port, datagramHandler)
|
||||||
|
if type(datagramHandler) ~= 'function' then
|
||||||
|
error "udp server cannot be opened without datagram handler"
|
||||||
|
end
|
||||||
|
|
||||||
|
local socket = setmetatable({id=network.__open_udp(port)}, DatagramServerSocket)
|
||||||
|
|
||||||
|
_udp_server_callbacks[socket.id] = function(address, port, data)
|
||||||
|
datagramHandler(address, port, data, socket)
|
||||||
|
end
|
||||||
|
|
||||||
|
return socket
|
||||||
|
end
|
||||||
|
|
||||||
|
network.udp_connect = function (address, port, datagramHandler, openCallback)
|
||||||
|
if type(datagramHandler) ~= 'function' then
|
||||||
|
error "udp client socket cannot be opened without datagram handler"
|
||||||
|
end
|
||||||
|
|
||||||
|
local socket = setmetatable({id=0}, WriteableSocket)
|
||||||
|
socket.id = network.__connect_udp(address, port)
|
||||||
|
|
||||||
|
_udp_client_datagram_callbacks[socket.id] = datagramHandler
|
||||||
|
_udp_client_open_callbacks[socket.id] = openCallback
|
||||||
|
|
||||||
|
return socket
|
||||||
|
end
|
||||||
|
|
||||||
|
local function clean(iterable, checkFun, ...)
|
||||||
|
local tables = { ... }
|
||||||
|
|
||||||
|
for id, _ in pairs(iterable) do
|
||||||
|
if not checkFun(id) then
|
||||||
|
for i = 1, #tables do
|
||||||
|
tables[i][id] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
network.__process_events = function()
|
network.__process_events = function()
|
||||||
local CLIENT_CONNECTED = 1
|
local CLIENT_CONNECTED = 1
|
||||||
local CONNECTED_TO_SERVER = 2
|
local CONNECTED_TO_SERVER = 2
|
||||||
|
local DATAGRAM = 3
|
||||||
|
|
||||||
|
local ON_SERVER = 1
|
||||||
|
local ON_CLIENT = 2
|
||||||
|
|
||||||
local cleaned = false
|
local cleaned = false
|
||||||
local events = network.__pull_events()
|
local events = network.__pull_events()
|
||||||
for i, event in ipairs(events) do
|
for i, event in ipairs(events) do
|
||||||
local etype, sid, cid = unpack(event)
|
local etype, sid, cid, addr, port, side, data = unpack(event)
|
||||||
|
|
||||||
if etype == CLIENT_CONNECTED then
|
if etype == CLIENT_CONNECTED then
|
||||||
local callback = _tcp_server_callbacks[sid]
|
local callback = _tcp_server_callbacks[sid]
|
||||||
@ -87,24 +148,26 @@ network.__process_events = function()
|
|||||||
callback(cid)
|
callback(cid)
|
||||||
end
|
end
|
||||||
elseif etype == CONNECTED_TO_SERVER then
|
elseif etype == CONNECTED_TO_SERVER then
|
||||||
local callback = _tcp_client_callbacks[cid]
|
local callback = _tcp_client_callbacks[cid] or _udp_client_open_callbacks[cid]
|
||||||
if callback then
|
if callback then
|
||||||
callback()
|
callback()
|
||||||
end
|
end
|
||||||
|
elseif etype == DATAGRAM then
|
||||||
|
if side == ON_CLIENT then
|
||||||
|
_udp_client_datagram_callbacks[cid](data)
|
||||||
|
elseif side == ON_SERVER then
|
||||||
|
_udp_server_callbacks[sid](addr, port, data)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- remove dead servers
|
-- remove dead servers
|
||||||
if not cleaned then
|
if not cleaned then
|
||||||
for sid, _ in pairs(_tcp_server_callbacks) do
|
clean(_tcp_server_callbacks, network.__is_serveropen, _tcp_server_callbacks)
|
||||||
if not network.__is_serveropen(sid) then
|
clean(_tcp_client_callbacks, network.__is_alive, _tcp_client_callbacks)
|
||||||
_tcp_server_callbacks[sid] = nil
|
|
||||||
end
|
clean(_udp_server_callbacks, network.__is_serveropen, _udp_server_callbacks)
|
||||||
end
|
clean(_udp_client_datagram_callbacks, network.__is_alive, _udp_client_open_callbacks, _udp_client_datagram_callbacks)
|
||||||
for cid, _ in pairs(_tcp_client_callbacks) do
|
|
||||||
if not network.__is_alive(cid) then
|
|
||||||
_tcp_client_callbacks[cid] = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
cleaned = true
|
cleaned = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -12,22 +12,31 @@ local names = {
|
|||||||
for name, _ in pairs(user_props) do
|
for name, _ in pairs(user_props) do
|
||||||
table.insert(names, name)
|
table.insert(names, name)
|
||||||
end
|
end
|
||||||
-- remove undefined properties
|
|
||||||
for id, blockprops in pairs(block.properties) do
|
-- remove undefined properties and build tags set
|
||||||
for propname, value in pairs(blockprops) do
|
local function process_properties(properties)
|
||||||
if not table.has(names, propname) then
|
for id, props in pairs(properties) do
|
||||||
blockprops[propname] = nil
|
local tags_set = nil
|
||||||
end
|
for propname, value in pairs(props) do
|
||||||
end
|
if propname == "tags" then
|
||||||
end
|
if #value > 0 then
|
||||||
for id, itemprops in pairs(item.properties) do
|
tags_set = tags_set or {}
|
||||||
for propname, value in pairs(itemprops) do
|
|
||||||
if not table.has(names, propname) then
|
|
||||||
itemprops[propname] = nil
|
|
||||||
end
|
end
|
||||||
|
for _, tag in ipairs(value) do
|
||||||
|
tags_set[tag] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if not table.has(names, propname) then
|
||||||
|
props[propname] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
props.tags_set = tags_set
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
process_properties(block.properties)
|
||||||
|
process_properties(item.properties)
|
||||||
|
|
||||||
local function make_read_only(t)
|
local function make_read_only(t)
|
||||||
setmetatable(t, {
|
setmetatable(t, {
|
||||||
__newindex = function()
|
__newindex = function()
|
||||||
@ -57,6 +66,15 @@ local function cache_names(library)
|
|||||||
function library.index(name)
|
function library.index(name)
|
||||||
return indices[name]
|
return indices[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function library.has_tag(id, tag)
|
||||||
|
local tags_set = library.properties[id].tags_set
|
||||||
|
if tags_set then
|
||||||
|
return tags_set[tag]
|
||||||
|
else
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
cache_names(block)
|
cache_names(block)
|
||||||
|
|||||||
@ -35,13 +35,15 @@ Content::Content(
|
|||||||
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
||||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
|
UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
|
||||||
ResourceIndicesSet resourceIndices,
|
ResourceIndicesSet resourceIndices,
|
||||||
dv::value defaults
|
dv::value defaults,
|
||||||
|
std::unordered_map<std::string, int> tags
|
||||||
)
|
)
|
||||||
: indices(std::move(indices)),
|
: indices(std::move(indices)),
|
||||||
packs(std::move(packs)),
|
packs(std::move(packs)),
|
||||||
blockMaterials(std::move(blockMaterials)),
|
blockMaterials(std::move(blockMaterials)),
|
||||||
skeletons(std::move(skeletons)),
|
skeletons(std::move(skeletons)),
|
||||||
defaults(std::move(defaults)),
|
defaults(std::move(defaults)),
|
||||||
|
tags(std::move(tags)),
|
||||||
blocks(std::move(blocks)),
|
blocks(std::move(blocks)),
|
||||||
items(std::move(items)),
|
items(std::move(items)),
|
||||||
entities(std::move(entities)),
|
entities(std::move(entities)),
|
||||||
|
|||||||
@ -176,6 +176,7 @@ class Content {
|
|||||||
UptrsMap<std::string, BlockMaterial> blockMaterials;
|
UptrsMap<std::string, BlockMaterial> blockMaterials;
|
||||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
|
UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
|
||||||
dv::value defaults = nullptr;
|
dv::value defaults = nullptr;
|
||||||
|
std::unordered_map<std::string, int> tags;
|
||||||
public:
|
public:
|
||||||
ContentUnitDefs<Block> blocks;
|
ContentUnitDefs<Block> blocks;
|
||||||
ContentUnitDefs<ItemDef> items;
|
ContentUnitDefs<ItemDef> items;
|
||||||
@ -195,7 +196,8 @@ public:
|
|||||||
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
UptrsMap<std::string, BlockMaterial> blockMaterials,
|
||||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
|
UptrsMap<std::string, rigging::SkeletonConfig> skeletons,
|
||||||
ResourceIndicesSet resourceIndices,
|
ResourceIndicesSet resourceIndices,
|
||||||
dv::value defaults
|
dv::value defaults,
|
||||||
|
std::unordered_map<std::string, int> tags
|
||||||
);
|
);
|
||||||
~Content();
|
~Content();
|
||||||
|
|
||||||
@ -211,6 +213,14 @@ public:
|
|||||||
return defaults;
|
return defaults;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int getTagIndex(const std::string& tag) const {
|
||||||
|
const auto& found = tags.find(tag);
|
||||||
|
if (found == tags.end()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return found->second;
|
||||||
|
}
|
||||||
|
|
||||||
const rigging::SkeletonConfig* getSkeleton(const std::string& id) const;
|
const rigging::SkeletonConfig* getSkeleton(const std::string& id) const;
|
||||||
const rigging::SkeletonConfig& requireSkeleton(const std::string& id) const;
|
const rigging::SkeletonConfig& requireSkeleton(const std::string& id) const;
|
||||||
const BlockMaterial* findBlockMaterial(const std::string& id) const;
|
const BlockMaterial* findBlockMaterial(const std::string& id) const;
|
||||||
|
|||||||
@ -28,6 +28,9 @@ std::unique_ptr<Content> ContentBuilder::build() {
|
|||||||
// Generating runtime info
|
// Generating runtime info
|
||||||
def.rt.id = blockDefsIndices.size();
|
def.rt.id = blockDefsIndices.size();
|
||||||
def.rt.emissive = *reinterpret_cast<uint32_t*>(def.emission);
|
def.rt.emissive = *reinterpret_cast<uint32_t*>(def.emission);
|
||||||
|
for (const auto& tag : def.tags) {
|
||||||
|
def.rt.tags.insert(tags.add(tag));
|
||||||
|
}
|
||||||
|
|
||||||
if (def.variants) {
|
if (def.variants) {
|
||||||
for (auto& variant : def.variants->variants) {
|
for (auto& variant : def.variants->variants) {
|
||||||
@ -58,7 +61,7 @@ std::unique_ptr<Content> ContentBuilder::build() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
blockDefsIndices.push_back(&def);
|
blockDefsIndices.push_back(&def);
|
||||||
groups->insert(def.defaults.drawGroup); // FIXME
|
groups->insert(def.defaults.drawGroup); // FIXME: variants
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ItemDef*> itemDefsIndices;
|
std::vector<ItemDef*> itemDefsIndices;
|
||||||
@ -93,7 +96,8 @@ std::unique_ptr<Content> ContentBuilder::build() {
|
|||||||
std::move(blockMaterials),
|
std::move(blockMaterials),
|
||||||
std::move(skeletons),
|
std::move(skeletons),
|
||||||
std::move(resourceIndices),
|
std::move(resourceIndices),
|
||||||
std::move(defaults)
|
std::move(defaults),
|
||||||
|
std::move(tags.map)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Now, it's time to resolve foreign keys
|
// Now, it's time to resolve foreign keys
|
||||||
|
|||||||
@ -62,6 +62,27 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct TagsIndices {
|
||||||
|
int nextIndex = 1;
|
||||||
|
std::unordered_map<std::string, int> map;
|
||||||
|
|
||||||
|
int add(const std::string& tag) {
|
||||||
|
const auto& found = map.find(tag);
|
||||||
|
if (found != map.end()) {
|
||||||
|
return found->second;
|
||||||
|
}
|
||||||
|
return map[tag] = nextIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int indexOf(const std::string& tag) {
|
||||||
|
const auto& found = map.find(tag);
|
||||||
|
if (found == map.end()) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return found->second;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
class ContentBuilder {
|
class ContentBuilder {
|
||||||
UptrsMap<std::string, BlockMaterial> blockMaterials;
|
UptrsMap<std::string, BlockMaterial> blockMaterials;
|
||||||
UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
|
UptrsMap<std::string, rigging::SkeletonConfig> skeletons;
|
||||||
@ -74,6 +95,7 @@ public:
|
|||||||
ContentUnitBuilder<GeneratorDef> generators {allNames, ContentType::GENERATOR};
|
ContentUnitBuilder<GeneratorDef> generators {allNames, ContentType::GENERATOR};
|
||||||
ResourceIndicesSet resourceIndices {};
|
ResourceIndicesSet resourceIndices {};
|
||||||
dv::value defaults = nullptr;
|
dv::value defaults = nullptr;
|
||||||
|
TagsIndices tags {};
|
||||||
|
|
||||||
~ContentBuilder();
|
~ContentBuilder();
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#define VC_ENABLE_REFLECTION
|
#define VC_ENABLE_REFLECTION
|
||||||
#include "ContentUnitLoader.hpp"
|
#include "ContentUnitLoader.hpp"
|
||||||
|
#include "ContentLoadingCommons.hpp"
|
||||||
|
|
||||||
#include "../ContentBuilder.hpp"
|
#include "../ContentBuilder.hpp"
|
||||||
#include "coders/json.hpp"
|
#include "coders/json.hpp"
|
||||||
@ -87,20 +88,8 @@ template<> void ContentUnitLoader<Block>::loadUnit(
|
|||||||
Block& def, const std::string& name, const io::path& file
|
Block& def, const std::string& name, const io::path& file
|
||||||
) {
|
) {
|
||||||
auto root = io::read_json(file);
|
auto root = io::read_json(file);
|
||||||
if (def.properties == nullptr) {
|
process_properties(def, name, root);
|
||||||
def.properties = dv::object();
|
process_tags(def, root);
|
||||||
def.properties["name"] = name;
|
|
||||||
}
|
|
||||||
for (auto& [key, value] : root.asObject()) {
|
|
||||||
auto pos = key.rfind('@');
|
|
||||||
if (pos == std::string::npos) {
|
|
||||||
def.properties[key] = value;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
auto field = key.substr(0, pos);
|
|
||||||
auto suffix = key.substr(pos + 1);
|
|
||||||
process_method(def.properties, suffix, field, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (root.has("parent")) {
|
if (root.has("parent")) {
|
||||||
const auto& parentName = root["parent"].asString();
|
const auto& parentName = root["parent"].asString();
|
||||||
|
|||||||
37
src/content/loading/ContentLoadingCommons.hpp
Normal file
37
src/content/loading/ContentLoadingCommons.hpp
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "data/dv.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline void process_properties(T& def, const std::string& name, const dv::value& root) {
|
||||||
|
if (def.properties == nullptr) {
|
||||||
|
def.properties = dv::object();
|
||||||
|
def.properties["name"] = name;
|
||||||
|
}
|
||||||
|
for (auto& [key, value] : root.asObject()) {
|
||||||
|
auto pos = key.rfind('@');
|
||||||
|
if (pos == std::string::npos) {
|
||||||
|
def.properties[key] = value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
auto field = key.substr(0, pos);
|
||||||
|
auto suffix = key.substr(pos + 1);
|
||||||
|
process_method(def.properties, suffix, field, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
inline void process_tags(T& def, const dv::value& root) {
|
||||||
|
if (!root.has("tags")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const auto& tags = root["tags"];
|
||||||
|
for (const auto& tagValue : tags) {
|
||||||
|
if (!tagValue.isString()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
def.tags.push_back(tagValue.asString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
#define VC_ENABLE_REFLECTION
|
#define VC_ENABLE_REFLECTION
|
||||||
#include "ContentUnitLoader.hpp"
|
#include "ContentUnitLoader.hpp"
|
||||||
|
#include "ContentLoadingCommons.hpp"
|
||||||
|
|
||||||
#include "../ContentBuilder.hpp"
|
#include "../ContentBuilder.hpp"
|
||||||
#include "coders/json.hpp"
|
#include "coders/json.hpp"
|
||||||
@ -12,11 +13,13 @@
|
|||||||
|
|
||||||
static debug::Logger logger("item-content-loader");
|
static debug::Logger logger("item-content-loader");
|
||||||
|
|
||||||
|
|
||||||
template<> void ContentUnitLoader<ItemDef>::loadUnit(
|
template<> void ContentUnitLoader<ItemDef>::loadUnit(
|
||||||
ItemDef& def, const std::string& name, const io::path& file
|
ItemDef& def, const std::string& name, const io::path& file
|
||||||
) {
|
) {
|
||||||
auto root = io::read_json(file);
|
auto root = io::read_json(file);
|
||||||
def.properties = root;
|
process_properties(def, name, root);
|
||||||
|
process_tags(def, root);
|
||||||
|
|
||||||
if (root.has("parent")) {
|
if (root.has("parent")) {
|
||||||
const auto& parentName = root["parent"].asString();
|
const auto& parentName = root["parent"].asString();
|
||||||
|
|||||||
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
#include "data/dv.hpp"
|
#include "data/dv.hpp"
|
||||||
#include "typedefs.hpp"
|
#include "typedefs.hpp"
|
||||||
@ -64,11 +66,15 @@ struct ItemDef {
|
|||||||
|
|
||||||
std::string scriptFile;
|
std::string scriptFile;
|
||||||
|
|
||||||
|
std::vector<std::string> tags;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
itemid_t id;
|
itemid_t id;
|
||||||
blockid_t placingBlock;
|
blockid_t placingBlock;
|
||||||
ItemFuncsSet funcsset {};
|
ItemFuncsSet funcsset {};
|
||||||
bool emissive = false;
|
bool emissive = false;
|
||||||
|
|
||||||
|
std::set<int> tags;
|
||||||
} rt {};
|
} rt {};
|
||||||
|
|
||||||
ItemDef(const std::string& name);
|
ItemDef(const std::string& name);
|
||||||
|
|||||||
@ -698,6 +698,15 @@ static int l_reload_script(lua::State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_has_tag(lua::State* L) {
|
||||||
|
if (auto def = require_block(L)) {
|
||||||
|
auto tag = lua::require_string(L, 2);
|
||||||
|
const auto& tags = def->rt.tags;
|
||||||
|
return lua::pushboolean(L, tags.find(content->getTagIndex(tag)) != tags.end());
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg blocklib[] = {
|
const luaL_Reg blocklib[] = {
|
||||||
{"index", lua::wrap<l_index>},
|
{"index", lua::wrap<l_index>},
|
||||||
{"name", lua::wrap<l_get_def>},
|
{"name", lua::wrap<l_get_def>},
|
||||||
@ -737,5 +746,6 @@ const luaL_Reg blocklib[] = {
|
|||||||
{"get_field", lua::wrap<l_get_field>},
|
{"get_field", lua::wrap<l_get_field>},
|
||||||
{"set_field", lua::wrap<l_set_field>},
|
{"set_field", lua::wrap<l_set_field>},
|
||||||
{"reload_script", lua::wrap<l_reload_script>},
|
{"reload_script", lua::wrap<l_reload_script>},
|
||||||
|
{"has_tag", lua::wrap<l_has_tag>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -108,6 +108,15 @@ static int l_reload_script(lua::State* L) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_has_tag(lua::State* L) {
|
||||||
|
if (auto def = get_item_def(L, 1)) {
|
||||||
|
auto tag = lua::require_string(L, 2);
|
||||||
|
const auto& tags = def->rt.tags;
|
||||||
|
return lua::pushboolean(L, tags.find(content->getTagIndex(tag)) != tags.end());
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
const luaL_Reg itemlib[] = {
|
const luaL_Reg itemlib[] = {
|
||||||
{"index", lua::wrap<l_index>},
|
{"index", lua::wrap<l_index>},
|
||||||
{"name", lua::wrap<l_name>},
|
{"name", lua::wrap<l_name>},
|
||||||
@ -121,5 +130,6 @@ const luaL_Reg itemlib[] = {
|
|||||||
{"emission", lua::wrap<l_emission>},
|
{"emission", lua::wrap<l_emission>},
|
||||||
{"uses", lua::wrap<l_uses>},
|
{"uses", lua::wrap<l_uses>},
|
||||||
{"reload_script", lua::wrap<l_reload_script>},
|
{"reload_script", lua::wrap<l_reload_script>},
|
||||||
|
{"has_tag", lua::wrap<l_has_tag>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,8 +1,7 @@
|
|||||||
#include "api_lua.hpp"
|
#include "api_lua.hpp"
|
||||||
|
#include "coders/json.hpp"
|
||||||
#include "engine/Engine.hpp"
|
#include "engine/Engine.hpp"
|
||||||
#include "network/Network.hpp"
|
#include "network/Network.hpp"
|
||||||
#include "coders/json.hpp"
|
|
||||||
|
|
||||||
using namespace scripting;
|
using namespace scripting;
|
||||||
|
|
||||||
@ -134,17 +133,58 @@ static int l_send(lua::State* L, network::Network& network) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int l_udp_server_send_to(lua::State* L, network::Network& network) {
|
||||||
|
u64id_t id = lua::tointeger(L, 1);
|
||||||
|
|
||||||
|
if (auto server = network.getServer(id)) {
|
||||||
|
if (server->getTransportType() != network::TransportType::UDP)
|
||||||
|
throw std::runtime_error("the server must work on UDP transport");
|
||||||
|
|
||||||
|
const std::string& addr = lua::tostring(L, 2);
|
||||||
|
const int& port = lua::tointeger(L, 3);
|
||||||
|
|
||||||
|
auto udpServer = dynamic_cast<network::UdpServer*>(server);
|
||||||
|
|
||||||
|
if (lua::istable(L, 4)) {
|
||||||
|
lua::pushvalue(L, 4);
|
||||||
|
size_t size = lua::objlen(L, 4);
|
||||||
|
util::Buffer<char> buffer(size);
|
||||||
|
for (size_t i = 0; i < size; i++) {
|
||||||
|
lua::rawgeti(L, i + 1);
|
||||||
|
buffer[i] = lua::tointeger(L, -1);
|
||||||
|
lua::pop(L);
|
||||||
|
}
|
||||||
|
lua::pop(L);
|
||||||
|
udpServer->sendTo(addr, port, buffer.data(), size);
|
||||||
|
} else if (lua::isstring(L, 4)) {
|
||||||
|
auto string = lua::tolstring(L, 4);
|
||||||
|
udpServer->sendTo(addr, port, string.data(), string.length());
|
||||||
|
} else {
|
||||||
|
auto string = lua::bytearray_as_string(L, 4);
|
||||||
|
udpServer->sendTo(addr, port, string.data(), string.length());
|
||||||
|
lua::pop(L);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int l_recv(lua::State* L, network::Network& network) {
|
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);
|
auto connection = engine->getNetwork().getConnection(id);
|
||||||
if (connection == nullptr) {
|
|
||||||
|
if (connection == nullptr || connection->getTransportType() != network::TransportType::TCP) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
length = glm::min(length, connection->available());
|
|
||||||
|
auto tcpConnection = dynamic_cast<network::TcpConnection*>(connection);
|
||||||
|
|
||||||
|
length = glm::min(length, tcpConnection->available());
|
||||||
util::Buffer<char> buffer(length);
|
util::Buffer<char> buffer(length);
|
||||||
|
|
||||||
int size = connection->recv(buffer.data(), length);
|
int size = tcpConnection->recv(buffer.data(), length);
|
||||||
if (size == -1) {
|
if (size == -1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -162,38 +202,123 @@ static int l_recv(lua::State* L, network::Network& network) {
|
|||||||
|
|
||||||
static int l_available(lua::State* L, network::Network& network) {
|
static int l_available(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)) {
|
if (auto connection = network.getConnection(id)) {
|
||||||
return lua::pushinteger(L, connection->available());
|
return lua::pushinteger(L, dynamic_cast<network::TcpConnection*>(connection)->available());
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum NetworkEventType {
|
enum NetworkEventType {
|
||||||
CLIENT_CONNECTED = 1,
|
CLIENT_CONNECTED = 1,
|
||||||
CONNECTED_TO_SERVER
|
CONNECTED_TO_SERVER,
|
||||||
|
DATAGRAM
|
||||||
};
|
};
|
||||||
|
|
||||||
struct NetworkEvent {
|
struct NetworkEvent {
|
||||||
NetworkEventType type;
|
NetworkEventType type;
|
||||||
u64id_t server;
|
u64id_t server;
|
||||||
u64id_t client;
|
u64id_t client;
|
||||||
|
|
||||||
|
NetworkEvent(
|
||||||
|
NetworkEventType type,
|
||||||
|
u64id_t server,
|
||||||
|
u64id_t client
|
||||||
|
) {
|
||||||
|
this->type = type;
|
||||||
|
this->server = server;
|
||||||
|
this->client = client;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual ~NetworkEvent() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::vector<NetworkEvent> events_queue {};
|
enum NetworkDatagramSide {
|
||||||
|
ON_SERVER = 1,
|
||||||
|
ON_CLIENT
|
||||||
|
};
|
||||||
|
|
||||||
static int l_connect(lua::State* L, network::Network& network) {
|
struct NetworkDatagramEvent : NetworkEvent {
|
||||||
|
NetworkDatagramSide side;
|
||||||
|
std::string addr;
|
||||||
|
int port;
|
||||||
|
const char* buffer;
|
||||||
|
size_t length;
|
||||||
|
|
||||||
|
NetworkDatagramEvent(
|
||||||
|
NetworkEventType datagram,
|
||||||
|
u64id_t sid,
|
||||||
|
u64id_t cid,
|
||||||
|
NetworkDatagramSide side,
|
||||||
|
const std::string& addr,
|
||||||
|
int port,
|
||||||
|
const char* data,
|
||||||
|
size_t length
|
||||||
|
) : NetworkEvent(DATAGRAM, sid, cid) {
|
||||||
|
this->side = side;
|
||||||
|
this->addr = addr;
|
||||||
|
this->port = port;
|
||||||
|
|
||||||
|
buffer = data;
|
||||||
|
|
||||||
|
this->length = length;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static std::vector<std::unique_ptr<NetworkEvent>> events_queue {};
|
||||||
|
|
||||||
|
static int l_connect_tcp(lua::State* L, network::Network& network) {
|
||||||
std::string address = lua::require_string(L, 1);
|
std::string address = lua::require_string(L, 1);
|
||||||
int port = lua::tointeger(L, 2);
|
int port = lua::tointeger(L, 2);
|
||||||
u64id_t id = network.connect(address, port, [](u64id_t cid) {
|
u64id_t id = network.connectTcp(address, port, [](u64id_t cid) {
|
||||||
events_queue.push_back({CONNECTED_TO_SERVER, 0, cid});
|
events_queue.push_back(std::make_unique<NetworkEvent>(CONNECTED_TO_SERVER, 0, cid));
|
||||||
});
|
});
|
||||||
return lua::pushinteger(L, id);
|
return lua::pushinteger(L, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_open(lua::State* L, network::Network& network) {
|
static int l_open_tcp(lua::State* L, network::Network& network) {
|
||||||
int port = lua::tointeger(L, 1);
|
int port = lua::tointeger(L, 1);
|
||||||
u64id_t id = network.openServer(port, [](u64id_t sid, u64id_t id) {
|
u64id_t id = network.openTcpServer(port, [](u64id_t sid, u64id_t id) {
|
||||||
events_queue.push_back({CLIENT_CONNECTED, sid, id});
|
events_queue.push_back(std::make_unique<NetworkEvent>(CLIENT_CONNECTED, sid, id));
|
||||||
|
});
|
||||||
|
return lua::pushinteger(L, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_connect_udp(lua::State* L, network::Network& network) {
|
||||||
|
std::string address = lua::require_string(L, 1);
|
||||||
|
int port = lua::tointeger(L, 2);
|
||||||
|
u64id_t id = network.connectUdp(address, port, [](u64id_t cid) {
|
||||||
|
events_queue.push_back(std::make_unique<NetworkEvent>(CONNECTED_TO_SERVER, 0, cid));
|
||||||
|
}, [address, port](
|
||||||
|
u64id_t cid,
|
||||||
|
const char* buffer,
|
||||||
|
size_t length
|
||||||
|
) {
|
||||||
|
events_queue.push_back(
|
||||||
|
std::make_unique<NetworkDatagramEvent>(
|
||||||
|
DATAGRAM, 0, cid, ON_CLIENT,
|
||||||
|
address, port, buffer, length
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return lua::pushinteger(L, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int l_open_udp(lua::State* L, network::Network& network) {
|
||||||
|
int port = lua::tointeger(L, 1);
|
||||||
|
u64id_t id = network.openUdpServer(port, [](
|
||||||
|
u64id_t sid,
|
||||||
|
const std::string& addr,
|
||||||
|
int port,
|
||||||
|
const char* buffer,
|
||||||
|
size_t length) {
|
||||||
|
events_queue.push_back(
|
||||||
|
std::make_unique<NetworkDatagramEvent>(
|
||||||
|
DATAGRAM, sid, 0, ON_SERVER,
|
||||||
|
addr, port, buffer, length
|
||||||
|
)
|
||||||
|
);
|
||||||
});
|
});
|
||||||
return lua::pushinteger(L, id);
|
return lua::pushinteger(L, id);
|
||||||
}
|
}
|
||||||
@ -204,7 +329,10 @@ static int l_is_alive(lua::State* L, network::Network& network) {
|
|||||||
return lua::pushboolean(
|
return lua::pushboolean(
|
||||||
L,
|
L,
|
||||||
connection->getState() != network::ConnectionState::CLOSED ||
|
connection->getState() != network::ConnectionState::CLOSED ||
|
||||||
connection->available() > 0
|
(
|
||||||
|
connection->getTransportType() == network::TransportType::TCP &&
|
||||||
|
dynamic_cast<network::TcpConnection*>(connection)->available() > 0
|
||||||
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return lua::pushboolean(L, false);
|
return lua::pushboolean(L, false);
|
||||||
@ -256,18 +384,35 @@ static int l_get_total_download(lua::State* L, network::Network& network) {
|
|||||||
|
|
||||||
static int l_pull_events(lua::State* L, network::Network& network) {
|
static int l_pull_events(lua::State* L, network::Network& network) {
|
||||||
lua::createtable(L, events_queue.size(), 0);
|
lua::createtable(L, events_queue.size(), 0);
|
||||||
for (size_t i = 0; i < events_queue.size(); i++) {
|
|
||||||
lua::createtable(L, 3, 0);
|
|
||||||
|
|
||||||
lua::pushinteger(L, events_queue[i].type);
|
for (size_t i = 0; i < events_queue.size(); i++) {
|
||||||
|
const auto* datagramEvent = dynamic_cast<NetworkDatagramEvent*>(events_queue[i].get());
|
||||||
|
|
||||||
|
lua::createtable(L, datagramEvent ? 7 : 3, 0);
|
||||||
|
|
||||||
|
lua::pushinteger(L, events_queue[i]->type);
|
||||||
lua::rawseti(L, 1);
|
lua::rawseti(L, 1);
|
||||||
|
|
||||||
lua::pushinteger(L, events_queue[i].server);
|
lua::pushinteger(L, events_queue[i]->server);
|
||||||
lua::rawseti(L, 2);
|
lua::rawseti(L, 2);
|
||||||
|
|
||||||
lua::pushinteger(L, events_queue[i].client);
|
lua::pushinteger(L, events_queue[i]->client);
|
||||||
lua::rawseti(L, 3);
|
lua::rawseti(L, 3);
|
||||||
|
|
||||||
|
if (datagramEvent) {
|
||||||
|
lua::pushstring(L, datagramEvent->addr);
|
||||||
|
lua::rawseti(L, 4);
|
||||||
|
|
||||||
|
lua::pushinteger(L, datagramEvent->port);
|
||||||
|
lua::rawseti(L, 5);
|
||||||
|
|
||||||
|
lua::pushinteger(L, datagramEvent->side);
|
||||||
|
lua::rawseti(L, 6);
|
||||||
|
|
||||||
|
lua::create_bytearray(L, datagramEvent->buffer, datagramEvent->length);
|
||||||
|
lua::rawseti(L, 7);
|
||||||
|
}
|
||||||
|
|
||||||
lua::rawseti(L, i + 1);
|
lua::rawseti(L, i + 1);
|
||||||
}
|
}
|
||||||
events_queue.clear();
|
events_queue.clear();
|
||||||
@ -298,9 +443,12 @@ 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>},
|
||||||
{"__pull_events", wrap<l_pull_events>},
|
{"__pull_events", wrap<l_pull_events>},
|
||||||
{"__open", wrap<l_open>},
|
{"__open_tcp", wrap<l_open_tcp>},
|
||||||
|
{"__open_udp", wrap<l_open_udp>},
|
||||||
{"__closeserver", wrap<l_closeserver>},
|
{"__closeserver", wrap<l_closeserver>},
|
||||||
{"__connect", wrap<l_connect>},
|
{"__udp_server_send_to", wrap<l_udp_server_send_to>},
|
||||||
|
{"__connect_tcp", wrap<l_connect_tcp>},
|
||||||
|
{"__connect_udp", wrap<l_connect_udp>},
|
||||||
{"__close", wrap<l_close>},
|
{"__close", wrap<l_close>},
|
||||||
{"__send", wrap<l_send>},
|
{"__send", wrap<l_send>},
|
||||||
{"__recv", wrap<l_recv>},
|
{"__recv", wrap<l_recv>},
|
||||||
|
|||||||
@ -291,7 +291,7 @@ static std::string to_string(const sockaddr_in& addr, bool port=true) {
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
class SocketConnection : public Connection {
|
class SocketTcpConnection : public TcpConnection {
|
||||||
SOCKET descriptor;
|
SOCKET descriptor;
|
||||||
sockaddr_in addr;
|
sockaddr_in addr;
|
||||||
size_t totalUpload = 0;
|
size_t totalUpload = 0;
|
||||||
@ -317,10 +317,10 @@ class SocketConnection : public Connection {
|
|||||||
state = ConnectionState::CONNECTED;
|
state = ConnectionState::CONNECTED;
|
||||||
}
|
}
|
||||||
public:
|
public:
|
||||||
SocketConnection(SOCKET descriptor, sockaddr_in addr)
|
SocketTcpConnection(SOCKET descriptor, sockaddr_in addr)
|
||||||
: descriptor(descriptor), addr(std::move(addr)), buffer(16'384) {}
|
: descriptor(descriptor), addr(std::move(addr)), buffer(16'384) {}
|
||||||
|
|
||||||
~SocketConnection() {
|
~SocketTcpConnection() {
|
||||||
if (state != ConnectionState::CLOSED) {
|
if (state != ConnectionState::CLOSED) {
|
||||||
shutdown(descriptor, 2);
|
shutdown(descriptor, 2);
|
||||||
}
|
}
|
||||||
@ -442,7 +442,7 @@ public:
|
|||||||
return to_string(addr, false);
|
return to_string(addr, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::shared_ptr<SocketConnection> connect(
|
static std::shared_ptr<SocketTcpConnection> connect(
|
||||||
const std::string& address, int port, runnable callback
|
const std::string& address, int port, runnable callback
|
||||||
) {
|
) {
|
||||||
addrinfo hints {};
|
addrinfo hints {};
|
||||||
@ -466,7 +466,7 @@ public:
|
|||||||
if (descriptor == -1) {
|
if (descriptor == -1) {
|
||||||
throw std::runtime_error("Could not create socket");
|
throw std::runtime_error("Could not create socket");
|
||||||
}
|
}
|
||||||
auto socket = std::make_shared<SocketConnection>(descriptor, std::move(serverAddress));
|
auto socket = std::make_shared<SocketTcpConnection>(descriptor, std::move(serverAddress));
|
||||||
socket->connect(std::move(callback));
|
socket->connect(std::move(callback));
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
@ -476,7 +476,7 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class SocketTcpSServer : public TcpServer {
|
class SocketTcpServer : public TcpServer {
|
||||||
u64id_t id;
|
u64id_t id;
|
||||||
Network* network;
|
Network* network;
|
||||||
SOCKET descriptor;
|
SOCKET descriptor;
|
||||||
@ -486,10 +486,10 @@ class SocketTcpSServer : public TcpServer {
|
|||||||
std::unique_ptr<std::thread> thread = nullptr;
|
std::unique_ptr<std::thread> thread = nullptr;
|
||||||
int port;
|
int port;
|
||||||
public:
|
public:
|
||||||
SocketTcpSServer(u64id_t id, Network* network, SOCKET descriptor, int port)
|
SocketTcpServer(u64id_t id, Network* network, SOCKET descriptor, int port)
|
||||||
: id(id), network(network), descriptor(descriptor), port(port) {}
|
: id(id), network(network), descriptor(descriptor), port(port) {}
|
||||||
|
|
||||||
~SocketTcpSServer() {
|
~SocketTcpServer() {
|
||||||
closeSocket();
|
closeSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -510,7 +510,7 @@ public:
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
logger.info() << "client connected: " << to_string(address);
|
logger.info() << "client connected: " << to_string(address);
|
||||||
auto socket = std::make_shared<SocketConnection>(
|
auto socket = std::make_shared<SocketTcpConnection>(
|
||||||
clientDescriptor, address
|
clientDescriptor, address
|
||||||
);
|
);
|
||||||
socket->startClient();
|
socket->startClient();
|
||||||
@ -558,7 +558,7 @@ public:
|
|||||||
return port;
|
return port;
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::shared_ptr<SocketTcpSServer> openServer(
|
static std::shared_ptr<SocketTcpServer> openServer(
|
||||||
u64id_t id, Network* network, int port, ConnectCallback handler
|
u64id_t id, Network* network, int port, ConnectCallback handler
|
||||||
) {
|
) {
|
||||||
SOCKET descriptor = socket(
|
SOCKET descriptor = socket(
|
||||||
@ -586,7 +586,222 @@ public:
|
|||||||
}
|
}
|
||||||
logger.info() << "opened server at port " << port;
|
logger.info() << "opened server at port " << port;
|
||||||
auto server =
|
auto server =
|
||||||
std::make_shared<SocketTcpSServer>(id, network, descriptor, port);
|
std::make_shared<SocketTcpServer>(id, network, descriptor, port);
|
||||||
|
server->startListen(std::move(handler));
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class SocketUdpConnection : public UdpConnection {
|
||||||
|
u64id_t id;
|
||||||
|
SOCKET descriptor;
|
||||||
|
sockaddr_in addr{};
|
||||||
|
bool open = true;
|
||||||
|
std::unique_ptr<std::thread> thread;
|
||||||
|
ClientDatagramCallback callback;
|
||||||
|
|
||||||
|
size_t totalUpload = 0;
|
||||||
|
size_t totalDownload = 0;
|
||||||
|
ConnectionState state = ConnectionState::INITIAL;
|
||||||
|
|
||||||
|
public:
|
||||||
|
SocketUdpConnection(u64id_t id, SOCKET descriptor, sockaddr_in addr)
|
||||||
|
: id(id), descriptor(descriptor), addr(std::move(addr)) {}
|
||||||
|
|
||||||
|
~SocketUdpConnection() override {
|
||||||
|
SocketUdpConnection::close();
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::shared_ptr<SocketUdpConnection> connect(
|
||||||
|
u64id_t id,
|
||||||
|
const std::string& address,
|
||||||
|
int port,
|
||||||
|
ClientDatagramCallback handler,
|
||||||
|
runnable callback
|
||||||
|
) {
|
||||||
|
SOCKET descriptor = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (descriptor == -1) {
|
||||||
|
throw std::runtime_error("could not create UDP socket");
|
||||||
|
}
|
||||||
|
|
||||||
|
sockaddr_in serverAddr{};
|
||||||
|
serverAddr.sin_family = AF_INET;
|
||||||
|
if (inet_pton(AF_INET, address.c_str(), &serverAddr.sin_addr) <= 0) {
|
||||||
|
closesocket(descriptor);
|
||||||
|
throw std::runtime_error("invalid UDP address: " + address);
|
||||||
|
}
|
||||||
|
serverAddr.sin_port = htons(port);
|
||||||
|
|
||||||
|
if (::connect(descriptor, (sockaddr*)&serverAddr, sizeof(serverAddr)) < 0) {
|
||||||
|
auto err = handle_socket_error("UDP connect failed");
|
||||||
|
closesocket(descriptor);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto socket = std::make_shared<SocketUdpConnection>(id, descriptor, serverAddr);
|
||||||
|
socket->connect(std::move(handler));
|
||||||
|
|
||||||
|
callback();
|
||||||
|
|
||||||
|
return socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void connect(ClientDatagramCallback handler) override {
|
||||||
|
callback = std::move(handler);
|
||||||
|
state = ConnectionState::CONNECTED;
|
||||||
|
|
||||||
|
thread = std::make_unique<std::thread>([this]() {
|
||||||
|
util::Buffer<char> buffer(16'384);
|
||||||
|
while (open) {
|
||||||
|
int size = recv(descriptor, buffer.data(), buffer.size(), 0);
|
||||||
|
if (size <= 0) {
|
||||||
|
if (!open) break;
|
||||||
|
closesocket(descriptor);
|
||||||
|
state = ConnectionState::CLOSED;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
totalDownload += size;
|
||||||
|
if (callback) {
|
||||||
|
callback(id, buffer.data(), size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
int send(const char* buffer, size_t length) override {
|
||||||
|
int len = sendto(descriptor, buffer, length, 0,
|
||||||
|
(sockaddr*)&addr, sizeof(addr));
|
||||||
|
if (len < 0) {
|
||||||
|
closesocket(descriptor);
|
||||||
|
state = ConnectionState::CLOSED;
|
||||||
|
} else totalUpload += len;
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
void close(bool discardAll=false) override {
|
||||||
|
if (!open) return;
|
||||||
|
open = false;
|
||||||
|
|
||||||
|
if (state != ConnectionState::CLOSED) {
|
||||||
|
shutdown(descriptor, 2);
|
||||||
|
closesocket(descriptor);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (thread) {
|
||||||
|
thread->join();
|
||||||
|
thread.reset();
|
||||||
|
}
|
||||||
|
state = ConnectionState::CLOSED;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t pullUpload() override {
|
||||||
|
size_t s = totalUpload;
|
||||||
|
totalUpload = 0;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t pullDownload() override {
|
||||||
|
size_t s = totalDownload;
|
||||||
|
totalDownload = 0;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] int getPort() const override {
|
||||||
|
return ntohs(addr.sin_port);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] std::string getAddress() const override {
|
||||||
|
return to_string(addr, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] ConnectionState getState() const override {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class SocketUdpServer : public UdpServer {
|
||||||
|
u64id_t id;
|
||||||
|
Network* network;
|
||||||
|
SOCKET descriptor;
|
||||||
|
bool open = true;
|
||||||
|
std::unique_ptr<std::thread> thread = nullptr;
|
||||||
|
int port;
|
||||||
|
ServerDatagramCallback callback;
|
||||||
|
|
||||||
|
public:
|
||||||
|
SocketUdpServer(u64id_t id, Network* network, SOCKET descriptor, int port)
|
||||||
|
: id(id), network(network), descriptor(descriptor), port(port) {}
|
||||||
|
|
||||||
|
~SocketUdpServer() override {
|
||||||
|
SocketUdpServer::close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void startListen(ServerDatagramCallback handler) {
|
||||||
|
callback = std::move(handler);
|
||||||
|
|
||||||
|
thread = std::make_unique<std::thread>([this]() {
|
||||||
|
util::Buffer<char> buffer(16384);
|
||||||
|
sockaddr_in clientAddr{};
|
||||||
|
socklen_t addrlen = sizeof(clientAddr);
|
||||||
|
|
||||||
|
while (open) {
|
||||||
|
int size = recvfrom(descriptor, buffer.data(), buffer.size(), 0,
|
||||||
|
reinterpret_cast<sockaddr*>(&clientAddr), &addrlen);
|
||||||
|
if (size <= 0) {
|
||||||
|
if (!open) break;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string addrStr = to_string(clientAddr, false);
|
||||||
|
int port = ntohs(clientAddr.sin_port);
|
||||||
|
|
||||||
|
callback(id, addrStr, port, buffer.data(), size);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendTo(const std::string& addr, int port, const char* buffer, size_t length) override {
|
||||||
|
sockaddr_in client{};
|
||||||
|
client.sin_family = AF_INET;
|
||||||
|
inet_pton(AF_INET, addr.c_str(), &client.sin_addr);
|
||||||
|
client.sin_port = htons(port);
|
||||||
|
|
||||||
|
sendto(descriptor, buffer, length, 0,
|
||||||
|
reinterpret_cast<sockaddr*>(&client), sizeof(client));
|
||||||
|
}
|
||||||
|
|
||||||
|
void close() override {
|
||||||
|
if (!open) return;
|
||||||
|
open = false;
|
||||||
|
shutdown(descriptor, 2);
|
||||||
|
closesocket(descriptor);
|
||||||
|
if (thread) {
|
||||||
|
thread->join();
|
||||||
|
thread = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isOpen() override { return open; }
|
||||||
|
int getPort() const override { return port; }
|
||||||
|
|
||||||
|
static std::shared_ptr<SocketUdpServer> openServer(
|
||||||
|
u64id_t id, Network* network, int port, const ServerDatagramCallback& handler
|
||||||
|
) {
|
||||||
|
SOCKET descriptor = socket(AF_INET, SOCK_DGRAM, 0);
|
||||||
|
if (descriptor == -1) throw std::runtime_error("Could not create UDP socket");
|
||||||
|
|
||||||
|
sockaddr_in address{};
|
||||||
|
address.sin_family = AF_INET;
|
||||||
|
address.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
address.sin_port = htons(port);
|
||||||
|
|
||||||
|
if (bind(descriptor, (sockaddr*)&address, sizeof(address)) < 0) {
|
||||||
|
closesocket(descriptor);
|
||||||
|
throw std::runtime_error("Could not bind UDP port " + std::to_string(port));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto server = std::make_shared<SocketUdpServer>(id, network, descriptor, port);
|
||||||
server->startListen(std::move(handler));
|
server->startListen(std::move(handler));
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
@ -627,7 +842,7 @@ Connection* Network::getConnection(u64id_t id) {
|
|||||||
return found->second.get();
|
return found->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
TcpServer* Network::getServer(u64id_t id) const {
|
Server* Network::getServer(u64id_t id) const {
|
||||||
const auto& found = servers.find(id);
|
const auto& found = servers.find(id);
|
||||||
if (found == servers.end()) {
|
if (found == servers.end()) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
@ -635,20 +850,38 @@ TcpServer* Network::getServer(u64id_t id) const {
|
|||||||
return found->second.get();
|
return found->second.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
u64id_t Network::connect(const std::string& address, int port, consumer<u64id_t> callback) {
|
u64id_t Network::connectTcp(const std::string& address, int port, consumer<u64id_t> callback) {
|
||||||
std::lock_guard lock(connectionsMutex);
|
std::lock_guard lock(connectionsMutex);
|
||||||
|
|
||||||
u64id_t id = nextConnection++;
|
u64id_t id = nextConnection++;
|
||||||
auto socket = SocketConnection::connect(address, port, [id, callback]() {
|
auto socket = SocketTcpConnection::connect(address, port, [id, callback]() {
|
||||||
callback(id);
|
callback(id);
|
||||||
});
|
});
|
||||||
connections[id] = std::move(socket);
|
connections[id] = std::move(socket);
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64id_t Network::openServer(int port, ConnectCallback handler) {
|
u64id_t Network::openTcpServer(int port, ConnectCallback handler) {
|
||||||
u64id_t id = nextServer++;
|
u64id_t id = nextServer++;
|
||||||
auto server = SocketTcpSServer::openServer(id, this, port, handler);
|
auto server = SocketTcpServer::openServer(id, this, port, handler);
|
||||||
|
servers[id] = std::move(server);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64id_t Network::connectUdp(const std::string& address, int port, const consumer<u64id_t>& callback, ClientDatagramCallback handler) {
|
||||||
|
std::lock_guard lock(connectionsMutex);
|
||||||
|
|
||||||
|
u64id_t id = nextConnection++;
|
||||||
|
auto socket = SocketUdpConnection::connect(id, address, port, std::move(handler), [id, callback]() {
|
||||||
|
callback(id);
|
||||||
|
});
|
||||||
|
connections[id] = std::move(socket);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64id_t Network::openUdpServer(int port, const ServerDatagramCallback& handler) {
|
||||||
|
u64id_t id = nextServer++;
|
||||||
|
auto server = SocketUdpServer::openServer(id, this, port, handler);
|
||||||
servers[id] = std::move(server);
|
servers[id] = std::move(server);
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@ -679,7 +912,10 @@ void Network::update() {
|
|||||||
auto socket = socketiter->second.get();
|
auto socket = socketiter->second.get();
|
||||||
totalDownload += socket->pullDownload();
|
totalDownload += socket->pullDownload();
|
||||||
totalUpload += socket->pullUpload();
|
totalUpload += socket->pullUpload();
|
||||||
if (socket->available() == 0 &&
|
if (
|
||||||
|
( socket->getTransportType() == TransportType::UDP ||
|
||||||
|
dynamic_cast<TcpConnection*>(socket)->available() == 0
|
||||||
|
) &&
|
||||||
socket->getState() == ConnectionState::CLOSED) {
|
socket->getState() == ConnectionState::CLOSED) {
|
||||||
socketiter = connections.erase(socketiter);
|
socketiter = connections.erase(socketiter);
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@ -13,6 +13,9 @@ namespace network {
|
|||||||
using OnResponse = std::function<void(std::vector<char>)>;
|
using OnResponse = std::function<void(std::vector<char>)>;
|
||||||
using OnReject = std::function<void(int)>;
|
using OnReject = std::function<void(int)>;
|
||||||
using ConnectCallback = std::function<void(u64id_t, u64id_t)>;
|
using ConnectCallback = std::function<void(u64id_t, u64id_t)>;
|
||||||
|
using ServerDatagramCallback = std::function<void(u64id_t sid, const std::string& addr, int port, const char* buffer, size_t length)>;
|
||||||
|
using ClientDatagramCallback = std::function<void(u64id_t cid, const char* buffer, size_t length)>;
|
||||||
|
|
||||||
|
|
||||||
class Requests {
|
class Requests {
|
||||||
public:
|
public:
|
||||||
@ -33,8 +36,8 @@ namespace network {
|
|||||||
long maxSize=0
|
long maxSize=0
|
||||||
) = 0;
|
) = 0;
|
||||||
|
|
||||||
virtual size_t getTotalUpload() const = 0;
|
[[nodiscard]] virtual size_t getTotalUpload() const = 0;
|
||||||
virtual size_t getTotalDownload() const = 0;
|
[[nodiscard]] virtual size_t getTotalDownload() const = 0;
|
||||||
|
|
||||||
virtual void update() = 0;
|
virtual void update() = 0;
|
||||||
};
|
};
|
||||||
@ -43,32 +46,82 @@ namespace network {
|
|||||||
INITIAL, CONNECTING, CONNECTED, CLOSED
|
INITIAL, CONNECTING, CONNECTED, CLOSED
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class TransportType {
|
||||||
|
TCP, UDP
|
||||||
|
};
|
||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
public:
|
public:
|
||||||
virtual ~Connection() {}
|
virtual ~Connection() = default;
|
||||||
|
|
||||||
virtual void connect(runnable callback) = 0;
|
|
||||||
virtual int recv(char* buffer, size_t length) = 0;
|
|
||||||
virtual int send(const char* buffer, size_t length) = 0;
|
|
||||||
virtual void close(bool discardAll=false) = 0;
|
virtual void close(bool discardAll=false) = 0;
|
||||||
virtual int available() = 0;
|
|
||||||
|
virtual int send(const char* buffer, size_t length) = 0;
|
||||||
|
|
||||||
virtual size_t pullUpload() = 0;
|
virtual size_t pullUpload() = 0;
|
||||||
virtual size_t pullDownload() = 0;
|
virtual size_t pullDownload() = 0;
|
||||||
|
|
||||||
virtual int getPort() const = 0;
|
[[nodiscard]] virtual int getPort() const = 0;
|
||||||
virtual std::string getAddress() const = 0;
|
[[nodiscard]] virtual std::string getAddress() const = 0;
|
||||||
|
|
||||||
virtual ConnectionState getState() const = 0;
|
[[nodiscard]] virtual ConnectionState getState() const = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] virtual TransportType getTransportType() const noexcept = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class TcpServer {
|
class TcpConnection : public Connection {
|
||||||
public:
|
public:
|
||||||
virtual ~TcpServer() {}
|
~TcpConnection() override = default;
|
||||||
virtual void startListen(ConnectCallback handler) = 0;
|
|
||||||
|
virtual void connect(runnable callback) = 0;
|
||||||
|
virtual int recv(char* buffer, size_t length) = 0;
|
||||||
|
virtual int available() = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] TransportType getTransportType() const noexcept override {
|
||||||
|
return TransportType::TCP;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class UdpConnection : public Connection {
|
||||||
|
public:
|
||||||
|
~UdpConnection() override = default;
|
||||||
|
|
||||||
|
virtual void connect(ClientDatagramCallback handler) = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] TransportType getTransportType() const noexcept override {
|
||||||
|
return TransportType::UDP;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Server {
|
||||||
|
public:
|
||||||
|
virtual ~Server() = default;
|
||||||
virtual void close() = 0;
|
virtual void close() = 0;
|
||||||
virtual bool isOpen() = 0;
|
virtual bool isOpen() = 0;
|
||||||
virtual int getPort() const = 0;
|
[[nodiscard]] virtual TransportType getTransportType() const noexcept = 0;
|
||||||
|
[[nodiscard]] virtual int getPort() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
class TcpServer : public Server {
|
||||||
|
public:
|
||||||
|
~TcpServer() override {}
|
||||||
|
virtual void startListen(ConnectCallback handler) = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] TransportType getTransportType() const noexcept override {
|
||||||
|
return TransportType::TCP;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class UdpServer : public Server {
|
||||||
|
public:
|
||||||
|
~UdpServer() override {}
|
||||||
|
virtual void startListen(ServerDatagramCallback handler) = 0;
|
||||||
|
|
||||||
|
virtual void sendTo(const std::string& addr, int port, const char* buffer, size_t length) = 0;
|
||||||
|
|
||||||
|
[[nodiscard]] TransportType getTransportType() const noexcept override {
|
||||||
|
return TransportType::UDP;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class Network {
|
class Network {
|
||||||
@ -78,7 +131,7 @@ namespace network {
|
|||||||
std::mutex connectionsMutex {};
|
std::mutex connectionsMutex {};
|
||||||
u64id_t nextConnection = 1;
|
u64id_t nextConnection = 1;
|
||||||
|
|
||||||
std::unordered_map<u64id_t, std::shared_ptr<TcpServer>> servers;
|
std::unordered_map<u64id_t, std::shared_ptr<Server>> servers;
|
||||||
u64id_t nextServer = 1;
|
u64id_t nextServer = 1;
|
||||||
|
|
||||||
size_t totalDownload = 0;
|
size_t totalDownload = 0;
|
||||||
@ -103,16 +156,18 @@ namespace network {
|
|||||||
);
|
);
|
||||||
|
|
||||||
[[nodiscard]] Connection* getConnection(u64id_t id);
|
[[nodiscard]] Connection* getConnection(u64id_t id);
|
||||||
[[nodiscard]] TcpServer* getServer(u64id_t id) const;
|
[[nodiscard]] Server* getServer(u64id_t id) const;
|
||||||
|
|
||||||
u64id_t connect(const std::string& address, int port, consumer<u64id_t> callback);
|
u64id_t connectTcp(const std::string& address, int port, consumer<u64id_t> callback);
|
||||||
|
u64id_t connectUdp(const std::string& address, int port, const consumer<u64id_t>& callback, ClientDatagramCallback handler);
|
||||||
|
|
||||||
u64id_t openServer(int port, ConnectCallback handler);
|
u64id_t openTcpServer(int port, ConnectCallback handler);
|
||||||
|
u64id_t openUdpServer(int port, const ServerDatagramCallback& handler);
|
||||||
|
|
||||||
u64id_t addConnection(const std::shared_ptr<Connection>& connection);
|
u64id_t addConnection(const std::shared_ptr<Connection>& connection);
|
||||||
|
|
||||||
size_t getTotalUpload() const;
|
[[nodiscard]] size_t getTotalUpload() const;
|
||||||
size_t getTotalDownload() const;
|
[[nodiscard]] size_t getTotalDownload() const;
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
|
|||||||
@ -4,6 +4,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <set>
|
||||||
|
|
||||||
#include "data/dv.hpp"
|
#include "data/dv.hpp"
|
||||||
#include "maths/UVRegion.hpp"
|
#include "maths/UVRegion.hpp"
|
||||||
@ -261,6 +262,8 @@ public:
|
|||||||
|
|
||||||
std::unique_ptr<Variants> variants;
|
std::unique_ptr<Variants> variants;
|
||||||
|
|
||||||
|
std::vector<std::string> tags;
|
||||||
|
|
||||||
/// @brief Runtime indices (content indexing results)
|
/// @brief Runtime indices (content indexing results)
|
||||||
struct {
|
struct {
|
||||||
/// @brief block runtime integer id
|
/// @brief block runtime integer id
|
||||||
@ -285,6 +288,8 @@ public:
|
|||||||
itemid_t pickingItem = 0;
|
itemid_t pickingItem = 0;
|
||||||
|
|
||||||
blockid_t surfaceReplacement = 0;
|
blockid_t surfaceReplacement = 0;
|
||||||
|
|
||||||
|
std::set<int> tags;
|
||||||
} rt {};
|
} rt {};
|
||||||
|
|
||||||
Block(const std::string& name);
|
Block(const std::string& name);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user