Move ssh and slave watcher to functions

This commit is contained in:
oupson 2023-02-26 16:09:17 +01:00
parent 287474bd41
commit d0f95935a9
Signed by: oupson
GPG Key ID: 3BD88615552EFCB7
1 changed files with 40 additions and 36 deletions

View File

@ -145,48 +145,52 @@ namespace FooTerm {
slave_channel.set_encoding (null); slave_channel.set_encoding (null);
slave_channel.set_buffered (false); slave_channel.set_buffered (false);
sock_channel.add_watch (GLib.IOCondition.IN, (source, condition) => { sock_channel.add_watch (GLib.IOCondition.IN, this.on_ssh_event);
if (condition == IOCondition.HUP) { slave_channel.add_watch (GLib.IOCondition.IN, this.on_slave_event);
print ("The connection has been broken.\n"); }
return false; }
}
try { private bool on_ssh_event(GLib.IOChannel source, GLib.IOCondition condition) {
var buffer = new uint8[1024]; if (condition == IOCondition.HUP) {
var size = this.channel.read(buffer); print ("The connection has been broken.\n");
return false;
}
if (Posix.write(this.slave_pty, buffer, size) < 0) { try {
throw GLib.IOError.from_errno(Posix.errno); var buffer = new uint8[1024];
} var size = this.channel.read(buffer);
debug("Got %zu from ssh", size);
return true; if (Posix.write(this.slave_pty, buffer, size) < 0) {
} catch(Error e) { throw GLib.IOError.from_errno(Posix.errno);
GLib.warning("Failed to read from ssh : %s", e.message); }
return false;
}
});
slave_channel.add_watch (GLib.IOCondition.IN, (source, condition) => { return true;
if (condition == IOCondition.HUP) { } catch(Error e) {
print ("The connection has been broken.\n"); GLib.warning("Failed to read from ssh : %s", e.message);
return false; return false;
} }
}
try { private bool on_slave_event(GLib.IOChannel source, GLib.IOCondition condition) {
var buffer = new char[1024]; if (condition == IOCondition.HUP) {
size_t size = 0; print ("The connection has been broken.\n");
source.read_chars(buffer, out size); return false;
}
var res = this.channel.write ((uint8[])buffer[0:size]); try {
if (res < 0) { var buffer = new char[1024];
warning("Channel write failed with %zu", res); size_t size = 0;
} source.read_chars(buffer, out size);
return true;
} catch (Error e) { var res = this.channel.write ((uint8[])buffer[0:size]);
GLib.warning("Failed to read from terminal : %s", e.message); if (res < 0) {
return false; warning("Channel write failed with %zu", res);
} }
}); return true;
} catch (Error e) {
GLib.warning("Failed to read from terminal : %s", e.message);
return false;
} }
} }
} }