-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmessage_decoder.js
More file actions
141 lines (102 loc) · 3.1 KB
/
message_decoder.js
File metadata and controls
141 lines (102 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2013 Mark Cavage. All rights reserved.
var Writable = require('readable-stream').Writable;
var util = require('util');
var assert = require('assert-plus');
var crc = require('crc');
var WError = require('verror').WError;
var proto = require('./protocol');
///--- Errors
function ChecksumError(exp, actual, msg) {
WError.call(this, {}, 'checksum error(%d): caclulated %d', exp, actual);
this.context = {
expected_crc: exp,
actual_crc: actual,
message: msg
};
this.name = this.constructor.name;
}
util.inherits(ChecksumError, WError);
function InvalidContentError(cause, msg) {
WError.call(this, cause, 'invalid JSON encountered');
this.context = {
message: msg
};
this.name = this.constructor.name;
}
util.inherits(InvalidContentError, WError);
///--- Internal Functions
function parseBuffer(buf, msg) {
assert.object(buf, 'buffer');
assert.object(msg, 'message');
if (buf.length < proto.HEADER_LEN)
return (false);
msg._offset = msg._offset || 0;
if (msg._offset === 0) {
msg.version = buf.readUInt8(msg._offset++, true);
msg.type = buf.readUInt8(msg._offset++, true);
msg.status = buf.readUInt8(msg._offset++, true);
msg.msgid = buf.readUInt32BE(msg._offset, true);
msg._offset += 4;
msg.checksum = buf.readInt32BE(msg._offset, true);
msg._offset += 4;
msg.length = buf.readUInt32BE(msg._offset, true);
msg._offset += 4;
}
var remain = msg._offset + msg.length;
if (buf.length < remain)
return (false);
msg.data = buf.slice(msg._offset, remain).toString('utf8');
msg._offset += msg.length;
return (true);
}
///--- API
function MessageDecoder() {
Writable.call(this);
this._buf = null;
this._msg = null;
}
util.inherits(MessageDecoder, Writable);
MessageDecoder.prototype._write = function _write(buf, encoding, cb) {
var checksum;
var msg;
var self = this;
if (this._buf) {
// Wed Underrun data on a previous call
var len = this._buf.length + buf.length;
buf = Buffer.concat([this._buf, buf], len);
}
assert.ok(Buffer.isBuffer(buf));
msg = this._msg || {};
while (buf.length > 0) {
if (!parseBuffer(buf, msg)) {
this._buf = buf;
this._msg = msg;
cb();
return;
}
checksum = crc.crc16(msg.data);
if (msg.checksum !== checksum) {
var e = new ChecksumError(msg.checksum, checksum, msg);
self.emit('error', e);
}
try {
msg.data = JSON.parse(msg.data);
} catch (parse_err) {
self.emit('error', new InvalidContentError(parse_err));
}
msg.start = process.hrtime();
this.emit('message', msg);
buf = buf.slice(msg._offset);
msg = {};
}
this._buf = null;
this._msg = null;
cb();
};
///--- Exports
module.exports = {
MessageDecoder: MessageDecoder,
createMessageDecoder: function createMessageDecoder() {
return (new MessageDecoder());
}
};