| 1 |
/** |
| 2 |
* Copyright (c) 2019 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { IOscHandler, IHandlerCollection, OscFallbackHandlerType, IOscParser, ISubParserStackState } from 'common/parser/Types'; |
| 7 |
import { OscState, PAYLOAD_LIMIT } from 'common/parser/Constants'; |
| 8 |
import { utf32ToString } from 'common/input/TextDecoder'; |
| 9 |
import { IDisposable } from 'common/Types'; |
| 10 |
|
| 11 |
const EMPTY_HANDLERS: IOscHandler[] = []; |
| 12 |
|
| 13 |
export class OscParser implements IOscParser { |
| 14 |
private _state = OscState.START; |
| 15 |
private _active = EMPTY_HANDLERS; |
| 16 |
private _id = -1; |
| 17 |
private _handlers: IHandlerCollection<IOscHandler> = Object.create(null); |
| 18 |
private _handlerFb: OscFallbackHandlerType = () => { }; |
| 19 |
private _stack: ISubParserStackState = { |
| 20 |
paused: false, |
| 21 |
loopPosition: 0, |
| 22 |
fallThrough: false |
| 23 |
}; |
| 24 |
|
| 25 |
public registerHandler(ident: number, handler: IOscHandler): IDisposable { |
| 26 |
if (this._handlers[ident] === undefined) { |
| 27 |
this._handlers[ident] = []; |
| 28 |
} |
| 29 |
const handlerList = this._handlers[ident]; |
| 30 |
handlerList.push(handler); |
| 31 |
return { |
| 32 |
dispose: () => { |
| 33 |
const handlerIndex = handlerList.indexOf(handler); |
| 34 |
if (handlerIndex !== -1) { |
| 35 |
handlerList.splice(handlerIndex, 1); |
| 36 |
} |
| 37 |
} |
| 38 |
}; |
| 39 |
} |
| 40 |
public clearHandler(ident: number): void { |
| 41 |
if (this._handlers[ident]) delete this._handlers[ident]; |
| 42 |
} |
| 43 |
public setHandlerFallback(handler: OscFallbackHandlerType): void { |
| 44 |
this._handlerFb = handler; |
| 45 |
} |
| 46 |
|
| 47 |
public dispose(): void { |
| 48 |
this._handlers = Object.create(null); |
| 49 |
this._handlerFb = () => { }; |
| 50 |
this._active = EMPTY_HANDLERS; |
| 51 |
} |
| 52 |
|
| 53 |
public reset(): void { |
| 54 |
// force cleanup handlers if payload was already sent |
| 55 |
if (this._state === OscState.PAYLOAD) { |
| 56 |
for (let j = this._stack.paused ? this._stack.loopPosition - 1 : this._active.length - 1; j >= 0; --j) { |
| 57 |
this._active[j].end(false); |
| 58 |
} |
| 59 |
} |
| 60 |
this._stack.paused = false; |
| 61 |
this._active = EMPTY_HANDLERS; |
| 62 |
this._id = -1; |
| 63 |
this._state = OscState.START; |
| 64 |
} |
| 65 |
|
| 66 |
private _start(): void { |
| 67 |
this._active = this._handlers[this._id] || EMPTY_HANDLERS; |
| 68 |
if (!this._active.length) { |
| 69 |
this._handlerFb(this._id, 'START'); |
| 70 |
} else { |
| 71 |
for (let j = this._active.length - 1; j >= 0; j--) { |
| 72 |
this._active[j].start(); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
private _put(data: Uint32Array, start: number, end: number): void { |
| 78 |
if (!this._active.length) { |
| 79 |
this._handlerFb(this._id, 'PUT', utf32ToString(data, start, end)); |
| 80 |
} else { |
| 81 |
for (let j = this._active.length - 1; j >= 0; j--) { |
| 82 |
this._active[j].put(data, start, end); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public start(): void { |
| 88 |
// always reset leftover handlers |
| 89 |
this.reset(); |
| 90 |
this._state = OscState.ID; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Put data to current OSC command. |
| 95 |
* Expects the identifier of the OSC command in the form |
| 96 |
* OSC id ; payload ST/BEL |
| 97 |
* Payload chunks are not further processed and get |
| 98 |
* directly passed to the handlers. |
| 99 |
*/ |
| 100 |
public put(data: Uint32Array, start: number, end: number): void { |
| 101 |
if (this._state === OscState.ABORT) { |
| 102 |
return; |
| 103 |
} |
| 104 |
if (this._state === OscState.ID) { |
| 105 |
while (start < end) { |
| 106 |
const code = data[start++]; |
| 107 |
if (code === 0x3b) { |
| 108 |
this._state = OscState.PAYLOAD; |
| 109 |
this._start(); |
| 110 |
break; |
| 111 |
} |
| 112 |
if (code < 0x30 || 0x39 < code) { |
| 113 |
this._state = OscState.ABORT; |
| 114 |
return; |
| 115 |
} |
| 116 |
if (this._id === -1) { |
| 117 |
this._id = 0; |
| 118 |
} |
| 119 |
this._id = this._id * 10 + code - 48; |
| 120 |
} |
| 121 |
} |
| 122 |
if (this._state === OscState.PAYLOAD && end - start > 0) { |
| 123 |
this._put(data, start, end); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Indicates end of an OSC command. |
| 129 |
* Whether the OSC got aborted or finished normally |
| 130 |
* is indicated by `success`. |
| 131 |
*/ |
| 132 |
public end(success: boolean, promiseResult: boolean = true): void | Promise<boolean> { |
| 133 |
if (this._state === OscState.START) { |
| 134 |
return; |
| 135 |
} |
| 136 |
// do nothing if command was faulty |
| 137 |
if (this._state !== OscState.ABORT) { |
| 138 |
// if we are still in ID state and get an early end |
| 139 |
// means that the command has no payload thus we still have |
| 140 |
// to announce START and send END right after |
| 141 |
if (this._state === OscState.ID) { |
| 142 |
this._start(); |
| 143 |
} |
| 144 |
|
| 145 |
if (!this._active.length) { |
| 146 |
this._handlerFb(this._id, 'END', success); |
| 147 |
} else { |
| 148 |
let handlerResult: boolean | Promise<boolean> = false; |
| 149 |
let j = this._active.length - 1; |
| 150 |
let fallThrough = false; |
| 151 |
if (this._stack.paused) { |
| 152 |
j = this._stack.loopPosition - 1; |
| 153 |
handlerResult = promiseResult; |
| 154 |
fallThrough = this._stack.fallThrough; |
| 155 |
this._stack.paused = false; |
| 156 |
} |
| 157 |
if (!fallThrough && handlerResult === false) { |
| 158 |
for (; j >= 0; j--) { |
| 159 |
handlerResult = this._active[j].end(success); |
| 160 |
if (handlerResult === true) { |
| 161 |
break; |
| 162 |
} else if (handlerResult instanceof Promise) { |
| 163 |
this._stack.paused = true; |
| 164 |
this._stack.loopPosition = j; |
| 165 |
this._stack.fallThrough = false; |
| 166 |
return handlerResult; |
| 167 |
} |
| 168 |
} |
| 169 |
j--; |
| 170 |
} |
| 171 |
// cleanup left over handlers |
| 172 |
// we always have to call .end for proper cleanup, |
| 173 |
// here we use `success` to indicate whether a handler should execute |
| 174 |
for (; j >= 0; j--) { |
| 175 |
handlerResult = this._active[j].end(false); |
| 176 |
if (handlerResult instanceof Promise) { |
| 177 |
this._stack.paused = true; |
| 178 |
this._stack.loopPosition = j; |
| 179 |
this._stack.fallThrough = true; |
| 180 |
return handlerResult; |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
this._active = EMPTY_HANDLERS; |
| 187 |
this._id = -1; |
| 188 |
this._state = OscState.START; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Convenient class to allow attaching string based handler functions |
| 194 |
* as OSC handlers. |
| 195 |
*/ |
| 196 |
export class OscHandler implements IOscHandler { |
| 197 |
private _data = ''; |
| 198 |
private _hitLimit: boolean = false; |
| 199 |
|
| 200 |
constructor(private _handler: (data: string) => boolean | Promise<boolean>) { } |
| 201 |
|
| 202 |
public start(): void { |
| 203 |
this._data = ''; |
| 204 |
this._hitLimit = false; |
| 205 |
} |
| 206 |
|
| 207 |
public put(data: Uint32Array, start: number, end: number): void { |
| 208 |
if (this._hitLimit) { |
| 209 |
return; |
| 210 |
} |
| 211 |
this._data += utf32ToString(data, start, end); |
| 212 |
if (this._data.length > PAYLOAD_LIMIT) { |
| 213 |
this._data = ''; |
| 214 |
this._hitLimit = true; |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
public end(success: boolean): boolean | Promise<boolean> { |
| 219 |
let ret: boolean | Promise<boolean> = false; |
| 220 |
if (this._hitLimit) { |
| 221 |
ret = false; |
| 222 |
} else if (success) { |
| 223 |
ret = this._handler(this._data); |
| 224 |
if (ret instanceof Promise) { |
| 225 |
// need to hold data until `ret` got resolved |
| 226 |
// dont care for errors, data will be freed anyway on next start |
| 227 |
return ret.then(res => { |
| 228 |
this._data = ''; |
| 229 |
this._hitLimit = false; |
| 230 |
return res; |
| 231 |
}); |
| 232 |
} |
| 233 |
} |
| 234 |
this._data = ''; |
| 235 |
this._hitLimit = false; |
| 236 |
return ret; |
| 237 |
} |
| 238 |
} |