udp support
This commit is contained in:
@@ -134,17 +134,58 @@ static int l_send(lua::State* L, network::Network& network) {
|
||||
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) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
int length = lua::tointeger(L, 2);
|
||||
|
||||
auto connection = engine->getNetwork().getConnection(id);
|
||||
if (connection == nullptr) {
|
||||
|
||||
if (connection == nullptr || connection->getTransportType() != network::TransportType::TCP) {
|
||||
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);
|
||||
|
||||
int size = connection->recv(buffer.data(), length);
|
||||
int size = tcpConnection->recv(buffer.data(), length);
|
||||
if (size == -1) {
|
||||
return 0;
|
||||
}
|
||||
@@ -162,15 +203,18 @@ static int l_recv(lua::State* L, network::Network& network) {
|
||||
|
||||
static int l_available(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
return lua::pushinteger(L, connection->available());
|
||||
return lua::pushinteger(L, dynamic_cast<network::TcpConnection*>(connection)->available());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum NetworkEventType {
|
||||
CLIENT_CONNECTED = 1,
|
||||
CONNECTED_TO_SERVER
|
||||
CONNECTED_TO_SERVER,
|
||||
DATAGRAM
|
||||
};
|
||||
|
||||
struct NetworkEvent {
|
||||
@@ -179,32 +223,96 @@ struct NetworkEvent {
|
||||
u64id_t client;
|
||||
};
|
||||
|
||||
enum NetworkDatagramSide {
|
||||
ON_SERVER = 1,
|
||||
ON_CLIENT
|
||||
};
|
||||
|
||||
struct NetworkDatagramEvent : NetworkEvent {
|
||||
NetworkDatagramSide side;
|
||||
std::string addr;
|
||||
int port;
|
||||
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
|
||||
);
|
||||
};
|
||||
|
||||
static std::vector<NetworkEvent> events_queue {};
|
||||
|
||||
static int l_connect(lua::State* L, network::Network& network) {
|
||||
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.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});
|
||||
});
|
||||
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);
|
||||
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});
|
||||
});
|
||||
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({CONNECTED_TO_SERVER, 0, cid});
|
||||
}, [address, port](
|
||||
u64id_t cid,
|
||||
const char* buffer,
|
||||
size_t length
|
||||
) {
|
||||
events_queue.push_back(
|
||||
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(
|
||||
NetworkDatagramEvent{
|
||||
DATAGRAM, sid, 0, ON_SERVER,
|
||||
addr, port, buffer, length
|
||||
}
|
||||
);
|
||||
});
|
||||
return lua::pushinteger(L, id);
|
||||
}
|
||||
|
||||
static int l_is_alive(lua::State* L, network::Network& network) {
|
||||
u64id_t id = lua::tointeger(L, 1);
|
||||
if (auto connection = network.getConnection(id)) {
|
||||
return lua::pushboolean(
|
||||
L,
|
||||
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);
|
||||
@@ -267,6 +375,22 @@ static int l_pull_events(lua::State* L, network::Network& network) {
|
||||
|
||||
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);
|
||||
lua::rawseti(L, 4);
|
||||
|
||||
lua::pushinteger(L, de.port);
|
||||
lua::rawseti(L, 5);
|
||||
|
||||
lua::pushinteger(L, de.side);
|
||||
lua::rawseti(L, 6);
|
||||
|
||||
lua::pushvalue(L, lua::create_bytearray(L, de.buffer, de.length));
|
||||
lua::rawseti(L, 7);
|
||||
}
|
||||
|
||||
lua::rawseti(L, i + 1);
|
||||
}
|
||||
@@ -298,9 +422,12 @@ const luaL_Reg networklib[] = {
|
||||
{"get_total_upload", wrap<l_get_total_upload>},
|
||||
{"get_total_download", wrap<l_get_total_download>},
|
||||
{"__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>},
|
||||
{"__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>},
|
||||
{"__send", wrap<l_send>},
|
||||
{"__recv", wrap<l_recv>},
|
||||
|
||||
Reference in New Issue
Block a user