This repository was archived by the owner on Aug 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathclient.js
More file actions
68 lines (57 loc) · 1.31 KB
/
client.js
File metadata and controls
68 lines (57 loc) · 1.31 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
module.exports = function (
logger,
inherits,
EventEmitter,
net,
ReadableStream,
Receiver
) {
function Client() {
net.Socket.call(this)
this.input = this;
if (!this.input.read) {
this.input = new ReadableStream()
this.input.wrap(this)
}
this.receiver = new Receiver(this.input)
this.onPing = receiverPing.bind(this)
this.onZxid = receiverZxid.bind(this)
this.onWatch = receiverWatch.bind(this)
this.onReceiverEnd = receiverEnd.bind(this)
this.receiver.on('ping', this.onPing)
this.receiver.on('zxid', this.onZxid)
this.receiver.on('watch', this.onWatch)
this.receiver.on('end', this.onReceiverEnd)
}
inherits(Client, net.Socket)
Client.prototype.send = function (message) {
var data = message.toBuffer()
var head = Buffer(12)
var extra = 0
if (message.xid) {
extra += 4
head.writeInt32BE(message.xid, 4)
}
if (message.type) {
extra += 4
head.writeInt32BE(message.type, 8)
}
head.writeInt32BE(data.length + extra, 0)
this.write(head.slice(0, extra + 4))
this.write(data)
this.receiver.push(message)
}
function receiverPing() {
//logger.info('ping')
}
function receiverZxid(zxid) {
this.emit('zxid', zxid)
}
function receiverWatch(watch) {
this.emit('watch', watch)
}
function receiverEnd() {
this.emit('receiverEnd')
}
return Client
}