-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection.go
154 lines (127 loc) · 2.67 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
package evnet
import (
"io"
"net"
. "github.com/bruce2233/evnet/socket"
"github.com/zyedidia/generic/queue"
"golang.org/x/sys/unix"
)
type Conn interface {
//working
io.Reader
Writer
Socket
Next(n int) ([]byte, error)
// LocalAddr is the connection's local socket address.
LocalAddr() (addr net.Addr)
// RemoteAddr is the connection's remote peer address.
RemoteAddr() (addr net.Addr)
SetContext(ctx interface{})
Context() interface{}
Close() error
}
type Writer interface {
io.Writer
AsyncWrite([]byte, func(Conn) error) error
}
type task struct {
data []byte
AfterWritten func(Conn) error
next *task
}
type conn struct {
fd int
localAddr net.Addr
remoteAddr net.Addr
inboundBuffer []byte
outboundBuffer []byte
reactor *SubReactor
ctx interface{}
asyncTaskQueue *queue.Queue[*task]
}
func (c conn) Read(p []byte) (n int, err error) {
//working
if err != nil {
return -1, err
}
return
}
//sync Write
func (c *conn) Write(p []byte) (n int, err error) {
// if len(c.outboundBuffer) > 0 {
// return -1, errors.New("Previous data waiting")
// }
sentSum := 0
// bufferedLen := len(c.outboundBuffer)
sent, err := unix.Write(c.Fd(), p)
if sent != -1 {
p = p[sent:]
sentSum += sent
}
for sent > 0 {
sent, err = unix.Write(c.Fd(), p)
p = p[sent:]
sentSum += sent
// if sent < bufferedLen {
// c.outboundBuffer = c.outboundBuffer[sent:]
// }
}
return sentSum, err
}
//unsafe Async Write
func (c *conn) AsyncWrite(p []byte, AfterWritten func(c Conn) (err error)) error {
newTask := &task{
data: p,
AfterWritten: AfterWritten,
}
if c.asyncTaskQueue.Empty() {
c.outboundBuffer = newTask.data
}
c.asyncTaskQueue.Enqueue(newTask)
c.reactor.poller.ModReadWrite(c.Fd())
return nil
}
func (c *conn) Next(n int) (buf []byte, err error) {
//working
if n <= 0 {
return c.inboundBuffer, nil
}
defer c.Discard(-1)
return nil, nil
}
//clsoe connection
func (c *conn) Close() error {
c.reactor.closeConn(c)
return nil
}
func (c *conn) Discard(n int) (int, error) {
if n == -1 {
discardedLen := len(c.inboundBuffer)
c.inboundBuffer = make([]byte, 0)
return discardedLen, nil
}
return 0, nil
}
func (c *conn) Fd() int {
return c.fd
}
func (c *conn) LocalAddr() net.Addr {
return c.localAddr
}
func (c *conn) RemoteAddr() net.Addr {
return c.remoteAddr
}
func (c *conn) SetContext(ctx interface{}) {
c.ctx = ctx
}
func (c *conn) Context() interface{} {
return c.ctx
}
func NewConn(fd int, la net.Addr, ra net.Addr) (Conn, error) {
conn := new(conn)
conn.fd = fd
conn.localAddr = la
conn.remoteAddr = ra
conn.asyncTaskQueue = queue.New[*task]()
return conn, nil
}