Mercurial > hg > rlgwebd
diff rlgwebd.js @ 174:dc12ba30d559
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.
author | John "Elwin" Edwards |
---|---|
date | Mon, 12 Jan 2015 17:10:35 +0000 |
parents | 671bed5039aa |
children | 4dd87508fc96 |
line wrap: on
line diff
--- a/rlgwebd.js Sun Jan 11 07:41:36 2015 -0500 +++ b/rlgwebd.js Mon Jan 12 17:10:35 2015 +0000 @@ -258,6 +258,7 @@ /* 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 @@ 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(); });