diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-10-06 17:34:29 +0100 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2024-10-06 17:34:29 +0100 |
commit | db10dc4fdd656b25442c63eb02ea30931eee300e (patch) | |
tree | 9b2a4e835d6d52b48c0e7fb9bcb83884999ee1da /src/relay | |
parent | 0dc8409b168b293e9566dc5a35bded510fbd043d (diff) |
window: Add interface for connecting and disconnecting
Diffstat (limited to 'src/relay')
-rw-r--r-- | src/relay/network.py | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/src/relay/network.py b/src/relay/network.py index 4308ecf..b502ddd 100644 --- a/src/relay/network.py +++ b/src/relay/network.py @@ -212,7 +212,7 @@ class Network(GObject.GObject): """Slot: data available on socket.""" try: gbytes = self.input.read_bytes_finish(res) - except GLib.Error as err: + except GLib.GError as err: self.handle_network_error(err) return @@ -281,6 +281,9 @@ class Network(GObject.GObject): except ValueError: self._lines = config.CONFIG_DEFAULT_RELAY_LINES + if self.cancel_network_reads.is_cancelled(): + self.cancel_network_reads.reset() + # TODO handle SSL self._socketclient.connect_async( Gio.NetworkAddress.new(self._hostname, self._port), @@ -306,15 +309,33 @@ class Network(GObject.GObject): def disconnect_weechat(self): """Disconnect from WeeChat.""" - if self._socket.state() == QtNetwork.QAbstractSocket.UnconnectedState: - self.set_status(STATUS_DISCONNECTED) + if not self._socket.is_connected(): return - if self._socket.state() == QtNetwork.QAbstractSocket.ConnectedState: - self.send_to_weechat('quit\n') - self._socket.waitForBytesWritten(1000) else: + self.send_to_weechat('quit\n') + self._socket.set_graceful_disconnect(True) + self._socket.close() + self._socket = None + self.cancel_network_reads.cancel() + self.ssh_tunnel.stop() + self.ssh_tunnel = None self.set_status(STATUS_DISCONNECTED) - self._socket.abort() + + def handle_network_error(self, err): + if err.matches(Gio.io_error_quark(), Gio.IOErrorEnum.CANCELLED): + print("Connection has been canceled by user.") + return + elif err.matches(Gio.tls_error_quark(), Gio.TlsError.EOF): + print("Server has closed the connection.") + return + elif err.matches(Gio.io_error_quark(), Gio.IOErrorEnum.BROKEN_PIPE): + print("Broken pipe, connection lost.") + return + elif err.matches(Gio.io_error_quark(), Gio.IOErrorEnum.TIMED_OUT): + print("Connection timed out.") + return + else: + raise def send_to_weechat(self, message): """Send a message to WeeChat.""" @@ -402,3 +423,4 @@ class Network(GObject.GObject): self.debug_dialog.chat.scroll_bottom() + |