Nodejs多线程GCD 队列 异步 网络 socket block 循环引用Nodejs、TypeScript和Vuejs

NodeJs中TCP粘包、分包解决方案!

2017-03-10  本文已影响894人  西门吹牛々

NodeJs中TCP粘包、分包解决方案!

最新更新请查看github项目
配置介绍

  1. 本类库提供对TCP粘包处理的解决方案
  2. 本库默认缓冲512个字节,当接收数据超过512字节,自动以512倍数扩大缓冲空间
  3. 本库默认采用包头两个字节表示包长度
  4. 本库默认采用大端接模式接收数据
  5. 本库可以配置自定义包头长度[后期迭代]
  6. 本头可以配置大端小端读取[后期迭代]

安装

npm i stickpackage

测试案例

var Stick = require('./stickPackage');

let stick = new Stick();

let bytes3 = Buffer.from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11, 0x11]);

let bytes4 = Buffer.from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11]);
let bytes5 = Buffer.from([0x11]);
// 512个字节包
let bytes6 = Buffer.from([0x01, 0xfe]);
let bytes7 = Buffer.alloc(510).fill(33);
// 513个字节包
let bytes8 = Buffer.from([0x01, 0xff]);
let bytes9 = Buffer.alloc(511).fill(33);

stick.onData(function (data) {
    console.log('receive data,length:' + data.length);
    console.log('receive data,contents:' + JSON.stringify(data));
    // console.log('receive data,contents:');
});

// 传入10个字节,一个长度为2,一个为4的数据包
console.log('log:传入两个包,一次Put[验证一次性Put数据包]');
stick.putData(bytes3);

// 传入10个字节,一个长度为2,一个为4的数据包,分两次Put
console.log('log:传入两个包,分两次Put[验证分两次Put数据包]');
stick.putData(bytes4);
stick.putData(bytes5);

console.log('log:传入512个字节的数据包[验证缓冲全满情况]');
stick.putData(bytes6);
stick.putData(bytes7);

console.log('log:传入513个字节的数据包[验证缓冲扩增情况]');
stick.putData(bytes8);
stick.putData(bytes9);

测试截图

源码地址

喜欢的话请点star,想订阅点watch


案例分析

测试数据包

let bytes3 = Buffer.from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11, 0x11]);
stick.putData(bytes3);

测试结果

[fly@fly Stickpackage]$ node stickTest.js 
log:传入两个包,一次Put[验证一次性Put数据包]
receive data,length:4
receive data,contents:{"type":"Buffer","data":[0,2,102,102]}
receive data,length:6
receive data,contents:{"type":"Buffer","data":[0,4,136,2,17,17]}

测试数据包

let bytes4 = Buffer.from([0x00, 0x02, 0x66, 0x66, 0x00, 0x04, 0x88, 0x02, 0x11]);
let bytes5 = Buffer.from([0x11]);
stick.putData(bytes4);
stick.putData(bytes5);

测试结果

log:传入两个包,分两次Put[验证分两次Put数据包]
receive data,length:4
receive data,contents:{"type":"Buffer","data":[0,2,102,102]}
receive data,length:6
receive data,contents:{"type":"Buffer","data":[0,4,136,2,17,17]}

测试数据包

let bytes6 = Buffer.from([0x01, 0xfe]);
let bytes7 = Buffer.alloc(510).fill(33);
stick.putData(bytes7);
stick.putData(bytes7);

测试结果

传入512个字节的数据包[验证缓冲全满情况]
receive data,length:512
receive data,contents:{"type":"Buffer","data":[1,254,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33]}

测试数据包

let bytes8 = Buffer.from([0x01, 0xff]);
let bytes9 = Buffer.alloc(511).fill(33);
stick.putData(bytes8);
stick.putData(bytes8);

测试结果

传入513个字节的数据包[验证缓冲扩增情况]
receive data,length:513
receive data,contents:{"type":"Buffer","data":[1,255,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33,33]}

源码地址,喜欢的话请点star,想订阅点watch

上一篇下一篇

猜你喜欢

热点阅读