Created
January 2, 2013 03:48
-
-
Save laheadle/4431978 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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