diff --git a/AceComm-3.0/AceComm-3.0.lua b/AceComm-3.0/AceComm-3.0.lua index 3f504ba..7271ac9 100644 --- a/AceComm-3.0/AceComm-3.0.lua +++ b/AceComm-3.0/AceComm-3.0.lua @@ -52,6 +52,8 @@ AceComm.multipart_reassemblers = nil -- the multipart message spool: indexed by a combination of sender+distribution+ AceComm.multipart_spool = AceComm.multipart_spool or {} +-- Sequence is an integer between 0 and 254 * 254 +AceComm.sequence = random(254 * 254) --- Register for Addon Traffic on a specified prefix -- @param prefix A printable character (\032-\255) classification of the message (typically AddonName or AddonNameEvent), max 16 characters @@ -122,8 +124,8 @@ function AceComm:SendCommMessage(prefix, text, distribution, target, prio, callb local chunkCount = math.ceil(#text / maxtextlen) local chunklen while(true) do - -- We send the part number and chunk count as strings, so reserve place for that - chunklen = maxtextlen - 3 - 2 * #tostring(chunkCount) + -- We send the sequence number, part number and chunk count as strings, so reserve place for that + chunklen = maxtextlen - 3 - 2 - 2 * #tostring(chunkCount) local newChunkCount = math.ceil(#text / chunklen) if newChunkCount == chunkCount then break @@ -133,11 +135,15 @@ function AceComm:SendCommMessage(prefix, text, distribution, target, prio, callb print("chunkCount", chunkCount, "chunklen", chunklen) + -- Convert sequence number to 2 bytes, while avoiding \0 bytes + local sequence = string.char(AceComm.sequence % 254 + 1) .. string.char(floor(AceComm.sequence / 254) + 1) + AceComm.sequence = (AceComm.sequence + 1) % (254 * 254) + for i = 1, chunkCount do - local header = MSG_MULTI_HEADER .. tostring(i) .. MSG_MULTI_HEADER_SEP .. tostring(chunkCount) .. MSG_MULTI_HEADER_END + local header = MSG_MULTI_HEADER .. sequence .. tostring(i) .. MSG_MULTI_HEADER_SEP .. tostring(chunkCount) .. MSG_MULTI_HEADER_END local chunkStart = (i - 1) * chunklen + 1 local chunkEnd = chunkStart + chunklen - 1 - print("Sending: ", i, chunkCount) + print("Sending: ", AceComm.sequence, i, chunkCount) CTL:SendAddonMessage(prio, prefix, header .. text:sub(chunkStart, chunkEnd), distribution, target, queueName, ctlCallback, chunkEnd) end end @@ -229,25 +235,30 @@ do local key = prefix.."\t"..distribution.."\t"..sender -- a unique stream is defined by the prefix + distribution + sender local spool = AceComm.multipart_spool - local msgNumber, msgTotal, rest = match(message, "^(%d*)" .. MSG_MULTI_HEADER_SEP .. "(%d*)" .. MSG_MULTI_HEADER_END .. "(.*)") + local sequence = message:sub(1, 2) + local msgNumber, msgTotal, rest = match(message:sub(3), "^(%d*)" .. MSG_MULTI_HEADER_SEP .. "(%d*)" .. MSG_MULTI_HEADER_END .. "(.*)") msgNumber = tonumber(msgNumber) msgTotal = tonumber(msgTotal) if not spool[key] then spool[key] = new() - spool[key].received = 0 - spool[key].total = msgTotal end - local data = spool[key] + if not spool[key][sequence] then + spool[key][sequence] = new() + spool[key][sequence].received = 0 + spool[key][sequence].total = msgTotal + end + + local data = spool[key][sequence] data[msgNumber] = rest data.received = data.received + 1 - print("Receiving: ", msgNumber, msgTotal) + print("Receiving: ", (string.byte(sequence:sub(2)) - 1) * 254 + string.byte(sequence:sub(1)) - 1, msgNumber, msgTotal) if data.received == data.total then AceComm.callbacks:Fire(prefix, tconcat(data, ""), distribution, sender) - spool[key] = nil + spool[key][sequence] = nil compost[data] = true end end