Better robusteness for errors when writing buffers
This commit is contained in:
+7
-1
@@ -69,7 +69,13 @@ function session(sock) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
log.verbose('larvitsmpp: lib/session.js: session() - sockWrite() - sending PDU. SeqNr: ' + pdu.readUInt32BE(12) + ' cmd: ' + defs.cmdsById[pdu.readUInt32BE(4)].command + ' cmdStatus: ' + defs.errorsById[parseInt(pdu.readUInt32BE(8))] + ' hex: ' + pdu.toString('hex'));
|
try {
|
||||||
|
log.verbose('larvitsmpp: lib/session.js: session() - sockWrite() - sending PDU. SeqNr: ' + pdu.readUInt32BE(12) + ' cmd: ' + defs.cmdsById[pdu.readUInt32BE(4)].command + ' cmdStatus: ' + defs.errorsById[parseInt(pdu.readUInt32BE(8))] + ' hex: ' + pdu.toString('hex'));
|
||||||
|
} catch (e) {
|
||||||
|
log.error('larvitsmpp: lib/session.js: session() - sockWrite() - PDU buffer is invalid. Buffer hex: "' + pdu.toString('hex') + '"');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
sock.write(pdu);
|
sock.write(pdu);
|
||||||
|
|
||||||
if (closeAfterSend) {
|
if (closeAfterSend) {
|
||||||
|
|||||||
+17
-6
@@ -218,15 +218,24 @@ function objToPdu(obj, callback) {
|
|||||||
|
|
||||||
// Used to write buffer once the command length is known
|
// Used to write buffer once the command length is known
|
||||||
function writeBuffer() {
|
function writeBuffer() {
|
||||||
var offset = 16; // Start the offset on the body
|
var offset = 16, // Start the offset on the body
|
||||||
|
err;
|
||||||
|
|
||||||
|
|
||||||
buff = new Buffer(cmdLength);
|
buff = new Buffer(cmdLength);
|
||||||
|
|
||||||
// Write PDU header
|
// Write PDU header
|
||||||
buff.writeUInt32BE(cmdLength, 0); // Command length for the first 4 octets
|
try {
|
||||||
buff.writeUInt32BE(defs.cmds[obj.cmdName].id, 4); // Command id for the second 4 octets
|
buff.writeUInt32BE(cmdLength, 0); // Command length for the first 4 octets
|
||||||
buff.writeUInt32BE(defs.errors[obj.cmdStatus], 8); // Command status for the third 4 octets
|
buff.writeUInt32BE(defs.cmds[obj.cmdName].id, 4); // Command id for the second 4 octets
|
||||||
buff.writeUInt32BE(seqNr, 12); // Sequence number as the fourth 4 octets
|
buff.writeUInt32BE(defs.errors[obj.cmdStatus], 8); // Command status for the third 4 octets
|
||||||
|
buff.writeUInt32BE(seqNr, 12); // Sequence number as the fourth 4 octets
|
||||||
|
} catch (e) {
|
||||||
|
err = new Error('Could not write PDU header, catched err: ' + e.message, obj);
|
||||||
|
log.error('larvitsmpp: lib/utils.js: objToPdu() - writeBuffer() - ' + err.message);
|
||||||
|
callback(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Cycle through the defs list to make sure the params are in the right order
|
// Cycle through the defs list to make sure the params are in the right order
|
||||||
for (param in defs.cmds[obj.cmdName].params) {
|
for (param in defs.cmds[obj.cmdName].params) {
|
||||||
@@ -315,7 +324,9 @@ function objToPdu(obj, callback) {
|
|||||||
|
|
||||||
calcCmdLength();
|
calcCmdLength();
|
||||||
writeBuffer();
|
writeBuffer();
|
||||||
callback(null, buff);
|
if (buff) {
|
||||||
|
callback(null, buff);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user