浏览代码

Merge pull request #3 from martinfelis/master

Added license information, functions and documentation
leaf 12 年之前
父节点
当前提交
13d56ed8e3
共有 2 个文件被更改,包括 231 次插入1 次删除
  1. 60 0
      docs/index.md
  2. 171 1
      enet.c

+ 60 - 0
docs/index.md

@@ -157,6 +157,31 @@ maximum allowable value is used.
 ### `host:bandwidth_limit(incoming, outgoing)`
 ### `host:bandwidth_limit(incoming, outgoing)`
 Sets the bandwidth limits of the host in bytes/sec. Set to `0` for unlimited.
 Sets the bandwidth limits of the host in bytes/sec. Set to `0` for unlimited.
 
 
+### `host:total_sent_data()`
+Returns the number of bytes that were sent through the given host.
+
+### `host:total_received_data()`
+Returns the number of bytes that were received by the given host. 
+
+### `host:peer_count()`
+Returns the number of peers that are allocated for the given host. This
+represents the maximum number of possible connections.
+
+### `host:connected_peer_count()`
+Returns the number of connected (i.e. active) peers.
+
+### `host:get_peer(index)`
+Returns the connected peer at the specified index (starting at 1). Please
+note that it is not guaranteed that the index stays the same for a single
+peer over time as enet pre-allocates memory in an array for all peers and
+sets their state to `connected` when a connection is established.
+
+For peers with closed connections the internal state is set to
+`disconnected`. The function `host:get_peer(i)` loops over all connections
+and returns the i-th connection that is active. If a peer with index j and
+j < i is disconnected all peers with index i and i > j have now index i -
+1.
+
 ### `peer:send(data [, channel, flag])`
 ### `peer:send(data [, channel, flag])`
 Queues a packet to be sent to peer. `data` is the contents of the packet, it
 Queues a packet to be sent to peer. `data` is the contents of the packet, it
 must be a Lua string.
 must be a Lua string.
@@ -208,6 +233,41 @@ Parameters:
 Attempts to dequeue an incoming packet for this peer.
 Attempts to dequeue an incoming packet for this peer.
 Returns `nil` if there are no packets waiting. Otherwise returns two values:
 Returns `nil` if there are no packets waiting. Otherwise returns two values:
 the string representing the packet data, and the channel the packet came from.
 the string representing the packet data, and the channel the packet came from.
+
+### `peer:round_trip_time(value)`
+Returns or sets the current round trip time (i.e. ping). If value is nil
+the current value of the peer is returned. Otherwise the value roundTripTime
+is set to the specified value and returned.
+
+Enet performs some filtering on
+the round trip times and it takes some time until the parameters are
+accurate.
+
+### `peer:round_trip_time(value)`
+Returns or sets the round trip time of the previous round trip time
+computation. If value is nil the current value of the peer is returned.
+Otherwise the value lastRoundTripTime is set to the specified value and
+returned. 
+
+Enet performs some filtering on the round trip times and it takes
+some time until the parameters are accurate.
+
+### `limit, minimum, maximum peer:timeout(limit, minimum, maximum)`
+Returns or sets the parameters when a timeout is detected. This is happens
+either after a fixed timeout or a variable timeout of time that takes the
+round trip time into account. The former is specified with the `maximum`
+parameter.
+
+Parameters:
+
+  * `limit` a factor that is multiplied with a value that based on the 
+	average round trip time to compute the timeout limit
+  * `minimum` timeout value in milliseconds that a reliable packet has to
+	be acknowledged if the variable timeout limit was exceeded
+  * `maximum` fixed timeout in milliseconds for which any packet has to be acknowledged
+
+See official enet documentation for detailed description.
+
 </div>
 </div>
 
 
 <a name="license"></a>
 <a name="license"></a>

+ 171 - 1
enet.c

@@ -1,3 +1,25 @@
+/**
+ *
+ * Copyright (C) 2011 by Leaf Corcoran
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * 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
+ * THE SOFTWARE.
+ */
 
 
 #include <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
@@ -197,7 +219,9 @@ static int host_create(lua_State *l) {
 			channel_count, in_bandwidth, out_bandwidth);
 			channel_count, in_bandwidth, out_bandwidth);
 
 
 	if (host == NULL) {
 	if (host == NULL) {
-		return luaL_error(l, "Failed to create host");
+		lua_pushnil (l);
+		lua_pushstring(l, "enet: failed to create host (already listening?)");
+		return 2;
 	}
 	}
 
 
 	*(ENetHost**)lua_newuserdata(l, sizeof(void*)) = host;
 	*(ENetHost**)lua_newuserdata(l, sizeof(void*)) = host;
@@ -311,6 +335,79 @@ static int host_bandwidth_limit(lua_State *l) {
 	return 0;
 	return 0;
 }
 }
 
 
