-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchan.go
255 lines (210 loc) · 4.14 KB
/
chan.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
244
245
246
247
248
249
250
251
252
253
254
package listener
import (
"bytes"
"context"
"fmt"
"io"
"net"
"sync"
"time"
)
var (
dnsOfChannels = map[string]*ChanListener{}
dnsLock = new(sync.RWMutex)
)
func registryChannelListener(lis *ChanListener) error {
dnsLock.Lock()
defer dnsLock.Unlock()
addr := lis.Addr().String()
_, ok := dnsOfChannels[addr]
if ok {
return fmt.Errorf("cannot bind to address %s: already listen",addr)
}
dnsOfChannels[addr] = lis
return nil
}
func unregistryChannelListener(addr string) {
dnsLock.Lock()
defer dnsLock.Unlock()
delete(dnsOfChannels,addr)
}
func getChannelListener(addr string) (lis *ChanListener, ok bool) {
dnsLock.RLock()
defer dnsLock.RUnlock()
lis, ok = dnsOfChannels[addr]
return
}
type ChanAddr string
func (c ChanAddr) Network() string {
return `channel`
}
func (c ChanAddr) String() string {
return string(c)
}
func DialChannel(ctx context.Context,network, address string) (*ChanConn,error) {
if ctx == nil {
panic("nil context")
}
if network != "channel" {
return nil, &net.OpError{
Op: "dial",
Net:"channel",
Addr:ChanAddr(address),
Err: fmt.Errorf("unexpected network %s",network),
}
}
listener , ok := getChannelListener(address)
if !ok {
return nil, &net.OpError{
Op: "dial",
Net:"channel",
Addr:ChanAddr(address),
Err: fmt.Errorf("cannot resolve address %s",address),
}
}
conn := &ChanConn{
ctx:ctx,
raddr:address,
readBuff: new(bytes.Buffer),
read:make(chan []byte,0),
write:make(chan []byte,1),
}
err := listener.send(conn)
if err != nil {
return nil, err
}
return conn,nil
}
func ListenChannel(ctx context.Context, address string) (*ChanListener,error) {
lis := &ChanListener{
ctx: ctx,
once: new(sync.Once),
addr: ChanAddr(address),
conns: make(chan *ChanConn,1),
}
err := registryChannelListener(lis)
if err != nil {
return nil, err
}
return lis,nil
}
type ChanConn struct {
raddr string
ctx context.Context
readBuff *bytes.Buffer
read chan []byte
write chan []byte
}
func (c ChanConn) Read(b []byte) (n int, err error) {
if c.readBuff.Len() > 0 {
n, err = c.readBuff.Read(b)
return
}
select {
case rb, ok := <-c.read:
if !ok {
return 0, io.EOF
}
if c.readBuff.Len() == 0 {
n, err = c.readBuff.Write(rb)
if err != nil {
return
}
}
n, err = c.readBuff.Read(b)
return
case <-c.ctx.Done():
err = c.ctx.Err()
return
}
}
func (c ChanConn) Write(b []byte) (n int, err error) {
select {
case c.write <- b:
return len(b), nil
case <-c.ctx.Done():
return 0, c.ctx.Err()
}
}
func (c ChanConn) Close() error {
close(c.read)
close(c.write)
return nil
}
func (c ChanConn) LocalAddr() net.Addr {
return ChanAddr(fmt.Sprintf("l%p",c.write))
}
func (c ChanConn) RemoteAddr() net.Addr {
return ChanAddr(c.raddr)
}
func (ChanConn) SetDeadline(t time.Time) error {
//TODO: implement
//println("call SetDeadline",t.String())
return nil
}
func (ChanConn) SetReadDeadline(t time.Time) error {
//TODO: implement
//println("call SetReadDeadline",t.String())
return nil
}
func (ChanConn) SetWriteDeadline(t time.Time) error {
//TODO: implement
//println("call SetWriteDeadline",t.String())
return nil
}
type ChanListener struct {
once *sync.Once
ctx context.Context
addr ChanAddr
conns chan *ChanConn
}
func (c ChanListener) Accept() (net.Conn, error) {
conn, ok := <- c.conns
if !ok {
return nil, io.EOF
}
lisConn := &ChanConn{
ctx: conn.ctx,
raddr: conn.raddr,
readBuff: new(bytes.Buffer),
read:make(chan []byte,1),
write:make(chan []byte,0),
}
p := <- conn.write
lisConn.read <- p
start := make(chan struct{})
go func () {
close(start)
select {
case d, ok := <- lisConn.write:
if !ok {
return
}
conn.read <- d
case <- conn.ctx.Done():
return
}
}()
<-start
return lisConn, nil
}
func (c ChanListener) send(conn *ChanConn) error {
select {
case c.conns <- conn:
return nil
case <-c.ctx.Done():
return c.ctx.Err()
case <-conn.ctx.Done():
return conn.ctx.Err()
}
}
func (c ChanListener) Close() error {
c.once.Do(func () {
unregistryChannelListener(c.Addr().String())
close(c.conns)
})
return nil
}
func (c ChanListener) Addr() net.Addr {
return c.addr
}