-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseConnector.js
68 lines (55 loc) · 1.63 KB
/
BaseConnector.js
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
'use strict'
var EventEmitter = require('events').EventEmitter
var inherits = require('util').inherits
var getBuffer = require('./utils').getBuffer
function BaseConnector (server) {
EventEmitter.apply(this)
this.server = server
this.isSettingOption = false
this.isEchoing = false
}
inherits(BaseConnector, EventEmitter)
BaseConnector.prototype.echo = function (workload) {
this.isEchoing = true
this.server.echo(getBuffer(workload))
}
BaseConnector.prototype.optionsRequest = function (option) {
this.isSettingOption = true
this.server.optionsRequest(getBuffer(option))
}
BaseConnector.prototype.handleEcho = function (content) {
this.isEchoing = false
this.emit('echo', content)
}
BaseConnector.prototype.handleOptionResponse = function (content) {
this.isSettingOption = false
this.emit('option-response', content)
}
BaseConnector.prototype.connect = function (_callback) {
var callback = _callback || function () {}
this.server.once('connect', callback)
this.server.connect()
}
BaseConnector.prototype.disconnect = function (_callback) {
var callback = _callback || function () {}
if (!this.server) return callback()
this.server.disconnect(callback)
this.server = null
}
BaseConnector.findNullBufferIndexes = function (buffer, expectedNullBufferNumber) {
var arr = new Array(expectedNullBufferNumber)
var current = 0
var i = -1
while (i < buffer.length) {
i++
if (buffer[i] !== 0x00) continue
arr[current++] = i
expectedNullBufferNumber--
}
return arr
}
BaseConnector.OPTION_REQUEST = {
EXCEPTION: 'exceptions'
}
Object.freeze(BaseConnector.OPTION_REQUEST)
module.exports = BaseConnector