stream_reader
stream_reader and pack_parser
Stream_reader handles continuous data blocks from sock or file. It will buffering the data blocks you pushed in. When the data reached the length you want, it contact data to a Buffer object and call back the function you registered.
Pack_parser pack or unpack nodejs Buffer with specified format.
stream_reader
Install
$ npm install stream_reader
Usage of stream_reader
stream_reader can be used to handle continuous data blocks from sock or file. It will buffering the data blocks you pushed in. When the data reached the length you want, it contact data to a Buffer object and call back the function you registered.
Create a stream rader
var StreamReader = ;var sockStreamReader = ;
Push data into reader
When data block come in from socket, file, etc., push into StreamReader
sock;
Register data reading function
You can register how long data you want, and when the buffering data reached the length, the callback function will bo called:
//Read pack head with fixed length 9 bytes sockStreamReader;
and you can register multi functions as the following:
var flag type; sockStreamReader
Each reading function is called a reading task.
Check the StreamReader has reading task
After a data reading function be called, it will be erased from StreamReader. So, we maybe need check if the StreamReader has reading task by calling function taskEmpty().
sock
loopRead()
The StreamReader will buffering the data blocks until they reached the length you want, so, if you want read a large data chunk, it will occupy large memory, that's not a good idea. So you maybe need the this function: loopRead(totalLen, unitLen, cb). Calling this function will create a loop task in StreamReader, it will call cb function repetitiously when buffering data reached unitLen; and until totalLen being read, the task will finish. This is example for read 2M data, the reading unit is 2K:
var total = 0;sockStreamReader
pack_parser
Install
$ npm install pack_parser
Usage of pack_parser
Pack_parser pack or unpack nodejs Buffer with specified format, is used to handle data package with special fomat.
Create a writer or reader
Writer is used to packaging data, and reader unpackaging data pack.
var PackParser = ;var writer = PackParser;var Reader = PackParser;
pack()
Package data to nodeJs Buffer:
var writer = PackParser; //Package 1 byte with value 3 and 4 bytes with value 12 to a nodejs Buffervar buff = writer;console;
Output as following:
< 03 00 00 00 0c>
unpack()
The function unpack() will unpackage nodeJs Buffer to a javascript object with fields you gave with field functions such as byte(), UInt32(), etc.
//buff value: < Buffer 03 00 00 00 0c >var reader = PackParser;var ret = reader;console;
Output as following:
Field0: 3 Field1: 12
Field functions
Now field functions include:
Int8(), int8(),
UInt8(), byte(), uint8(),
UInt16(), uint16(), ushort(),
UInt32(), uint32(),
Int16(), int16(), short(),
Int32(), int32(),
Float(), float(),
Double(), double(),
string(),
fstring(),
buffer() .
In these field functions:
Int8(), int8() are equivalent.
UInt8(), byte(), uint8() are equivalent.
UInt16(), uint16(), ushort() are equivalent.
UInt32(), uint32() are equivalent.
Int16(), int16(), short() are equivalent.
Int32(), int32() are equivalent.
Float(), float() are equivalent.
Double(), double() are equivalent.
In writer, call field functions with the value you want to package into. In reader, call field functions with the field name.
string()
Field function string() in writer will pack/unpack a string beginning with 4 bytes string length into/from result buffer.
var pack = writer;console;
Output as following:
< Buffer 00 00 00 0d 31 32 33 34 35 36 37 38 39 30 31 32 33 >
For a reader, function string() will read 4 byte string length(in bytes) firstly, and then read string body according to this length.
var out = reader;console;
Output as following:
{ str_name: '1234567890123' }
fstring()
fstring(string, length) means fixed length string.
writer reader
Call this function in writer will package a fixed length(in bytes) string according to argument length. If string is shorter than length, zero(s) will be padding at the end of string. If string is longer than length, it will be truncated.
var pack = writer;console; var pack = writer;console;
Output as following:
< Buffer 31 32 33 34 00 00 00 00 00 00 >
< Buffer 31 32 33 34 35 36 37 38 39 30 >
For writer, if fixedLength is undefined, fixedLength is the string's length(in bytes); and for the the reader, fixedLength must be specified when calling this function.
buffer()
buffer() function likes fstring(), it pack/unpack a nodeJs Buffer object into/from result.
writer reader
bigEndian() & littleEndian()
bigEndian() & littleEndian() set number encoding mode: big endian or little endian.
setEncoding() & getEncoding()
setEncoding() & getEncoding() set/get string encoding mode such as 'utf8', 'ascii', 'hex', 'base64', etc., the more detail can reference to nodeJs docment for Buffer: Buffers and Character Encodings.
Example
A client and server communicat with the pack format as the following format:
|------------------------------------------------------|
| Flag(4 bytes) | Type(1 byte) | Length(4 byte) | Data |
|------------------------------------------------------|
Flag: pack flag, must be string "Test"
Type: 1 -- Echo, 2 -- Long string data
Length: Length for data
Data: Data
Full example as a TCP client
//This is a full example for a tcp client using stream_reader and pack_parser package.//The pack format is: //// |------------------------------------------------------|// | Flag(4 bytes) | Type(1 byte) | Length(4 byte) | Data |// |------------------------------------------------------|//// Flag: pack flag, must be string "Test"// Type: 1 -- Echo, 2 -- Long string data// Length: Length for data// Data: Data var net = ;var PackParser = ;var StreamReader = ; var PACK_TYPE_ECHO = 1;var PACK_TYPE_STR = 2; var HOST = '127.0.0.1';var PORT = 8888; var client = ; var sockStreamReader = ; client; client; client; client; { sockStreamReader;}
Full example as a TCP server
//This is a full example for a tcp server using stream_reader and pack_parser package.//The pack format is: //// |------------------------------------------------------|// | Flag(4 bytes) | Type(1 byte) | Length(4 byte) | Data |// |------------------------------------------------------|//// Flag: pack flag, must be string "Test"// Type: 1 -- Echo, 2 -- Long string data// Length: Length for data// Data: Data var server = ;var StreamReader = ;var PackParser = ; var PACK_TYPE_ECHO = 1;var PACK_TYPE_STR = 2; { var sockStreamReader = ; sock; sock; sock; { //Read pack head with fixed length 9 bytes sockStreamReader; } } { server; server; server; server; server; } ;