Created
December 12, 2023 09:28
-
-
Save ffAudio/ef9553e14f257c1bae82048af03498bc to your computer and use it in GitHub Desktop.
JUCE-IPC-JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: this is a fraction of a backup, it is not supposed to work out of the box! | |
import * as path from 'path'; | |
import * as os from 'os'; | |
import {createConnection} from 'net'; | |
import {Socket} from 'net'; | |
import {v1} from 'uuid'; | |
import * as Promise from 'bluebird'; | |
import * as retry from 'bluebird-retry'; | |
import { EventEmitter } from 'events'; | |
// This is the magic header you specified when creating the juce::InterProcessConnection | |
const HEADER = 0x4d4c4946; | |
private recv(msg) { | |
const uuid = msg['uuid']; | |
const cb: AECommandCallback = this.callbackStore[uuid]; | |
if (cb) { | |
delete this.callbackStore[uuid]; | |
cb(msg); | |
} else { | |
this.emit('missing_callback_for_message', msg); | |
} | |
} | |
private withcb(msg): Promise<any> { | |
return new Promise((res, _) => { | |
const uuid = v1(); | |
this.callbackStore[uuid] = res; | |
msg['uuid'] = uuid; | |
this.send(msg); | |
}); | |
} | |
private send(message) { | |
this.client.then( (c) => { | |
c.write(this.msgToBuffer(message)); | |
}); | |
} | |
private msgToBuffer(msg: Object): Buffer { | |
const jsonObj = JSON.stringify(msg); | |
// BUG: length of unicode != string.length | |
// const jsonLen = jsonObj.length; | |
const jsonLen = Buffer.byteLength(jsonObj, 'utf-8'); | |
const buff = new Buffer(4 + 4 + jsonLen); | |
buff.writeInt32LE(HEADER, 0); | |
buff.writeInt32LE(jsonLen, 4); | |
buff.write(jsonObj, 8, jsonLen, 'utf-8'); | |
return buff; | |
} | |
private bufferToMsg(buff: Buffer): void { | |
this.respBuffer = Buffer.concat([this.respBuffer, buff]); | |
while (this.respBuffer.length > 0) { | |
const buffLength = this.respBuffer.length; | |
if (this.respBuffer.readInt32LE(0) == HEADER && buffLength >= 8) { | |
const msgLength = this.respBuffer.readInt32LE(4); | |
if (msgLength > 0) { | |
if (buffLength >= msgLength + 8) { | |
// Lets get the message content; | |
const content = this.respBuffer.toString('utf-8', 8, 8 + msgLength); | |
this.respBuffer = this.respBuffer.slice(8 + msgLength); | |
this.recv(JSON.parse(content)); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment