forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpointclient.go
231 lines (194 loc) · 4.26 KB
/
endpointclient.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package gomavlib
import (
"fmt"
"io"
"net"
"sync"
"time"
"github.com/aler9/gomavlib/pkg/multibuffer"
)
type endpointClientConf interface {
isUDP() bool
getAddress() string
init() (Endpoint, error)
}
// EndpointTCPClient sets up a endpoint that works with a TCP client.
// TCP is fit for routing frames through the internet, but is not the most
// appropriate way for transferring frames from a UAV to a GCS, since it does
// not allow frame losses.
type EndpointTCPClient struct {
// domain name or IP of the server to connect to, example: 1.2.3.4:5600
Address string
}
func (EndpointTCPClient) isUDP() bool {
return false
}
func (conf EndpointTCPClient) getAddress() string {
return conf.Address
}
func (conf EndpointTCPClient) init() (Endpoint, error) {
return initEndpointClient(conf)
}
// EndpointUDPClient sets up a endpoint that works with a UDP client.
type EndpointUDPClient struct {
// domain name or IP of the server to connect to, example: 1.2.3.4:5600
Address string
}
func (EndpointUDPClient) isUDP() bool {
return true
}
func (conf EndpointUDPClient) getAddress() string {
return conf.Address
}
func (conf EndpointUDPClient) init() (Endpoint, error) {
return initEndpointClient(conf)
}
type endpointClient struct {
conf endpointClientConf
writerMutex sync.Mutex
writer io.Writer
// in
terminate chan struct{}
read chan []byte
}
func initEndpointClient(conf endpointClientConf) (Endpoint, error) {
_, _, err := net.SplitHostPort(conf.getAddress())
if err != nil {
return nil, fmt.Errorf("invalid address")
}
t := &endpointClient{
conf: conf,
terminate: make(chan struct{}),
read: make(chan []byte),
}
// work in a separate routine
// in this way we connect immediately, not after the first Read()
go t.do()
return t, nil
}
func (t *endpointClient) isEndpoint() {}
func (t *endpointClient) Conf() EndpointConf {
return t.conf
}
func (t *endpointClient) Label() string {
return fmt.Sprintf("%s:%s", func() string {
if t.conf.isUDP() {
return "udp"
}
return "tcp"
}(), t.conf.getAddress())
}
func (t *endpointClient) Close() error {
close(t.terminate)
return nil
}
func (t *endpointClient) do() {
mb := multibuffer.New(2, bufferSize)
for {
// solve address and connect
// in UDP, the only possible error is a DNS failure
// in TCP, the handshake must be completed
var rawConn net.Conn
dialDone := make(chan struct{}, 1)
go func() {
defer close(dialDone)
network := func() string {
if t.conf.isUDP() {
return "udp4"
}
return "tcp4"
}()
var err error
rawConn, err = net.DialTimeout(network, t.conf.getAddress(), netConnectTimeout)
if err != nil {
rawConn = nil // ensure rawConn is nil in case of error
}
}()
select {
case <-dialDone:
case <-t.terminate:
go func() {
for range t.read {
}
}()
close(t.read)
return
}
if rawConn == nil {
ok := func() bool {
// wait some seconds before reconnecting
timer := time.NewTimer(netReconnectPeriod)
defer timer.Stop()
select {
case <-timer.C:
return true
case <-t.terminate:
go func() {
for range t.read {
}
}()
close(t.read)
return false
}
}()
if !ok {
return
}
continue
}
conn := &netTimedConn{rawConn}
func() {
t.writerMutex.Lock()
defer t.writerMutex.Unlock()
t.writer = conn
}()
readerDone := make(chan struct{})
go func() {
defer close(readerDone)
for {
buf := mb.Next()
n, err := conn.Read(buf)
if err != nil {
return
}
t.read <- buf[:n]
}
}()
select {
case <-readerDone:
case <-t.terminate:
go func() {
for range t.read {
}
}()
conn.Close()
<-readerDone
close(t.read)
return
}
// unexpected error, restart connection
conn.Close()
func() {
t.writerMutex.Lock()
defer t.writerMutex.Unlock()
t.writer = nil
}()
}
}
func (t *endpointClient) Read(buf []byte) (int, error) {
src, ok := <-t.read
if !ok {
return 0, errorTerminated
}
n := copy(buf, src)
return n, nil
}
func (t *endpointClient) Write(buf []byte) (int, error) {
t.writerMutex.Lock()
defer t.writerMutex.Unlock()
// drop packets if disconnected
if t.writer == nil {
return 0, fmt.Errorf("disconnected")
}
return t.writer.Write(buf)
}