forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_unix.go
224 lines (195 loc) · 5.27 KB
/
connection_unix.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
// Copyright 2019 Andy Pan. All rights reserved.
// Copyright 2018 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// +build linux darwin netbsd freebsd openbsd dragonfly
package gnet
import (
"net"
"github.com/panjf2000/gnet/internal/netpoll"
"github.com/panjf2000/gnet/pool/bytebuffer"
prb "github.com/panjf2000/gnet/pool/ringbuffer"
"github.com/panjf2000/gnet/ringbuffer"
"golang.org/x/sys/unix"
)
type conn struct {
fd int // file descriptor
sa unix.Sockaddr // remote socket address
ctx interface{} // user-defined context
loop *eventloop // connected event-loop
buffer []byte // reuse memory of inbound data as a temporary buffer
codec ICodec // codec for TCP
opened bool // connection opened event fired
localAddr net.Addr // local addr
remoteAddr net.Addr // remote addr
byteBuffer *bytebuffer.ByteBuffer // bytes buffer for buffering current packet and data in ring-buffer
inboundBuffer *ringbuffer.RingBuffer // buffer for data from client
outboundBuffer *ringbuffer.RingBuffer // buffer for data that is ready to write to client
}
func newTCPConn(fd int, el *eventloop, sa unix.Sockaddr) *conn {
return &conn{
fd: fd,
sa: sa,
loop: el,
codec: el.codec,
inboundBuffer: prb.Get(),
outboundBuffer: prb.Get(),
}
}
func (c *conn) releaseTCP() {
c.opened = false
c.sa = nil
c.ctx = nil
c.buffer = nil
c.localAddr = nil
c.remoteAddr = nil
prb.Put(c.inboundBuffer)
prb.Put(c.outboundBuffer)
c.inboundBuffer = nil
c.outboundBuffer = nil
bytebuffer.Put(c.byteBuffer)
c.byteBuffer = nil
}
func newUDPConn(fd int, el *eventloop, sa unix.Sockaddr) *conn {
return &conn{
fd: fd,
sa: sa,
localAddr: el.svr.ln.lnaddr,
remoteAddr: netpoll.SockaddrToUDPAddr(sa),
}
}
func (c *conn) releaseUDP() {
c.ctx = nil
c.localAddr = nil
c.remoteAddr = nil
}
func (c *conn) open(buf []byte) {
n, err := unix.Write(c.fd, buf)
if err != nil {
_, _ = c.outboundBuffer.Write(buf)
return
}
if n < len(buf) {
_, _ = c.outboundBuffer.Write(buf[n:])
}
}
func (c *conn) read() ([]byte, error) {
return c.codec.Decode(c)
}
func (c *conn) write(buf []byte) {
if !c.outboundBuffer.IsEmpty() {
_, _ = c.outboundBuffer.Write(buf)
return
}
n, err := unix.Write(c.fd, buf)
if err != nil {
if err == unix.EAGAIN {
_, _ = c.outboundBuffer.Write(buf)
_ = c.loop.poller.ModReadWrite(c.fd)
return
}
_ = c.loop.loopCloseConn(c, err)
return
}
if n < len(buf) {
_, _ = c.outboundBuffer.Write(buf[n:])
_ = c.loop.poller.ModReadWrite(c.fd)
}
}
func (c *conn) sendTo(buf []byte) error {
return unix.Sendto(c.fd, buf, 0, c.sa)
}
// ================================= Public APIs of gnet.Conn =================================
func (c *conn) Read() []byte {
if c.inboundBuffer.IsEmpty() {
return c.buffer
}
c.byteBuffer = c.inboundBuffer.WithByteBuffer(c.buffer)
return c.byteBuffer.Bytes()
}
func (c *conn) ResetBuffer() {
c.buffer = c.buffer[:0]
c.inboundBuffer.Reset()
bytebuffer.Put(c.byteBuffer)
c.byteBuffer = nil
}
func (c *conn) ReadN(n int) (size int, buf []byte) {
inBufferLen := c.inboundBuffer.Length()
tempBufferLen := len(c.buffer)
if totalLen := inBufferLen + tempBufferLen; totalLen < n || n <= 0 {
n = totalLen
}
size = n
if c.inboundBuffer.IsEmpty() {
buf = c.buffer[:n]
return
}
head, tail := c.inboundBuffer.LazyRead(n)
c.byteBuffer = bytebuffer.Get()
_, _ = c.byteBuffer.Write(head)
_, _ = c.byteBuffer.Write(tail)
if inBufferLen >= n {
buf = c.byteBuffer.Bytes()
return
}
restSize := n - inBufferLen
_, _ = c.byteBuffer.Write(c.buffer[:restSize])
buf = c.byteBuffer.Bytes()
return
}
func (c *conn) ShiftN(n int) (size int) {
inBufferLen := c.inboundBuffer.Length()
tempBufferLen := len(c.buffer)
if inBufferLen+tempBufferLen < n || n <= 0 {
c.ResetBuffer()
size = inBufferLen + tempBufferLen
return
}
size = n
if c.inboundBuffer.IsEmpty() {
c.buffer = c.buffer[n:]
return
}
bytebuffer.Put(c.byteBuffer)
c.byteBuffer = nil
if inBufferLen >= n {
c.inboundBuffer.Shift(n)
return
}
c.inboundBuffer.Reset()
restSize := n - inBufferLen
c.buffer = c.buffer[restSize:]
return
}
func (c *conn) BufferLength() int {
return c.inboundBuffer.Length() + len(c.buffer)
}
func (c *conn) AsyncWrite(buf []byte) (err error) {
var encodedBuf []byte
if encodedBuf, err = c.codec.Encode(c, buf); err == nil {
return c.loop.poller.Trigger(func() error {
if c.opened {
c.write(encodedBuf)
}
return nil
})
}
return
}
func (c *conn) SendTo(buf []byte) error {
return c.sendTo(buf)
}
func (c *conn) Wake() error {
return c.loop.poller.Trigger(func() error {
return c.loop.loopWake(c)
})
}
func (c *conn) Close() error {
return c.loop.poller.Trigger(func() error {
return c.loop.loopCloseConn(c, nil)
})
}
func (c *conn) Context() interface{} { return c.ctx }
func (c *conn) SetContext(ctx interface{}) { c.ctx = ctx }
func (c *conn) LocalAddr() net.Addr { return c.localAddr }
func (c *conn) RemoteAddr() net.Addr { return c.remoteAddr }