+static int host_total_sent_data(lua_State *l) {
+	ENetHost *host = check_host(l, 1);
+
+	lua_pushinteger (l, host->totalSentData);
+
+	return 1;
+}
+
+static int host_total_received_data(lua_State *l) {
+	ENetHost *host = check_host(l, 1);
+
+	lua_pushinteger (l, host->totalReceivedData);
+
+	return 1;
+}
+static int host_service_time(lua_State *l) {
+	ENetHost *host = check_host(l, 1);
+
+	lua_pushinteger (l, host->serviceTime);
+
+	return 1;
+}
+
+static int host_peer_count(lua_State *l) {
+	ENetHost *host = check_host(l, 1);
+
+	lua_pushinteger (l, host->peerCount);
+
+	return 1;
+}
+
+static int host_connected_peer_count(lua_State *l) {
+	ENetHost *host = check_host(l, 1);
+	unsigned int connected_peer_count = 0;
+
+	ENetPeer *peer;
+	for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer) {
+		if (peer->state != ENET_PEER_STATE_CONNECTED 
+				&& peer->state != ENET_PEER_STATE_DISCONNECT_LATER)
+			continue;
+
+		connected_peer_count++;
+	}
+
+	lua_pushinteger (l, connected_peer_count);
+
+	return 1;
+}
+
+static int host_get_peer(lua_State *l) {
+	ENetHost *host = check_host(l, 1);
+
+	unsigned int peer_index = luaL_checkint(l, 2) - 1;
+	ENetPeer *peer;
+
+	for (peer = host->peers; peer < &host->peers[host->peerCount]; ++peer) {
+		if (peer->state != ENET_PEER_STATE_CONNECTED 
+				&& peer->state != ENET_PEER_STATE_DISCONNECT_LATER)
+			continue;
+
+		if (peer_index == 0)
+			break;
+
+		peer_index --;
+	}
+
+	if (peer_index > 0)
+		return 0;
+
+	push_peer (l, peer);
+	return 1;
+}
+
 static int host_gc(lua_State *l) {
 static int host_gc(lua_State *l) {
 	ENetHost *host = check_host(l, 1);
 	ENetHost *host = check_host(l, 1);
 	enet_host_destroy(host);
 	enet_host_destroy(host);
@@ -346,6 +443,69 @@ static int peer_throttle_configure(lua_State *l) {
 	return 0;
 	return 0;
 }
 }
 
 
+static int peer_round_trip_time(lua_State *l) {
+	ENetPeer *peer = check_peer(l, 1);
+
+	if (lua_gettop(l) > 1) {
+		enet_uint32 round_trip_time = luaL_checkint(l, 2);
+		peer->roundTripTime = round_trip_time;
+	}
+
+	lua_pushinteger (l, peer->roundTripTime);
+
+	return 1;
+}
+
+static int peer_last_round_trip_time(lua_State *l) {
+	ENetPeer *peer = check_peer(l, 1);
+
+	if (lua_gettop(l) > 1) {
+		enet_uint32 round_trip_time = luaL_checkint(l, 2);
+		peer->lastRoundTripTime = round_trip_time;
+	}
+	lua_pushinteger (l, peer->lastRoundTripTime);
+
+	return 1;
+}
+
+static int peer_ping_interval(lua_State *l) {
+	ENetPeer *peer = check_peer(l, 1);
+
+	if (lua_gettop(l) > 1) {
+		enet_uint32 interval = luaL_checkint(l, 2);
+		enet_peer_ping_interval (peer, interval);
+	}
+
+	lua_pushinteger (l, peer->pingInterval);
+
+	return 1;
+}
+
+static int peer_timeout(lua_State *l) {
+	ENetPeer *peer = check_peer(l, 1);
+
+	enet_uint32 timeout_limit = 0;
+	enet_uint32 timeout_minimum = 0;
+	enet_uint32 timeout_maximum = 0;
+
+	switch (lua_gettop(l)) {
+		case 4:
+			if (!lua_isnil(l, 4)) timeout_maximum = luaL_checkint(l, 4);
+		case 3:
+			if (!lua_isnil(l, 3)) timeout_minimum = luaL_checkint(l, 3);
+		case 2:
+			if (!lua_isnil(l, 2)) timeout_limit = luaL_checkint(l, 2);
+	}
+
+	enet_peer_timeout (peer, timeout_limit, timeout_minimum, timeout_maximum);
+
+	lua_pushinteger (l, peer->timeoutLimit);
+	lua_pushinteger (l, peer->timeoutMinimum);
+	lua_pushinteger (l, peer->timeoutMaximum);
+
+	return 3;
+}
+
 static int peer_disconnect(lua_State *l) {
 static int peer_disconnect(lua_State *l) {
 	ENetPeer *peer = check_peer(l, 1);
 	ENetPeer *peer = check_peer(l, 1);
 
 
@@ -428,6 +588,12 @@ static const struct luaL_Reg enet_host_funcs [] = {
 	{"flush", host_flush},
 	{"flush", host_flush},
 	{"channel_limit", host_channel_limit},
 	{"channel_limit", host_channel_limit},
 	{"bandwidth_limit", host_bandwidth_limit},
 	{"bandwidth_limit", host_bandwidth_limit},
+	{"total_sent_data", host_total_sent_data},
+	{"total_received_data", host_total_received_data},
+	{"service_time", host_service_time},
+	{"peer_count", host_peer_count},
+	{"connected_peer_count", host_connected_peer_count},
+	{"get_peer", host_get_peer},
 	{NULL, NULL}
 	{NULL, NULL}
 };
 };
 
 
@@ -440,6 +606,10 @@ static const struct luaL_Reg enet_peer_funcs [] = {
 	{"receive", peer_receive},
 	{"receive", peer_receive},
 	{"send", peer_send},
 	{"send", peer_send},
 	{"throttle_configure", peer_throttle_configure},
 	{"throttle_configure", peer_throttle_configure},
+	{"round_trip_time", peer_round_trip_time},
+	{"last_round_trip_time", peer_last_round_trip_time},
+	{"ping_interval", peer_ping_interval},
+	{"timeout", peer_timeout},
 	{NULL, NULL}
 	{NULL, NULL}
 };
 };