« get me outta code hell

more socat stuff : shrug emoji : :) - mtui - Music Text User Interface - user-friendly command line music player
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2020-02-06 17:40:33 -0400
committerFlorrie <towerofnix@gmail.com>2020-02-06 17:40:33 -0400
commitfd09d0196f8db2102f9364a56f3075bf2cd93c88 (patch)
treeae517e71ad9d1d96e0cb37a7e17c867cb1a3fefb
parent9a0316cf5214f1eb9f13651c9b3f7cc852ee1bd1 (diff)
more socat stuff : shrug emoji : :)
-rw-r--r--players.js33
-rw-r--r--socat.js26
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')