add error callback to network.tcp_connect

This commit is contained in:
MihailRis
2025-11-15 12:46:36 +03:00
parent 553ee8b1f2
commit f8be3e74f1
6 changed files with 79 additions and 17 deletions
+12 -1
View File
@@ -13,11 +13,13 @@ enum NetworkEventType {
CONNECTED_TO_SERVER,
DATAGRAM,
RESPONSE,
CONNECTION_ERROR,
};
struct ConnectionEventDto {
u64id_t server;
u64id_t client;
std::string comment {};
};
struct ResponseEventDto {
@@ -283,6 +285,11 @@ static int l_connect_tcp(lua::State* L, network::Network& network) {
CONNECTED_TO_SERVER,
ConnectionEventDto {0, cid}
));
}, [](u64id_t cid, std::string errorMessage) {
push_event(NetworkEvent(
CONNECTION_ERROR,
ConnectionEventDto {0, cid, std::move(errorMessage)}
));
});
return lua::pushinteger(L, id);
}
@@ -439,7 +446,8 @@ static int l_pull_events(lua::State* L, network::Network& network) {
const auto& event = local_queue[i];
switch (event.type) {
case CLIENT_CONNECTED:
case CONNECTED_TO_SERVER: {
case CONNECTED_TO_SERVER:
case CONNECTION_ERROR: {
const auto& dto = std::get<ConnectionEventDto>(event.payload);
lua::pushinteger(L, event.type);
lua::rawseti(L, 1);
@@ -449,6 +457,9 @@ static int l_pull_events(lua::State* L, network::Network& network) {
lua::pushinteger(L, dto.client);
lua::rawseti(L, 3);
lua::pushlstring(L, dto.comment);
lua::rawseti(L, 4);
break;
}
case DATAGRAM: {