From df7590c105cdb4bd382d3274acb608b8157b9ba5 Mon Sep 17 00:00:00 2001 From: Simon Schick Date: Fri, 27 Nov 2015 19:37:03 +0100 Subject: [PATCH] Potential fix for streams not terminating --- src/Voice/VoiceConnection.js | 84 ++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 41 deletions(-) diff --git a/src/Voice/VoiceConnection.js b/src/Voice/VoiceConnection.js index 5aded0e70..606beb7db 100644 --- a/src/Voice/VoiceConnection.js +++ b/src/Voice/VoiceConnection.js @@ -95,24 +95,26 @@ class VoiceConnection extends EventEmitter { return; } 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) { - if (onWarning) { - retStream.emit("end"); - stream.destroy(); - self.setSpeaking(false); - return; - } else { - onWarning = true; - setTimeout(send, length * 10); // give chance for some data in 200ms to appear - return; - } - } + var buffer = stream.read(1920); + + if (!buffer) { + if (onWarning) { + retStream.emit("end"); + self.setSpeaking(false); + return; + } else { + onWarning = true; + 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++; sequence + 10 < 65535 ? sequence += 1 : sequence = 0; @@ -125,7 +127,7 @@ class VoiceConnection extends EventEmitter { self.streamTime = count * length; setTimeout(send, length + (nextTime - Date.now())); - + if (!self.playing) self.setSpeaking(true); @@ -250,29 +252,29 @@ class VoiceConnection extends EventEmitter { var discordIP = "", discordPort = ""; udpClient.bind({ exclusive: true }); - udpClient.on('message', function (msg, rinfo) { - var buffArr = JSON.parse(JSON.stringify(msg)).data; - if (firstPacket === true) { - for (var i = 4; i < buffArr.indexOf(0, i); i++) { - discordIP += String.fromCharCode(buffArr[i]); - } - discordPort = msg.readUIntLE(msg.length - 2, 2).toString(10); + udpClient.on('message', function (msg, rinfo) { + var buffArr = JSON.parse(JSON.stringify(msg)).data; + if (firstPacket === true) { + for (var i = 4; i < buffArr.indexOf(0, i); i++) { + discordIP += String.fromCharCode(buffArr[i]); + } + discordPort = msg.readUIntLE(msg.length - 2, 2).toString(10); - var wsDiscPayload = { - "op": 1, - "d": { - "protocol": "udp", - "data": { - "address": discordIP, - "port": Number(discordPort), - "mode": self.vWSData.modes[0] //Plain - } - } - } - vWS.send(JSON.stringify(wsDiscPayload)); - firstPacket = false; - } - }); + var wsDiscPayload = { + "op": 1, + "d": { + "protocol": "udp", + "data": { + "address": discordIP, + "port": Number(discordPort), + "mode": self.vWSData.modes[0] //Plain + } + } + } + vWS.send(JSON.stringify(wsDiscPayload)); + firstPacket = false; + } + }); vWS.on("open", () => { vWS.send(JSON.stringify({ @@ -324,4 +326,4 @@ class VoiceConnection extends EventEmitter { } } -module.exports = VoiceConnection; \ No newline at end of file +module.exports = VoiceConnection;