1 local verse = require "verse"; |
1 local verse = require "verse"; |
|
2 local now = socket.gettime; |
2 |
3 |
3 local xmlns_sm = "urn:xmpp:sm:2"; |
4 local xmlns_sm = "urn:xmpp:sm:2"; |
4 |
5 |
5 function verse.plugins.smacks(stream) |
6 function verse.plugins.smacks(stream) |
6 -- State for outgoing stanzas |
7 -- State for outgoing stanzas |
7 local outgoing_queue = {}; |
8 local outgoing_queue = {}; |
8 local last_ack = 0; |
9 local last_ack = 0; |
|
10 local last_stanza_time = now(); |
|
11 local timer_active; |
9 |
12 |
10 -- State for incoming stanzas |
13 -- State for incoming stanzas |
11 local handled_stanza_count = 0; |
14 local handled_stanza_count = 0; |
12 |
15 |
13 -- Catch incoming stanzas |
16 -- Catch incoming stanzas |
22 function outgoing_stanza(stanza) |
25 function outgoing_stanza(stanza) |
23 -- NOTE: This will not behave nice if stanzas are serialized before this point |
26 -- NOTE: This will not behave nice if stanzas are serialized before this point |
24 if stanza.name and not stanza.attr.xmlns then |
27 if stanza.name and not stanza.attr.xmlns then |
25 -- serialize stanzas in order to bypass this on resumption |
28 -- serialize stanzas in order to bypass this on resumption |
26 outgoing_queue[#outgoing_queue+1] = tostring(stanza); |
29 outgoing_queue[#outgoing_queue+1] = tostring(stanza); |
27 verse.add_task(1, function() |
30 last_stanza_time = now(); |
28 if #outgoing_queue > 0 then |
31 if not timer_active then |
|
32 timer_active = true; |
|
33 stream:debug("Waiting to send ack request..."); |
|
34 verse.add_task(1, function() |
|
35 if #outgoing_queue == 0 then |
|
36 timer_active = false; |
|
37 return; |
|
38 end |
|
39 local time_since_last_stanza = now() - last_stanza_time; |
|
40 if time_since_last_stanza < 1 and #outgoing_queue < 10 then |
|
41 return 1 - time_since_last_stanza; |
|
42 end |
|
43 stream:debug("Time up, sending <r>..."); |
|
44 timer_active = false; |
29 stream:send(verse.stanza("r", { xmlns = xmlns_sm })); |
45 stream:send(verse.stanza("r", { xmlns = xmlns_sm })); |
30 end |
46 end); |
31 end); |
47 end |
32 end |
48 end |
33 end |
49 end |
34 |
50 |
35 local function on_disconnect() |
51 local function on_disconnect() |
36 stream:debug("smacks: connection lost"); |
52 stream:debug("smacks: connection lost"); |