forked from andeya/erpc
/
jsonSubProto.go
119 lines (101 loc) · 2.63 KB
/
jsonSubProto.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
// Package jsonSubProto is implemented JSON socket communication protocol.
package jsonSubProto
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"sync"
"github.com/henrylee2cn/goutil"
tp "github.com/henrylee2cn/teleport"
"github.com/tidwall/gjson"
)
// NewJsonSubProtoFunc is creation function of JSON socket protocol.
var NewJsonSubProtoFunc = func(rw tp.IOWithReadBuffer) tp.Proto {
return &jsonSubProto{
id: 'j',
name: "json",
rw: rw,
}
}
type jsonSubProto struct {
id byte
name string
rw tp.IOWithReadBuffer
rMu sync.Mutex
}
// Version returns the protocol's id and name.
func (j *jsonSubProto) Version() (byte, string) {
return j.id, j.name
}
const format = `{"seq":%d,"mtype":%d,"serviceMethod":%q,"meta":%q,"bodyCodec":%d,"body":"%s","xferPipe":%s}`
// Pack writes the Message into the connection.
// NOTE: Make sure to write only once or there will be package contamination!
func (j *jsonSubProto) Pack(m tp.Message) error {
// marshal body
bodyBytes, err := m.MarshalBody()
if err != nil {
return err
}
// do transfer pipe
bodyBytes, err = m.XferPipe().OnPack(bodyBytes)
if err != nil {
return err
}
// marshal transfer pipe ids
var xferPipeIDs = make([]int, m.XferPipe().Len())
for i, id := range m.XferPipe().IDs() {
xferPipeIDs[i] = int(id)
}
xferPipeIDsBytes, err := json.Marshal(xferPipeIDs)
if err != nil {
return err
}
// join json format
s := fmt.Sprintf(format,
m.Seq(),
m.Mtype(),
m.ServiceMethod(),
m.Meta().QueryString(),
m.BodyCodec(),
bytes.Replace(bodyBytes, []byte{'"'}, []byte{'\\', '"'}, -1),
xferPipeIDsBytes,
)
b := goutil.StringToBytes(s)
m.SetSize(uint32(len(b)))
_, err = j.rw.Write(b)
return err
}
// Unpack reads bytes from the connection to the Message.
// NOTE: Concurrent unsafe!
func (j *jsonSubProto) Unpack(m tp.Message) error {
j.rMu.Lock()
defer j.rMu.Unlock()
b, err := ioutil.ReadAll(j.rw)
if err != nil {
return err
}
m.SetSize(uint32(len(b)))
s := goutil.BytesToString(b)
// read transfer pipe
xferPipe := gjson.Get(s, "xferPipe")
for _, r := range xferPipe.Array() {
m.XferPipe().Append(byte(r.Int()))
}
// read body
m.SetBodyCodec(byte(gjson.Get(s, "bodyCodec").Int()))
body := gjson.Get(s, "body").String()
bodyBytes, err := m.XferPipe().OnUnpack(goutil.StringToBytes(body))
if err != nil {
return err
}
// read other
m.SetSeq(int32(gjson.Get(s, "seq").Int()))
m.SetMtype(byte(gjson.Get(s, "mtype").Int()))
m.SetServiceMethod(gjson.Get(s, "serviceMethod").String())
meta := gjson.Get(s, "meta").String()
m.Meta().ParseBytes(goutil.StringToBytes(meta))
// unmarshal new body
err = m.UnmarshalBody(bodyBytes)
return err
}