mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-11 18:19:58 +00:00
Add additional error/lifetime handling to bridge
This commit is contained in:
@@ -26,18 +26,6 @@ import { __applicationName
|
|||||||
events.EventEmitter.defaultMaxListeners = 50;
|
events.EventEmitter.defaultMaxListeners = 50;
|
||||||
|
|
||||||
|
|
||||||
const browser = new dnssd.Browser(dnssd.tcp("googlecast"));
|
|
||||||
|
|
||||||
// Local media server
|
|
||||||
let mediaServer: http.Server;
|
|
||||||
|
|
||||||
process.on("SIGTERM", () => {
|
|
||||||
if (mediaServer && mediaServer.listening) {
|
|
||||||
mediaServer.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
const decodeTransform = new DecodeTransform();
|
const decodeTransform = new DecodeTransform();
|
||||||
const encodeTransform = new EncodeTransform();
|
const encodeTransform = new EncodeTransform();
|
||||||
|
|
||||||
@@ -46,6 +34,10 @@ process.stdin.pipe(decodeTransform);
|
|||||||
decodeTransform.on("data", handleMessage);
|
decodeTransform.on("data", handleMessage);
|
||||||
encodeTransform.pipe(process.stdout);
|
encodeTransform.pipe(process.stdout);
|
||||||
|
|
||||||
|
decodeTransform.on("error", err => {
|
||||||
|
console.error("Failed to decode message", err);
|
||||||
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode and send a message to the extension. If message is
|
* Encode and send a message to the extension. If message is
|
||||||
* a string, send that as the message subject, else send a
|
* a string, send that as the message subject, else send a
|
||||||
@@ -70,12 +62,33 @@ interface InitializeOptions {
|
|||||||
shouldWatchStatus?: boolean;
|
shouldWatchStatus?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let receiverSelectorApp: child_process.ChildProcess;
|
||||||
|
let receiverSelectorAppClosed = true;
|
||||||
|
|
||||||
|
// Local media server
|
||||||
|
let mediaServer: http.Server;
|
||||||
|
|
||||||
|
let browser: dnssd.Browser;
|
||||||
|
|
||||||
|
|
||||||
// Existing counterpart Media/Session objects
|
// Existing counterpart Media/Session objects
|
||||||
const existingSessions: Map<string, Session> = new Map();
|
const existingSessions: Map<string, Session> = new Map();
|
||||||
const existingMedia: Map<string, Media> = new Map();
|
const existingMedia: Map<string, Media> = new Map();
|
||||||
|
|
||||||
let receiverSelectorApp: child_process.ChildProcess;
|
|
||||||
let receiverSelectorAppClosed = true;
|
process.on("SIGTERM", () => {
|
||||||
|
if (mediaServer && mediaServer.listening) {
|
||||||
|
mediaServer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (receiverSelectorApp && !receiverSelectorAppClosed) {
|
||||||
|
receiverSelectorApp.kill()
|
||||||
|
}
|
||||||
|
|
||||||
|
browser.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle incoming messages from the extension and forward
|
* Handle incoming messages from the extension and forward
|
||||||
@@ -86,7 +99,12 @@ let receiverSelectorAppClosed = true;
|
|||||||
*/
|
*/
|
||||||
async function handleMessage (message: Message) {
|
async function handleMessage (message: Message) {
|
||||||
if (message.subject.startsWith("bridge:/media/")) {
|
if (message.subject.startsWith("bridge:/media/")) {
|
||||||
const mediaId = message._id!;
|
if (!message._id) {
|
||||||
|
console.error("Media message missing _id");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mediaId = message._id;
|
||||||
|
|
||||||
if (existingMedia.has(mediaId)) {
|
if (existingMedia.has(mediaId)) {
|
||||||
// Forward message to instance message handler
|
// Forward message to instance message handler
|
||||||
@@ -111,7 +129,12 @@ async function handleMessage (message: Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (message.subject.startsWith("bridge:/session/")) {
|
if (message.subject.startsWith("bridge:/session/")) {
|
||||||
const sessionId = message._id!;
|
if (!message._id) {
|
||||||
|
console.error("Session message missing _id");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sessionId = message._id;
|
||||||
|
|
||||||
if (existingSessions.has(sessionId)) {
|
if (existingSessions.has(sessionId)) {
|
||||||
// Forward message to instance message handler
|
// Forward message to instance message handler
|
||||||
@@ -175,6 +198,11 @@ function handleReceiverSelectorMessage (message: Message) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Kill existing process if it exists
|
||||||
|
if (receiverSelectorApp && !receiverSelectorAppClosed) {
|
||||||
|
receiverSelectorApp.kill();
|
||||||
|
}
|
||||||
|
|
||||||
receiverSelectorApp = child_process.spawn(
|
receiverSelectorApp = child_process.spawn(
|
||||||
path.join(process.cwd(), "selector")
|
path.join(process.cwd(), "selector")
|
||||||
, [ receiverSelectorData ]);
|
, [ receiverSelectorData ]);
|
||||||
@@ -290,6 +318,11 @@ function handleMediaServerMessage (message: Message) {
|
|||||||
|
|
||||||
|
|
||||||
function initialize (options: InitializeOptions) {
|
function initialize (options: InitializeOptions) {
|
||||||
|
browser = new dnssd.Browser(dnssd.tcp("googlecast"));
|
||||||
|
browser.on("error", err => {
|
||||||
|
console.error("Discovery failed", err);
|
||||||
|
});
|
||||||
|
|
||||||
if (options.shouldWatchStatus) {
|
if (options.shouldWatchStatus) {
|
||||||
browser.on("serviceUp", onStatusBrowserServiceUp);
|
browser.on("serviceUp", onStatusBrowserServiceUp);
|
||||||
browser.on("serviceDown", onStatusBrowserServiceDown);
|
browser.on("serviceDown", onStatusBrowserServiceDown);
|
||||||
|
|||||||
Reference in New Issue
Block a user