Explorar o código

all the peer functions, channels

leaf corcoran %!s(int64=14) %!d(string=hai) anos
pai
achega
39a91e105a
Modificáronse 8 ficheiros con 179 adicións e 14 borrados
  1. 2 1
      .gitignore
  2. 6 0
      Makefile
  3. 56 1
      docs/docs.md
  4. 64 12
      enet.c
  5. 24 0
      test/channels/client.lua
  6. 27 0
      test/channels/server.lua
  7. 0 0
      test/simple/client.lua
  8. 0 0
      test/simple/server.lua

+ 2 - 1
.gitignore

@@ -1,2 +1,3 @@
-enet.so
+*.so
+*.o
 /*.lua

+ 6 - 0
Makefile

@@ -1,3 +1,9 @@
+all: enet.so docs/index.html
+
 enet.so: enet.c
 	luarocks make --local
 
+docs/index.html: docs/docs.md
+	markdown $< -o $@
+
+

+ 56 - 1
docs/docs.md

@@ -110,9 +110,19 @@ Parameters:
  * `out_bandwidth` upstream bandwidth in bytes/sec, defaults to `0`
    (unlimited)
 
+### `host:connect(address [, channels_count, data])`
+Connects a host to a remote host. Returns peer object associated with remote host.
 
-### `host:service([timeout])`
+The actual connection will not take place until the next `host:service` done,
+in which a `"connect"` event will be generated.
+
+`channel_count` is the number of channels to allocate. It should be the same as
+the channel count on the server. Defaults to `1`.
+
+`data` is an integer value that can be associated with the connect event.
+Defaults to `0`.
 
+### `host:service([timeout])`
 Wait for events, send and receive any ready packets. `timeout` is the max
 number of milliseconds to be wait for an event. By default `timeout` is `0`.
 
@@ -125,6 +135,51 @@ which is one of `"connect"`, `"disconnect"`, `"receive"`. Events also have a
 A receive event also has a `data` entry which is a Lua string containing the
 data received.
 
+### `host:flush()`
+Sends any queued packets. This is only required to send packets earlier than
+the next call to `host:service`, or if `host:service` will not be called again.
+
+### `peer:disconnect([data])`
+Requests a disconnection from the peer. The message is sent on next
+`host:service` or `host:flush`.
+
+`data` is optional integer value to be associated with the disconnect.
+
+### `peer:disconnect_now([data])`
+Force immediate disconnection from peer. Foreign peer not guaranteed to receive
+disconnect notification.
+
+`data` is optional integer value to be associated with the disconnect.
+
+### `peer:disconnect_later([data])`
+Request a disconnection from peer, but only after all queued outgoing packets
+are sent.
+
+`data` is optional integer value to be associated with the disconnect.
+
+### `peer:reset()`
+Forcefully disconnects peer. The peer is not notified of the disconnection.
+
+### `peer:ping()`
+Send a ping request to peer, updates `round_trip_time`. This is called
+automatically at regular intervals.
+
+### `peer:throttle_configure(interval, acceleration, decceleration)`
+Changes the probability at which unreliable packets should not be dropped.
+
+Parameters:
+
+ * `interval` interval in milliseconds to measure lowest mean RTT
+ * `acceleration` rate at which to increase throttle probability as mean RTT
+   declines
+ * `decceleration` rate at which to decrease throttle probability as mean RTT
+   increases
+
+### `peer:receive()`
+Attempts to dequeue an incoming packet for this peer.
+
+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.
 
 <a name="contact"></a>
 ## Contact

+ 64 - 12
enet.c

@@ -131,15 +131,17 @@ static int host_create(lua_State *l) {
 
 	switch (lua_gettop(l)) {
 		case 5:
-			out_bandwidth = luaL_checkint(l, 5);
+			if (!lua_isnil(l, 5)) out_bandwidth = luaL_checkint(l, 5);
 		case 4:
-			in_bandwidth = luaL_checkint(l, 4);
+			if (!lua_isnil(l, 4)) in_bandwidth = luaL_checkint(l, 4);
 		case 3:
-			channel_count = luaL_checkint(l, 3);
+			if (!lua_isnil(l, 3)) channel_count = luaL_checkint(l, 3);
 		case 2:
-			peer_count = luaL_checkint(l, 2);
+			if (!lua_isnil(l, 2)) peer_count = luaL_checkint(l, 2);
 	}
 
+	printf("host create, peers=%d, channels=%d, in=%d, out=%d\n",
+			peer_count, channel_count, in_bandwidth, out_bandwidth);
 	ENetHost *host = enet_host_create(have_address ? &address : NULL, peer_count,
 			channel_count, in_bandwidth, out_bandwidth);
 
@@ -186,12 +188,21 @@ static int host_service(lua_State *l) {
 			lua_pushstring(l, "connect");
 			break;
 		case ENET_EVENT_TYPE_DISCONNECT:
+			lua_pushinteger(l, event.data);
+			lua_setfield(l, -2, "data");
+
 			lua_pushstring(l, "disconnect");
 			break;
 		case ENET_EVENT_TYPE_RECEIVE:
 			lua_pushlstring(l, (const char *)event.packet->data, event.packet->dataLength);
 			lua_setfield(l, -2, "data");
+
+			lua_pushinteger(l, event.channelID);
+			lua_setfield(l, -2, "channel");
+
 			lua_pushstring(l, "receive");
+
+			enet_packet_destroy(event.packet);
 			break;
 		case ENET_EVENT_TYPE_NONE: break;
 	}
@@ -214,15 +225,19 @@ static int host_connect(lua_State *l) {
 	parse_address(l, luaL_checkstring(l, 2), &address);
 
 	switch (lua_gettop(l)) {
+		case 4:
+			if (!lua_isnil(l, 4)) data = luaL_checkint(l, 4);
+		case 3:
+			if (!lua_isnil(l, 3)) channel_count = luaL_checkint(l, 3);
 	}
 
+	printf("host connect, channels=%d, data=%d\n", channel_count, data);
 	ENetPeer *peer = enet_host_connect(host, &address, channel_count, data);
 
 	if (peer == NULL) {
 		return luaL_error(l, "Failed to create peer");
 	}
 
-
 	push_peer(l, peer);
 
 	return 1;
@@ -262,18 +277,47 @@ static int peer_tostring(lua_State *l) {
 	return 1;
 }
 
-static int peer_disconnect(lua_State *l) {
+static int peer_ping(lua_State *l) {
 	ENetPeer *peer = check_peer(l, 1);
+	enet_peer_ping(peer);
+	return 0;
+}
 
-	enet_uint32 data = 0;
-	if (lua_gettop(l) > 1) {
-		data = luaL_checkint(l, 2);
-	}
+static int peer_throttle_configure(lua_State *l) {
+	ENetPeer *peer = check_peer(l, 1);
+
+	enet_uint32 interval = luaL_checkint(l, 2);
+	enet_uint32 acceleration = luaL_checkint(l, 3);
+	enet_uint32 decceleration = luaL_checkint(l, 4);
 
+	enet_peer_throttle_configure(peer, interval, acceleration, decceleration);
+	return 0;
+}
+
+static int peer_disconnect(lua_State *l) {
+	ENetPeer *peer = check_peer(l, 1);
+
+	enet_uint32 data = lua_gettop(l) > 1 ? luaL_checkint(l, 2) : 0;
 	enet_peer_disconnect(peer, data);
 	return 0;
 }
 
+static int peer_disconnect_now(lua_State *l) {
+	ENetPeer *peer = check_peer(l, 1);
+
+	enet_uint32 data = lua_gettop(l) > 1 ? luaL_checkint(l, 2) : 0;
+	enet_peer_disconnect_now(peer, data);
+	return 0;
+}
+
+static int peer_disconnect_later(lua_State *l) {
+	ENetPeer *peer = check_peer(l, 1);
+
+	enet_uint32 data = lua_gettop(l) > 1 ? luaL_checkint(l, 2) : 0;
+	enet_peer_disconnect_later(peer, data);
+	return 0;
+}
+
 static int peer_reset(lua_State *l) {
 	ENetPeer *peer = check_peer(l, 1);
 	enet_peer_reset(peer);
@@ -288,9 +332,14 @@ static int peer_receive(lua_State *l) {
 		channel_id = luaL_checkint(l, 2);
 	}
 
-	ENetPacket *packet = enet_peer_receive(peer, channel_id);
+	ENetPacket *packet = enet_peer_receive(peer, &channel_id);
 	if (packet == NULL) return 0;
-	return 0;
+
+	lua_pushlstring(l, (const char *)packet->data, packet->dataLength);
+	lua_pushinteger(l, channel_id);
+
+	enet_packet_destroy(packet);
+	return 2;
 }
 
 
@@ -327,7 +376,10 @@ static const struct luaL_Reg enet_host_funcs [] = {
 
 static const struct luaL_Reg enet_peer_funcs [] = {
 	{"disconnect", peer_disconnect},
+	{"disconnect_now", peer_disconnect_now},
+	{"disconnect_later", peer_disconnect_later},
 	{"reset", peer_reset},
+	{"ping", peer_ping},
 	{"receive", peer_receive},
 	{"send", peer_send},
 	{NULL, NULL}

+ 24 - 0
test/channels/client.lua

@@ -0,0 +1,24 @@
+
+require "enet"
+
+local host = enet.host_create()
+local server = host:connect("localhost:7890", 2)
+
+local done = false
+while not done do
+	local event = host:service(100)
+	if event then
+		if event.type == "connect" then
+			server:send("wally world", 0)
+			server:send("nut house", 1)
+		elseif event.type == "receive" then
+			print(event.type, event.data)
+			done = true
+		end
+	end
+end
+
+server:disconnect()
+host:flush()
+
+print "done"

+ 27 - 0
test/channels/server.lua

@@ -0,0 +1,27 @@
+
+require "enet"
+
+local channel_responders = {
+	[0] = function(event)
+		print("doing nothing")
+	end,
+	[1] = function(event)
+		print("sending back...")
+		event.peer:send(event.data, event.channel)
+	end
+}
+
+local host = enet.host_create("localhost:7890", nil, 2)
+
+while true do
+	local event = host:service(100)
+	if event then
+		if event.type == "receive" then
+			print("receive, channel:", event.channel)
+			channel_responders[event.channel](event)
+		else
+			print(event.type, event.peer)
+		end
+	end
+end
+

+ 0 - 0
test/client.lua → test/simple/client.lua


+ 0 - 0
test/server.lua → test/simple/server.lua