-
Notifications
You must be signed in to change notification settings - Fork 0
/
inproc.go
203 lines (179 loc) · 4.43 KB
/
inproc.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
// Copyright (c) 2021 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
// Package inproc implements an in-process connection.
package inproc
import (
"errors"
"io"
"net"
"sync"
"sync/atomic"
"time"
)
// network represents name of the network.
const network = "inproc"
// addr represents a network end point address.
//
// addr implements the net.Addr interface.
type addr struct {
network string
address string
}
// Network returns name of the network.
func (a addr) Network() string {
return a.network
}
// String returns string form of address.
func (a addr) String() string {
return a.address
}
// addrs contains in-process listeners.
var addrs struct {
locker sync.RWMutex
listeners map[addr]*listener
}
func init() {
addrs.listeners = make(map[addr]*listener)
}
// conn implements the net.Conn interface.
type conn struct {
r io.ReadCloser
w io.WriteCloser
laddr addr
raddr addr
}
// Read reads data from the connection.
func (c *conn) Read(b []byte) (n int, err error) {
return c.r.Read(b)
}
// Write writes data to the connection.
func (c *conn) Write(b []byte) (n int, err error) {
return c.w.Write(b)
}
// Close closes the connection.
func (c *conn) Close() (err error) {
if c.w != nil {
err = c.w.Close()
}
if c.r != nil {
err = c.r.Close()
}
return
}
// LocalAddr returns the local network address.
func (c *conn) LocalAddr() net.Addr {
return c.laddr
}
// RemoteAddr returns the remote network address.
func (c *conn) RemoteAddr() net.Addr {
return c.raddr
}
// SetDeadline implements the net.Conn SetDeadline method.
func (c *conn) SetDeadline(t time.Time) error {
return errors.New("not supported")
}
// SetReadDeadline implements the net.Conn SetReadDeadline method.
func (c *conn) SetReadDeadline(t time.Time) error {
return errors.New("not supported")
}
// SetWriteDeadline implements the net.Conn SetWriteDeadline method.
func (c *conn) SetWriteDeadline(t time.Time) error {
return errors.New("not supported")
}
// Dial connects to an address.
func Dial(address string) (net.Conn, error) {
raddr := addr{network: network, address: address}
var accepter *accepter
r, w := io.Pipe()
conn := &conn{w: w, laddr: raddr}
addrs.locker.RLock()
l, ok := addrs.listeners[raddr]
if !ok {
addrs.locker.RUnlock()
return nil, errors.New("connection refused")
}
addrs.locker.RUnlock()
l.locker.Lock()
for {
if len(l.accepters) > 0 {
accepter = l.accepters[len(l.accepters)-1]
l.accepters = l.accepters[:len(l.accepters)-1]
break
}
l.cond.Wait()
}
l.locker.Unlock()
conn.r = accepter.reader
conn.raddr = conn.laddr
accepter.conn.r = r
accepter.conn.raddr = conn.laddr
close(accepter.done)
return conn, nil
}
// listener implements the net.Listener interface.
type listener struct {
laddr addr
cond sync.Cond
locker sync.Mutex
accepters []*accepter
done chan struct{}
closed int32
}
type accepter struct {
*conn
reader io.ReadCloser
done chan struct{}
}
// Listen announces on the local address.
func Listen(address string) (net.Listener, error) {
laddr := addr{network: network, address: address}
l := &listener{laddr: laddr, done: make(chan struct{})}
l.cond.L = &l.locker
addrs.locker.Lock()
if _, ok := addrs.listeners[l.laddr]; ok {
addrs.locker.Unlock()
return nil, errors.New("address already in use")
}
addrs.listeners[l.laddr] = l
addrs.locker.Unlock()
return l, nil
}
// Accept waits for and returns the next connection to the listener.
func (l *listener) Accept() (net.Conn, error) {
r, w := io.Pipe()
accepter := &accepter{conn: &conn{w: w, laddr: l.laddr}, reader: r}
accepter.done = make(chan struct{})
l.locker.Lock()
l.accepters = append(l.accepters, accepter)
l.locker.Unlock()
l.cond.Signal()
select {
case <-accepter.done:
return accepter.conn, nil
case <-l.done:
return nil, errors.New("use of closed network connection")
}
}
// Close closes the listener.
// Any blocked Accept operations will be unblocked and return errors.
func (l *listener) Close() error {
if atomic.CompareAndSwapInt32(&l.closed, 0, 1) {
close(l.done)
}
addrs.locker.Lock()
delete(addrs.listeners, l.laddr)
addrs.locker.Unlock()
l.locker.Lock()
accepters := l.accepters
l.accepters = nil
l.locker.Unlock()
l.cond.Broadcast()
for _, accepter := range accepters {
accepter.Close()
}
return nil
}
// Addr returns the listener's network address.
func (l *listener) Addr() net.Addr {
return l.laddr
}