Tercio@4: --- **AceComm-3.0** allows you to send messages of unlimited length over the addon comm channels. Tercio@4: -- It'll automatically split the messages into multiple parts and rebuild them on the receiving end.\\ Tercio@4: -- **ChatThrottleLib** is of course being used to avoid being disconnected by the server. Tercio@4: -- Tercio@4: -- **AceComm-3.0** can be embeded into your addon, either explicitly by calling AceComm:Embed(MyAddon) or by Tercio@4: -- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object Tercio@4: -- and can be accessed directly, without having to explicitly call AceComm itself.\\ Tercio@4: -- It is recommended to embed AceComm, otherwise you'll have to specify a custom `self` on all calls you Tercio@4: -- make into AceComm. Tercio@4: -- @class file Tercio@4: -- @name AceComm-3.0 Tercio@4: -- @release $Id: AceComm-3.0.lua 1107 2014-02-19 16:40:32Z nevcairiel $ Tercio@4: Tercio@4: --[[ AceComm-3.0 Tercio@4: Tercio@4: TODO: Time out old data rotting around from dead senders? Not a HUGE deal since the number of possible sender names is somewhat limited. Tercio@4: Tercio@4: ]] Tercio@4: Tercio@4: local MAJOR, MINOR = "AceComm-3.0", 9 Tercio@4: Tercio@4: local AceComm,oldminor = LibStub:NewLibrary(MAJOR, MINOR) Tercio@4: Tercio@4: if not AceComm then return end Tercio@4: Tercio@4: local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0") Tercio@4: local CTL = assert(ChatThrottleLib, "AceComm-3.0 requires ChatThrottleLib") Tercio@4: Tercio@4: -- Lua APIs Tercio@4: local type, next, pairs, tostring = type, next, pairs, tostring Tercio@4: local strsub, strfind = string.sub, string.find Tercio@4: local match = string.match Tercio@4: local tinsert, tconcat = table.insert, table.concat Tercio@4: local error, assert = error, assert Tercio@4: Tercio@4: -- WoW APIs Tercio@4: local Ambiguate = Ambiguate Tercio@4: Tercio@4: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Tercio@4: -- List them here for Mikk's FindGlobals script Tercio@4: -- GLOBALS: LibStub, DEFAULT_CHAT_FRAME, geterrorhandler, RegisterAddonMessagePrefix Tercio@4: Tercio@4: AceComm.embeds = AceComm.embeds or {} Tercio@4: Tercio@4: -- for my sanity and yours, let's give the message type bytes some names Tercio@4: local MSG_MULTI_FIRST = "\001" Tercio@4: local MSG_MULTI_NEXT = "\002" Tercio@4: local MSG_MULTI_LAST = "\003" Tercio@4: local MSG_ESCAPE = "\004" Tercio@4: Tercio@4: -- remove old structures (pre WoW 4.0) Tercio@4: AceComm.multipart_origprefixes = nil Tercio@4: AceComm.multipart_reassemblers = nil Tercio@4: Tercio@4: -- the multipart message spool: indexed by a combination of sender+distribution+ Tercio@4: AceComm.multipart_spool = AceComm.multipart_spool or {} Tercio@4: Tercio@4: --- Register for Addon Traffic on a specified prefix Tercio@4: -- @param prefix A printable character (\032-\255) classification of the message (typically AddonName or AddonNameEvent), max 16 characters Tercio@4: -- @param method Callback to call on message reception: Function reference, or method name (string) to call on self. Defaults to "OnCommReceived" Tercio@4: function AceComm:RegisterComm(prefix, method) Tercio@4: if method == nil then Tercio@4: method = "OnCommReceived" Tercio@4: end Tercio@4: Tercio@4: if #prefix > 16 then -- TODO: 15? Tercio@4: error("AceComm:RegisterComm(prefix,method): prefix length is limited to 16 characters") Tercio@4: end Tercio@4: RegisterAddonMessagePrefix(prefix) Tercio@4: Tercio@4: return AceComm._RegisterComm(self, prefix, method) -- created by CallbackHandler Tercio@4: end Tercio@4: Tercio@4: local warnedPrefix=false Tercio@4: Tercio@4: --- Send a message over the Addon Channel Tercio@4: -- @param prefix A printable character (\032-\255) classification of the message (typically AddonName or AddonNameEvent) Tercio@4: -- @param text Data to send, nils (\000) not allowed. Any length. Tercio@4: -- @param distribution Addon channel, e.g. "RAID", "GUILD", etc; see SendAddonMessage API Tercio@4: -- @param target Destination for some distributions; see SendAddonMessage API Tercio@4: -- @param prio OPTIONAL: ChatThrottleLib priority, "BULK", "NORMAL" or "ALERT". Defaults to "NORMAL". Tercio@4: -- @param callbackFn OPTIONAL: callback function to be called as each chunk is sent. receives 3 args: the user supplied arg (see next), the number of bytes sent so far, and the number of bytes total to send. Tercio@4: -- @param callbackArg: OPTIONAL: first arg to the callback function. nil will be passed if not specified. Tercio@4: function AceComm:SendCommMessage(prefix, text, distribution, target, prio, callbackFn, callbackArg) Tercio@4: prio = prio or "NORMAL" -- pasta's reference implementation had different prio for singlepart and multipart, but that's a very bad idea since that can easily lead to out-of-sequence delivery! Tercio@4: if not( type(prefix)=="string" and Tercio@4: type(text)=="string" and Tercio@4: type(distribution)=="string" and Tercio@4: (target==nil or type(target)=="string") and Tercio@4: (prio=="BULK" or prio=="NORMAL" or prio=="ALERT") Tercio@4: ) then Tercio@4: error('Usage: SendCommMessage(addon, "prefix", "text", "distribution"[, "target"[, "prio"[, callbackFn, callbackarg]]])', 2) Tercio@4: end Tercio@4: Tercio@4: local textlen = #text Tercio@4: local maxtextlen = 255 -- Yes, the max is 255 even if the dev post said 256. I tested. Char 256+ get silently truncated. /Mikk, 20110327 Tercio@4: local queueName = prefix..distribution..(target or "") Tercio@4: Tercio@4: local ctlCallback = nil Tercio@4: if callbackFn then Tercio@4: ctlCallback = function(sent) Tercio@4: return callbackFn(callbackArg, sent, textlen) Tercio@4: end Tercio@4: end Tercio@4: Tercio@4: local forceMultipart Tercio@4: if match(text, "^[\001-\009]") then -- 4.1+: see if the first character is a control character Tercio@4: -- we need to escape the first character with a \004 Tercio@4: if textlen+1 > maxtextlen then -- would we go over the size limit? Tercio@4: forceMultipart = true -- just make it multipart, no escape problems then Tercio@4: else Tercio@4: text = "\004" .. text Tercio@4: end Tercio@4: end Tercio@4: Tercio@4: if not forceMultipart and textlen <= maxtextlen then Tercio@4: -- fits all in one message Tercio@4: CTL:SendAddonMessage(prio, prefix, text, distribution, target, queueName, ctlCallback, textlen) Tercio@4: else Tercio@4: maxtextlen = maxtextlen - 1 -- 1 extra byte for part indicator in prefix(4.0)/start of message(4.1) Tercio@4: Tercio@4: -- first part Tercio@4: local chunk = strsub(text, 1, maxtextlen) Tercio@4: CTL:SendAddonMessage(prio, prefix, MSG_MULTI_FIRST..chunk, distribution, target, queueName, ctlCallback, maxtextlen) Tercio@4: Tercio@4: -- continuation Tercio@4: local pos = 1+maxtextlen Tercio@4: Tercio@4: while pos+maxtextlen <= textlen do Tercio@4: chunk = strsub(text, pos, pos+maxtextlen-1) Tercio@4: CTL:SendAddonMessage(prio, prefix, MSG_MULTI_NEXT..chunk, distribution, target, queueName, ctlCallback, pos+maxtextlen-1) Tercio@4: pos = pos + maxtextlen Tercio@4: end Tercio@4: Tercio@4: -- final part Tercio@4: chunk = strsub(text, pos) Tercio@4: CTL:SendAddonMessage(prio, prefix, MSG_MULTI_LAST..chunk, distribution, target, queueName, ctlCallback, textlen) Tercio@4: end Tercio@4: end Tercio@4: Tercio@4: Tercio@4: ---------------------------------------- Tercio@4: -- Message receiving Tercio@4: ---------------------------------------- Tercio@4: Tercio@4: do Tercio@4: local compost = setmetatable({}, {__mode = "k"}) Tercio@4: local function new() Tercio@4: local t = next(compost) Tercio@4: if t then Tercio@4: compost[t]=nil Tercio@4: for i=#t,3,-1 do -- faster than pairs loop. don't even nil out 1/2 since they'll be overwritten Tercio@4: t[i]=nil Tercio@4: end Tercio@4: return t Tercio@4: end Tercio@4: Tercio@4: return {} Tercio@4: end Tercio@4: Tercio@4: local function lostdatawarning(prefix,sender,where) Tercio@4: DEFAULT_CHAT_FRAME:AddMessage(MAJOR..": Warning: lost network data regarding '"..tostring(prefix).."' from '"..tostring(sender).."' (in "..where..")") Tercio@4: end Tercio@4: Tercio@4: function AceComm:OnReceiveMultipartFirst(prefix, message, distribution, sender) Tercio@4: local key = prefix.."\t"..distribution.."\t"..sender -- a unique stream is defined by the prefix + distribution + sender Tercio@4: local spool = AceComm.multipart_spool Tercio@4: Tercio@4: --[[ Tercio@4: if spool[key] then Tercio@4: lostdatawarning(prefix,sender,"First") Tercio@4: -- continue and overwrite Tercio@4: end Tercio@4: --]] Tercio@4: Tercio@4: spool[key] = message -- plain string for now Tercio@4: end Tercio@4: Tercio@4: function AceComm:OnReceiveMultipartNext(prefix, message, distribution, sender) Tercio@4: local key = prefix.."\t"..distribution.."\t"..sender -- a unique stream is defined by the prefix + distribution + sender Tercio@4: local spool = AceComm.multipart_spool Tercio@4: local olddata = spool[key] Tercio@4: Tercio@4: if not olddata then Tercio@4: --lostdatawarning(prefix,sender,"Next") Tercio@4: return Tercio@4: end Tercio@4: Tercio@4: if type(olddata)~="table" then Tercio@4: -- ... but what we have is not a table. So make it one. (Pull a composted one if available) Tercio@4: local t = new() Tercio@4: t[1] = olddata -- add old data as first string Tercio@4: t[2] = message -- and new message as second string Tercio@4: spool[key] = t -- and put the table in the spool instead of the old string Tercio@4: else Tercio@4: tinsert(olddata, message) Tercio@4: end Tercio@4: end Tercio@4: Tercio@4: function AceComm:OnReceiveMultipartLast(prefix, message, distribution, sender) Tercio@4: local key = prefix.."\t"..distribution.."\t"..sender -- a unique stream is defined by the prefix + distribution + sender Tercio@4: local spool = AceComm.multipart_spool Tercio@4: local olddata = spool[key] Tercio@4: Tercio@4: if not olddata then Tercio@4: --lostdatawarning(prefix,sender,"End") Tercio@4: return Tercio@4: end Tercio@4: Tercio@4: spool[key] = nil Tercio@4: Tercio@4: if type(olddata) == "table" then Tercio@4: -- if we've received a "next", the spooled data will be a table for rapid & garbage-free tconcat Tercio@4: tinsert(olddata, message) Tercio@4: AceComm.callbacks:Fire(prefix, tconcat(olddata, ""), distribution, sender) Tercio@4: compost[olddata] = true Tercio@4: else Tercio@4: -- if we've only received a "first", the spooled data will still only be a string Tercio@4: AceComm.callbacks:Fire(prefix, olddata..message, distribution, sender) Tercio@4: end Tercio@4: end Tercio@4: end Tercio@4: Tercio@4: Tercio@4: Tercio@4: Tercio@4: Tercio@4: Tercio@4: ---------------------------------------- Tercio@4: -- Embed CallbackHandler Tercio@4: ---------------------------------------- Tercio@4: Tercio@4: if not AceComm.callbacks then Tercio@4: AceComm.callbacks = CallbackHandler:New(AceComm, Tercio@4: "_RegisterComm", Tercio@4: "UnregisterComm", Tercio@4: "UnregisterAllComm") Tercio@4: end Tercio@4: Tercio@4: AceComm.callbacks.OnUsed = nil Tercio@4: AceComm.callbacks.OnUnused = nil Tercio@4: Tercio@4: local function OnEvent(self, event, prefix, message, distribution, sender) Tercio@4: if event == "CHAT_MSG_ADDON" then Tercio@4: sender = Ambiguate(sender, "none") Tercio@4: local control, rest = match(message, "^([\001-\009])(.*)") Tercio@4: if control then Tercio@4: if control==MSG_MULTI_FIRST then Tercio@4: AceComm:OnReceiveMultipartFirst(prefix, rest, distribution, sender) Tercio@4: elseif control==MSG_MULTI_NEXT then Tercio@4: AceComm:OnReceiveMultipartNext(prefix, rest, distribution, sender) Tercio@4: elseif control==MSG_MULTI_LAST then Tercio@4: AceComm:OnReceiveMultipartLast(prefix, rest, distribution, sender) Tercio@4: elseif control==MSG_ESCAPE then Tercio@4: AceComm.callbacks:Fire(prefix, rest, distribution, sender) Tercio@4: else Tercio@4: -- unknown control character, ignore SILENTLY (dont warn unnecessarily about future extensions!) Tercio@4: end Tercio@4: else Tercio@4: -- single part: fire it off immediately and let CallbackHandler decide if it's registered or not Tercio@4: AceComm.callbacks:Fire(prefix, message, distribution, sender) Tercio@4: end Tercio@4: else Tercio@4: assert(false, "Received "..tostring(event).." event?!") Tercio@4: end Tercio@4: end Tercio@4: Tercio@4: AceComm.frame = AceComm.frame or CreateFrame("Frame", "AceComm30Frame") Tercio@4: AceComm.frame:SetScript("OnEvent", OnEvent) Tercio@4: AceComm.frame:UnregisterAllEvents() Tercio@4: AceComm.frame:RegisterEvent("CHAT_MSG_ADDON") Tercio@4: Tercio@4: Tercio@4: ---------------------------------------- Tercio@4: -- Base library stuff Tercio@4: ---------------------------------------- Tercio@4: Tercio@4: local mixins = { Tercio@4: "RegisterComm", Tercio@4: "UnregisterComm", Tercio@4: "UnregisterAllComm", Tercio@4: "SendCommMessage", Tercio@4: } Tercio@4: Tercio@4: -- Embeds AceComm-3.0 into the target object making the functions from the mixins list available on target:.. Tercio@4: -- @param target target object to embed AceComm-3.0 in Tercio@4: function AceComm:Embed(target) Tercio@4: for k, v in pairs(mixins) do Tercio@4: target[v] = self[v] Tercio@4: end Tercio@4: self.embeds[target] = true Tercio@4: return target Tercio@4: end Tercio@4: Tercio@4: function AceComm:OnEmbedDisable(target) Tercio@4: target:UnregisterAllComm() Tercio@4: end Tercio@4: Tercio@4: -- Update embeds Tercio@4: for target, v in pairs(AceComm.embeds) do Tercio@4: AceComm:Embed(target) Tercio@4: end