forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_unix.go
243 lines (213 loc) · 6.25 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (c) 2019 Andy Pan
// Copyright (c) 2018 Joshua J Baker
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// +build linux freebsd dragonfly darwin
package gnet
import (
"net"
"os"
"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.svr.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.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) (err error) {
var outFrame []byte
if outFrame, err = c.codec.Encode(c, buf); err != nil {
return
}
if !c.outboundBuffer.IsEmpty() {
_, _ = c.outboundBuffer.Write(outFrame)
return
}
var n int
if n, err = unix.Write(c.fd, outFrame); err != nil {
if err == unix.EAGAIN {
_, _ = c.outboundBuffer.Write(outFrame)
err = c.loop.poller.ModReadWrite(c.fd)
return
}
_ = c.loop.loopCloseConn(c, os.NewSyscallError("write", err))
return
}
if n < len(outFrame) {
_, _ = c.outboundBuffer.Write(outFrame[n:])
err = c.loop.poller.ModReadWrite(c.fd)
}
return
}
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) error {
return c.loop.poller.Trigger(func() (err error) {
if c.opened {
err = c.write(buf)
}
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 }