Skip to content

Instantly share code, notes, and snippets.

@laheadle
Created January 2, 2013 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laheadle/4431978 to your computer and use it in GitHub Desktop.
Save laheadle/4431978 to your computer and use it in GitHub Desktop.
function makeRecvPacket()
buf, len = nextPacketBuf()
if buf == nil then
return false
end
log("received")
if len ~= maxpacketlen then
log("packet len "..tostring(len))
end
packet = {}
local i = 0
packet.version = getb(buf, 'uint8_t', i)
log('v '..tostring(packet.version))
assert(packet.version == 1)
i = i + versionsize
local filenamelen = getb(buf, 'uint16_t', i)
i = i + filenamelensize
log(filenamelen)
local contentlen = getb(buf, 'uint32_t', i)
log(contentlen)
assert(contentlen + headerlen + filenamelen <= maxpacketlen)
i = i + contentlensize
packet.from = getb(buf, 'uint32_t', i)
log(packet.from)
i = i + fromsize
packet.seq = getb(buf, 'uint32_t', i) -- ffi.new('uint32_t[?]', 1, buf[i])[0]
log(packet.seq)
i = i + seqsize
packet.filename = ffi.string(buf + i, filenamelen)
log(packet.filename)
i = i + filenamelen
packet.content = ffi.string(buf + i, contentlen)
log(#packet.content)
-- sanity checks
assert(packet.version == 1)
assert(#packet.content <= maxpacketlen - headerlen - 1)
return true, packet
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment