Merge pull request #227 from roblabla/feature-addOption

Add option type
This commit is contained in:
Robin Lambertz 2015-09-02 19:37:04 +02:00
commit 388879bd4b
2 changed files with 31 additions and 0 deletions

View file

@ -2,6 +2,7 @@ var { getField, getFieldInfo } = require('../utils');
module.exports = { module.exports = {
'switch': [readSwitch, writeSwitch, sizeOfSwitch], 'switch': [readSwitch, writeSwitch, sizeOfSwitch],
'option': [readOption, writeOption, sizeOfOption],
}; };
function readSwitch(buffer, offset, typeArgs, rootNode) { function readSwitch(buffer, offset, typeArgs, rootNode) {
@ -39,3 +40,30 @@ function sizeOfSwitch(value, typeArgs, rootNode) {
fieldInfo = getFieldInfo(typeArgs.fields[compareTo]); fieldInfo = getFieldInfo(typeArgs.fields[compareTo]);
return this.sizeOf(value, fieldInfo, rootNode); return this.sizeOf(value, fieldInfo, rootNode);
} }
function readOption(buffer, offset, typeArgs, context) {
var val = buffer.readUInt8(offset++);
if (val !== 0) {
var retVal = this.read(buffer, offset, typeArgs, context);
retval.size++;
return retval;
} else {
return {
size: 1
};
}
}
function writeOption(value, buffer, offset, typeArgs, context) {
if (value != null) {
buffer.writeUInt8(1, offset++);
this.write(value, buffer, offset, typeArgs, context);
} else {
buffer.writeUInt8(0, offset++);
}
return offset;
}
function sizeOfOption(value, typeArgs, context) {
return value == null ? 1 : this.sizeOf(value, typeArgs, context) + 1;
}

View file

@ -138,6 +138,9 @@ var values = {
else else
return getValue(i, packet); return getValue(i, packet);
}, },
'option': function(typeArgs, packet) {
return getValue(typeArgs, packet);
}
}; };
function getValue(_type, packet) { function getValue(_type, packet) {