-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathchannel.go
208 lines (172 loc) · 3.84 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
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
package gomavlib
import (
"context"
"crypto/rand"
"errors"
"io"
"github.com/bluenviron/gomavlib/v3/pkg/frame"
"github.com/bluenviron/gomavlib/v3/pkg/message"
"github.com/bluenviron/gomavlib/v3/pkg/streamwriter"
)
const (
writeBufferSize = 64
)
func randomByte() (byte, error) {
var buf [1]byte
_, err := rand.Read(buf[:])
return buf[0], err
}
// 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 {
node *Node
endpoint Endpoint
label string
rwc io.ReadWriteCloser
ctx context.Context
ctxCancel func()
frameWriter *frame.ReadWriter
streamWriter *streamwriter.Writer
running bool
// in
chWrite chan interface{}
// out
done chan struct{}
}
func (ch *Channel) initialize() error {
linkID, err := randomByte()
if err != nil {
return err
}
ch.frameWriter = &frame.ReadWriter{
ByteReadWriter: ch.rwc,
DialectRW: ch.node.dialectRW,
InKey: ch.node.InKey,
}
err = ch.frameWriter.Initialize()
if err != nil {
return err
}
ch.streamWriter = &streamwriter.Writer{
FrameWriter: ch.frameWriter.Writer,
SystemID: ch.node.OutSystemID,
Version: func() streamwriter.Version {
if ch.node.OutVersion == V2 {
return streamwriter.V2
}
return streamwriter.V1
}(),
ComponentID: ch.node.OutComponentID,
SignatureLinkID: linkID,
Key: ch.node.OutKey,
}
err = ch.streamWriter.Initialize()
if err != nil {
return err
}
ch.ctx, ch.ctxCancel = context.WithCancel(context.Background())
ch.chWrite = make(chan interface{}, writeBufferSize)
ch.done = make(chan struct{})
return nil
}
func (ch *Channel) close() {
ch.ctxCancel()
if !ch.running {
ch.rwc.Close()
}
}
func (ch *Channel) start() {
ch.running = true
ch.node.wg.Add(1)
go ch.run()
}
func (ch *Channel) run() {
defer close(ch.done)
defer ch.node.wg.Done()
readerDone := make(chan error)
go func() {
readerDone <- ch.runReader()
}()
writerTerminate := make(chan struct{})
writerDone := make(chan error)
go func() {
writerDone <- ch.runWriter(writerTerminate)
}()
var err error
select {
case err = <-readerDone:
ch.rwc.Close()
close(writerTerminate)
<-writerDone
case <-ch.ctx.Done():
close(writerTerminate)
<-writerDone
ch.rwc.Close()
<-readerDone
}
ch.ctxCancel()
ch.node.pushEvent(&EventChannelClose{
Channel: ch,
Error: err,
})
ch.node.closeChannel(ch)
}
func (ch *Channel) runReader() error {
// wait client here, in order to allow the writer goroutine to start
// and allow clients to write messages before starting listening to events
ch.node.pushEvent(&EventChannelOpen{ch})
for {
fr, err := ch.frameWriter.Read()
if err != nil {
var eerr frame.ReadError
if errors.As(err, &eerr) {
ch.node.pushEvent(&EventParseError{err, ch})
continue
}
return err
}
evt := &EventFrame{fr, ch}
if ch.node.nodeStreamRequest != nil {
ch.node.nodeStreamRequest.onEventFrame(evt)
}
ch.node.pushEvent(evt)
}
}
func (ch *Channel) runWriter(writerTerminate chan struct{}) error {
for {
select {
case what := <-ch.chWrite:
switch wh := what.(type) {
case message.Message:
err := ch.streamWriter.Write(wh)
if err != nil {
return err
}
case frame.Frame:
err := ch.frameWriter.Write(wh)
if err != nil {
return err
}
}
case <-writerTerminate:
return nil
}
}
}
// String implements fmt.Stringer.
func (ch *Channel) String() string {
return ch.label
}
// Endpoint returns the channel Endpoint.
func (ch *Channel) Endpoint() Endpoint {
return ch.endpoint
}
func (ch *Channel) write(what interface{}) {
select {
case ch.chWrite <- what:
case <-ch.ctx.Done():
default: // buffer is full
}
}