fix osc bundles not parsing multiple content chunks correctly

This commit is contained in:
2024-10-13 16:41:49 -05:00
parent 7a3ad57482
commit b44e5d306a
+19 -9
View File
@@ -22,14 +22,23 @@ export function bundleFromBuffer(bytes: Uint8Array): [OSCBundle | undefined, Uin
let endOfBundle = false; let endOfBundle = false;
let [contentSize, remainingBytes] = oscTypeConverterMap.i.fromBuffer(bytesAfterTimeTag); // let [contentSize, remainingBytes] = oscTypeConverterMap.i.fromBuffer(bytesAfterTimeTag);
if (typeof contentSize === 'number') { let remainingBytes = bytesAfterTimeTag;
while (!endOfBundle) { while (!endOfBundle) {
let [contentSize, bytesAfterContentSize] = oscTypeConverterMap.i.fromBuffer(remainingBytes);
if (typeof contentSize !== 'number') {
throw new Error('problem decoding content size');
}
remainingBytes = bytesAfterContentSize;
if (remainingBytes.length < contentSize) { if (remainingBytes.length < contentSize) {
throw new Error('bundle does not contain enough data'); throw new Error('bundle does not contain enough data');
} }
const bundleContentBytes = remainingBytes.subarray(0, contentSize); const bundleContentBytes = bytesAfterContentSize.subarray(0, contentSize);
if (bundleContentBytes[0] === 35) { if (bundleContentBytes[0] === 35) {
// # character indicating contents is a bundle // # character indicating contents is a bundle
@@ -46,18 +55,19 @@ export function bundleFromBuffer(bytes: Uint8Array): [OSCBundle | undefined, Uin
throw new Error('bundle contents does not look like a OSC message or bundle'); throw new Error('bundle contents does not look like a OSC message or bundle');
} }
remainingBytes = remainingBytes.subarray(contentSize); remainingBytes = bytesAfterContentSize.subarray(contentSize);
if (remainingBytes.length === 0) { if (remainingBytes.length === 0) {
endOfBundle = true; endOfBundle = true;
} }
} }
return [{
return [
{
timeTag, timeTag,
contents: bundleContents, contents: bundleContents,
},remainingBytes]; },
} remainingBytes,
];
return [undefined, undefined]
} }
export function bundleToBuffer(bundle: OSCBundle): Uint8Array { export function bundleToBuffer(bundle: OSCBundle): Uint8Array {