fixes
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
#include "api_lua.hpp"
|
||||
#include <iostream>
|
||||
|
||||
#include "api_lua.hpp"
|
||||
#include "coders/json.hpp"
|
||||
#include "engine/Engine.hpp"
|
||||
#include "network/Network.hpp"
|
||||
#include "coders/json.hpp"
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
@@ -221,6 +222,18 @@ struct NetworkEvent {
|
||||
NetworkEventType type;
|
||||
u64id_t server;
|
||||
u64id_t client;
|
||||
|
||||
NetworkEvent(
|
||||
NetworkEventType type,
|
||||
u64id_t server,
|
||||
u64id_t client
|
||||
) {
|
||||
this->type = type;
|
||||
this->server = server;
|
||||
this->client = client;
|
||||
}
|
||||
|
||||
virtual ~NetworkEvent() = default;
|
||||
};
|
||||
|
||||
enum NetworkDatagramSide {
|
||||
@@ -232,10 +245,11 @@ struct NetworkDatagramEvent : NetworkEvent {
|
||||
NetworkDatagramSide side;
|
||||
std::string addr;
|
||||
int port;
|
||||
char* buffer;
|
||||
const char* buffer;
|
||||
size_t length;
|
||||
|
||||
NetworkDatagramEvent(NetworkEventType datagram,
|
||||
NetworkDatagramEvent(
|
||||
NetworkEventType datagram,
|
||||
u64id_t sid,
|
||||
u64id_t cid,
|
||||
NetworkDatagramSide side,
|
||||
@@ -243,16 +257,24 @@ struct NetworkDatagramEvent : NetworkEvent {
|
||||
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<NetworkEvent> events_queue {};
|
||||
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);
|
||||
int port = lua::tointeger(L, 2);
|
||||
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);
|
||||
}
|
||||
@@ -260,7 +282,7 @@ static int l_connect_tcp(lua::State* L, network::Network& network) {
|
||||
static int l_open_tcp(lua::State* L, network::Network& network) {
|
||||
int port = lua::tointeger(L, 1);
|
||||
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);
|
||||
}
|
||||
@@ -269,17 +291,17 @@ 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({CONNECTED_TO_SERVER, 0, 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(
|
||||
NetworkDatagramEvent{
|
||||
std::make_unique<NetworkDatagramEvent>(
|
||||
DATAGRAM, 0, cid, ON_CLIENT,
|
||||
address, port, buffer, length
|
||||
}
|
||||
)
|
||||
);
|
||||
});
|
||||
return lua::pushinteger(L, id);
|
||||
@@ -294,10 +316,10 @@ static int l_open_udp(lua::State* L, network::Network& network) {
|
||||
const char* buffer,
|
||||
size_t length) {
|
||||
events_queue.push_back(
|
||||
NetworkDatagramEvent{
|
||||
std::make_unique<NetworkDatagramEvent>(
|
||||
DATAGRAM, sid, 0, ON_SERVER,
|
||||
addr, port, buffer, length
|
||||
}
|
||||
)
|
||||
);
|
||||
});
|
||||
return lua::pushinteger(L, id);
|
||||
@@ -364,31 +386,32 @@ static int l_get_total_download(lua::State* L, network::Network& network) {
|
||||
|
||||
static int l_pull_events(lua::State* L, network::Network& network) {
|
||||
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::pushinteger(L, events_queue[i].server);
|
||||
lua::pushinteger(L, events_queue[i]->server);
|
||||
lua::rawseti(L, 2);
|
||||
|
||||
lua::pushinteger(L, events_queue[i].client);
|
||||
lua::pushinteger(L, events_queue[i]->client);
|
||||
lua::rawseti(L, 3);
|
||||
|
||||
if (typeid(events_queue[i]) == typeid(NetworkDatagramEvent)) {
|
||||
const NetworkDatagramEvent& de = (NetworkDatagramEvent&) events_queue[i];
|
||||
|
||||
lua::pushstring(L, de.addr);
|
||||
if (datagramEvent) {
|
||||
lua::pushstring(L, datagramEvent->addr);
|
||||
lua::rawseti(L, 4);
|
||||
|
||||
lua::pushinteger(L, de.port);
|
||||
lua::pushinteger(L, datagramEvent->port);
|
||||
lua::rawseti(L, 5);
|
||||
|
||||
lua::pushinteger(L, de.side);
|
||||
lua::pushinteger(L, datagramEvent->side);
|
||||
lua::rawseti(L, 6);
|
||||
|
||||
lua::pushvalue(L, lua::create_bytearray(L, de.buffer, de.length));
|
||||
lua::create_bytearray(L, datagramEvent->buffer, datagramEvent->length);
|
||||
lua::rawseti(L, 7);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user