lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


	Hi Martin

	What's the problem with this version?

-- pseudo-code...
while true do
	-- Read 10 bytes of data.
	-- Last 9 bytes of previous run are re-used first if needed.
	data = socket:read(10, data:sub(2))
	if not data then
		-- Serious error, break the loop
		break
	end

	-- Check packet start code
	if data:byte(1) == 0x47 then -- inverted condition
		-- Parse raw bytes into a table with keys
		pkt = convert(data)

		-- More packet validation checking
		if verifyHeader(pkt) then -- inverted condition
			-- We used all data of this packet, don't re-use any of it
			-- in the next run
			data = ""

			-- Do something with the packet
			if pkt.cmd == CMD_EXIT then
				break
			elseif pkt.cmd == CMD_FOO then
				foo()
			end
		end
	end
end

	The code becomes more indented, but in this specific case, it is
not a problem to me.  To me this version is clearer than yours, but I
am sure this is a matter of taste :-)

	Regards,
		Tomás