|
@@ -17,7 +17,7 @@
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING IN
|
|
* THE SOFTWARE.
|
|
* THE SOFTWARE.
|
|
*/
|
|
*/
|
|
|
|
|
|
@@ -27,7 +27,6 @@
|
|
#include "lua.h"
|
|
#include "lua.h"
|
|
#include "lualib.h"
|
|
#include "lualib.h"
|
|
#include "lauxlib.h"
|
|
#include "lauxlib.h"
|
|
-
|
|
|
|
#include <enet/enet.h>
|
|
#include <enet/enet.h>
|
|
|
|
|
|
#define check_host(l, idx)\
|
|
#define check_host(l, idx)\
|
|
@@ -186,6 +185,7 @@ static ENetPacket *read_packet(lua_State *l, int idx, enet_uint8 *channel_id) {
|
|
luaL_error(l, "Unknown packet flag: %s", flag_str);
|
|
luaL_error(l, "Unknown packet flag: %s", flag_str);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
if (argc >= idx+1 && !lua_isnil(l, idx+1)) {
|
|
if (argc >= idx+1 && !lua_isnil(l, idx+1)) {
|
|
*channel_id = luaL_checkint(l, idx+1);
|
|
*channel_id = luaL_checkint(l, idx+1);
|
|
}
|
|
}
|
|
@@ -250,6 +250,26 @@ static int host_create(lua_State *l) {
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+static int linked_version(lua_State *l) {
|
|
|
|
+ char* enet_version_str = (char*) malloc (sizeof(char) * 32);
|
|
|
|
+
|
|
|
|
+ if (enet_version_str == NULL) {
|
|
|
|
+ luaL_error(l, "Error allocating memory for version string.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ snprintf (enet_version_str, sizeof(char) * 32, "%d.%d.%d",
|
|
|
|
+ ENET_VERSION_GET_MAJOR(enet_linked_version()),
|
|
|
|
+ ENET_VERSION_GET_MINOR(enet_linked_version()),
|
|
|
|
+ ENET_VERSION_GET_PATCH(enet_linked_version())
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ lua_pushstring (l, enet_version_str);
|
|
|
|
+
|
|
|
|
+ free (enet_version_str);
|
|
|
|
+
|
|
|
|
+ return 1;
|
|
|
|
+}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* Serice a host
|
|
* Serice a host
|
|
* Args:
|
|
* Args:
|
|
@@ -373,6 +393,29 @@ static int host_bandwidth_limit(lua_State *l) {
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+static int host_socket_get_address(lua_State *l) {
|
|
|
|
+ ENetHost *host = check_host(l, 1);
|
|
|
|
+ ENetAddress address;
|
|
|
|
+ enet_socket_get_address (host->socket, &address);
|
|
|
|
+
|
|
|
|
+ char* address_str = (char *) malloc(sizeof(char) * 64);
|
|
|
|
+ if (address_str == NULL) {
|
|
|
|
+ luaL_error(l, "Error allocating memory for address string.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ snprintf (address_str, sizeof(char) * 64, "%d.%d.%d.%d:%d",
|
|
|
|
+ ((address.host) & 0xFF),
|
|
|
|
+ ((address.host >> 8) & 0xFF),
|
|
|
|
+ ((address.host >> 16) & 0xFF),
|
|
|
|
+ (address.host >> 24& 0xFF),
|
|
|
|
+ address.port);
|
|
|
|
+
|
|
|
|
+ lua_pushstring(l, address_str);
|
|
|
|
+
|
|
|
|
+ free (address_str);
|
|
|
|
+
|
|
|
|
+ return 1;
|
|
|
|
+}
|
|
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);
|
|
|
|
|
|
@@ -407,7 +450,7 @@ 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);
|
|
|
|
|
|
- int peer_index = luaL_checkint(l, 2) - 1;
|
|
|
|
|
|
+ size_t peer_index = (size_t) luaL_checkint(l, 2) - 1;
|
|
|
|
|
|
if (peer_index < 0 || peer_index >= host->peerCount) {
|
|
if (peer_index < 0 || peer_index >= host->peerCount) {
|
|
luaL_argerror (l, 2, "Invalid peer index");
|
|
luaL_argerror (l, 2, "Invalid peer index");
|
|
@@ -647,6 +690,7 @@ static int peer_send(lua_State *l) {
|
|
|
|
|
|
static const struct luaL_Reg enet_funcs [] = {
|
|
static const struct luaL_Reg enet_funcs [] = {
|
|
{"host_create", host_create},
|
|
{"host_create", host_create},
|
|
|
|
+ {"linked_version", linked_version},
|
|
{NULL, NULL}
|
|
{NULL, NULL}
|
|
};
|
|
};
|
|
|
|
|
|
@@ -659,6 +703,7 @@ 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},
|
|
|
|
|
|
// additional convenience functions (mostly accessors)
|
|
// additional convenience functions (mostly accessors)
|
|
{"total_sent_data", host_total_sent_data},
|
|
{"total_sent_data", host_total_sent_data},
|
|
@@ -694,7 +739,7 @@ static const struct luaL_Reg enet_event_funcs [] = {
|
|
{NULL, NULL}
|
|
{NULL, NULL}
|
|
};
|
|
};
|
|
|
|
|
|
-LUALIB_API int luaopen_enet(lua_State *l) {
|
|
|
|
|
|
+int luaopen_enet(lua_State *l) {
|
|
enet_initialize();
|
|
enet_initialize();
|
|
atexit(enet_deinitialize);
|
|
atexit(enet_deinitialize);
|
|
|
|
|