Add files via upload
This commit is contained in:
@@ -0,0 +1,306 @@
|
||||
--- **AceComm-3.0** allows you to send messages of unlimited length over the addon comm channels.
|
||||
-- It'll automatically split the messages into multiple parts and rebuild them on the receiving end.\\
|
||||
-- **ChatThrottleLib** is of course being used to avoid being disconnected by the server.
|
||||
--
|
||||
-- **AceComm-3.0** can be embeded into your addon, either explicitly by calling AceComm:Embed(MyAddon) or by
|
||||
-- specifying it as an embeded library in your AceAddon. All functions will be available on your addon object
|
||||
-- and can be accessed directly, without having to explicitly call AceComm itself.\\
|
||||
-- It is recommended to embed AceComm, otherwise you'll have to specify a custom `self` on all calls you
|
||||
-- make into AceComm.
|
||||
-- @class file
|
||||
-- @name AceComm-3.0
|
||||
-- @release $Id: AceComm-3.0.lua 1107 2014-02-19 16:40:32Z nevcairiel $
|
||||
|
||||
--[[ AceComm-3.0
|
||||
|
||||
TODO: Time out old data rotting around from dead senders? Not a HUGE deal since the number of possible sender names is somewhat limited.
|
||||
|
||||
]]
|
||||
|
||||
local MAJOR, MINOR = "AceComm-3.0", 9
|
||||
|
||||
local AceComm,oldminor = LibStub:NewLibrary(MAJOR, MINOR)
|
||||
|
||||
if not AceComm then return end
|
||||
|
||||
local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0")
|
||||
local CTL = assert(ChatThrottleLib, "AceComm-3.0 requires ChatThrottleLib")
|
||||
|
||||
-- Lua APIs
|
||||
local type, next, pairs, tostring = type, next, pairs, tostring
|
||||
local strlen, strsub, strfind = string.len, string.sub, string.find
|
||||
local tinsert, tconcat, tgetn, tremove = table.insert, table.concat, table.getn, table.remove
|
||||
local error, assert = error, assert
|
||||
|
||||
|
||||
-- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
|
||||
-- List them here for Mikk's FindGlobals script
|
||||
-- GLOBALS: LibStub, DEFAULT_CHAT_FRAME, geterrorhandler, RegisterAddonMessagePrefix
|
||||
|
||||
AceComm.embeds = AceComm.embeds or {}
|
||||
|
||||
-- for my sanity and yours, let's give the message type bytes some names
|
||||
local MSG_MULTI_FIRST = "\001"
|
||||
local MSG_MULTI_NEXT = "\002"
|
||||
local MSG_MULTI_LAST = "\003"
|
||||
local MSG_ESCAPE = "\004"
|
||||
|
||||
-- remove old structures (pre WoW 4.0)
|
||||
AceComm.multipart_origprefixes = nil
|
||||
AceComm.multipart_reassemblers = nil
|
||||
|
||||
-- the multipart message spool: indexed by a combination of sender+distribution+
|
||||
AceComm.multipart_spool = AceComm.multipart_spool or {}
|
||||
|
||||
--- 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
|
||||
-- @param method Callback to call on message reception: Function reference, or method name (string) to call on self. Defaults to "OnCommReceived"
|
||||
function AceComm:RegisterComm(prefix, method)
|
||||
if method == nil then
|
||||
method = "OnCommReceived"
|
||||
end
|
||||
|
||||
if strlen(prefix) > 16 then -- TODO: 15?
|
||||
error("AceComm:RegisterComm(prefix,method): prefix length is limited to 16 characters")
|
||||
end
|
||||
|
||||
return AceComm._RegisterComm(self, prefix, method) -- created by CallbackHandler
|
||||
end
|
||||
|
||||
local warnedPrefix=false
|
||||
|
||||
--- Send a message over the Addon Channel
|
||||
-- @param prefix A printable character (\032-\255) classification of the message (typically AddonName or AddonNameEvent)
|
||||
-- @param text Data to send, nils (\000) not allowed. Any length.
|
||||
-- @param distribution Addon channel, e.g. "RAID", "GUILD", etc; see SendAddonMessage API
|
||||
-- @param target Destination for some distributions; see SendAddonMessage API
|
||||
-- @param prio OPTIONAL: ChatThrottleLib priority, "BULK", "NORMAL" or "ALERT". Defaults to "NORMAL".
|
||||
-- @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.
|
||||
-- @param callbackArg: OPTIONAL: first arg to the callback function. nil will be passed if not specified.
|
||||
function AceComm:SendCommMessage(prefix, text, distribution, target, prio, callbackFn, callbackArg)
|
||||
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!
|
||||
if not( type(prefix)=="string" and
|
||||
type(text)=="string" and
|
||||
type(distribution)=="string" and
|
||||
(target==nil or type(target)=="string") and
|
||||
(prio=="BULK" or prio=="NORMAL" or prio=="ALERT")
|
||||
) then
|
||||
error('Usage: SendCommMessage(addon, "prefix", "text", "distribution"[, "target"[, "prio"[, callbackFn, callbackarg]]])', 2)
|
||||
end
|
||||
|
||||
local textlen = strlen(text)
|
||||
-- Yes, the max is 255 even if the dev post said 256. I tested. Char 256+ get silently truncated. /Mikk, 20110327
|
||||
-- Ace3v: substract the prefix length
|
||||
local maxtextlen = 254 - strlen(prefix)
|
||||
local queueName = prefix..distribution..(target or "")
|
||||
|
||||
local ctlCallback = nil
|
||||
if callbackFn then
|
||||
ctlCallback = function(sent)
|
||||
return callbackFn(callbackArg, sent, textlen)
|
||||
end
|
||||
end
|
||||
|
||||
local forceMultipart
|
||||
if strfind(text, "^[\001-\009]") then -- 4.1+: see if the first character is a control character
|
||||
-- we need to escape the first character with a \004
|
||||
if textlen+1 > maxtextlen then -- would we go over the size limit?
|
||||
forceMultipart = true -- just make it multipart, no escape problems then
|
||||
else
|
||||
text = "\004" .. text
|
||||
end
|
||||
end
|
||||
|
||||
if not forceMultipart and textlen <= maxtextlen then
|
||||
-- fits all in one message
|
||||
CTL:SendAddonMessage(prio, prefix, text, distribution, target, queueName, ctlCallback, textlen)
|
||||
else
|
||||
maxtextlen = maxtextlen - 1 -- 1 extra byte for part indicator in prefix(4.0)/start of message(4.1)
|
||||
|
||||
-- first part
|
||||
local chunk = strsub(text, 1, maxtextlen)
|
||||
CTL:SendAddonMessage(prio, prefix, MSG_MULTI_FIRST..chunk, distribution, target, queueName, ctlCallback, maxtextlen)
|
||||
|
||||
-- continuation
|
||||
local pos = 1+maxtextlen
|
||||
|
||||
while pos+maxtextlen <= textlen do
|
||||
chunk = strsub(text, pos, pos+maxtextlen-1)
|
||||
CTL:SendAddonMessage(prio, prefix, MSG_MULTI_NEXT..chunk, distribution, target, queueName, ctlCallback, pos+maxtextlen-1)
|
||||
pos = pos + maxtextlen
|
||||
end
|
||||
|
||||
-- final part
|
||||
chunk = strsub(text, pos)
|
||||
CTL:SendAddonMessage(prio, prefix, MSG_MULTI_LAST..chunk, distribution, target, queueName, ctlCallback, textlen)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
----------------------------------------
|
||||
-- Message receiving
|
||||
----------------------------------------
|
||||
|
||||
do
|
||||
local compost = setmetatable({}, {__mode = "k"})
|
||||
local function new()
|
||||
local t = next(compost)
|
||||
if t then
|
||||
compost[t]=nil
|
||||
for i=tgetn(t),3,-1 do -- faster than pairs loop. don't even nil out 1/2 since they'll be overwritten
|
||||
tremove(t) -- Ace3v: t[i] = nil wont affect the tgetn return value
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
return {}
|
||||
end
|
||||
|
||||
local function lostdatawarning(prefix,sender,where)
|
||||
DEFAULT_CHAT_FRAME:AddMessage(MAJOR..": Warning: lost network data regarding '"..tostring(prefix).."' from '"..tostring(sender).."' (in "..where..")")
|
||||
end
|
||||
|
||||
function AceComm:OnReceiveMultipartFirst(prefix, message, distribution, sender)
|
||||
local key = prefix.."\t"..distribution.."\t"..sender -- a unique stream is defined by the prefix + distribution + sender
|
||||
local spool = AceComm.multipart_spool
|
||||
|
||||
--[[
|
||||
if spool[key] then
|
||||
lostdatawarning(prefix,sender,"First")
|
||||
-- continue and overwrite
|
||||
end
|
||||
--]]
|
||||
|
||||
spool[key] = message -- plain string for now
|
||||
end
|
||||
|
||||
function AceComm:OnReceiveMultipartNext(prefix, message, distribution, sender)
|
||||
local key = prefix.."\t"..distribution.."\t"..sender -- a unique stream is defined by the prefix + distribution + sender
|
||||
local spool = AceComm.multipart_spool
|
||||
local olddata = spool[key]
|
||||
|
||||
if not olddata then
|
||||
--lostdatawarning(prefix,sender,"Next")
|
||||
return
|
||||
end
|
||||
|
||||
if type(olddata)~="table" then
|
||||
-- ... but what we have is not a table. So make it one. (Pull a composted one if available)
|
||||
local t = new()
|
||||
t[1] = olddata -- add old data as first string
|
||||
t[2] = message -- and new message as second string
|
||||
spool[key] = t -- and put the table in the spool instead of the old string
|
||||
else
|
||||
tinsert(olddata, message)
|
||||
end
|
||||
end
|
||||
|
||||
function AceComm:OnReceiveMultipartLast(prefix, message, distribution, sender)
|
||||
local key = prefix.."\t"..distribution.."\t"..sender -- a unique stream is defined by the prefix + distribution + sender
|
||||
local spool = AceComm.multipart_spool
|
||||
local olddata = spool[key]
|
||||
|
||||
if not olddata then
|
||||
--lostdatawarning(prefix,sender,"End")
|
||||
return
|
||||
end
|
||||
|
||||
spool[key] = nil
|
||||
|
||||
if type(olddata) == "table" then
|
||||
-- if we've received a "next", the spooled data will be a table for rapid & garbage-free tconcat
|
||||
tinsert(olddata, message)
|
||||
AceComm.callbacks:Fire(prefix, 3, tconcat(olddata, ""), distribution, sender)
|
||||
compost[olddata] = true
|
||||
else
|
||||
-- if we've only received a "first", the spooled data will still only be a string
|
||||
AceComm.callbacks:Fire(prefix, 3, olddata..message, distribution, sender)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
----------------------------------------
|
||||
-- Embed CallbackHandler
|
||||
----------------------------------------
|
||||
|
||||
if not AceComm.callbacks then
|
||||
AceComm.callbacks = CallbackHandler:New(AceComm,
|
||||
"_RegisterComm",
|
||||
"UnregisterComm",
|
||||
"UnregisterAllComm")
|
||||
end
|
||||
|
||||
AceComm.callbacks.OnUsed = nil
|
||||
AceComm.callbacks.OnUnused = nil
|
||||
|
||||
-- Ace3v: in vanilla, global vars:
|
||||
-- event -> event type
|
||||
-- arg1 -> prefix
|
||||
-- arg2 -> message
|
||||
-- arg3 -> channel
|
||||
-- arg4 -> sender
|
||||
local function OnEvent()
|
||||
local prefix, message, distribution, sender = arg1, arg2, arg3, arg4
|
||||
if event == "CHAT_MSG_ADDON" then
|
||||
local _, _, control, rest = strfind(message, "^([\001-\009])(.*)")
|
||||
if control then
|
||||
if control==MSG_MULTI_FIRST then
|
||||
AceComm:OnReceiveMultipartFirst(prefix, rest, distribution, sender)
|
||||
elseif control==MSG_MULTI_NEXT then
|
||||
AceComm:OnReceiveMultipartNext(prefix, rest, distribution, sender)
|
||||
elseif control==MSG_MULTI_LAST then
|
||||
AceComm:OnReceiveMultipartLast(prefix, rest, distribution, sender)
|
||||
elseif control==MSG_ESCAPE then
|
||||
AceComm.callbacks:Fire(prefix, 3, rest, distribution, sender)
|
||||
else
|
||||
-- unknown control character, ignore SILENTLY (dont warn unnecessarily about future extensions!)
|
||||
end
|
||||
else
|
||||
-- single part: fire it off immediately and let CallbackHandler decide if it's registered or not
|
||||
AceComm.callbacks:Fire(prefix, 3, message, distribution, sender)
|
||||
end
|
||||
else
|
||||
assert(false, "Received "..tostring(event).." event?!")
|
||||
end
|
||||
end
|
||||
|
||||
AceComm.frame = AceComm.frame or CreateFrame("Frame", "AceComm30Frame")
|
||||
AceComm.frame:SetScript("OnEvent", OnEvent)
|
||||
AceComm.frame:UnregisterAllEvents()
|
||||
AceComm.frame:RegisterEvent("CHAT_MSG_ADDON")
|
||||
|
||||
|
||||
----------------------------------------
|
||||
-- Base library stuff
|
||||
----------------------------------------
|
||||
|
||||
local mixins = {
|
||||
"RegisterComm",
|
||||
"UnregisterComm",
|
||||
"UnregisterAllComm",
|
||||
"SendCommMessage",
|
||||
}
|
||||
|
||||
-- Embeds AceComm-3.0 into the target object making the functions from the mixins list available on target:..
|
||||
-- @param target target object to embed AceComm-3.0 in
|
||||
function AceComm:Embed(target)
|
||||
for k, v in pairs(mixins) do
|
||||
target[v] = self[v]
|
||||
end
|
||||
self.embeds[target] = true
|
||||
return target
|
||||
end
|
||||
|
||||
function AceComm:OnEmbedDisable(target)
|
||||
target:UnregisterAllComm()
|
||||
end
|
||||
|
||||
-- Update embeds
|
||||
for target, v in pairs(AceComm.embeds) do
|
||||
AceComm:Embed(target)
|
||||
end
|
||||
@@ -0,0 +1,5 @@
|
||||
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/
|
||||
..\FrameXML\UI.xsd">
|
||||
<Script file="ChatThrottleLib.lua"/>
|
||||
<Script file="AceComm-3.0.lua"/>
|
||||
</Ui>
|
||||
@@ -0,0 +1,502 @@
|
||||
--
|
||||
-- ChatThrottleLib by Mikk
|
||||
--
|
||||
-- Manages AddOn chat output to keep player from getting kicked off.
|
||||
--
|
||||
-- ChatThrottleLib.SendChatMessage/.SendAddonMessage functions that accept
|
||||
-- a Priority ("BULK", "NORMAL", "ALERT") as well as prefix for SendChatMessage.
|
||||
--
|
||||
-- Priorities get an equal share of available bandwidth when fully loaded.
|
||||
-- Communication channels are separated on extension+chattype+destination and
|
||||
-- get round-robinned. (Destination only matters for whispers and channels,
|
||||
-- obviously)
|
||||
--
|
||||
-- Will install hooks for SendChatMessage and SendAdd[Oo]nMessage to measure
|
||||
-- bandwidth bypassing the library and use less bandwidth itself.
|
||||
--
|
||||
--
|
||||
-- Fully embeddable library. Just copy this file into your addon directory,
|
||||
-- add it to the .toc, and it's done.
|
||||
--
|
||||
-- Can run as a standalone addon also, but, really, just embed it! :-)
|
||||
--
|
||||
|
||||
--
|
||||
-- ChangeLog and notes for this version:
|
||||
-- There is no historic CTL version 14 and this would supersede all other Vanilla era
|
||||
-- versions (<=13) while also not stepping on private server TBC versions (15+)
|
||||
--
|
||||
-- Modifications for this version are simply to throttle raw chat lines per second and
|
||||
-- have nothing to do with the bytes sent. Chat to CHANNEL, RAID, etc all count toward
|
||||
-- this limit. Otherwise, this is the same as version 13 as it relates to throttling
|
||||
-- raw bytes sent. To be clear, this update doesn't further throttle raw add-on messages.
|
||||
--
|
||||
local CTL_VERSION = 14
|
||||
|
||||
local MAX_CPS = 800 -- 2000 seems to be safe if NOTHING ELSE is happening. let's call it 800.
|
||||
local MSG_OVERHEAD = 40 -- Guesstimate overhead for sending a message; source+dest+chattype+protocolstuff
|
||||
|
||||
local BURST = 4000 -- WoW's server buffer seems to be about 32KB. 8KB should be safe, but seen disconnects on _some_ servers. Using 4KB now.
|
||||
|
||||
local MIN_FPS = 20 -- Reduce output CPS to half (and don't burst) if FPS drops below this value
|
||||
|
||||
-- Turtle seems to allow > 6 lines per second in some situations; but for pure spam throughput, a value of 6 here seems to be the limit.
|
||||
-- Due to timing issues, setting this to 5.75 seems to allow the most throughput without any accidental soft bans.
|
||||
local TURTLE_MAX_CHAT_LINES_PER_SECOND = 5
|
||||
|
||||
if(ChatThrottleLib and ChatThrottleLib.version>=CTL_VERSION) then
|
||||
-- There's already a newer (or same) version loaded. Buh-bye.
|
||||
return;
|
||||
end
|
||||
|
||||
|
||||
|
||||
if(not ChatThrottleLib) then
|
||||
ChatThrottleLib = {}
|
||||
end
|
||||
|
||||
local ChatThrottleLib = ChatThrottleLib
|
||||
local strlen = strlen
|
||||
local setmetatable = setmetatable
|
||||
local getn = getn
|
||||
local tremove = tremove
|
||||
local tinsert = tinsert
|
||||
local tostring = tostring
|
||||
local GetTime = GetTime
|
||||
local format = format
|
||||
|
||||
ChatThrottleLib.version=CTL_VERSION;
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- Double-linked ring implementation
|
||||
|
||||
local Ring = {}
|
||||
local RingMeta = { __index=Ring }
|
||||
|
||||
function Ring:New()
|
||||
local ret = {}
|
||||
setmetatable(ret, RingMeta)
|
||||
return ret;
|
||||
end
|
||||
|
||||
function Ring:Add(obj) -- Append at the "far end" of the ring (aka just before the current position)
|
||||
if(self.pos) then
|
||||
obj.prev = self.pos.prev;
|
||||
obj.prev.next = obj;
|
||||
obj.next = self.pos;
|
||||
obj.next.prev = obj;
|
||||
else
|
||||
obj.next = obj;
|
||||
obj.prev = obj;
|
||||
self.pos = obj;
|
||||
end
|
||||
end
|
||||
|
||||
function Ring:Remove(obj)
|
||||
obj.next.prev = obj.prev;
|
||||
obj.prev.next = obj.next;
|
||||
if(self.pos == obj) then
|
||||
self.pos = obj.next;
|
||||
if(self.pos == obj) then
|
||||
self.pos = nil;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- Recycling bin for pipes (kept in a linked list because that's
|
||||
-- how they're worked with in the rotating rings; just reusing members)
|
||||
|
||||
ChatThrottleLib.PipeBin = { count=0 }
|
||||
|
||||
function ChatThrottleLib.PipeBin:Put(pipe)
|
||||
for i=getn(pipe),1,-1 do
|
||||
tremove(pipe, i);
|
||||
end
|
||||
pipe.prev = nil;
|
||||
pipe.next = self.list;
|
||||
self.list = pipe;
|
||||
self.count = self.count+1;
|
||||
end
|
||||
|
||||
function ChatThrottleLib.PipeBin:Get()
|
||||
if(self.list) then
|
||||
local ret = self.list;
|
||||
self.list = ret.next;
|
||||
ret.next=nil;
|
||||
self.count = self.count - 1;
|
||||
return ret;
|
||||
end
|
||||
return {};
|
||||
end
|
||||
|
||||
function ChatThrottleLib.PipeBin:Tidy()
|
||||
if(self.count < 25) then
|
||||
return;
|
||||
end
|
||||
|
||||
if(self.count > 100) then
|
||||
n=self.count-90;
|
||||
else
|
||||
n=10;
|
||||
end
|
||||
for i=2,n do
|
||||
self.list = self.list.next;
|
||||
end
|
||||
local delme = self.list;
|
||||
self.list = self.list.next;
|
||||
delme.next = nil;
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- Recycling bin for messages
|
||||
|
||||
ChatThrottleLib.MsgBin = {}
|
||||
|
||||
function ChatThrottleLib.MsgBin:Put(msg)
|
||||
msg.text = nil;
|
||||
tinsert(self, msg);
|
||||
end
|
||||
|
||||
function ChatThrottleLib.MsgBin:Get()
|
||||
local ret = tremove(self, getn(self));
|
||||
if(ret) then return ret; end
|
||||
return {};
|
||||
end
|
||||
|
||||
function ChatThrottleLib.MsgBin:Tidy()
|
||||
if(getn(self)<50) then
|
||||
return;
|
||||
end
|
||||
if(getn(self)>150) then -- "can't happen" but ...
|
||||
for n=getn(self),120,-1 do
|
||||
tremove(self,n);
|
||||
end
|
||||
else
|
||||
for n=getn(self),getn(self)-20,-1 do
|
||||
tremove(self,n);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- ChatThrottleLib:Init
|
||||
-- Initialize queues, set up frame for OnUpdate, etc
|
||||
|
||||
|
||||
function ChatThrottleLib:Init()
|
||||
|
||||
-- Set up queues
|
||||
if(not self.Prio) then
|
||||
self.Prio = {}
|
||||
self.Prio["ALERT"] = { ByName={}, Ring = Ring:New(), avail=0 };
|
||||
self.Prio["NORMAL"] = { ByName={}, Ring = Ring:New(), avail=0 };
|
||||
self.Prio["BULK"] = { ByName={}, Ring = Ring:New(), avail=0 };
|
||||
end
|
||||
|
||||
-- v4: total send counters per priority
|
||||
for _,Prio in pairs(self.Prio) do
|
||||
Prio.nTotalSent = Prio.nTotalSent or 0;
|
||||
end
|
||||
|
||||
self.avail = self.avail or 0; -- v5
|
||||
self.nTotalSent = self.nTotalSent or 0; -- v5
|
||||
|
||||
|
||||
-- Set up a frame to get OnUpdate events
|
||||
if(not self.Frame) then
|
||||
self.Frame = CreateFrame("Frame");
|
||||
self.Frame:Hide();
|
||||
end
|
||||
self.Frame.Show = self.Frame.Show; -- cache for speed
|
||||
self.Frame.Hide = self.Frame.Hide; -- cache for speed
|
||||
self.Frame:SetScript("OnUpdate", self.OnUpdate);
|
||||
self.Frame:SetScript("OnEvent", self.OnEvent); -- v11: Monitor P_E_W so we can throttle hard for a few seconds
|
||||
self.Frame:RegisterEvent("PLAYER_ENTERING_WORLD");
|
||||
self.OnUpdateDelay=0;
|
||||
self.TurtleChatLinesAvailable=TURTLE_MAX_CHAT_LINES_PER_SECOND;
|
||||
self.LastAvailUpdate=GetTime();
|
||||
self.HardThrottlingBeginTime=GetTime(); -- v11: Throttle hard for a few seconds after startup
|
||||
|
||||
-- Hook SendChatMessage and SendAddonMessage so we can measure unpiped traffic and avoid overloads (v7)
|
||||
if(not self.ORIG_SendChatMessage) then
|
||||
--SendChatMessage
|
||||
self.ORIG_SendChatMessage = SendChatMessage;
|
||||
SendChatMessage = function(a1,a2,a3,a4) return ChatThrottleLib.Hook_SendChatMessage(a1,a2,a3,a4); end
|
||||
--SendAdd[Oo]nMessage
|
||||
if(SendAddonMessage or SendAddOnMessage) then -- v10: don't pretend like it doesn't exist if it doesn't!
|
||||
self.ORIG_SendAddonMessage = SendAddonMessage or SendAddOnMessage;
|
||||
SendAddonMessage = function(a1,a2,a3) return ChatThrottleLib.Hook_SendAddonMessage(a1,a2,a3); end
|
||||
if(SendAddOnMessage) then -- in case Slouken changes his mind...
|
||||
SendAddOnMessage = SendAddonMessage;
|
||||
end
|
||||
end
|
||||
end
|
||||
self.nBypass = 0;
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- ChatThrottleLib.Hook_SendChatMessage / .Hook_SendAddonMessage
|
||||
function ChatThrottleLib.Hook_SendChatMessage(text, chattype, language, destination)
|
||||
local self = ChatThrottleLib;
|
||||
local size = strlen(tostring(text or "")) + strlen(tostring(chattype or "")) + strlen(tostring(destination or "")) + 40;
|
||||
self.avail = self.avail - size;
|
||||
self.nBypass = self.nBypass + size;
|
||||
self.TurtleSendChat()
|
||||
return self.ORIG_SendChatMessage(text, chattype, language, destination);
|
||||
end
|
||||
function ChatThrottleLib.Hook_SendAddonMessage(prefix, text, chattype)
|
||||
local self = ChatThrottleLib;
|
||||
local size = strlen(tostring(text or "")) + strlen(tostring(chattype or "")) + strlen(tostring(prefix or "")) + 40;
|
||||
self.avail = self.avail - size;
|
||||
self.nBypass = self.nBypass + size;
|
||||
return self.ORIG_SendAddonMessage(prefix, text, chattype);
|
||||
end
|
||||
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- ChatThrottleLib:UpdateAvail
|
||||
-- Update self.avail with how much bandwidth is currently available
|
||||
|
||||
function ChatThrottleLib:UpdateAvail()
|
||||
local now = GetTime();
|
||||
local newavail = MAX_CPS * (now-self.LastAvailUpdate);
|
||||
|
||||
if(now - self.HardThrottlingBeginTime < 5) then
|
||||
-- First 5 seconds after startup/zoning: VERY hard clamping to avoid irritating the server rate limiter, it seems very cranky then
|
||||
self.avail = min(self.avail + (newavail*0.1), MAX_CPS*0.5);
|
||||
elseif(GetFramerate()<MIN_FPS) then -- GetFrameRate call takes ~0.002 secs
|
||||
newavail = newavail * 0.5;
|
||||
self.avail = min(MAX_CPS, self.avail + newavail);
|
||||
self.bChoking = true; -- just for stats
|
||||
else
|
||||
self.avail = min(BURST, self.avail + newavail);
|
||||
self.bChoking = false;
|
||||
end
|
||||
|
||||
self.avail = max(self.avail, 0-(MAX_CPS*2)); -- Can go negative when someone is eating bandwidth past the lib. but we refuse to stay silent for more than 2 seconds; if they can do it, we can.
|
||||
self.LastAvailUpdate = now;
|
||||
|
||||
return self.avail;
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- Despooling logic
|
||||
function ChatThrottleLib.TurtleSendChat()
|
||||
self = ChatThrottleLib;
|
||||
self.TurtleChatLinesAvailable = self.TurtleChatLinesAvailable - 1
|
||||
-- Showing the frame will start to build back the available buffer and re-hide when max lines are available
|
||||
self.Frame:Show();
|
||||
end
|
||||
|
||||
function ChatThrottleLib.IsTurtleSendChatReady()
|
||||
self = ChatThrottleLib;
|
||||
if self.TurtleChatLinesAvailable > 1 then
|
||||
return true
|
||||
end
|
||||
-- print("Chat Throttled")
|
||||
return false
|
||||
end
|
||||
|
||||
function ChatThrottleLib:Despool(Prio)
|
||||
local ring = Prio.Ring;
|
||||
while(ring.pos and Prio.avail>ring.pos[1].nSize and self.IsTurtleSendChatReady()) do
|
||||
local msg = tremove(Prio.Ring.pos, 1);
|
||||
if(not Prio.Ring.pos[1]) then
|
||||
local pipe = Prio.Ring.pos;
|
||||
Prio.Ring:Remove(pipe);
|
||||
Prio.ByName[pipe.name] = nil;
|
||||
self.PipeBin:Put(pipe);
|
||||
else
|
||||
Prio.Ring.pos = Prio.Ring.pos.next;
|
||||
end
|
||||
Prio.avail = Prio.avail - msg.nSize;
|
||||
msg.f(msg[1], msg[2], msg[3], msg[4]);
|
||||
if msg.type == "chat" then self.TurtleSendChat() end
|
||||
Prio.nTotalSent = Prio.nTotalSent + msg.nSize;
|
||||
self.MsgBin:Put(msg);
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ChatThrottleLib.OnEvent()
|
||||
-- v11: We know that the rate limiter is touchy after login. Assume that it's touch after zoning, too.
|
||||
self = ChatThrottleLib;
|
||||
if(event == "PLAYER_ENTERING_WORLD") then
|
||||
self.HardThrottlingBeginTime=GetTime(); -- Throttle hard for a few seconds after zoning
|
||||
self.avail = 0;
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function ChatThrottleLib.OnUpdate()
|
||||
self = ChatThrottleLib;
|
||||
|
||||
self.OnUpdateDelay = self.OnUpdateDelay + arg1;
|
||||
if(self.OnUpdateDelay < 0.08) then
|
||||
return;
|
||||
end
|
||||
if self.TurtleChatLinesAvailable < TURTLE_MAX_CHAT_LINES_PER_SECOND then
|
||||
self.TurtleChatLinesAvailable = math.min(self.TurtleChatLinesAvailable + self.OnUpdateDelay * TURTLE_MAX_CHAT_LINES_PER_SECOND, TURTLE_MAX_CHAT_LINES_PER_SECOND)
|
||||
end
|
||||
self.OnUpdateDelay = 0;
|
||||
|
||||
self:UpdateAvail();
|
||||
|
||||
if(self.avail<0) then
|
||||
return; -- argh. some bastard is spewing stuff past the lib. just bail early to save cpu.
|
||||
end
|
||||
|
||||
-- See how many of or priorities have queued messages
|
||||
local n=0;
|
||||
for prioname,Prio in pairs(self.Prio) do
|
||||
if(Prio.Ring.pos or Prio.avail<0) then
|
||||
n=n+1;
|
||||
end
|
||||
end
|
||||
|
||||
-- Anything queued still?
|
||||
if(n<1 and self.TurtleChatLinesAvailable >= TURTLE_MAX_CHAT_LINES_PER_SECOND) then
|
||||
-- Nope. Move spillover bandwidth to global availability gauge and clear self.bQueueing
|
||||
for prioname,Prio in pairs(self.Prio) do
|
||||
self.avail = self.avail + Prio.avail;
|
||||
Prio.avail = 0;
|
||||
end
|
||||
self.bQueueing = false;
|
||||
self.Frame:Hide();
|
||||
return;
|
||||
end
|
||||
|
||||
-- There's stuff queued. Hand out available bandwidth to priorities as needed and despool their queues
|
||||
local avail= self.avail/n;
|
||||
self.avail = 0;
|
||||
|
||||
for prioname,Prio in pairs(self.Prio) do
|
||||
if(Prio.Ring.pos or Prio.avail<0) then
|
||||
Prio.avail = Prio.avail + avail;
|
||||
if(Prio.Ring.pos and Prio.avail>Prio.Ring.pos[1].nSize) then
|
||||
self:Despool(Prio);
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Expire recycled tables if needed
|
||||
self.MsgBin:Tidy();
|
||||
self.PipeBin:Tidy();
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- Spooling logic
|
||||
|
||||
|
||||
function ChatThrottleLib:Enqueue(prioname, pipename, msg)
|
||||
local Prio = self.Prio[prioname];
|
||||
local pipe = Prio.ByName[pipename];
|
||||
if(not pipe) then
|
||||
self.Frame:Show();
|
||||
pipe = self.PipeBin:Get();
|
||||
pipe.name = pipename;
|
||||
Prio.ByName[pipename] = pipe;
|
||||
Prio.Ring:Add(pipe);
|
||||
end
|
||||
|
||||
tinsert(pipe, msg);
|
||||
|
||||
self.bQueueing = true;
|
||||
end
|
||||
|
||||
|
||||
|
||||
function ChatThrottleLib:SendChatMessage(prio, prefix, text, chattype, language, destination)
|
||||
if(not (self and prio and text and self.Prio[prio] ) ) then
|
||||
error('Usage: ChatThrottleLib:SendChatMessage("{BULK||NORMAL||ALERT}", "prefix" or nil, "text"[, "chattype"[, "language"[, "destination"]]]', 2);
|
||||
end
|
||||
|
||||
prefix = prefix or tostring(this); -- each frame gets its own queue if prefix is not given
|
||||
|
||||
local nSize = strlen(text) + MSG_OVERHEAD;
|
||||
|
||||
-- Check if there's room in the global available bandwidth gauge to send directly
|
||||
if(not self.bQueueing and nSize < self:UpdateAvail() and self.IsTurtleSendChatReady()) then
|
||||
self.avail = self.avail - nSize;
|
||||
self.ORIG_SendChatMessage(text, chattype, language, destination);
|
||||
self.TurtleSendChat()
|
||||
self.Prio[prio].nTotalSent = self.Prio[prio].nTotalSent + nSize;
|
||||
return;
|
||||
end
|
||||
|
||||
-- Message needs to be queued
|
||||
msg=self.MsgBin:Get();
|
||||
msg.f=self.ORIG_SendChatMessage
|
||||
msg.type="chat";
|
||||
msg[1]=text;
|
||||
msg[2]=chattype or "SAY";
|
||||
msg[3]=language;
|
||||
msg[4]=destination;
|
||||
msg.n = 4
|
||||
msg.nSize = nSize;
|
||||
|
||||
self:Enqueue(prio, format("%s/%s/%s", prefix, chattype, destination or ""), msg);
|
||||
end
|
||||
|
||||
|
||||
function ChatThrottleLib:SendAddonMessage(prio, prefix, text, chattype)
|
||||
if(not (self and prio and prefix and text and chattype and self.Prio[prio] ) ) then
|
||||
error('Usage: ChatThrottleLib:SendAddonMessage("{BULK||NORMAL||ALERT}", "prefix", "text", "chattype")', 0);
|
||||
end
|
||||
|
||||
local nSize = strlen(prefix) + 1 + strlen(text) + MSG_OVERHEAD;
|
||||
|
||||
-- Check if there's room in the global available bandwidth gauge to send directly
|
||||
if(not self.bQueueing and nSize < self:UpdateAvail()) then
|
||||
self.avail = self.avail - nSize;
|
||||
self.ORIG_SendAddonMessage(prefix, text, chattype);
|
||||
self.Prio[prio].nTotalSent = self.Prio[prio].nTotalSent + nSize;
|
||||
return;
|
||||
end
|
||||
|
||||
-- Message needs to be queued
|
||||
msg=self.MsgBin:Get();
|
||||
msg.f=self.ORIG_SendAddonMessage;
|
||||
msg.type="addon";
|
||||
msg[1]=prefix;
|
||||
msg[2]=text;
|
||||
msg[3]=chattype;
|
||||
msg.n = 3
|
||||
msg.nSize = nSize;
|
||||
|
||||
self:Enqueue(prio, format("%s/%s", prefix, chattype), msg);
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- Get the ball rolling!
|
||||
|
||||
ChatThrottleLib:Init();
|
||||
|
||||
--[[ WoWBench debugging snippet
|
||||
if(WOWB_VER) then
|
||||
local function SayTimer()
|
||||
print("SAY: "..GetTime().." "..arg1);
|
||||
end
|
||||
ChatThrottleLib.Frame:SetScript("OnEvent", SayTimer);
|
||||
ChatThrottleLib.Frame:RegisterEvent("CHAT_MSG_SAY");
|
||||
end
|
||||
]]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user