-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
157 lines (142 loc) · 2.99 KB
/
conn.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
package tun
import (
"context"
"errors"
"io"
"net"
"sync"
"time"
)
// Conn implements net.Conn
type Conn struct {
LocalNetAddr net.Addr
RemoteNetAddr net.Addr
mu sync.Mutex
isOpen bool
recvBuf []byte
recvChan chan []byte
recvCtx context.Context
recvMu sync.Mutex
cancelRecv context.CancelFunc
sendChan chan []byte
sendCtx context.Context
cancelSend context.CancelFunc
}
func (c *Conn) Open() (recv, send chan []byte, err error) {
c.mu.Lock()
defer c.mu.Unlock()
if c.isOpen {
return nil, nil, &ErrType{
error: errors.New("already open"),
}
}
c.isOpen = true
c.recvChan, c.sendChan = make(chan []byte), make(chan []byte)
c.recvCtx, c.cancelRecv = context.WithCancel(context.Background())
c.sendCtx, c.cancelSend = context.WithCancel(context.Background())
return c.recvChan, c.sendChan, nil
}
func (c *Conn) Read(b []byte) (n int, err error) {
if !c.isOpen {
err = &ErrType{
error: errors.New("conn not open"),
}
return
}
c.recvMu.Lock()
defer c.recvMu.Unlock()
n = copy(b, c.recvBuf)
b = b[n:]
if len(b) == 0 {
// log.Debugf("Read %d bytes: %s", n, hex.EncodeToString(b0[:n]))
return
}
c.recvBuf = nil
select {
case <-c.recvCtx.Done():
err1 := &ErrType{
error: c.recvCtx.Err(),
}
if c.recvCtx.Err() == context.DeadlineExceeded {
err1.IsTimeout = true
}
err = err1
case recv, ok := <-c.recvChan:
if !ok {
err = io.EOF
} else {
n1 := copy(b, recv)
c.recvBuf = recv[n1:]
n += n1
}
}
// log.Debugf("Read %d bytes: %s", n, hex.EncodeToString(b0[:n]))
return
}
func (c *Conn) Write(b []byte) (n int, err error) {
// log.Debugf("Beging Write %d bytes: %s", len(b), hex.EncodeToString(b))
if !c.isOpen {
err = &ErrType{
error: errors.New("conn not open"),
}
return
}
bCopy := make([]byte, len(b))
copy(bCopy, b)
select {
case <-c.sendCtx.Done():
err1 := &ErrType{
error: c.sendCtx.Err(),
}
if c.sendCtx.Err() == context.DeadlineExceeded {
err1.IsTimeout = true
}
err = err1
case c.sendChan <- bCopy:
n = len(b)
}
// log.Debugf("Wrote %d bytes", n)
return
}
func (c *Conn) Close() error {
// log.Debugf("Close")
c.mu.Lock()
defer c.mu.Unlock()
if !c.isOpen {
return &ErrType{
error: errors.New("already closed"),
}
}
c.isOpen = false
c.cancelRecv()
c.cancelSend()
close(c.sendChan)
return nil
}
func (c *Conn) LocalAddr() net.Addr {
return c.LocalNetAddr
}
func (c *Conn) RemoteAddr() net.Addr {
return c.RemoteNetAddr
}
func (c *Conn) SetDeadline(t time.Time) error {
c.SetReadDeadline(t)
c.SetWriteDeadline(t)
return nil
}
func (c *Conn) SetReadDeadline(t time.Time) error {
if t.IsZero() {
c.recvCtx, c.cancelRecv = context.WithCancel(context.Background())
} else {
c.recvCtx, c.cancelRecv = context.WithDeadline(c.recvCtx, t)
}
return nil
}
func (c *Conn) SetWriteDeadline(t time.Time) error {
if t.IsZero() {
c.sendCtx, c.cancelSend = context.WithCancel(context.Background())
} else {
c.sendCtx, c.cancelSend = context.WithDeadline(c.recvCtx, t)
}
return nil
}