From 7e435b703de9ef184ea92d3822d9f85e3beebf79 Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Tue, 26 Mar 2019 11:01:36 +0100 Subject: Propagate disconnect() reason into 'disconnected' event --- slixmpp/xmlstream/xmlstream.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index db3d4684..9e407d7e 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -215,6 +215,9 @@ class XMLStream(asyncio.BaseProtocol): #: ``_xmpp-client._tcp`` service. self.dns_service = None + #: The reason why we are disconnecting from the server + self.disconnect_reason = None + #: An asyncio Future being done when the stream is disconnected. self.disconnected = asyncio.Future() @@ -268,6 +271,7 @@ class XMLStream(asyncio.BaseProtocol): localhost """ + self.disconnect_reason = None self.cancel_connection_attempt() if host and port: self.address = (host, int(port)) @@ -403,6 +407,7 @@ class XMLStream(asyncio.BaseProtocol): # The stream's root element has closed, # terminating the stream. log.debug("End of stream received") + self.disconnect_reason = "End of stream" self.abort() elif self.xml_depth == 1: # A stanza is an XML element that is a direct child of @@ -437,7 +442,7 @@ class XMLStream(asyncio.BaseProtocol): closure of the TCP connection """ log.info("connection_lost: %s", (exception,)) - self.event("disconnected") + self.event("disconnected", self.disconnect_reason or exception and exception.strerror) if self.end_session_on_disconnect: self.event('session_end') # All these objects are associated with one TCP connection. Since @@ -457,7 +462,7 @@ class XMLStream(asyncio.BaseProtocol): self._current_connection_attempt.cancel() self._current_connection_attempt = None - def disconnect(self, wait=2.0): + def disconnect(self, wait=2.0, reason=None): """Close the XML stream and wait for an acknowldgement from the server for at most `wait` seconds. After the given number of seconds has passed without a response from the serveur, or when the server @@ -470,6 +475,7 @@ class XMLStream(asyncio.BaseProtocol): :param wait: Time to wait for a response from the server. """ + self.disconnect_reason = reason self.cancel_connection_attempt() if self.transport: self.send_raw(self.stream_footer) @@ -488,12 +494,12 @@ class XMLStream(asyncio.BaseProtocol): self.disconnected.set_result(True) self.disconnected = asyncio.Future() - def reconnect(self, wait=2.0): + def reconnect(self, wait=2.0, reason="Reconnecting"): """Calls disconnect(), and once we are disconnected (after the timeout, or when the server acknowledgement is received), call connect() """ log.debug("reconnecting...") - self.disconnect(wait) + self.disconnect(wait, reason) self.add_event_handler('disconnected', self.connect, disposable=True) def configure_socket(self): -- cgit v1.2.3 From bcb90a653eef0564a7a6d604b1f7ec809a777520 Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Tue, 26 Mar 2019 11:02:28 +0100 Subject: Do not close stream on 0-timeout disconnect, allows 0198 resume --- slixmpp/xmlstream/xmlstream.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index 9e407d7e..1fe52cee 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -467,8 +467,8 @@ class XMLStream(asyncio.BaseProtocol): at most `wait` seconds. After the given number of seconds has passed without a response from the serveur, or when the server successfully responds with a closure of its own stream, abort() is - called. If wait is 0.0, this is almost equivalent to calling abort() - directly. + called. If wait is 0.0, this will call abort() directly without closing + the stream. Does nothing if we are not connected. @@ -478,7 +478,8 @@ class XMLStream(asyncio.BaseProtocol): self.disconnect_reason = reason self.cancel_connection_attempt() if self.transport: - self.send_raw(self.stream_footer) + if wait > 0.0: + self.send_raw(self.stream_footer) self.schedule('Disconnect wait', wait, self.abort, repeat=False) -- cgit v1.2.3 From 4f96e5fa75773f8f3c5df849edfca8315d746905 Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Tue, 26 Mar 2019 11:08:45 +0100 Subject: Do not directly enqueue connect() as event handler, parameter mismatch --- slixmpp/xmlstream/xmlstream.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index 1fe52cee..ce7e29d9 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -501,7 +501,7 @@ class XMLStream(asyncio.BaseProtocol): """ log.debug("reconnecting...") self.disconnect(wait, reason) - self.add_event_handler('disconnected', self.connect, disposable=True) + self.add_event_handler('disconnected', lambda event: self.connect(), disposable=True) def configure_socket(self): """Set timeout and other options for self.socket. -- cgit v1.2.3 From 6b1b58a33944c141d6cd504d045205b95c0969f5 Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Tue, 26 Mar 2019 11:09:24 +0100 Subject: XEP-0199: use new 0-timeout reconnect() with reason --- slixmpp/plugins/xep_0199/ping.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/slixmpp/plugins/xep_0199/ping.py b/slixmpp/plugins/xep_0199/ping.py index f1070305..adbf2203 100644 --- a/slixmpp/plugins/xep_0199/ping.py +++ b/slixmpp/plugins/xep_0199/ping.py @@ -112,9 +112,9 @@ class XEP_0199(BasePlugin): try: rtt = await self.ping(self.xmpp.boundjid.host, timeout=self.timeout) except IqTimeout: - log.debug("Did not receive ping back in time." + \ + log.debug("Did not receive ping back in time. " + \ "Requesting Reconnect.") - self.xmpp.reconnect() + self.xmpp.reconnect(0.0, "Ping timeout after %ds" % self.timeout) else: log.debug('Keepalive RTT: %s' % rtt) -- cgit v1.2.3 From 704161a285847c633fbe7edca56cfefc0f63c15e Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Tue, 26 Mar 2019 15:16:52 +0100 Subject: mark end-of-stream as session-ending event --- slixmpp/xmlstream/xmlstream.py | 1 + 1 file changed, 1 insertion(+) diff --git a/slixmpp/xmlstream/xmlstream.py b/slixmpp/xmlstream/xmlstream.py index ce7e29d9..17d23ff2 100644 --- a/slixmpp/xmlstream/xmlstream.py +++ b/slixmpp/xmlstream/xmlstream.py @@ -406,6 +406,7 @@ class XMLStream(asyncio.BaseProtocol): if self.xml_depth == 0: # The stream's root element has closed, # terminating the stream. + self.end_session_on_disconnect = True log.debug("End of stream received") self.disconnect_reason = "End of stream" self.abort() -- cgit v1.2.3