tcp粘包和拆包、斷包
阿新 • • 發佈:2019-01-22
while (1) {
var packLen = _.unpack('packLen', tcpBuffer.slice(0, 2));
len = packLen.len;
// console.log('pack len------:' + len);
// console.log('true len------:' + tcpBuffer.length);
if (len > tcpBuffer.length) {
writeLog('info', 'code:2011 half pack', null);
return;
}
tcp_pack_unpack(tcpBuffer.slice(0, len));
if (len == tcpBuffer.length) { //pack end
tcpBuffer = new Buffer(0);
return;
} else { //pack not end
writeLog('info', 'code:2100 more pack in one buffer', null);
tcpBuffer = tcpBuffer.slice(len);
}
}
var packLen = _.unpack('packLen', tcpBuffer.slice(0, 2));
len = packLen.len;
// console.log('pack len------:' + len);
// console.log('true len------:' + tcpBuffer.length);
if (len > tcpBuffer.length) {
writeLog('info', 'code:2011 half pack', null);
return;
}
tcp_pack_unpack(tcpBuffer.slice(0, len));
if (len == tcpBuffer.length) { //pack end
tcpBuffer = new Buffer(0);
return;
} else { //pack not end
writeLog('info', 'code:2100 more pack in one buffer', null);
tcpBuffer = tcpBuffer.slice(len);
}
}