-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.js
More file actions
231 lines (184 loc) · 6.41 KB
/
Copy pathConnection.js
File metadata and controls
231 lines (184 loc) · 6.41 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const { Socket } = require('net')
const EventEmitter = require('events')
const FrameAssembler = require('./FrameAssembler')
const FrameParser = require('./FrameParser')
const FrameBuilder = require('./FrameBuilder')
const NullByte = Buffer.alloc(1)
const NullUint32 = Buffer.concat([NullByte, NullByte, NullByte, NullByte])
//Events:
//- connection:lost
//- connection:closed
//- connection:failed
//- connection:established
//- data:response
//- data::media
class Connection extends EventEmitter {
constructor({ camIp, camMediaPort = 34567, commandTimeoutMs = 5000, log = null }, SessionId) {
super()
this._settings = {
camIp,
camMediaPort,
commandTimeoutMs
}
this._callbacks = {}
this._cmdSeq = 0
this._sessionId
this.setSessionId(SessionId)
this._aliveKeeperInterval
this._commandParser = new FrameParser()
this._commandParser.onResponse = this.onResponse.bind(this)
this._commandParser.onVideoFrame = this.onVideoFrame.bind(this)
this._commandParser.onAudioFrame = this.onAudioFrame.bind(this)
this._commandParser.onDisconnect = this.onDisconnect.bind(this)
this._receiver = new FrameAssembler(this._commandParser)
this._builder = new FrameBuilder()
this._socket = new Socket()
this._socket.setKeepAlive(true, 10000)
this._socket.on('data', data=>this._receiver.applyData(data))
this._socket.on('close', this.onSocketClose.bind(this) )
}
clone() {
return new Connection(this._settings, this._sessionId)
}
onSocketClose() {
//I dont think there is any point to having an unauthenticated connection, so lets 'ignore'
//emitting of the connection:lost event if we are unauthenticated. This also doubles as
//not emitting a connection:lost when manually calling disconnect()
if (this._sessionId.buffer !== NullUint32) {
this.disconnect()
}
else {
this.emit('connection:closed')
}
}
onResponse(frame) {
//Sometimes the sequence ID in the response will be +1 of what we've sent. I dont even know.
const theCallback = this._callbacks[frame.header.SequenceID] || this._callbacks[frame.header.SequenceID - 1]
if (theCallback) {
const { resolve, reject } = theCallback()
try {
resolve(frame)
}
catch (e) {
reject(`Failed to parse what seemed like a JSON response: ${frame.toString('utf8')}`)
}
}
//this.emit('data:response', data)
}
onVideoFrame(frame) {
this.emit('data:video', frame)
}
onAudioFrame(frame) {
this.emit('data:audio', frame)
}
onDisconnect(frame) {
this.emit('data:eof')
}
/**
* Get current session. The buffer is passed by reference, so dont modify it.
* @return {DVRIPSession}
*/
get sessionId() { return this._sessionId }
/**
* Get if we are connected
* @return {boolean}
*/
get isConnected() { return this._socket && !this._socket.destroyed && this._socket.remoteAddress }
/**
* Get if we are logged in
* @return {boolean}
*/
get isLoggedIn() { return this.isConnected && (this._sessionId.buffer !== NullUint32) }
/**
* Set session ID used for communication
*
* @param {Buffer|string|DVRIPSession} [byteBuffer] Session to use
*/
setSessionId(newSessionId = NullUint32) {
let newSess
if (newSessionId instanceof Buffer) {
if (newSessionId.length !== 4) throw 'Session ID must be 4 bytes long'
//Cloning the Buffer here so we arent relieant on a user-space passed buffer.
newSess = { buffer: newSessionId, string: `0x${newSessionId.toString('hex')}` }
}
else if (typeof newSessionId === 'string') {
if (newSessionId.length !== 10) throw 'Invalid Session ID string'
newSess = { buffer: Buffer.from(newSessionId.substr(2) /*Skip '0x'*/, 'hex').reverse(), string: newSessionId }
}
else if ((typeof newSessionId === 'object') && newSessionId.buffer && newSessionId.string) {
newSess = newSessionId
}
else {
throw 'newSessionId must be a Buffer or String'
}
this._sessionId = Object.freeze(newSess)
}
setTimeout(timeout) {
if (this.isConnected) this._socket.setTimeout(timeout)
}
resetSeqCounter() { this._cmdSeq = 0 }
sendMessage(msg) {
return new Promise((resolve, reject) => {
if (!this.isConnected) return reject('Not connected')
try {
const cmdSeq = this._cmdSeq++
const builtMessage = this._builder.buildMessage(msg, this._sessionId, cmdSeq)
this._socket.write(builtMessage)
if (msg.IgnoreResponse) return resolve()
let timeoutReject = setTimeout(() => {
if (this._callbacks[cmdSeq]) {
reject('Execution timed out')
delete this._callbacks[cmdSeq]
timeoutReject = undefined
}
}, this._settings.commandTimeoutMs)
const promiseWrapper = () => {
if (!timeoutReject) return
clearTimeout(timeoutReject)
delete this._callbacks[cmdSeq]
return { resolve, reject }
}
this._callbacks[cmdSeq] = promiseWrapper
}
catch (e) {
reject(e)
}
})
}
setupAliveKeeper(msg, interval = 20) {
if (this._aliveKeeperInterval)
clearInterval(this._aliveKeeperInterval)
this._aliveKeeperInterval = setInterval(() => {
this.sendMessage(msg)
}, interval * 1000)
}
connect() {
if (this._socket.connecting || this.isConnected) throw 'Already connected'
return new Promise((resolve, reject) => {
const errorCb = () => {
this.emit('connection:failed')
reject('Failed to connect')
}
this._socket.once('error', errorCb)
this._socket.setNoDelay(true)
this._socket.connect(this._settings.camMediaPort, this._settings.camIp, () => {
if (this._socket) this._socket.removeListener('error', errorCb)
this.emit('connection:established')
resolve()
})
})
}
disconnect() {
this.setSessionId()
clearInterval(this._aliveKeeperInterval)
return new Promise((resolve, reject) => {
if (!this.isConnected) return resolve()
this.emit('connection:lost')
this._socket.end(() => {
this._socket.unref()
resolve()
})
})
}
}
module.exports = Connection