forked from skycoin/skywire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
190 lines (158 loc) · 4.45 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package app
import (
"io"
"net"
"net/rpc"
"strings"
"github.com/sirupsen/logrus"
"github.com/skycoin/skywire/pkg/app/appcommon"
"github.com/skycoin/skywire/pkg/app/appevent"
"github.com/skycoin/skywire/pkg/app/appnet"
"github.com/skycoin/skywire/pkg/app/appserver"
"github.com/skycoin/skywire/pkg/app/idmanager"
"github.com/skycoin/skywire/pkg/routing"
)
// Client is used by skywire apps.
type Client struct {
log logrus.FieldLogger
conf appcommon.ProcConfig
rpcC appserver.RPCIngressClient
lm *idmanager.Manager // contains listeners associated with their IDs
cm *idmanager.Manager // contains connections associated with their IDs
closers []io.Closer // additional things to close on close
}
// NewClient creates a new Client, panicking on any error.
func NewClient(eventSubs *appevent.Subscriber) *Client {
log := logrus.New()
conf, err := appcommon.ProcConfigFromEnv()
if err != nil {
log.WithError(err).Fatal("Failed to obtain proc config.")
}
client, err := NewClientFromConfig(log, conf, eventSubs)
if err != nil {
log.WithError(err).Panic("Failed to create app client.")
}
return client
}
// NewClientFromConfig creates a new client from a given proc config.
func NewClientFromConfig(log logrus.FieldLogger, conf appcommon.ProcConfig, subs *appevent.Subscriber) (*Client, error) {
conn, closers, err := appevent.DoReqHandshake(conf, subs)
if err != nil {
return nil, err
}
return &Client{
log: log,
conf: conf,
rpcC: appserver.NewRPCIngressClient(rpc.NewClient(conn), conf.ProcKey),
lm: idmanager.New(),
cm: idmanager.New(),
closers: closers,
}, nil
}
// Config returns the underlying proc config.
func (c *Client) Config() appcommon.ProcConfig {
return c.conf
}
// Dial dials the remote visor using `remote`.
func (c *Client) Dial(remote appnet.Addr) (net.Conn, error) {
connID, localPort, err := c.rpcC.Dial(remote)
if err != nil {
return nil, err
}
conn := &Conn{
id: connID,
rpc: c.rpcC,
local: appnet.Addr{
Net: remote.Net,
PubKey: c.conf.VisorPK,
Port: localPort,
},
remote: remote,
}
conn.freeConnMx.Lock()
free, err := c.cm.Add(connID, conn)
if err != nil {
conn.freeConnMx.Unlock()
if err := conn.Close(); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
c.log.WithError(err).Error("Received unexpected error when closing conn.")
}
return nil, err
}
conn.freeConn = free
conn.freeConnMx.Unlock()
return conn, nil
}
// Listen listens on the specified `port` for the incoming connections.
func (c *Client) Listen(n appnet.Type, port routing.Port) (net.Listener, error) {
local := appnet.Addr{
Net: n,
PubKey: c.conf.VisorPK,
Port: port,
}
lisID, err := c.rpcC.Listen(local)
if err != nil {
return nil, err
}
listener := &Listener{
log: c.log,
id: lisID,
rpc: c.rpcC,
addr: local,
cm: idmanager.New(),
}
listener.freeLisMx.Lock()
freeLis, err := c.lm.Add(lisID, listener)
if err != nil {
listener.freeLisMx.Unlock()
if err := listener.Close(); err != nil {
c.log.WithError(err).Error("Unexpected error while closing listener.")
}
return nil, err
}
listener.freeLis = freeLis
listener.freeLisMx.Unlock()
return listener, nil
}
// Close closes client/server communication entirely. It closes all open
// listeners and connections.
func (c *Client) Close() {
var (
listeners []net.Listener
conns []net.Conn
)
// Fill listeners and connections.
c.lm.DoRange(func(_ uint16, v interface{}) bool {
lis, err := idmanager.AssertListener(v)
if err != nil {
c.log.Error(err)
return true
}
listeners = append(listeners, lis)
return true
})
c.cm.DoRange(func(_ uint16, v interface{}) bool {
conn, err := idmanager.AssertConn(v)
if err != nil {
c.log.Error(err)
return true
}
conns = append(conns, conn)
return true
})
// Close everything.
for _, lis := range listeners {
if err := lis.Close(); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
c.log.WithError(err).Error("Error closing listener.")
}
}
for _, conn := range conns {
if err := conn.Close(); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
c.log.WithError(err).Error("Error closing conn.")
}
}
for _, v := range c.closers {
if err := v.Close(); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
c.log.WithError(err).Error("Error closing closer.")
}
}
}