From d0f95935a9d49f087a54140efb20e1afb8fda9c4 Mon Sep 17 00:00:00 2001 From: oupson Date: Sun, 26 Feb 2023 16:09:17 +0100 Subject: [PATCH] Move ssh and slave watcher to functions --- src/terminalpane.vala | 76 +++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 36 deletions(-) diff --git a/src/terminalpane.vala b/src/terminalpane.vala index 788b5a7..ca06fb2 100644 --- a/src/terminalpane.vala +++ b/src/terminalpane.vala @@ -145,48 +145,52 @@ namespace FooTerm { slave_channel.set_encoding (null); slave_channel.set_buffered (false); - sock_channel.add_watch (GLib.IOCondition.IN, (source, condition) => { - if (condition == IOCondition.HUP) { - print ("The connection has been broken.\n"); - return false; - } + sock_channel.add_watch (GLib.IOCondition.IN, this.on_ssh_event); + slave_channel.add_watch (GLib.IOCondition.IN, this.on_slave_event); + } + } - try { - var buffer = new uint8[1024]; - var size = this.channel.read(buffer); + private bool on_ssh_event(GLib.IOChannel source, GLib.IOCondition condition) { + if (condition == IOCondition.HUP) { + print ("The connection has been broken.\n"); + return false; + } - if (Posix.write(this.slave_pty, buffer, size) < 0) { - throw GLib.IOError.from_errno(Posix.errno); - } + try { + var buffer = new uint8[1024]; + var size = this.channel.read(buffer); + debug("Got %zu from ssh", size); - return true; - } catch(Error e) { - GLib.warning("Failed to read from ssh : %s", e.message); - return false; - } - }); + if (Posix.write(this.slave_pty, buffer, size) < 0) { + throw GLib.IOError.from_errno(Posix.errno); + } - slave_channel.add_watch (GLib.IOCondition.IN, (source, condition) => { - if (condition == IOCondition.HUP) { - print ("The connection has been broken.\n"); - return false; - } + return true; + } catch(Error e) { + GLib.warning("Failed to read from ssh : %s", e.message); + return false; + } + } - try { - var buffer = new char[1024]; - size_t size = 0; - source.read_chars(buffer, out size); + private bool on_slave_event(GLib.IOChannel source, GLib.IOCondition condition) { + if (condition == IOCondition.HUP) { + print ("The connection has been broken.\n"); + return false; + } - var res = this.channel.write ((uint8[])buffer[0:size]); - if (res < 0) { - warning("Channel write failed with %zu", res); - } - return true; - } catch (Error e) { - GLib.warning("Failed to read from terminal : %s", e.message); - return false; - } - }); + try { + var buffer = new char[1024]; + size_t size = 0; + source.read_chars(buffer, out size); + + var res = this.channel.write ((uint8[])buffer[0:size]); + if (res < 0) { + warning("Channel write failed with %zu", res); + } + return true; + } catch (Error e) { + GLib.warning("Failed to read from terminal : %s", e.message); + return false; } } }