瀏覽代碼

added packet flags

leaf corcoran 14 年之前
父節點
當前提交
0e6f82f3c1
共有 4 個文件被更改,包括 59 次插入3 次删除
  1. 11 0
      docs/docs.md
  2. 14 3
      enet.c
  3. 21 0
      test/unreliable/client.lua
  4. 13 0
      test/unreliable/server.lua

+ 11 - 0
docs/docs.md

@@ -139,6 +139,17 @@ data received.
 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:send(data [, channel, flag])`
+Queues  a packet to be sent to dir. `data` is the contents of the packet, it
+must be a Lua string.
+
+`channel` is the channel to send the packet on. Defaults to `0`.
+
+`flag` is one of `"reliable"`, `"unsequenced"`, or `"unreliable"`. Reliable
+packets are guaranteed to arrive, and arrive in the order in which they are sent.
+Unsequenced packets are unreliable and have no guarantee on the order they
+arrive. Defaults to reliable.
+
 ### `peer:disconnect([data])`
 Requests a disconnection from the peer. The message is sent on next
 `host:service` or `host:flush`.

+ 14 - 3
enet.c

@@ -96,9 +96,20 @@ static ENetPacket *read_packet(lua_State *l, int idx, enet_uint8 *channel_id) {
 	*channel_id = 0;
 
 	int argc = lua_gettop(l);
-	if (argc >= idx+2) { /* flags */ }
-	if (argc >= idx+1) {
-		*channel_id = luaL_checkint(l, 3);
+	if (argc >= idx+2 && !lua_isnil(l, idx+2)) {
+		const char *flag_str = luaL_checkstring(l, idx+2);
+		if (strcmp("unsequenced", flag_str) == 0) {
+			flags = ENET_PACKET_FLAG_RELIABLE;
+		} else if (strcmp("reliable", flag_str) == 0) {
+			flags = ENET_PACKET_FLAG_RELIABLE;
+		} else if (strcmp("unreliable", flag_str) == 0) {
+			flags = 0;
+		} else {
+			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);
 	}
 
 	ENetPacket *packet = enet_packet_create(data, size, flags);

+ 21 - 0
test/unreliable/client.lua

@@ -0,0 +1,21 @@
+
+require "enet"
+
+local host = enet.host_create()
+local server = host:connect("localhost:6789", 2)
+
+local done = false
+while not done do
+	local event = host:service(200)
+	if event then
+		if event.type == "receive" then
+			print(event.channel, event.data)
+		end
+	end
+end
+
+server:disconnect()
+host:flush()
+
+print "done"
+

+ 13 - 0
test/unreliable/server.lua

@@ -0,0 +1,13 @@
+require "enet"
+
+local host = enet.host_create("localhost:6789", nil, 2)
+
+while true do
+	local event = host:service(100)
+	if event and event.type == "connect" then
+		event.peer:send("hello world")
+		for i = 0, 100 do
+			event.peer:send(tostring(i), 1, "something else")
+		end
+	end
+end