-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
166 lines (131 loc) · 2.66 KB
/
client.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
package vsrpc
import (
"context"
"fmt"
"net"
"sync"
"github.com/chronos-tachyon/assert"
)
type Client struct {
options []Option
observers []Observer
pd PacketDialer
mu sync.Mutex
conns map[*Conn]void
state State
}
func NewClient(pd PacketDialer, options ...Option) *Client {
options = ConcatOptions(nil, options...)
c := &Client{options: options, pd: pd}
for _, opt := range options {
opt.applyToClient(c)
}
return c
}
func (c *Client) Dialer() PacketDialer {
if c == nil {
return nil
}
return c.pd
}
func (c *Client) Dial(ctx context.Context, addr net.Addr, options ...Option) (*Conn, error) {
assert.NotNil(&ctx)
assert.NotNil(&addr)
if c == nil {
return nil, ErrClientClosed
}
c.mu.Lock()
defer c.mu.Unlock()
if c.state >= StateClosed {
return nil, ErrClientClosed
}
if c.state >= StateShuttingDown {
return nil, ErrClientClosing
}
pd := c.pd
if pd == nil {
pd = DefaultPacketDialer
}
if pd == nil {
panic(fmt.Errorf("vsrpc.DefaultPacketDialer is nil"))
}
pc, err := pd.DialPacket(ctx, addr)
if err != nil {
onDialError(c.observers, err)
return nil, err
}
return c.lockedDial(pc, options)
}
func (c *Client) DialExisting(pc PacketConn, options ...Option) (*Conn, error) {
assert.NotNil(&pc)
if c == nil {
return nil, ErrClientClosed
}
c.mu.Lock()
defer c.mu.Unlock()
if c.state >= StateClosed {
return nil, ErrClientClosed
}
if c.state >= StateShuttingDown {
return nil, ErrClientClosing
}
return c.lockedDial(pc, options)
}
func (c *Client) Shutdown(ctx context.Context) error {
assert.NotNil(&ctx)
if c == nil {
return ErrClientClosed
}
c.mu.Lock()
defer c.mu.Unlock()
if c.state >= StateClosed {
return ErrClientClosed
}
if c.state >= StateShuttingDown {
return nil
}
onGlobalShutdown(c.observers)
c.state = StateShuttingDown
for conn := range c.conns {
_ = conn.Shutdown(ctx)
}
return nil
}
func (c *Client) Close() error {
if c == nil {
return ErrClientClosed
}
c.mu.Lock()
defer c.mu.Unlock()
if c.state >= StateClosed {
return ErrClientClosed
}
onGlobalClose(c.observers)
c.state = StateClosed
for conn := range c.conns {
_ = conn.Close()
}
c.conns = nil
return nil
}
func (c *Client) forgetConn(conn *Conn) {
if c == nil || conn == nil {
return
}
c.mu.Lock()
if c.conns != nil {
delete(c.conns, conn)
}
c.mu.Unlock()
}
func (c *Client) lockedDial(pc PacketConn, options []Option) (*Conn, error) {
options = ConcatOptions(c.options, options...)
conn := newConn(ClientRole, c, nil, pc, options)
if c.conns == nil {
c.conns = make(map[*Conn]void, 16)
}
c.conns[conn] = void{}
onDial(c.observers, conn)
conn.start()
return conn, nil
}