mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 08:13:31 +02:00
Give forward plugin its own folder. Should make it easier to clean it up.
This commit is contained in:
45
lib/units/device/plugins/forward/util/writer.js
Normal file
45
lib/units/device/plugins/forward/util/writer.js
Normal file
@@ -0,0 +1,45 @@
|
||||
var util = require('util')
|
||||
var stream = require('stream')
|
||||
|
||||
var HEADER_SIZE = 4
|
||||
var MAX_PACKET_SIZE = 0xFFFF
|
||||
|
||||
function ForwardWriter(target) {
|
||||
stream.Transform.call(this)
|
||||
this._target = target
|
||||
}
|
||||
|
||||
util.inherits(ForwardWriter, stream.Transform)
|
||||
|
||||
ForwardWriter.prototype._transform = function(chunk, encoding, done) {
|
||||
var header
|
||||
, length
|
||||
|
||||
do {
|
||||
length = Math.min(MAX_PACKET_SIZE, chunk.length)
|
||||
|
||||
header = new Buffer(HEADER_SIZE)
|
||||
header.writeUInt16LE(this._target, 0)
|
||||
header.writeUInt16LE(length, 2)
|
||||
|
||||
this.push(header)
|
||||
this.push(chunk.slice(0, length))
|
||||
|
||||
chunk = chunk.slice(length)
|
||||
}
|
||||
while (chunk.length)
|
||||
|
||||
done()
|
||||
}
|
||||
|
||||
ForwardWriter.prototype._flush = function(done) {
|
||||
var header = new Buffer(HEADER_SIZE)
|
||||
header.writeUInt16LE(this._target, 0)
|
||||
header.writeUInt16LE(0, 2)
|
||||
|
||||
this.push(header)
|
||||
|
||||
done()
|
||||
}
|
||||
|
||||
module.exports = ForwardWriter
|
||||
Reference in New Issue
Block a user