buffer tcp data to reconstruct incomplete messages

This commit is contained in:
devl0rd 2020-04-29 15:25:51 -06:00
parent a3d4c7c46b
commit 9db699f863

View file

@ -105,10 +105,8 @@ hidStreamClient.on('connect', function () {
startAudioProcess(); startAudioProcess();
} }
}); });
var switchHidBuffer = new Buffer.alloc(0);
function parseInputStruct(buff) { function parseInputStruct(buff) {
var input = Struct() var input = Struct()
.word64Ule('streamStart')
.word32Ule('HeldKeys1') .word32Ule('HeldKeys1')
.word32Sle('LJoyX1') .word32Sle('LJoyX1')
.word32Sle('LJoyY1') .word32Sle('LJoyY1')
@ -161,7 +159,6 @@ function parseInputStruct(buff) {
.floatle('gyroZ') .floatle('gyroZ')
.word32Ule('controllerCount') .word32Ule('controllerCount')
.word32Ule('frameRate') .word32Ule('frameRate')
.word64Ule('streamEnd')
input._setBuff(buff); input._setBuff(buff);
return input; return input;
}; };
@ -478,24 +475,19 @@ function handleGyroAndAccel(hid) {
GyroServ.sendMotionData(gyro, accel); GyroServ.sendMotionData(gyro, accel);
} }
var fpsPrintTimer = 0; var fpsPrintTimer = 0;
var hidDataBuffer; var hidDataBuffer = "";
hidStreamClient.on('data', function (data) { hidStreamClient.on('data', function (chunk) {
if (data.length < 224) { hidDataBuffer += chunk.toString("hex");
console.log("HID data too short. Data length: " + data.length) var completeData = "";
return; if (hidDataBuffer.includes("ffffffffffffffff") && hidDataBuffer.includes("ffffffffffffff7")) {
} //packet is 216 in length. anyless then the data is bad completeData = hidDataBuffer.split("ffffffffffffffff")[1].split("ffffffffffffff7")[0];
if (data.length > 224) { hidDataBuffer = "";
data.length = 224; //ignore extra data } else {
//console.log("Duplicate Data: " + (data.length / 224))
}
switchHidBuffer = new Buffer.from(data);
var hid = parseInputStruct(switchHidBuffer)
if (!(hid.get("streamStart") === "18446744073709551615" && hid.get("streamEnd") === "9223372036854775807")) {
console.log("HID Data malformed. Data length: " + data.length);
console.log(hid.get("streamStart") + " - " + hid.get("streamEnd"))
return; return;
} }
var data = Buffer.from(completeData, 'hex');
console.log(data)
var hid = parseInputStruct(data);
var controllerCount = hid.get("controllerCount"); var controllerCount = hid.get("controllerCount");
if (controllerCount > controllerIds.length) { if (controllerCount > controllerIds.length) {