/
message.go
135 lines (111 loc) · 2.75 KB
/
message.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
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
package okex
import (
"bytes"
"compress/flate"
"encoding/json"
"io/ioutil"
"time"
"github.com/NadiaSama/ccexgo/internal/rpc"
"github.com/pkg/errors"
)
type (
CodeC struct {
LastSUBID string //okex op fail do not return operate type. we have to record
}
callParam struct {
OP string `json:"op"`
Args []string `json:"args"`
}
response struct {
Event string `json:"event"`
Table string `json:"table"`
Action string `json:"action"`
Data json.RawMessage `json:"data"`
Message string `json:"message"`
ErrorCode int `json:"errorCode"`
Success bool `json:"success"` //login response
}
pingReq struct {
}
ResponseParseCB func(string, string, json.RawMessage) (*rpc.Notify, error)
)
const (
eventError = "error"
idPingPong = "ping-pong"
pingMsg = "ping"
pongMsg = "pong"
timeFMT = "2006-01-02T15:04:05.000Z"
)
var (
rcbMap map[string]ResponseParseCB = map[string]ResponseParseCB{}
pingMessage = &pingReq{}
)
func NewCodeC() *CodeC {
return &CodeC{}
}
func (cc *CodeC) Encode(req rpc.Request) ([]byte, error) {
param := req.Params()
r, ok := param.(*callParam)
if ok {
cc.LastSUBID = r.OP
}
if _, ok := param.(*pingReq); ok {
return []byte{'p', 'i', 'n', 'g'}, nil
}
return json.Marshal(param)
}
func (cc *CodeC) Decode(raw []byte) (rpc.Response, error) {
reader := flate.NewReader(bytes.NewReader(raw))
resp, err := ioutil.ReadAll(reader)
if err != nil {
return nil, rpc.NewStreamError(errors.WithMessagef(err, "decompress error"))
}
if len(resp) == 4 && string(resp) == "pong" {
return &rpc.Result{
ID: idPingPong,
//return a empty json for json.Unmarshal work
Result: []byte(`{}`),
}, nil
}
var r response
if err := json.Unmarshal(resp, &r); err != nil {
return nil, rpc.NewMsgError(raw, err)
}
if r.Event == eventError {
return &rpc.Result{
ID: cc.LastSUBID,
Error: errors.Errorf("error: %s code: %d", r.Message, r.ErrorCode),
}, nil
}
if r.Event == opSubscribe || r.Event == opUnSubscribe {
return &rpc.Result{
ID: r.Event,
Result: resp,
}, nil
}
if r.Event == opLogin {
return &rpc.Result{
ID: opLogin,
Result: resp,
}, nil
}
if len(r.Table) != 0 {
n, e := r.transfer()
return n, e
}
return nil, errors.Errorf("unkown message '%s'", string(resp))
}
func (r *response) transfer() (*rpc.Notify, error) {
cb, ok := rcbMap[r.Table]
if !ok {
return nil, errors.Errorf("unkown channel '%s'", r.Table)
}
return cb(r.Table, r.Action, r.Data)
}
func ParseTime(timestamp string) (time.Time, error) {
t, err := time.Parse(timeFMT, timestamp)
return t, err
}
func SubscribeCB(channel string, cb ResponseParseCB) {
rcbMap[channel] = cb
}