Created
January 1, 2014 10:51
-
-
Save jedi4ever/8206898 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var events = require('events'); | |
var util = require('util'); | |
var Transform = require('stream').Transform; | |
/** | |
* @constructor | |
* @name TtyPlayStream | |
* @param {Hash} options | |
*/ | |
var TtyPlayStream = function(options) { | |
var self = this; | |
if (options === undefined) { | |
self.settings = {}; | |
} else { | |
// Copy the options | |
self.settings = JSON.parse(JSON.stringify(options)); | |
} | |
// PlaceHolder for unparsed chunk parts | |
self.prevChunk = null; | |
// Run parent constructor | |
Transform.call(this, self.settings); | |
}; | |
util.inherits(TtyPlayStream, Transform); | |
module.exports = TtyPlayStream; | |
TtyPlayStream.prototype._transform = function(chunk, encoding, callback) { | |
var self = this; | |
var combinedChunk ; | |
if (self.prevChunk === null) { | |
combinedChunk = chunk.slice(0); | |
} else { | |
combinedChunk = Buffer.concat([self.prevChunk, chunk]); | |
} | |
var action = function(header, packet, cb) { | |
//console.log(header); | |
self.push(packet.payload); | |
cb(null); | |
}; | |
// This is called when there is no more to parse | |
// buffer contains the chunk that can not be parsed into a record | |
var done = function(err, buffer) { | |
self.prevChunk = buffer; | |
callback(null); | |
}; | |
parseBuffer(combinedChunk, action , done); | |
}; | |
TtyPlayStream.prototype._flush = function(callback) { | |
console.log('before end'); | |
callback(null); | |
}; | |
function parseBuffer(chunk, action, callback) { | |
var buffer = chunk.slice(0); | |
var record = parseRecord(buffer); | |
if ( record === null) { | |
callback(null, buffer); | |
} else { | |
var header = record.header; | |
var packet = record.packet; | |
action(header, packet, function() { | |
var rest = record.packet.rest; | |
parseBuffer(rest, action, callback); | |
}); | |
} | |
} | |
function parseHeader(chunk) { | |
var result = {}; | |
if (chunk.length >= 12) { | |
// 32Bit (= 4 Bytes) Little Endian | |
var sec = chunk.readUInt32LE(0); | |
var usec = chunk.readUInt32LE(4); | |
var payloadLength = chunk.readUInt32LE(8); | |
result.sec = sec; | |
result.usec = usec; | |
result.length = payloadLength; | |
result.header = chunk.slice(0, 11); | |
result.rest = chunk.slice(12); | |
//console.log(result); | |
return result; | |
} else { | |
return null; | |
} | |
} | |
function parsePacket(chunk, length) { | |
var result = {}; | |
if (chunk.length >= length) { | |
result.payload = chunk.slice(0,length -1); | |
result.rest = chunk.slice(length); | |
//console.log(result); | |
return result; | |
} else { | |
return null; | |
} | |
} | |
function parseRecord(chunk) { | |
var result = {}; | |
var header = parseHeader(chunk); | |
if ( header === null) { | |
return null; | |
} else { | |
var chunkLeft = header.rest; | |
var packet = parsePacket(chunkLeft, header.length); | |
if (packet === null) { | |
return null; | |
} else { | |
return { | |
header: header, | |
packet: packet | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment