mirror of
https://github.com/love2d/love.git
synced 2026-08-20 12:40:20 +02:00
Added function host:destroy(), added error handling for trying to work with a destroyed host, and fixed awkward naming of socket_get_address (it now matches the rest of the lib and official documentation as get_socket_address now).
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright (C) 2011 by Leaf Corcoran
|
* Copyright (C) 2014 by Leaf Corcoran
|
||||||
*
|
*
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
@@ -272,6 +272,9 @@ static int linked_version(lua_State *l) {
|
|||||||
*/
|
*/
|
||||||
static int host_service(lua_State *l) {
|
static int host_service(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
ENetEvent event;
|
ENetEvent event;
|
||||||
int timeout = 0, out;
|
int timeout = 0, out;
|
||||||
|
|
||||||
@@ -291,6 +294,9 @@ static int host_service(lua_State *l) {
|
|||||||
*/
|
*/
|
||||||
static int host_check_events(lua_State *l) {
|
static int host_check_events(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
ENetEvent event;
|
ENetEvent event;
|
||||||
int out = enet_host_check_events(host, &event);
|
int out = enet_host_check_events(host, &event);
|
||||||
if (out == 0) return 0;
|
if (out == 0) return 0;
|
||||||
@@ -306,6 +312,9 @@ static int host_check_events(lua_State *l) {
|
|||||||
*/
|
*/
|
||||||
static int host_compress_with_range_coder(lua_State *l) {
|
static int host_compress_with_range_coder(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
|
|
||||||
int result = enet_host_compress_with_range_coder (host);
|
int result = enet_host_compress_with_range_coder (host);
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
@@ -326,6 +335,9 @@ static int host_compress_with_range_coder(lua_State *l) {
|
|||||||
*/
|
*/
|
||||||
static int host_connect(lua_State *l) {
|
static int host_connect(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
ENetAddress address;
|
ENetAddress address;
|
||||||
ENetPeer *peer;
|
ENetPeer *peer;
|
||||||
|
|
||||||
@@ -355,12 +367,18 @@ static int host_connect(lua_State *l) {
|
|||||||
|
|
||||||
static int host_flush(lua_State *l) {
|
static int host_flush(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
enet_host_flush(host);
|
enet_host_flush(host);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int host_broadcast(lua_State *l) {
|
static int host_broadcast(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
|
|
||||||
enet_uint8 channel_id;
|
enet_uint8 channel_id;
|
||||||
ENetPacket *packet = read_packet(l, 2, &channel_id);
|
ENetPacket *packet = read_packet(l, 2, &channel_id);
|
||||||
@@ -371,6 +389,9 @@ static int host_broadcast(lua_State *l) {
|
|||||||
// Args: limit:number
|
// Args: limit:number
|
||||||
static int host_channel_limit(lua_State *l) {
|
static int host_channel_limit(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
int limit = luaL_checkint(l, 2);
|
int limit = luaL_checkint(l, 2);
|
||||||
enet_host_channel_limit(host, limit);
|
enet_host_channel_limit(host, limit);
|
||||||
return 0;
|
return 0;
|
||||||
@@ -378,14 +399,20 @@ static int host_channel_limit(lua_State *l) {
|
|||||||
|
|
||||||
static int host_bandwidth_limit(lua_State *l) {
|
static int host_bandwidth_limit(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
enet_uint32 in_bandwidth = luaL_checkint(l, 2);
|
enet_uint32 in_bandwidth = luaL_checkint(l, 2);
|
||||||
enet_uint32 out_bandwidth = luaL_checkint(l, 2);
|
enet_uint32 out_bandwidth = luaL_checkint(l, 2);
|
||||||
enet_host_bandwidth_limit(host, in_bandwidth, out_bandwidth);
|
enet_host_bandwidth_limit(host, in_bandwidth, out_bandwidth);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int host_socket_get_address(lua_State *l) {
|
static int host_get_socket_address(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
ENetAddress address;
|
ENetAddress address;
|
||||||
enet_socket_get_address (host->socket, &address);
|
enet_socket_get_address (host->socket, &address);
|
||||||
|
|
||||||
@@ -399,6 +426,9 @@ static int host_socket_get_address(lua_State *l) {
|
|||||||
}
|
}
|
||||||
static int host_total_sent_data(lua_State *l) {
|
static int host_total_sent_data(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
|
|
||||||
lua_pushinteger (l, host->totalSentData);
|
lua_pushinteger (l, host->totalSentData);
|
||||||
|
|
||||||
@@ -407,6 +437,9 @@ static int host_total_sent_data(lua_State *l) {
|
|||||||
|
|
||||||
static int host_total_received_data(lua_State *l) {
|
static int host_total_received_data(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
|
|
||||||
lua_pushinteger (l, host->totalReceivedData);
|
lua_pushinteger (l, host->totalReceivedData);
|
||||||
|
|
||||||
@@ -414,6 +447,9 @@ static int host_total_received_data(lua_State *l) {
|
|||||||
}
|
}
|
||||||
static int host_service_time(lua_State *l) {
|
static int host_service_time(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
|
|
||||||
lua_pushinteger (l, host->serviceTime);
|
lua_pushinteger (l, host->serviceTime);
|
||||||
|
|
||||||
@@ -422,6 +458,9 @@ static int host_service_time(lua_State *l) {
|
|||||||
|
|
||||||
static int host_peer_count(lua_State *l) {
|
static int host_peer_count(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
|
|
||||||
lua_pushinteger (l, host->peerCount);
|
lua_pushinteger (l, host->peerCount);
|
||||||
|
|
||||||
@@ -430,6 +469,9 @@ static int host_peer_count(lua_State *l) {
|
|||||||
|
|
||||||
static int host_get_peer(lua_State *l) {
|
static int host_get_peer(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
ENetHost *host = check_host(l, 1);
|
||||||
|
if (!host) {
|
||||||
|
return luaL_error(l, "Tried to index a nil host!");
|
||||||
|
}
|
||||||
|
|
||||||
int peer_index = luaL_checkint(l, 2) - 1;
|
int peer_index = luaL_checkint(l, 2) - 1;
|
||||||
|
|
||||||
@@ -444,8 +486,13 @@ static int host_get_peer(lua_State *l) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int host_gc(lua_State *l) {
|
static int host_gc(lua_State *l) {
|
||||||
ENetHost *host = check_host(l, 1);
|
// We have to manually grab the userdata so that we can set it to NULL.
|
||||||
enet_host_destroy(host);
|
ENetHost** host = luaL_checkudata(l, 1, "enet_host");
|
||||||
|
// We don't want to crash by destroying a non-existant host.
|
||||||
|
if (*host) {
|
||||||
|
enet_host_destroy(*host);
|
||||||
|
}
|
||||||
|
*host = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -684,7 +731,14 @@ static const struct luaL_Reg enet_host_funcs [] = {
|
|||||||
{"broadcast", host_broadcast},
|
{"broadcast", host_broadcast},
|
||||||
{"channel_limit", host_channel_limit},
|
{"channel_limit", host_channel_limit},
|
||||||
{"bandwidth_limit", host_bandwidth_limit},
|
{"bandwidth_limit", host_bandwidth_limit},
|
||||||
{"socket_get_address", host_socket_get_address},
|
// Since ENetSocket isn't part of enet-lua, we should try to keep
|
||||||
|
// naming conventions the same as the rest of the lib.
|
||||||
|
{"get_socket_address", host_get_socket_address},
|
||||||
|
// TODO: Remove the line below in future versions, it's for backward
|
||||||
|
// compatibility only.
|
||||||
|
{"socket_get_address", host_get_socket_address},
|
||||||
|
// We need this function to free up our ports when needed!
|
||||||
|
{"destroy", host_gc},
|
||||||
|
|
||||||
// additional convenience functions (mostly accessors)
|
// additional convenience functions (mostly accessors)
|
||||||
{"total_sent_data", host_total_sent_data},
|
{"total_sent_data", host_total_sent_data},
|
||||||
|
|||||||
Reference in New Issue
Block a user