-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener.go
146 lines (131 loc) · 3.27 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
140
141
142
143
144
145
146
package utp
import (
"math"
"math/rand"
"net"
"sync"
"sync/atomic"
"syscall"
"time"
)
// Listener is a UTP network listener. Clients should typically
// use variables of type Listener instead of assuming UTP.
type Listener struct {
// RawConn represents an out-of-band connection.
// This allows a single socket to handle multiple protocols.
RawConn net.PacketConn
conn *baseConn
deadline time.Time
deadlineMutex sync.RWMutex
closed int32
}
func (l *Listener) ok() bool { return l != nil && l.conn != nil }
// Listen announces on the UTP address laddr and returns a UTP
// listener. Net must be "utp", "utp4", or "utp6". If laddr has a
// port of 0, ListenUTP will choose an available port. The caller can
// use the Addr method of Listener to retrieve the chosen address.
func Listen(n string, laddr *Addr) (*Listener, error) {
conn, err := newBaseConn(n, laddr)
if err != nil {
return nil, err
}
l := &Listener{
RawConn: conn,
conn: conn,
}
conn.Register(-1, nil)
return l, nil
}
// Accept implements the Accept method in the Listener interface; it
// waits for the next call and returns a generic Conn.
func (l *Listener) Accept() (net.Conn, error) {
return l.AcceptUTP()
}
// AcceptUTP accepts the next incoming call and returns the new
// connection.
func (l *Listener) AcceptUTP() (*Conn, error) {
if !l.ok() {
return nil, syscall.EINVAL
}
if !l.isOpen() {
return nil, &net.OpError{
Op: "accept",
Net: l.conn.LocalAddr().Network(),
Addr: l.conn.LocalAddr(),
Err: errClosing,
}
}
l.deadlineMutex.RLock()
d := timeToDeadline(l.deadline)
l.deadlineMutex.RUnlock()
p, err := l.conn.RecvSyn(d)
if err != nil {
return nil, &net.OpError{
Op: "accept",
Net: l.conn.LocalAddr().Network(),
Addr: l.conn.LocalAddr(),
Err: errClosing,
}
}
seq := rand.Intn(math.MaxUint16)
rid := p.header.id + 1
c := newConn()
c.state = stateConnected
c.conn = l.conn
c.raddr = p.addr
c.rid = p.header.id + 1
c.sid = p.header.id
c.seq = uint16(seq)
c.ack = p.header.seq
c.recvbuf = newPacketBuffer(windowSize, int(p.header.seq))
c.sendbuf = newPacketBuffer(windowSize*2, seq)
l.conn.Register(int32(rid), c.recv)
go c.loop()
c.recv <- p
ulog.Printf(2, "baseConn(%v): accept #%d from %v", c.LocalAddr(), c.rid, c.raddr)
return c, nil
}
// Addr returns the listener's network address, a *Addr.
func (l *Listener) Addr() net.Addr {
if !l.ok() {
return nil
}
return l.conn.LocalAddr()
}
// Close stops listening on the UTP address.
// Already Accepted connections are not closed.
func (l *Listener) Close() error {
if !l.ok() {
return syscall.EINVAL
}
if !l.close() {
return &net.OpError{
Op: "close",
Net: l.conn.LocalAddr().Network(),
Addr: l.conn.LocalAddr(),
Err: errClosing,
}
}
return nil
}
// SetDeadline sets the deadline associated with the listener.
// A zero time value disables the deadline.
func (l *Listener) SetDeadline(t time.Time) error {
if !l.ok() {
return syscall.EINVAL
}
l.deadlineMutex.Lock()
defer l.deadlineMutex.Unlock()
l.deadline = t
return nil
}
func (l *Listener) close() bool {
if atomic.CompareAndSwapInt32(&l.closed, 0, 1) {
l.conn.Unregister(-1)
return true
}
return false
}
func (l *Listener) isOpen() bool {
return atomic.LoadInt32(&l.closed) == 0
}