From 56024418c3e84ddad6b4c63c44c35675a1e02d9c Mon Sep 17 00:00:00 2001 From: "John \"Elwin\" Edwards" Date: Mon, 12 Jan 2015 17:10:35 +0000 Subject: [PATCH] Fix further crashes when following dgamelaunch games. The crashes apparently resulted from reading a ttyrec header and then trying to read the data chunk before dgamelaunch produced it. When the data chunk did become available, it would be read by the header function. The simplest solution was to store the position for reading the ttyrec file in the DGLSession, and to leave it unchanged if anything unexpected occurs when reading. --- rlgwebd.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/rlgwebd.js b/rlgwebd.js index 71a3d94..6f7ced6 100755 --- a/rlgwebd.js +++ b/rlgwebd.js @@ -258,6 +258,7 @@ function DglSession(filename) { /* Flag to prevent multiple handlers from reading simultaneously and * getting into a race. */ this.reading = false; + this.rpos = 0; this.framebuf = new Buffer(1024); this.frameoff = 0; this.framepush = function(chunk) { @@ -286,24 +287,36 @@ function DglSession(filename) { return; this.reading = true; var header = new Buffer(12); - fs.read(ss.fd, header, 0, 12, null, function (err, n, buf) { + fs.read(ss.fd, header, 0, 12, ss.rpos, function (err, n, buf) { /* Stop recursion if end of file has been reached. */ if (err || n < 12) { + if (!err && n > 0) { + tslog("DGL %s: expected 12-byte header, got %d", ss.tag(), n); + } ss.reading = false; return; } + ss.rpos += 12; var datalen = buf.readUInt32LE(8); //tslog("Allocating %d bytes", datalen); + if (datalen > 16384) { + tslog("DGL %s: looking for %d bytes", ss.tag(), datalen); + } var databuf = new Buffer(datalen); - fs.read(ss.fd, databuf, 0, datalen, null, function (err, n, buf) { - ss.reading = false; + fs.read(ss.fd, databuf, 0, datalen, ss.rpos, function (err, n, buf) { if (err || n < datalen) { + /* Next time, read the header again. */ + ss.rpos -= 12; + ss.reading = false; + tslog("DGL %s: expected %d bytes, got %d", ss.tag(), datalen, n); return; } + ss.rpos += n; + ss.reading = false; /* Process the data */ ss.framepush(buf); ss.emit("data", buf); - tslog("DGL %s: %d bytes", ss.tag(), buf.length); + //tslog("DGL %s: %d bytes", ss.tag(), buf.length); /* Recurse. */ ss.readchunk(); });