From fd09d0196f8db2102f9364a56f3075bf2cd93c88 Mon Sep 17 00:00:00 2001 From: Florrie Date: Thu, 6 Feb 2020 17:40:33 -0400 Subject: more socat stuff : shrug emoji : :) --- socat.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'socat.js') diff --git a/socat.js b/socat.js index 8c6b9ed..95ee889 100644 --- a/socat.js +++ b/socat.js @@ -33,19 +33,37 @@ module.exports = class Socat extends EventEmitter { } } - stop() { - if (this.subprocess) { - killProcess(this.subprocess) + async stop() { + const proc = this.subprocess + if (proc) { this.subprocess = null + await killProcess(proc) } } + async dispose() { + // Don't accept any more messages. + this.disposed = true + await this.stop() + } + async send(message) { + if (this.disposed) { + return + } if (!this.subprocess) { await this.start() } if (this.subprocess) { - this.subprocess.stdin.write(message + '\r\n') + try { + this.subprocess.stdin.write(message + '\r\n') + } catch (error) { + // There's no guarantee we'll actually suceed to write - the process + // or pipe we're writing to could have closed unexpectedly. If that + // happens, unestablish the socat process; it'll try to reconnect if + // we send another message. + this.stop(); + } } else { try { await writeFile(path.resolve(__dirname, this.path), message + '\r\n') -- cgit 1.3.0-6-gf8a5