-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLibLatencyMonitor.lua
89 lines (77 loc) · 2.82 KB
/
LibLatencyMonitor.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
local MAJOR_VERSION = "LibLatencyMonitor"
local MINOR_VERSION = 1
local lib = LibStub:NewLibrary(MAJOR_VERSION, MINOR_VERSION)
if not lib then
return
end
local CreateFrame = CreateFrame
local C_Timer = C_Timer
local GetTime = GetTime
local GetNetStats = GetNetStats
local type = type
lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
lib.LATENCY_CHANGED = "LATENCY_CHANGED"
lib.bandwidth_down_KBps = nil
lib.bandwidth_up_KBps = nil
lib.home_latency_ms = nil
lib.world_latency_ms = nil
lib.update_interval_s = 1
-- Once we have an indication of when the GetNetStats endpoint updates,
-- this will be set to a timestamp, and will remain nil until then.
lib.time_since_last_update = nil
function lib:set_update_timestamp_ticker()
-- First called when we detect a valid change in latency and
-- know when the 30s update happens.
lib.time_since_last_update = GetTime()
-- print(lib.time_since_last_update)
C_Timer.After(30, function() self:set_update_timestamp_ticker() end)
end
function lib:latency_checker()
-- prevent duplicate events booking multiple C_Timers
lib.is_registered = true
local old_home = self.home_latency_ms
local old_world = self.world_latency_ms
local down, up, home, world = GetNetStats()
self.bandwidth_down_KBps = down
self.bandwidth_up_KBps = up
self.home_latency_ms = home
self.world_latency_ms = world
if old_home ~= self.home_latency_ms or old_world ~= self.world_latency_ms then
-- print('new latency')
-- print('old_home says: '..tostring(old_home))
-- print(old_home == nil)
-- print(lib.time_since_last_update == nil)
if lib.time_since_last_update == nil and not (old_home == nil) then
-- We'll poll every 1s until we detect a change, then we know subsequent
-- changes are on a 30s timer.
-- We'll also book a C_Timer to timestamp the lag updates,
-- and fire an event to let addons know the lib has found the update
-- interval
self.callbacks:Fire("ACQUIRED_UPDATE_INTERVAL")
self.update_interval_s = 30
self:set_update_timestamp_ticker()
end
self.callbacks:Fire("LATENCY_CHANGED", home, world)
end
C_Timer.After(self.update_interval_s, function() self:latency_checker() end)
end
function lib:set_check_interval(interval)
if type(interval) == "number" then
self.update_interval_s = interval
end
end
function lib:activate()
if not self.frame then
local frame = CreateFrame("Frame")
self.frame = frame
frame:RegisterEvent("PLAYER_LOGIN")
end
self.frame:SetScript("OnEvent", function()
if not self.is_registered then
self:latency_checker()
self.is_registered = true
end
end
)
end
lib:activate()