No description, website, or topics provided.
JavaScript
Switch branches/tags
Nothing to show
Latest commit b34ae1a Dec 13, 2013 @freedaxin freedaxin Merge pull request #2 from liuaifu/master
change "4" to "this.head_length" in checkEnoughData
Permalink
Failed to load latest commit information.
test
README.md
head_body_buffers.js
package.json

README.md

Overview

When transfer packets in network composed of a fixed length "head" and a variable length "body", the "data" event emitted by node socket probably not a complete packet, but part of several packets. This tool help you get the "head" and "body" from the buffers.

If the "head" and "body" is in a single buffer, data will not be copyed to a new buffer, just use buffer.slice() to reference data.

see the test for more usage samples.

Install

npm install head_body_buffers

Example

var net = require('net');
var HeadBodyBuffers = require('head_body_buffers').HeadBodyBuffers;

function packetLength(data) {
    return data[0] + (data[1] << 8) + (data[2] << 16);
}

var hbd = new HeadBodyBuffers(4, packetLength);
hbd.on('packet', function (packet) {
    var head = packet.slice(0, 4);
    var body = packet.slice(4);
    console.log("head:", head, head.length);
    console.log("body:", body.toString(), body.length);
});

var client = net.connect(3306);
client.on('data', function(data) {
  hbd.addBuffer(data);
});