Merge pull request #68 from SimonSchick/streamfix

Potential fix for streams not terminating
This commit is contained in:
abalabahaha
2015-11-27 10:52:12 -08:00

View File

@@ -95,24 +95,26 @@ class VoiceConnection extends EventEmitter {
return; return;
} }
try { try {
var buffer = stream.read(1920);
if (!buffer) {
setTimeout(send, length * 10); // give chance for some data in 200ms to appear
return;
}
if (buffer.length !== 1920) { var buffer = stream.read(1920);
if (onWarning) {
retStream.emit("end"); if (!buffer) {
stream.destroy(); if (onWarning) {
self.setSpeaking(false); retStream.emit("end");
return; self.setSpeaking(false);
} else { return;
onWarning = true; } else {
setTimeout(send, length * 10); // give chance for some data in 200ms to appear onWarning = true;
return; setTimeout(send, length * 10); // give chance for some data in 200ms to appear
} return;
} }
}
if(buffer.length !== 1920) {
var newBuffer = new Buffer(1920).fill(0);
buffer.copy(newBuffer);
buffer = newBuffer;
}
count++; count++;
sequence + 10 < 65535 ? sequence += 1 : sequence = 0; sequence + 10 < 65535 ? sequence += 1 : sequence = 0;
@@ -125,7 +127,7 @@ class VoiceConnection extends EventEmitter {
self.streamTime = count * length; self.streamTime = count * length;
setTimeout(send, length + (nextTime - Date.now())); setTimeout(send, length + (nextTime - Date.now()));
if (!self.playing) if (!self.playing)
self.setSpeaking(true); self.setSpeaking(true);
@@ -250,29 +252,29 @@ class VoiceConnection extends EventEmitter {
var discordIP = "", discordPort = ""; var discordIP = "", discordPort = "";
udpClient.bind({ exclusive: true }); udpClient.bind({ exclusive: true });
udpClient.on('message', function (msg, rinfo) { udpClient.on('message', function (msg, rinfo) {
var buffArr = JSON.parse(JSON.stringify(msg)).data; var buffArr = JSON.parse(JSON.stringify(msg)).data;
if (firstPacket === true) { if (firstPacket === true) {
for (var i = 4; i < buffArr.indexOf(0, i); i++) { for (var i = 4; i < buffArr.indexOf(0, i); i++) {
discordIP += String.fromCharCode(buffArr[i]); discordIP += String.fromCharCode(buffArr[i]);
} }
discordPort = msg.readUIntLE(msg.length - 2, 2).toString(10); discordPort = msg.readUIntLE(msg.length - 2, 2).toString(10);
var wsDiscPayload = { var wsDiscPayload = {
"op": 1, "op": 1,
"d": { "d": {
"protocol": "udp", "protocol": "udp",
"data": { "data": {
"address": discordIP, "address": discordIP,
"port": Number(discordPort), "port": Number(discordPort),
"mode": self.vWSData.modes[0] //Plain "mode": self.vWSData.modes[0] //Plain
} }
} }
} }
vWS.send(JSON.stringify(wsDiscPayload)); vWS.send(JSON.stringify(wsDiscPayload));
firstPacket = false; firstPacket = false;
} }
}); });
vWS.on("open", () => { vWS.on("open", () => {
vWS.send(JSON.stringify({ vWS.send(JSON.stringify({
@@ -324,4 +326,4 @@ class VoiceConnection extends EventEmitter {
} }
} }
module.exports = VoiceConnection; module.exports = VoiceConnection;