-
Notifications
You must be signed in to change notification settings - Fork 1
/
coder.go
105 lines (94 loc) · 2.47 KB
/
coder.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
package ginkgo
import (
"reflect"
"github.com/Sirupsen/logrus"
hio "github.com/hprose/hprose-golang/io"
)
type coderMessageType int
const (
coderMessageType_Unknown = iota
coderMessageType_Register
coderMessageType_Binary
coderMessageType_Invoke
coderMessageType_InvokeRecive
coderMessageType_InvokeError
coderMessageType_InvokeNameError
coderMessageType_SessionCloseError
)
func (c coderMessageType) String() (r string) {
switch c {
case coderMessageType_Unknown:
r = "coderMessageType_Unknown"
case coderMessageType_Register:
r = "coderMessageType_Register"
case coderMessageType_Binary:
r = "coderMessageType_Binary"
case coderMessageType_Invoke:
r = "coderMessageType_Invoke"
case coderMessageType_InvokeRecive:
r = "coderMessageType_InvokeRecive"
case coderMessageType_InvokeError:
r = "coderMessageType_InvokeError"
case coderMessageType_InvokeNameError:
r = "coderMessageType_InvokeNameError"
case coderMessageType_SessionCloseError:
r = "coderMessageType_SessionCloseError"
}
return
}
type CoderMessage struct {
ID string
Type coderMessageType
Name string
Msg []reflect.Value
}
type Coder interface {
Encoder(CoderMessage) []byte
Decoder([]byte) (CoderMessage, error)
}
type HproseCoder struct {
}
func NewHproseCoder() *HproseCoder {
return &HproseCoder{}
}
func (g *HproseCoder) Encoder(message CoderMessage) []byte {
//w := hio.NewWriter(true)
//switch message.Type {
//case coderMessageType_Invoke:
// w.WriteByte(hio.TagCall)
//case coderMessageType_InvokeRecive:
// w.WriteByte(hio.TagResult)
//}
//w.WriteString(message.ID)
//w.WriteString(message.Name)
//w.WriteInt(int64(len(message.Msg)))
//w.WriteSlice(message.Msg)
//return w.Bytes()
return hio.Marshal(message)
}
func (g *HproseCoder) Decoder(data []byte) (CoderMessage, error) {
//n := len(data)
//if n == 0 {
// return CoderMessage{{}}, io.ErrUnexpectedEOF
//}
//reader := hio.AcquireReader(data, false)
//defer hio.ReleaseReader(reader)
//message := CoderMessage{}
//tag, _ := reader.ReadByte()
//switch tag {
//case hio.TagCall:
// message.Type = coderMessageType_Invoke
//case hio.TagResult:
// message.Type = coderMessageType_InvokeRecive
//}
//message.ID = reader.ReadString()
//message.Name = reader.ReadString()
//l := reader.ReadInt()
//message.Msg = make([]reflect.Value, l)
//reader.ReadSlice(message.Msg)
//return message, nil
var message CoderMessage
logrus.Debugln("Coder", "Decoder", string(data))
hio.Unmarshal(data, &message)
return message, nil
}