diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-10-03 09:16:32 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-10-09 14:09:00 +0200 |
| commit | aa39852931032233259bb7495fec0ed6b6a81e0c (patch) | |
| tree | 99fe41a937b4f8c53bc0fa656d9f59e6cdfbb833 | |
| parent | 0f3d7e27ceb37a0946cd319370f151f22e02b25d (diff) | |
connection_manager.vala: Refactor reconnect stream
| -rw-r--r-- | libdino/src/service/connection_manager.vala | 51 |
1 files changed, 33 insertions, 18 deletions
diff --git a/libdino/src/service/connection_manager.vala b/libdino/src/service/connection_manager.vala index 35ee9160..19e46ed2 100644 --- a/libdino/src/service/connection_manager.vala +++ b/libdino/src/service/connection_manager.vala @@ -252,11 +252,32 @@ public class ConnectionManager : Object { } } + private void check_ping_reconnect(Xmpp.Xep.Ping.Module identity, + XmppStream stream, Account account) { + identity.send_ping.begin(stream, account.bare_jid.domain_jid, () => { + if (connections[account].stream != stream) return; + connections[account].acked = true; + change_connection_state(account, ConnectionState.CONNECTED); + }); + + Timeout.add_seconds(10, () => { + if (!connections.has_key(account)) return false; + if (connections[account].stream != stream) return false; + if (connections[account].acked) return false; + + // Reconnect. Nothing gets through the stream. + debug("[%s %p] Ping timeouted. Reconnecting", account.bare_jid.to_string(), stream); + change_connection_state(account, ConnectionState.DISCONNECTED); + + connections[account].reset(); + connect_stream.begin(account); + return false; + }); + } + private void check_reconnect(Account account, bool directly_reconnect = false) { if (!connections.has_key(account)) return; - XmppStream? stream = connections[account].stream; - var cancellable = new Cancellable(); debug(@"account.domainpart=$(account.domainpart)"); @@ -267,29 +288,23 @@ public class ConnectionManager : Object { debug(@"can-reach: $(reachable)"); if (reachable) { + Xmpp.Xep.Ping.Module? identity = null; if (connections[account].connection_state == ConnectionState.CONNECTING) return; - connections[account].acked = false; - change_connection_state(account, ConnectionState.CONNECTING); - stream.get_module(Xep.Ping.Module.IDENTITY).send_ping.begin(stream, account.bare_jid.domain_jid, () => { - connections[account].acked = true; - if (connections[account].stream != stream) return; - change_connection_state(account, ConnectionState.CONNECTED); - }); - - Timeout.add_seconds(10, () => { - if (!connections.has_key(account)) return false; - if (connections[account].stream != stream) return false; - if (connections[account].acked) return false; + XmppStream? stream = connections[account].stream; + connections[account].acked = false; - // Reconnect. Nothing gets through the stream. - debug("[%s %p] Ping timeouted. Reconnecting", account.bare_jid.to_string(), stream); + if (stream != null + && (identity = stream.get_module(Xep.Ping.Module.IDENTITY)) != null) { + change_connection_state(account, ConnectionState.CONNECTING); + check_ping_reconnect(identity, stream, account); + } + else { change_connection_state(account, ConnectionState.DISCONNECTED); connections[account].reset(); connect_stream.begin(account); - return false; - }); + } } } catch (Error e) { |
