forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnection.go
More file actions
144 lines (126 loc) 路 3.37 KB
/
Copy pathconnection.go
File metadata and controls
144 lines (126 loc) 路 3.37 KB
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
// 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 darwin netbsd freebsd openbsd dragonfly linux
package gnet
import (
"net"
"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 *loop // connected loop
cache []byte // reuse memory of inbound data
opened bool // connection opened event fired
action Action // next user action
localAddr net.Addr // local addre
remoteAddr net.Addr // remote addr
inboundBuffer *ringbuffer.RingBuffer // buffer for data from client
outboundBuffer *ringbuffer.RingBuffer // buffer for data that is ready to write to client
}
func (c *conn) Read() []byte {
if c.inboundBuffer.IsEmpty() {
return c.cache
}
top, _ := c.inboundBuffer.PreReadAll()
return append(top, c.cache...)
}
func (c *conn) ResetBuffer() {
c.cache = c.cache[:0]
c.inboundBuffer.Reset()
}
func (c *conn) ReadN(n int) (size int, buf []byte) {
oneOffBufferLen := len(c.cache)
inBufferLen := c.inboundBuffer.Length()
if inBufferLen+oneOffBufferLen < n {
return
}
if c.inboundBuffer.IsEmpty() {
size = n
buf = c.cache[:n]
if n == oneOffBufferLen {
c.cache = c.cache[:0]
} else {
c.cache = c.cache[n:]
}
return
}
size = n
buf, tail := c.inboundBuffer.PreRead(n)
if tail != nil {
buf = append(buf, tail...)
}
if inBufferLen >= n {
//c.ShiftN(n)
c.inboundBuffer.Shift(n)
return
}
c.inboundBuffer.Reset()
restSize := n - inBufferLen
buf = append(buf, c.cache[:restSize]...)
if restSize == oneOffBufferLen {
c.cache = c.cache[:0]
} else {
c.cache = c.cache[restSize:]
}
return
}
//func (c *conn) ShiftN(n int) {
// c.inboundBuffer.Shift(n)
//}
func (c *conn) BufferLength() int {
return c.inboundBuffer.Length() + len(c.cache)
}
func (c *conn) AsyncWrite(buf []byte) {
_ = c.loop.poller.Trigger(func() error {
c.write(buf)
//ringbuffer.Recycle(buf)
return 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) 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, sa unix.Sockaddr) {
_ = unix.Sendto(c.fd, buf, 0, sa)
}
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 }
//func (c *conn) Wake() {
// if c.loop != nil {
// sniffError(c.loop.poller.Trigger(c))
// }
//}