-
Notifications
You must be signed in to change notification settings - Fork 17
/
tcodec.go
48 lines (40 loc) · 1.25 KB
/
tcodec.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
package turbo
import "errors"
const (
//最大packet的字节数
MAX_PACKET_BYTES = 2 * 1024 * 1024
PACKET_HEAD_LEN = (4 + 1 + 2 + 8 + 4) //请求头部长度 int32
)
//包体过大
var ERR_TOO_LARGE_PACKET = errors.New("Too Large Packet")
var ERR_TIMEOUT = errors.New("WAIT RESPONSE TIMEOUT ")
var ERR_CONNECTION_BROKEN = errors.New("Connection Broken ")
var ERR_INVALID_PAYLOAD = errors.New("INVALID PAYLOAD TYPE ! ")
var ERR_MARSHAL_PACKET = errors.New("ERROR MARSHAL PACKET")
var ERR_OVER_FLOW = errors.New("Group Over Flow")
var ERR_NO_HOSTS = errors.New("NO VALID RemoteClient")
var ERR_PONG = errors.New("ERROR PONG TYPE !")
type ICodec interface {
//反序列化payload
//返回解析后的interface{}
UnmarshalPayload(p *Packet) (interface{}, error)
//序列化payload
//返回序列化之后的payload字节
MarshalPayload(p *Packet) ([]byte, error)
}
type LengthBytesCodec struct {
ICodec
MaxFrameLength int32 //最大的包大小
}
//反序列化
func (self LengthBytesCodec) UnmarshalPayload(p *Packet) (interface{}, error) {
return p.Data, nil
}
//序列化
func (self LengthBytesCodec) MarshalPayload(packet *Packet) ([]byte, error) {
if raw, ok := packet.PayLoad.([]byte); ok {
return raw, nil
} else {
return nil, ERR_INVALID_PAYLOAD
}
}