-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
183 lines (160 loc) · 3.6 KB
/
connection.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package hconx
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"net"
"sync"
"sync/atomic"
)
const (
HeadLenght = 20
)
var (
connctionPool sync.Pool
connectIndex uint64
connectCreateCount uint64
)
func init() {
connctionPool = sync.Pool{
New: func() interface{} {
atomic.AddUint64(&connectCreateCount, 1)
return &Connection{lock: new(sync.RWMutex)}
},
}
}
type OnConnHandle func(conn *Connection) error
type Connection struct {
ConnIndex int64
lock *sync.RWMutex
conn net.Conn
Head *HeadInfo
headBuf []byte
Body []byte
}
// readHead read head message with HeadLenght
func (c *Connection) readHead() error {
err := c.readSize(HeadLenght, &c.headBuf)
if err != nil {
return err
}
c.lock.Lock()
defer c.lock.Unlock()
c.Head = &HeadInfo{}
c.Head.FromBytes(c.headBuf)
return nil
}
// readBody read body message with head datalen
func (c *Connection) readBody(size int64) error {
err := c.readSize(size, &c.Body)
return err
}
// readMessage read head and body from conn, check head flag is match
func (c *Connection) readMessage() error {
err := c.readHead()
if err != nil {
return err
}
if c.Head == nil {
err := errors.New("read HeadInfo is nil")
return err
}
if c.Head.Flag != HeadFlag {
err := errors.New(fmt.Sprintf("check head-flag not match readFlag:%v mustFlag:%v", c.Head.Flag, HeadFlag))
return err
}
return c.readBody(int64(c.Head.DataLen))
}
// readSize read message with size
func (c *Connection) readSize(size int64, buf *[]byte) error {
*buf = make([]byte, 0)
var err error
leftSize := size
for {
bufinner := make([]byte, leftSize)
var n int
n, err = c.conn.Read(bufinner)
leftSize -= int64(n)
if err == nil {
*buf = slice_merge(*buf, bufinner)
if leftSize <= 0 {
//read end
break
}
} else {
break
}
}
return err
}
func (c *Connection) Write(p []byte) (int, error) {
return c.conn.Write(p)
}
func (c *Connection) WriteMerge(head []byte, body []byte) (int, error) {
return c.conn.Write(slice_merge(head, body))
}
// GobEncode default encoding with gob mode
func (c *Connection) GobEncode(data interface{}) ([]byte, error) {
var encoded bytes.Buffer
enc := gob.NewEncoder(&encoded)
err := enc.Encode(data)
if err != nil {
return nil, err
} else {
return encoded.Bytes(), nil
}
}
// GobDecode default decoding with gob mode
func (c *Connection) GobDecode(data []byte, dst interface{}) error {
decoder := gob.NewDecoder(bytes.NewReader(data))
err := decoder.Decode(dst)
return err
}
// ParseMessage parse body to message ptr
func (c *Connection) ParseMessage() (*Message, error) {
msg := new(Message)
err := c.GobDecode(c.Body, msg)
return msg, err
}
// SendMessage send message to remote addr
func (c *Connection) SendMessage(msg *Message) error {
head := DefaultHead()
message, err := c.GobEncode(msg)
if err != nil {
return err
}
head.DataLen = uint64(len(message))
_, err = c.WriteMerge(head.GetBytes(), message)
return err
}
// Close close conn and put to pool
func (c *Connection) Close() {
c.conn.Close()
putConnction(c)
}
func (c *Connection) RemoteAddr() string {
return c.conn.RemoteAddr().String()
}
// NewConnction create new conn with net.Conn
func NewConnction(con net.Conn) *Connection {
atomic.AddUint64(&connectIndex, 1)
c := connctionPool.Get()
if h, ok := c.(*Connection); ok {
h.conn = con
return h
}
return &Connection{lock: new(sync.RWMutex), conn: con}
}
func putConnction(c *Connection) {
c.ConnIndex = 0
c.conn = nil
c.Head = nil
c.headBuf = nil
c.Body = nil
connctionPool.Put(c)
}
func slice_merge(s1, s2 []byte) (c []byte) {
c = append(s1, s2...)
return c
}