Changeset

6899:5f3da8b00b9b

net.websocket.frames: Use struct packing in Lua 5.3 or struct lib if available
author Kim Alvefur <zash@zash.se>
date Tue, 06 Oct 2015 18:05:27 +0200
parents 6898:d01254d5a825
children 6900:44a7e9152b9a
files net/websocket/frames.lua
diffstat 1 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/net/websocket/frames.lua	Tue Oct 06 18:03:04 2015 +0200
+++ b/net/websocket/frames.lua	Tue Oct 06 18:05:27 2015 +0200
@@ -22,6 +22,13 @@
 local s_byte = string.byte;
 local s_char= string.char;
 local s_sub = string.sub;
+local s_pack = string.pack;
+local s_unpack = string.unpack;
+
+if not s_pack and softreq"struct" then
+	s_pack = softreq"struct".pack;
+	s_unpack = softreq"struct".unpack;
+end
 
 local function read_uint16be(str, pos)
 	local l1, l2 = s_byte(str, pos, pos+1);
@@ -46,6 +53,24 @@
 		get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF);
 end
 
+if s_pack then
+	function pack_uint16be(x)
+		return s_pack(">I2", x);
+	end
+	function pack_uint64be(x)
+		return s_pack(">I8", x);
+	end
+end
+
+if s_unpack then
+	function read_uint16be(str, pos)
+		return s_unpack(">I2", str, pos);
+	end
+	function read_uint64be(str, pos)
+		return s_unpack(">I8", str, pos);
+	end
+end
+
 local function parse_frame_header(frame)
 	if #frame < 2 then return; end