فهرست منبع

Merge pull request #8 from martinfelis/master

added functions from enet 1.3.8
leaf 12 سال پیش
والد
کامیت
8773a7f6a9
2فایلهای تغییر یافته به همراه55 افزوده شده و 4 حذف شده
  1. 6 0
      docs/index.md
  2. 49 4
      enet.c

+ 6 - 0
docs/index.md

@@ -202,6 +202,12 @@ stores all peers in an array of the corresponding host and re-uses unused
 peers for new connections. You can query the state of a peer using 
 [peer:state](#peerstate).
 
+### `host:get_socket_address()`
+
+Returns a string that describes the socket address of the given host. The
+string is formatted as "a.b.c.d:port", where "a.b.c.d" is the ip address of
+the used socket.
+
 ### `peer:connect_id()`
 
 Returns the field ENetPeer::connectID that is assigned for each

+ 49 - 4
enet.c

@@ -17,7 +17,7 @@
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  * 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.
  */
 
@@ -27,7 +27,6 @@
 #include "lua.h"
 #include "lualib.h"
 #include "lauxlib.h"
-
 #include <enet/enet.h>
 
 #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);
 		}
 	}
+
 	if (argc >= idx+1 && !lua_isnil(l, idx+1)) {
 		*channel_id = luaL_checkint(l, idx+1);
 	}
@@ -250,6 +250,26 @@ static int host_create(lua_State *l) {
 	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
  * Args:
@@ -373,6 +393,29 @@ static int host_bandwidth_limit(lua_State *l) {
 	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) {
 	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) {
 	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) {
 		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 [] = {
 	{"host_create", host_create},
+	{"linked_version", linked_version},
 	{NULL, NULL}
 };
 
@@ -659,6 +703,7 @@ static const struct luaL_Reg enet_host_funcs [] = {
 	{"broadcast", host_broadcast},
 	{"channel_limit", host_channel_limit},
 	{"bandwidth_limit", host_bandwidth_limit},
+	{"socket_get_address", host_socket_get_address},
 
 	// additional convenience functions (mostly accessors)
 	{"total_sent_data", host_total_sent_data},
@@ -694,7 +739,7 @@ static const struct luaL_Reg enet_event_funcs [] = {
 	{NULL, NULL}
 };
 
-LUALIB_API int luaopen_enet(lua_State *l) {
+int luaopen_enet(lua_State *l) {
 	enet_initialize();
 	atexit(enet_deinitialize);