package: rename browser to client and main to server

This commit is contained in:
bordun 2018-10-28 15:06:11 +01:00
parent fd2f6b8af7
commit 263eb84780
123 changed files with 43 additions and 42 deletions

234
src/server/osc/index.js Normal file
View file

@ -0,0 +1,234 @@
// destroy serialport module
// (electron-rebuild breaks it for node-only mode)
require('vmodule')('serialport', {}, {global: true})
var ipc = require('../server').ipc,
settings = require('../settings'),
tcpInPort = settings.read('tcpInPort'),
debug = settings.read('debug'),
vm = require('vm'),
fs = require('fs'),
midi = settings.read('midi') ? require('../midi') : false,
oscUDPServer = require('./udp'),
oscTCPServer = require('./tcp'),
EventEmitter = require('events').EventEmitter
class OscServer {
constructor(){
this.customModuleEventEmitter = new EventEmitter()
this.customModule = (()=>{
if (!settings.read('customModule')) return false
var file = (()=>{
try {
return fs.readFileSync(settings.read('customModule'),'utf8')
} catch(err) {
console.log('CustomModule Error: File not found: ' + settings.read('customModule'))
return false
}
})(),
mod,
context = {
console,
sendOsc: this.sendOsc.bind(this),
receiveOsc: this.receiveOsc.bind(this),
app: this.customModuleEventEmitter,
setTimeout,
clearTimeout,
setInterval,
clearInterval,
settings
}
try {
mod = vm.runInContext(file, vm.createContext(context))
} catch(err) {
console.error(err)
return false
}
return mod
})()
}
parseType(type){
var t = type[0].match(/[bhtdscrmifTFNI]/)
if (t!==null) {
return t[0]
} else {
return 's'
}
}
parseArg(arg, precision){
if (arg==null) return null
switch (typeof arg) {
case 'number':
var typeTagMatch = typeof precision == 'string' ? precision.match(/[0-9]+([a-zA-Z]{1})/) : null,
typeTag = typeTagMatch === null ? precision == 0 ? 'i' : 'f' : typeTagMatch[1],
precisionValue = parseFloat(precision) || 0
return {type: typeTag, value: parseFloat(arg.toFixed(precisionValue))}
case 'boolean':
return {type: arg ? 'T' : 'F', value: arg}
case 'object':
if (arg.type) {
return {type: this.parseType(arg.type), value: arg.value}
} else {
return {type: 's', value:JSON.stringify(arg)}
}
case 'string':
return {type: 's', value:arg}
default:
return {type: 's', value:JSON.stringify(arg)}
}
}
sendOsc(data){
if (!data) return
if (data.host == 'midi') {
if (midi) midi.send(data)
} else {
if (typeof data.address !== 'string' || data.address[0] !== '/') {
console.error('OSC error: malformed address: "' + data.address + '')
return
}
oscUDPServer.send({
address: data.address,
args: data.args
}, data.host, data.port)
if (tcpInPort && oscTCPServer.clients[data.host] && oscTCPServer.clients[data.host][data.port]) {
oscTCPServer.clients[data.host][data.port].send({
address: data.address,
args: data.args
})
}
if (debug) console.log('OSC sent: ',{address:data.address, args: data.args}, 'To : ' + data.host + ':' + data.port)
}
}
receiveOsc(data, info){
if (!data) return
for (i in data.args) {
data.args[i] = data.args[i].value
}
if (data.args.length==1) data.args = data.args[0]
ipc.send('receiveOsc',data)
if (debug) console.log('OSC received: ', {address:data.address, args: data.args}, 'From : ' + data.host + ':' + data.port)
}
oscInFilter(data){
if (this.customModule.oscInFilter) {
return this.customModule.oscInFilter(data)
} else {
return data
}
}
oscOutFilter(data){
if (this.customModule.oscOutFilter) {
return this.customModule.oscOutFilter(data)
} else {
return data
}
}
oscInHandler(msg, timetag, info){
var data = this.oscInFilter({address: msg.address, args: msg.args, host: info.address, port: info.port})
if (!data) return
this.receiveOsc(data)
}
oscInHandlerWrapper(msg, timetag, info) {
var delay = timetag? Math.max(0,timetag.native - Date.now()) : 0
if (delay) {
setTimeout(()=>{
this.oscInHandler(msg, timetag, info)
}, delay)
} else {
this.oscInHandler(msg, timetag, info)
}
}
init(){
if (midi) midi.init((data)=>{
data = this.oscInFilter({address: data.address, args: data.args, host: data.host, port: data.port})
if (!data) return
this.receiveOsc(data)
})
oscUDPServer.on('message', this.oscInHandlerWrapper.bind(this))
oscUDPServer.open()
if (settings.read('tcpInPort')) {
oscTCPServer.on('message', this.oscInHandlerWrapper.bind(this))
oscTCPServer.open()
}
if (this.customModule.init) {
this.customModule.init()
}
}
}
var oscServer = new OscServer()
oscServer.init()
module.exports = {
server: oscServer,
send: function(host,port,address,args,precision) {
var message = []
if (!Array.isArray(args)) args = [args]
if (typeof args=='object' && args!==null) {
for (var i in args) {
var arg = oscServer.parseArg(args[i],precision)
if (arg!=null) message.push(arg)
}
}
var data = oscServer.oscOutFilter({address:address, args: message, host: host, port: port})
oscServer.sendOsc(data)
},
midi:midi
}

