/
init.go
169 lines (148 loc) · 3.26 KB
/
init.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package local
import (
"fmt"
"io"
"net"
"strconv"
"sync"
"time"
"v.io/v23/context"
"v.io/v23/flow"
)
func init() {
flow.RegisterProtocol("local", &local{listeners: map[addr]*listener{}})
}
type addr string
func (a addr) Network() string { return "local" }
func (a addr) String() string { return string(a) }
type conn struct {
addr addr
incoming chan []byte
peer *conn
*wire
}
func (c *conn) LocalAddr() net.Addr { return c.addr }
func (c *conn) RemoteAddr() net.Addr { return c.peer.addr }
func (c *conn) ReadMsg() ([]byte, error) {
select {
case msg := <-c.incoming:
return msg, nil
case <-c.wire.closech:
return nil, io.EOF
}
}
func (c *conn) WriteMsg(data ...[]byte) (int, error) {
l := 0
for _, b := range data {
l += len(b)
}
agg := make([]byte, 0, l)
for _, b := range data {
agg = append(agg, b...)
}
select {
case c.peer.incoming <- agg:
return l, nil
case <-c.wire.closech:
return 0, io.EOF
}
}
type wire struct {
mu sync.Mutex
closed bool
closech chan struct{}
}
func (w *wire) Close() error {
w.mu.Lock()
if !w.closed {
w.closed = true
close(w.closech)
}
w.mu.Unlock()
return nil
}
type listener struct {
addr addr
conns chan *conn
closech chan struct{}
local *local
closed bool // protected by local.mu
}
func (l *listener) Accept(ctx *context.T) (flow.Conn, error) {
select {
case c := <-l.conns:
return c, nil
case <-l.closech:
return nil, fmt.Errorf("listener closed")
}
}
func (l *listener) Addr() net.Addr {
return l.addr
}
func (l *listener) Close() error {
l.local.mu.Lock()
if !l.closed {
l.closed = true
close(l.closech)
}
l.local.mu.Unlock()
return nil
}
type local struct {
mu sync.Mutex
next int
listeners map[addr]*listener
}
func (l *local) nextAddrLocked() addr {
ret := strconv.FormatInt(int64(l.next), 10)
l.next++
return addr(ret)
}
func (l *local) Dial(ctx *context.T, network, address string, timeout time.Duration) (flow.Conn, error) {
l.mu.Lock()
listener := l.listeners[addr(address)]
daddr, aaddr := l.nextAddrLocked(), l.nextAddrLocked()
l.mu.Unlock()
if listener == nil {
return nil, fmt.Errorf("unreachable")
}
w := &wire{closech: make(chan struct{})}
d := &conn{addr: daddr, incoming: make(chan []byte), wire: w}
a := &conn{addr: aaddr, incoming: make(chan []byte), wire: w}
d.peer, a.peer = a, d
t := time.NewTimer(timeout)
defer t.Stop()
select {
case <-t.C:
return nil, fmt.Errorf("timeout")
case listener.conns <- a:
return d, nil
case <-listener.closech:
return nil, fmt.Errorf("unreachable")
}
}
func (l *local) Resolve(ctx *context.T, network, address string) (string, []string, error) {
return network, []string{address}, nil
}
func (l *local) Listen(ctx *context.T, network, address string) (flow.Listener, error) {
defer l.mu.Unlock()
l.mu.Lock()
a := addr(address)
if a == "" {
a = l.nextAddrLocked()
}
if _, ok := l.listeners[a]; ok {
return nil, fmt.Errorf("address in use")
}
listener := &listener{
addr: a,
conns: make(chan *conn, 1),
closech: make(chan struct{}),
local: l,
}
l.listeners[a] = listener
return listener, nil
}