forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel.go
176 lines (147 loc) · 3.24 KB
/
channel.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
package gomavlib
import (
"crypto/rand"
"io"
"github.com/bluenviron/gomavlib/v2/pkg/frame"
"github.com/bluenviron/gomavlib/v2/pkg/message"
)
func randomByte() byte {
var buf [1]byte
rand.Read(buf[:])
return buf[0]
}
// Channel is a communication channel created by an Endpoint.
// An Endpoint can create channels.
// For instance, a TCP client endpoint creates a single channel, while a TCP
// server endpoint creates a channel for each incoming connection.
type Channel struct {
e Endpoint
label string
rwc io.ReadWriteCloser
n *Node
frw *frame.ReadWriter
running bool
// in
write chan interface{}
terminate chan struct{}
}
func newChannel(n *Node, e Endpoint, label string, rwc io.ReadWriteCloser) (*Channel, error) {
frw, err := frame.NewReadWriter(frame.ReadWriterConf{
ReadWriter: rwc,
DialectRW: n.dialectRW,
InKey: n.conf.InKey,
OutSystemID: n.conf.OutSystemID,
OutVersion: func() frame.WriterOutVersion {
if n.conf.OutVersion == V2 {
return frame.V2
}
return frame.V1
}(),
OutComponentID: n.conf.OutComponentID,
OutSignatureLinkID: randomByte(),
OutKey: n.conf.OutKey,
})
if err != nil {
return nil, err
}
return &Channel{
e: e,
label: label,
rwc: rwc,
n: n,
frw: frw,
write: make(chan interface{}),
terminate: make(chan struct{}),
}, nil
}
func (ch *Channel) close() {
if ch.running {
close(ch.terminate)
} else {
ch.rwc.Close()
}
}
func (ch *Channel) start() {
ch.running = true
ch.n.channelsWg.Add(1)
go ch.run()
}
func (ch *Channel) run() {
defer ch.n.channelsWg.Done()
readerDone := make(chan struct{})
go func() {
defer close(readerDone)
// wait client here, in order to allow the writer goroutine to start
// and allow clients to write messages before starting listening to events
select {
case ch.n.events <- &EventChannelOpen{ch}:
case <-ch.n.terminate:
}
for {
fr, err := ch.frw.Read()
if err != nil {
// ignore parse errors
if _, ok := err.(*frame.ReadError); ok {
select {
case ch.n.events <- &EventParseError{err, ch}:
case <-ch.n.terminate:
}
continue
}
return
}
evt := &EventFrame{fr, ch}
if ch.n.nodeStreamRequest != nil {
ch.n.nodeStreamRequest.onEventFrame(evt)
}
select {
case ch.n.events <- evt:
case <-ch.n.terminate:
}
}
}()
writerDone := make(chan struct{})
go func() {
defer close(writerDone)
for what := range ch.write {
switch wh := what.(type) {
case message.Message:
ch.frw.WriteMessage(wh)
case frame.Frame:
ch.frw.WriteFrame(wh)
}
}
}()
select {
case <-readerDone:
select {
case ch.n.events <- &EventChannelClose{ch}:
case <-ch.n.terminate:
}
select {
case ch.n.channelClose <- ch:
case <-ch.n.terminate:
}
<-ch.terminate
close(ch.write)
<-writerDone
ch.rwc.Close()
case <-ch.terminate:
select {
case ch.n.events <- &EventChannelClose{ch}:
case <-ch.n.terminate:
}
close(ch.write)
<-writerDone
ch.rwc.Close()
<-readerDone
}
}
// String implements fmt.Stringer.
func (ch *Channel) String() string {
return ch.label
}
// Endpoint returns the channel Endpoint.
func (ch *Channel) Endpoint() Endpoint {
return ch.e
}