146
src/server/osc/tcp.js Normal file
View file

@ -0,0 +1,146 @@
var osc = require('osc'),
EventEmitter = require('events').EventEmitter,
settings = require('../settings'),
zeroconf = require('../zeroconf'),
tcpInPort = settings.read('tcpInPort'),
tcpTargets = settings.read('tcpTargets'),
net = require('net')
class OscTCPClient extends EventEmitter {
constructor(options) {
super()
this.options = options
this.remoteAddress = options.address
this.remotePort = options.port
this.open()
}
open() {
this.port = new osc.TCPSocketPort(this.options)
this.port.on('error', (err)=>{
console.error(`TCP: Error: ${err.message}`)
})
this.port.on('close', (hadErr)=>{
console.log(`TCP: connection closed (${this.remoteAddress}:${this.remotePort})`)
if (!this.options.socket) {
console.log(`TCP: attempting reconnection in 5s (${this.remoteAddress}:${this.remotePort})`)
this.reconnect()
} else {
this.emit('die')
}
})
this.port.on('message', (msg, timetag)=>{
this.emit('message', msg, timetag, {address: this.remoteAddress, port: this.remotePort})
})
this.port.on('ready', ()=>{
console.log(`TCP: connection established (${this.remoteAddress}:${this.remotePort})`)
})
if (!this.options.socket) {
this.port.open()
} else {
this.port.emit('ready')
}
}
reconnect() {
setTimeout(()=>{
this.open()
}, 5000)
}
send(msg) {
this.port.send(msg)
}
}
class OscTCPServer extends EventEmitter {
constructor() {
super()
this.clients = {}
}
bindSocket(socket) {
this.addClient(new OscTCPClient({
metadata: true,
socket: socket,
address: socket.remoteAddress.indexOf('::ffff:') === 0 ? socket.remoteAddress.replace('::ffff:', '') : socket.remoteAddress,
port: socket.remotePort
}))
}
addClient(client) {
this.clients[client.remoteAddress] = this.clients[client.remoteAddress] || {}
this.clients[client.remoteAddress][client.remotePort] = client
client.on('message', (msg, timetag, info)=>{
this.emit('message', msg, timetag, info)
})
client.on('die', (hadError)=>{
if (this.clients[client.remoteAddress]) delete this.clients[client.remoteAddress][client.remotePort]
})
}
open() {
this.server = net.createServer(this.bindSocket.bind(this))
this.server.listen({port: tcpInPort})
for (var i in tcpTargets) {
this.addClient(new OscTCPClient({
metadata: true,
address: tcpTargets[i].split(':')[0],
port: tcpTargets[i].split(':')[1]
}))
}
zeroconf.publish({
name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''),
protocol: 'tcp',
type: 'osc',
port: tcpInPort
}).on('error', (e)=>{
console.error(`Error: Zeroconf: ${e.message}`)
})
}
}
var oscTCPServer = new OscTCPServer()
module.exports = oscTCPServer

26
src/server/osc/udp.js Normal file
View file

@ -0,0 +1,26 @@
var osc = require('osc'),
settings = require('../settings'),
zeroconf = require('../zeroconf'),
oscInPort = settings.read('oscInPort') || settings.read('httpPort')
var oscUDPServer = new osc.UDPPort({
localAddress: '0.0.0.0',
localPort: oscInPort,
metadata: true,
broadcast: true
})
oscUDPServer.on('error', function(error) {
console.error(error)
})
zeroconf.publish({
name: settings.read('appName') + (settings.read('instanceName') ? ' (' + settings.read('instanceName') + ')' : ''),
protocol: 'udp',
type: 'osc',
port: oscInPort
}).on('error', (e)=>{
console.error(`Error: Zeroconf: ${e.message}`)
})
module.exports = oscUDPServer