forked from skycoin/dmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
139 lines (112 loc) · 2.67 KB
/
listener.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
package dmsg
import (
"fmt"
"net"
"sync"
"sync/atomic"
"github.com/skycoin/dmsg/netutil"
)
// Listener listens for remote-initiated streams.
type Listener struct {
porter *netutil.Porter
addr Addr // local listening address
accept chan *Stream
mx sync.Mutex // protects 'accept'
doneFunc atomic.Value // callback when done, type: func()
done chan struct{}
once sync.Once
}
func newListener(porter *netutil.Porter, addr Addr) *Listener {
return &Listener{
porter: porter,
addr: addr,
accept: make(chan *Stream, AcceptBufferSize),
done: make(chan struct{}),
}
}
// addCloseCallback adds a function that triggers when listener is closed.
// This should be called right after the listener is created and is not thread safe.
func (l *Listener) addCloseCallback(cb func()) { l.doneFunc.Store(cb) }
// introduceStream handles a stream after receiving a REQUEST frame.
func (l *Listener) introduceStream(tp *Stream) error {
if tp.LocalAddr() != l.addr {
return fmt.Errorf("local addresses do not match: expected %s but got %s", l.addr, tp.LocalAddr())
}
l.mx.Lock()
defer l.mx.Unlock()
if l.isClosed() {
_ = tp.Close() //nolint:errcheck
return ErrEntityClosed
}
select {
case l.accept <- tp:
return nil
case <-l.done:
_ = tp.Close() //nolint:errcheck
return ErrEntityClosed
default:
_ = tp.Close() //nolint:errcheck
return ErrAcceptChanMaxed
}
}
// Accept accepts a connection.
func (l *Listener) Accept() (net.Conn, error) {
return l.AcceptStream()
}
// AcceptStream accepts a stream connection.
func (l *Listener) AcceptStream() (*Stream, error) {
select {
case tp, ok := <-l.accept:
if !ok {
return nil, ErrEntityClosed
}
if ok, closeFn := l.porter.ReserveChild(tp.lAddr.Port, tp.rAddr.Port, tp); ok {
tp.close = closeFn
}
return tp, nil
case <-l.done:
return nil, ErrEntityClosed
}
}
// Close closes the listener.
func (l *Listener) Close() error {
if l.close() {
return nil
}
return ErrEntityClosed
}
func (l *Listener) close() (closed bool) {
l.once.Do(func() {
closed = true
doneFunc, ok := l.doneFunc.Load().(func())
if ok {
doneFunc()
}
l.mx.Lock()
defer l.mx.Unlock()
close(l.done)
for {
select {
case <-l.accept:
default:
close(l.accept)
return
}
}
})
return closed
}
func (l *Listener) isClosed() bool {
select {
case <-l.done:
return true
default:
return false
}
}
// Addr returns the listener's address.
func (l *Listener) Addr() net.Addr { return l.addr }
// DmsgAddr returns the listener's address in as `dmsg.Addr`.
func (l *Listener) DmsgAddr() Addr { return l.addr }
// Type returns the stream type.
func (l *Listener) Type() string { return Type }