-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.iced
45 lines (36 loc) · 1.13 KB
/
index.iced
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
msgpack = require 'msgpack'
class MsgpackRPC
id : 0
cbs : {}
methods : {}
constructor : (@namespace, @sock) ->
@sock.on 'drain', (h) -> console.log "Draining: #{h}"
ms = new msgpack.Stream @sock
ms.addListener 'msg', @_parse.bind this
_write : (data) ->
try
@sock.write data
catch e
setTimeout @_write.bind(this, data), 500
invoke : (method, params, cb) ->
req = msgpack.pack [0, @id, "#{@namespace}.#{method}", params]
@cbs[@id++] = cb
@_write req
_parse : (packet) ->
# Method: [type, msgid, method, params]
if packet[0] is 0
method = packet[2].replace "#{@namespace}.", ''
method = @methods[method]
# Call method, wait for output and reply with it
method packet[3], (err, res) =>
if err
res = msgpack.pack [1, packet[1], err, null]
else
res = msgpack.pack [1, packet[1], null, res]
@_write res
# Response: [type, msgid, error, result]
else if packet[0] is 1
# Call the adecuate callback and delete it
@cbs[packet[1]](packet[2], packet[3])
delete @cbs[packet[1]]
module.exports = MsgpackRPC