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 : :) --- players.js | 33 +++++++++++++++++++++++++-------- socat.js | 26 ++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/players.js b/players.js index f2cced2..5d332b3 100644 --- a/players.js +++ b/players.js @@ -4,6 +4,10 @@ const { spawn } = require('child_process') const { commandExists, killProcess, getTimeStrings } = require('./general-util') const EventEmitter = require('events') const Socat = require('./socat') +const fs = require('fs') +const util = require('util') + +const unlink = util.promisify(fs.unlink) class Player extends EventEmitter { constructor() { @@ -120,16 +124,19 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { } playFile(file) { - let path + this.removeSocket(this.socketPath) + do { - // path = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000) - path = './mtui-socket-' + Math.floor(Math.random() * 10000) - } while (this.existsSync(path)) + // this.socketPathpath = '/tmp/mtui-socket-' + Math.floor(Math.random() * 10000) + this.socketPath = __dirname + '/mtui-socket-' + Math.floor(Math.random() * 10000) + } while (this.existsSync(this.socketPath)) - this.socat = new Socat(path) + this.socat = new Socat(this.socketPath) const mpv = super.playFile(file) + mpv.then(() => this.removeSocket(this.socketPath)) + return mpv } @@ -191,11 +198,21 @@ module.exports.ControllableMPVPlayer = class extends module.exports.MPVPlayer { this.sendCommand('set', 'loop', this.isLooping) } - kill() { + async kill() { + const path = this.socketPath + delete this.socketPath if (this.socat) { - this.socat.stop() + await this.socat.dispose() + await this.socat.stop() + } + await super.kill() + await this.removeSocket(path) + } + + async removeSocket(path) { + if (path) { + await unlink(path).catch(() => {}) } - return super.kill() } } 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