/
jrpc.go
75 lines (63 loc) · 1.77 KB
/
jrpc.go
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
package apiserver
import (
"sync"
)
// Handler handles a rpc method
type Handler func(ID interface{}, arg *Argument) (interface{}, error)
// JRPCSub provides the json rpc feature of the sub name
type JRPCSub struct {
sync.Mutex
funcMap map[string]Handler
}
// NewJRPCSub returns a JRPCSub
func NewJRPCSub() *JRPCSub {
s := &JRPCSub{
funcMap: map[string]Handler{},
}
return s
}
// Set sets a handler of the method
func (s *JRPCSub) Set(Method string, h Handler) {
s.funcMap[Method] = h
}
// JRPCRequest is a jrpc request
type JRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID interface{} `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
// jRPCRequest is a jrpc request
type jRPCRequest struct {
JSONRPC string `json:"jsonrpc"`
ID interface{} `json:"id"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
// // jRPCRequest2 is a jrpc request
// type jRPCRequest2 struct {
// JSONRPC string `json:"jsonrpc"`
// ID interface{} `json:"id"`
// Method string `json:"method"`
// Params []interface{} `json:"params"`
// }
// JRPCResponse is a jrpc response
type JRPCResponse struct {
ID interface{} `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result interface{} `json:"result"`
}
const defaultErrorCode = -32000
// JRPCResponse is a jrpc response
type jsonError struct {
CODE int `json:"code,omitempty"`
MESSAGE string `json:"message,omitempty"`
DATA interface{} `json:"data,omitempty"`
}
// JRPCResponse is a jrpc response
type JRPCResponseWithError struct {
JSONRPC string `json:"jsonrpc"`
ID interface{} `json:"id"`
Result interface{} `json:"result,omitempty"`
Error interface{} `json:"error,omitempty"`
}