/
PacketConnection.go
249 lines (206 loc) · 6.58 KB
/
PacketConnection.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package netutil
import (
"fmt"
"net"
"encoding/binary"
"sync"
"time"
"sync/atomic"
"github.com/pkg/errors"
"github.com/xiaonanln/goworld/engine/consts"
"github.com/xiaonanln/goworld/engine/gwioutil"
"github.com/xiaonanln/goworld/engine/gwlog"
"github.com/xiaonanln/goworld/engine/netutil/compress"
"github.com/xiaonanln/goworld/engine/opmon"
)
const (
_MAX_PACKET_SIZE = 25 * 1024 * 1024 // _MAX_PACKET_SIZE is the max size limit of packets in packet connections
_SIZE_FIELD_SIZE = 4 // _SIZE_FIELD_SIZE is the packet size field (uint32) size
_PREPAYLOAD_SIZE = _SIZE_FIELD_SIZE
_MAX_PAYLOAD_LENGTH = _MAX_PACKET_SIZE - _PREPAYLOAD_SIZE
)
var (
// NETWORK_ENDIAN is the network Endian of connections
NETWORK_ENDIAN = binary.LittleEndian
errRecvAgain = _ErrRecvAgain{}
//compressWritersPool = xnsyncutil.NewNewlessPool()
)
func init() {
//for i := 0; i < consts.COMPRESS_WRITER_POOL_SIZE; i++ {
// cw, err := flate.NewWriter(os.Stderr, flate.BestSpeed)
// if err != nil {
// gwlog.Fatalf("create flate compressor failed: %v", err)
// }
//
// compressWritersPool.Put(cw)
//}
//gwlog.Infof("%d compress writer created.", consts.COMPRESS_WRITER_POOL_SIZE)
}
type _ErrRecvAgain struct{}
func (err _ErrRecvAgain) Error() string {
return "recv again"
}
func (err _ErrRecvAgain) Temporary() bool {
return true
}
func (err _ErrRecvAgain) Timeout() bool {
return true
}
// PacketConnection is a connection that send and receive data packets upon a network stream connection
type PacketConnection struct {
conn Connection
compressed bool
pendingPackets []*Packet
pendingPacketsLock sync.Mutex
// buffers and infos for receiving a packet
payloadLenBuf [_SIZE_FIELD_SIZE]byte
payloadLenBytesRecved int
recvCompressed bool
recvTotalPayloadLen uint32
recvedPayloadLen uint32
recvingPacket *Packet
compressor compress.Compressor
}
// NewPacketConnection creates a packet connection based on network connection
func NewPacketConnection(conn Connection, compressor compress.Compressor) *PacketConnection {
pc := &PacketConnection{
conn: conn,
compressed: compressor != nil,
}
pc.compressor = compressor
return pc
}
// NewPacket allocates a new packet (usually for sending)
func (pc *PacketConnection) NewPacket() *Packet {
return allocPacket()
}
// SendPacket send packets to remote
func (pc *PacketConnection) SendPacket(packet *Packet) error {
if consts.DEBUG_PACKETS {
gwlog.Debugf("%s SEND PACKET %p: msgtype=%v, payload(%d)=%v", pc, packet,
packetEndian.Uint16(packet.bytes[_PREPAYLOAD_SIZE:_PREPAYLOAD_SIZE+2]),
packet.GetPayloadLen(),
packet.bytes[_PREPAYLOAD_SIZE+2:_PREPAYLOAD_SIZE+packet.GetPayloadLen()])
}
if atomic.LoadInt64(&packet.refcount) <= 0 {
gwlog.Panicf("sending packet with refcount=%d", packet.refcount)
}
packet.AddRefCount(1)
pc.pendingPacketsLock.Lock()
pc.pendingPackets = append(pc.pendingPackets, packet)
pc.pendingPacketsLock.Unlock()
return nil
}
// Flush connection writes
func (pc *PacketConnection) Flush(reason string) (err error) {
pc.pendingPacketsLock.Lock()
if len(pc.pendingPackets) == 0 { // no packets to send, common to happen, so handle efficiently
pc.pendingPacketsLock.Unlock()
return
}
packets := make([]*Packet, 0, len(pc.pendingPackets))
packets, pc.pendingPackets = pc.pendingPackets, packets
pc.pendingPacketsLock.Unlock()
// flush should only be called in one goroutine
op := opmon.StartOperation("FlushPackets-" + reason)
defer op.Finish(time.Millisecond * 300)
//var cw *flate.Writer
if len(packets) == 1 {
// only 1 packet to send, just send it directly, no need to use send buffer
packet := packets[0]
if pc.compressed && packet.requireCompress() {
packet.compress(pc.compressor)
}
err = gwioutil.WriteAll(pc.conn, packet.data())
packet.Release()
if err == nil {
err = pc.conn.Flush()
}
return
}
for _, packet := range packets {
if pc.compressed && packet.requireCompress() {
packet.compress(pc.compressor)
}
gwioutil.WriteAll(pc.conn, packet.data())
packet.Release()
}
// now we send all data in the send buffer
if err == nil {
err = pc.conn.Flush()
}
return
}
// SetRecvDeadline sets the receive deadline
func (pc *PacketConnection) SetRecvDeadline(deadline time.Time) error {
return pc.conn.SetReadDeadline(deadline)
}
// RecvPacket receives the next packet
func (pc *PacketConnection) RecvPacket() (*Packet, error) {
if pc.payloadLenBytesRecved < _SIZE_FIELD_SIZE {
// receive more of payload len bytes
n, err := pc.conn.Read(pc.payloadLenBuf[pc.payloadLenBytesRecved:])
pc.payloadLenBytesRecved += n
if pc.payloadLenBytesRecved < _SIZE_FIELD_SIZE {
if err == nil {
err = errRecvAgain
}
return nil, err // packet not finished yet
}
pc.recvTotalPayloadLen = NETWORK_ENDIAN.Uint32(pc.payloadLenBuf[:])
//pc.recvCompressed = false
if pc.recvCompressed {
gwlog.Panicf("should be false")
}
if pc.recvTotalPayloadLen&_PAYLOAD_COMPRESSED_BIT_MASK != 0 {
pc.recvTotalPayloadLen &= _PAYLOAD_LEN_MASK
pc.recvCompressed = true
}
if pc.recvTotalPayloadLen == 0 || pc.recvTotalPayloadLen > _MAX_PAYLOAD_LENGTH {
err := errors.Errorf("invalid payload length: %v", pc.recvTotalPayloadLen)
pc.resetRecvStates()
pc.Close()
return nil, err
}
pc.recvedPayloadLen = 0
pc.recvingPacket = NewPacket()
pc.recvingPacket.AssureCapacity(pc.recvTotalPayloadLen)
}
// now all bytes of payload len is received, start receiving payload
n, err := pc.conn.Read(pc.recvingPacket.bytes[_PREPAYLOAD_SIZE+pc.recvedPayloadLen : _PREPAYLOAD_SIZE+pc.recvTotalPayloadLen])
pc.recvedPayloadLen += uint32(n)
if pc.recvedPayloadLen == pc.recvTotalPayloadLen {
// full packet received, return the packet
packet := pc.recvingPacket
packet.setPayloadLenCompressed(pc.recvTotalPayloadLen, pc.recvCompressed)
pc.resetRecvStates()
packet.decompress(pc.compressor)
return packet, nil
}
if err == nil {
err = errRecvAgain
}
return nil, err
}
func (pc *PacketConnection) resetRecvStates() {
pc.payloadLenBytesRecved = 0
pc.recvTotalPayloadLen = 0
pc.recvedPayloadLen = 0
pc.recvingPacket = nil
pc.recvCompressed = false
}
// Close the connection
func (pc *PacketConnection) Close() error {
return pc.conn.Close()
}
// RemoteAddr return the remote address
func (pc *PacketConnection) RemoteAddr() net.Addr {
return pc.conn.RemoteAddr()
}
// LocalAddr returns the local address
func (pc *PacketConnection) LocalAddr() net.Addr {
return pc.conn.LocalAddr()
}
func (pc *PacketConnection) String() string {
return fmt.Sprintf("[%s >>> %s]", pc.LocalAddr(), pc.RemoteAddr())
}