2011-01-02 01:30:04 +08:00
|
|
|
var Writer = function(size) {
|
2011-01-02 01:27:52 +08:00
|
|
|
this.size = size || 1024;
|
2011-01-15 05:19:10 +08:00
|
|
|
this.buffer = new Buffer(this.size + 5);
|
|
|
|
this.offset = 5;
|
2010-11-01 07:21:37 +08:00
|
|
|
};
|
|
|
|
|
2011-01-02 01:30:04 +08:00
|
|
|
var p = Writer.prototype;
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
//resizes internal buffer if not enough size left
|
|
|
|
p._ensure = function(size) {
|
2011-01-15 05:19:10 +08:00
|
|
|
var remaining = this.buffer.length - this.offset;
|
|
|
|
if(remaining < size) {
|
2011-01-15 04:52:16 +08:00
|
|
|
var oldBuffer = this.buffer;
|
|
|
|
this.buffer = Buffer(oldBuffer.length + size);
|
|
|
|
oldBuffer.copy(this.buffer);
|
2010-12-30 09:04:33 +08:00
|
|
|
}
|
2011-01-02 01:27:52 +08:00
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.addInt32 = function(num) {
|
|
|
|
this._ensure(4)
|
|
|
|
this.buffer[this.offset++] = (num >>> 24 & 0xFF)
|
|
|
|
this.buffer[this.offset++] = (num >>> 16 & 0xFF)
|
|
|
|
this.buffer[this.offset++] = (num >>> 8 & 0xFF)
|
|
|
|
this.buffer[this.offset++] = (num >>> 0 & 0xFF)
|
|
|
|
return this;
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.addInt16 = function(num) {
|
|
|
|
this._ensure(2)
|
|
|
|
this.buffer[this.offset++] = (num >>> 8 & 0xFF)
|
|
|
|
this.buffer[this.offset++] = (num >>> 0 & 0xFF)
|
|
|
|
return this;
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.addCString = function(string) {
|
|
|
|
var string = string || "";
|
|
|
|
var len = Buffer.byteLength(string) + 1;
|
|
|
|
this._ensure(len);
|
|
|
|
this.buffer.write(string, this.offset);
|
|
|
|
this.offset += len;
|
|
|
|
this.buffer[this.offset] = 0; //add null terminator
|
2011-01-15 04:52:16 +08:00
|
|
|
return this;
|
2011-01-02 01:27:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p.addChar = function(char) {
|
|
|
|
this._ensure(1);
|
|
|
|
this.buffer.write(char, this.offset);
|
|
|
|
this.offset++;
|
|
|
|
return this;
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 02:36:26 +08:00
|
|
|
p.addString = function(string) {
|
|
|
|
var string = string || "";
|
|
|
|
var len = Buffer.byteLength(string);
|
|
|
|
this._ensure(len);
|
|
|
|
this.buffer.write(string, this.offset);
|
|
|
|
this.offset += len;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2011-01-02 01:27:52 +08:00
|
|
|
p.getByteLength = function() {
|
2011-01-15 05:19:10 +08:00
|
|
|
return this.offset - 5;
|
2011-01-02 01:27:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p.add = function(otherBuffer) {
|
|
|
|
this._ensure(otherBuffer.length);
|
|
|
|
otherBuffer.copy(this.buffer, this.offset);
|
|
|
|
this.offset += otherBuffer.length;
|
|
|
|
return this;
|
|
|
|
}
|
2010-11-01 07:21:37 +08:00
|
|
|
|
2011-01-02 01:33:50 +08:00
|
|
|
p.clear = function() {
|
2011-01-15 05:19:10 +08:00
|
|
|
this.offset=5;
|
|
|
|
}
|
|
|
|
|
|
|
|
p.join = function(code) {
|
|
|
|
if(code) {
|
|
|
|
var end = this.offset;
|
|
|
|
this.offset = 0;
|
|
|
|
this.buffer[this.offset++] = code;
|
|
|
|
//write the length which is length of entire packet not including
|
|
|
|
//message type code byte
|
|
|
|
this.addInt32(end - 1);
|
|
|
|
this.offset = end;
|
|
|
|
}
|
|
|
|
return this.buffer.slice(code ? 0 : 5, this.offset);
|
2011-01-02 01:33:50 +08:00
|
|
|
}
|
|
|
|
|
2011-01-15 05:19:10 +08:00
|
|
|
p.flush = function(code) {
|
|
|
|
var result = this.join(code);
|
2011-01-02 01:51:33 +08:00
|
|
|
this.clear();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-01-02 01:30:04 +08:00
|
|
|
module.exports = Writer;
|