-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.js
182 lines (157 loc) · 3.98 KB
/
rpc.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
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
const debug = require('debug')
const MAX_SEQ = 4294967295
const RPCErrorBuilder = {
methodNotFound(data) {
return { code: -32601, message: 'Method not found', data }
},
parseError(data) {
return { code: -32700, message: 'Parse error', data }
},
invalidRequest(data) {
return { code: -32600, message: 'Invalid request', data }
},
invalidParams(data) {
return { code: -32602, message: 'Invalid params', data }
},
internalError(data) {
return { code: -32603, message: 'Internal error', data }
},
serverError(data) {
return { code: -32000, message: 'Server error', data }
},
}
class RPC {
constructor({node}) {
this.node = node
this.protocol = '/rpc/0.1.0'
this.seq = 0
this.waitingReqs = new Map()
this.reqCheckTimer = null
this.reqCheckInterval = 1000
this.defaultReqTimeout = 4000
this.reqHandlers = new Map()
this.log = debug('fastp2p:rpc')
}
async initialize() {
this.node.on('message', this.receiveMessage.bind(this))
}
start() {
this.reqCheckTimer = setInterval(this.checkReqTimeout.bind(this), this.reqCheckInterval)
}
stop() {
}
async finalize() {
}
request(peer, method, params, opts, callback) {
// TODO validate params schema
const msg = this.buildRequestMessage(method, params)
let cb
if (typeof opts === 'function') {
cb = opts
} else if (typeof callback === 'function') {
cb = callback
}
if (cb) {
const timeout = (opts && opts.timeout) ? opts.timeout : this.defaultReqTimeout
this.waitingReqs.set(msg.seq, { timestamp: Date.now(), callback: cb, timeout })
}
this.node.send(peer, msg)
}
serve(method, handler) {
this.reqHandlers.set(method, handler)
}
//********************************************
// Private methods
//********************************************
checkReqTimeout() {
const toDeleted = []
this.waitingReqs.forEach((req, seq) => {
const { timestamp, callback, timeout } = req
if (Date.now() > timestamp + timeout) {
toDeleted.push(seq)
callback('request timeout')
}
})
for (const key of toDeleted) {
this.waitingReqs.delete(key)
}
}
buildRequestMessage(method, params) {
const msg = {
protocol: this.protocol,
method,
params,
seq: this.getSeq()
}
return msg
}
buildResponseMessage(error, result, seq) {
const msg = {
protocol: this.protocol,
seq,
}
if (error) {
msg.error = error
} else if (result) {
msg.result = result
}
return msg
}
getSeq() {
if (this.seq < MAX_SEQ) {
this.seq++
} else {
this.seq = 1
}
return this.seq
}
receiveMessage(msg, peer) {
if (msg.protocol !== this.protocol) return
// TODO validate msg schema
// this.log('receive rpc message===================:', msg)
if (msg.method) {
this.processRequest(msg, peer)
} else {
this.processResponse(msg, peer)
}
}
processRequest(msg, peer) {
const { method, params, seq } = msg
const handler = this.reqHandlers.get(method)
const done = (error, result) => {
const msg = this.buildResponseMessage(error, result, seq)
this.node.send(peer, msg)
}
if (!handler) {
return done(RPCErrorBuilder.methodNotFound())
}
try {
handler({ params, peer }, (err, result) => {
if (err) {
return done(RPCErrorBuilder.serverError(err))
}
return done(null, result)
})
} catch (e) {
return done(RPCErrorBuilder.internalError())
}
}
processResponse(msg, peer) {
const { seq, error, result } = msg
if (!seq) {
this.log('Ignore response message without seq', msg)
return
}
const req = this.waitingReqs.get(seq)
if (!req) {
return
}
this.waitingReqs.delete(seq)
try {
req.callback(error, result)
} catch (e) {
this.log('Request callback exception:', e)
}
}
}
module.exports = RPC