fix 'resume dead coroutine' for client
This commit is contained in:
parent
aa3d1bb2e3
commit
47c97ccd46
@ -46,14 +46,6 @@ 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,
|
||||||
}}
|
}}
|
||||||
|
|
||||||
network.tcp_connect = function(address, port, callback)
|
|
||||||
local socket = setmetatable({id=0}, Socket)
|
|
||||||
socket.id = network.__connect(address, port, function(id)
|
|
||||||
callback(socket)
|
|
||||||
end)
|
|
||||||
return socket
|
|
||||||
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,
|
||||||
@ -62,34 +54,57 @@ local ServerSocket = {__index={
|
|||||||
|
|
||||||
|
|
||||||
local _tcp_server_callbacks = {}
|
local _tcp_server_callbacks = {}
|
||||||
|
local _tcp_client_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(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))
|
||||||
end
|
end
|
||||||
return socket
|
return socket
|
||||||
end
|
end
|
||||||
|
|
||||||
network.__pull_events = function()
|
network.tcp_connect = function(address, port, callback)
|
||||||
local cleaned = false
|
local socket = setmetatable({id=0}, Socket)
|
||||||
local connections = network.__pull_connections()
|
socket.id = network.__connect(address, port)
|
||||||
for i, pair in ipairs(connections) do
|
_tcp_client_callbacks[socket.id] = function() callback(socket) end
|
||||||
local sid, cid = unpack(pair)
|
return socket
|
||||||
local callback = _tcp_server_callbacks[sid]
|
end
|
||||||
|
|
||||||
if callback then
|
network.__process_events = function()
|
||||||
callback(cid)
|
local CLIENT_CONNECTED = 1
|
||||||
|
local CONNECTED_TO_SERVER = 2
|
||||||
|
|
||||||
|
local cleaned = false
|
||||||
|
local events = network.__pull_events()
|
||||||
|
for i, event in ipairs(events) do
|
||||||
|
local etype, sid, cid = unpack(event)
|
||||||
|
|
||||||
|
if etype == CLIENT_CONNECTED then
|
||||||
|
local callback = _tcp_server_callbacks[sid]
|
||||||
|
if callback then
|
||||||
|
callback(cid)
|
||||||
|
end
|
||||||
|
elseif etype == CONNECTED_TO_SERVER then
|
||||||
|
local callback = _tcp_client_callbacks[cid]
|
||||||
|
if callback then
|
||||||
|
callback()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- remove dead servers
|
-- remove dead servers
|
||||||
if not cleaned then
|
if not cleaned then
|
||||||
for sid, callback in pairs(_tcp_server_callbacks) do
|
for sid, _ in pairs(_tcp_server_callbacks) do
|
||||||
if not network.__is_serveropen(sid) then
|
if not network.__is_serveropen(sid) then
|
||||||
_tcp_server_callbacks[sid] = nil
|
_tcp_server_callbacks[sid] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
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
|
||||||
|
|||||||
@ -36,7 +36,7 @@ local function complete_app_lib(app)
|
|||||||
app.set_setting = core.set_setting
|
app.set_setting = core.set_setting
|
||||||
app.tick = function()
|
app.tick = function()
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
network.__pull_events()
|
network.__process_events()
|
||||||
end
|
end
|
||||||
app.get_version = core.get_version
|
app.get_version = core.get_version
|
||||||
app.get_setting_info = core.get_setting_info
|
app.get_setting_info = core.get_setting_info
|
||||||
|
|||||||
@ -89,20 +89,6 @@ static int l_post(lua::State* L, network::Network& network) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_connect(lua::State* L, network::Network& network) {
|
|
||||||
std::string address = lua::require_string(L, 1);
|
|
||||||
int port = lua::tointeger(L, 2);
|
|
||||||
lua::pushvalue(L, 3);
|
|
||||||
auto callback = lua::create_lambda_nothrow(L);
|
|
||||||
u64id_t id = network.connect(address, port, [callback](u64id_t id) {
|
|
||||||
engine->postRunnable([=]() {
|
|
||||||
callback({id});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
return lua::pushinteger(L, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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)) {
|
if (auto connection = network.getConnection(id)) {
|
||||||
@ -182,17 +168,32 @@ static int l_available(lua::State* L, network::Network& network) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ConnectionEvent {
|
enum NetworkEventType {
|
||||||
|
CLIENT_CONNECTED = 1,
|
||||||
|
CONNECTED_TO_SERVER
|
||||||
|
};
|
||||||
|
|
||||||
|
struct NetworkEvent {
|
||||||
|
NetworkEventType type;
|
||||||
u64id_t server;
|
u64id_t server;
|
||||||
u64id_t client;
|
u64id_t client;
|
||||||
};
|
};
|
||||||
|
|
||||||
static std::vector<ConnectionEvent> clients_queue {};
|
static std::vector<NetworkEvent> events_queue {};
|
||||||
|
|
||||||
|
static int l_connect(lua::State* L, network::Network& network) {
|
||||||
|
std::string address = lua::require_string(L, 1);
|
||||||
|
int port = lua::tointeger(L, 2);
|
||||||
|
u64id_t id = network.connect(address, port, [](u64id_t cid) {
|
||||||
|
events_queue.push_back({CONNECTED_TO_SERVER, 0, cid});
|
||||||
|
});
|
||||||
|
return lua::pushinteger(L, id);
|
||||||
|
}
|
||||||
|
|
||||||
static int l_open(lua::State* L, network::Network& network) {
|
static int l_open(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.openServer(port, [](u64id_t sid, u64id_t id) {
|
||||||
clients_queue.push_back({sid, id});
|
events_queue.push_back({CLIENT_CONNECTED, sid, id});
|
||||||
});
|
});
|
||||||
return lua::pushinteger(L, id);
|
return lua::pushinteger(L, id);
|
||||||
}
|
}
|
||||||
@ -253,20 +254,23 @@ static int l_get_total_download(lua::State* L, network::Network& network) {
|
|||||||
return lua::pushinteger(L, network.getTotalDownload());
|
return lua::pushinteger(L, network.getTotalDownload());
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_pull_connections(lua::State* L, network::Network& network) {
|
static int l_pull_events(lua::State* L, network::Network& network) {
|
||||||
lua::createtable(L, clients_queue.size(), 0);
|
lua::createtable(L, events_queue.size(), 0);
|
||||||
for (size_t i = 0; i < clients_queue.size(); i++) {
|
for (size_t i = 0; i < events_queue.size(); i++) {
|
||||||
lua::createtable(L, 2, 0);
|
lua::createtable(L, 3, 0);
|
||||||
|
|
||||||
lua::pushinteger(L, clients_queue[i].server);
|
lua::pushinteger(L, events_queue[i].type);
|
||||||
lua::rawseti(L, 1);
|
lua::rawseti(L, 1);
|
||||||
|
|
||||||
lua::pushinteger(L, clients_queue[i].client);
|
lua::pushinteger(L, events_queue[i].server);
|
||||||
lua::rawseti(L, 2);
|
lua::rawseti(L, 2);
|
||||||
|
|
||||||
|
lua::pushinteger(L, events_queue[i].client);
|
||||||
|
lua::rawseti(L, 3);
|
||||||
|
|
||||||
lua::rawseti(L, i + 1);
|
lua::rawseti(L, i + 1);
|
||||||
}
|
}
|
||||||
clients_queue.clear();
|
events_queue.clear();
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,7 +297,7 @@ const luaL_Reg networklib[] = {
|
|||||||
{"post", wrap<l_post>},
|
{"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>},
|
||||||
{"__pull_connections", wrap<l_pull_connections>},
|
{"__pull_events", wrap<l_pull_events>},
|
||||||
{"__open", wrap<l_open>},
|
{"__open", wrap<l_open>},
|
||||||
{"__closeserver", wrap<l_closeserver>},
|
{"__closeserver", wrap<l_closeserver>},
|
||||||
{"__connect", wrap<l_connect>},
|
{"__connect", wrap<l_connect>},
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user