-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_session.go
79 lines (65 loc) · 1.57 KB
/
tcp_session.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
package dnet
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"net"
"reflect"
)
// default编解码器
// 消息 -- 格式: 消息头(消息len), 消息体
const (
lenSize = 2 // 消息长度(消息体的长度)
headSize = lenSize // 消息头长度
buffSize = 65535 // 缓存容量(与lenSize有关,2字节最大65535)
)
type DefTCPCodec struct{}
//解码
func (_ DefTCPCodec) Decode(reader io.Reader) (interface{}, error) {
hdr := make([]byte, headSize)
_, err := io.ReadFull(reader, hdr)
if err != nil {
return nil, err
}
length := binary.BigEndian.Uint16(hdr)
buff := make([]byte, length)
if _, err := io.ReadFull(reader, buff); err != nil {
return nil, err
}
return buff, nil
}
//编码
func (_ DefTCPCodec) Encode(o interface{}) ([]byte, error) {
data, ok := o.([]byte)
if !ok {
return nil, fmt.Errorf("dnet:Encode interface{} is %s, need type []byte", reflect.TypeOf(o))
}
length := len(data)
if length > buffSize {
return nil, fmt.Errorf("dnet:Encode dataLen is too large,len: %d", length)
}
buff := new(bytes.Buffer)
binary.Write(buff, binary.BigEndian, uint16(length))
buff.Write(data)
return buff.Bytes(), nil
}
// TCPSession
type TCPSession struct {
*session
}
// NewTCPSession return an initialized *TCPSession
func NewTCPSession(conn net.Conn, options ...Option) *TCPSession {
op := loadOptions(options...)
if op.MsgCallback == nil {
// need message callback
panic(ErrNilMsgCallBack)
}
// init default codec
if op.Codec == nil {
op.Codec = DefTCPCodec{}
}
return &TCPSession{
session: newSession(conn, op),
}
}