-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathchannel.go
199 lines (163 loc) · 3.63 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
package gomavlib
import (
"context"
"crypto/rand"
"errors"
"io"
"github.com/bluenviron/gomavlib/v3/pkg/frame"
"github.com/bluenviron/gomavlib/v3/pkg/message"
)
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 {
n *Node
e Endpoint
label string
rwc io.Closer
ctx context.Context
ctxCancel func()
frw *frame.ReadWriter
running bool
// in
chWrite chan interface{}
// out
done chan struct{}
}
func newChannel(
n *Node,
e Endpoint,
label string,
rwc io.ReadWriteCloser,
) (*Channel, error) {
linkID, err := randomByte()
if err != nil {
return nil, err
}
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: linkID,
OutKey: n.conf.OutKey,
})
if err != nil {
return nil, err
}
ctx, ctxCancel := context.WithCancel(context.Background())
return &Channel{
n: n,
e: e,
label: label,
rwc: rwc,
ctx: ctx,
ctxCancel: ctxCancel,
frw: frw,
chWrite: make(chan interface{}, writeBufferSize),
done: make(chan struct{}),
}, nil
}
func (ch *Channel) close() {
ch.ctxCancel()
if !ch.running {
ch.rwc.Close()
}
}
func (ch *Channel) start() {
ch.running = true
ch.n.wg.Add(1)
go ch.run()
}
func (ch *Channel) run() {
defer close(ch.done)
defer ch.n.wg.Done()
readerDone := make(chan struct{})
go ch.runReader(readerDone)
writerTerminate := make(chan struct{})
writerDone := make(chan struct{})
go ch.runWriter(writerTerminate, writerDone)
select {
case <-readerDone:
ch.rwc.Close()
close(writerTerminate)
<-writerDone
case <-ch.ctx.Done():
close(writerTerminate)
<-writerDone
ch.rwc.Close()
<-readerDone
}
ch.ctxCancel()
ch.n.pushEvent(&EventChannelClose{ch})
ch.n.closeChannel(ch)
}
func (ch *Channel) runReader(readerDone chan struct{}) {
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
ch.n.pushEvent(&EventChannelOpen{ch})
for {
fr, err := ch.frw.Read()
if err != nil {
var eerr frame.ReadError
if errors.As(err, &eerr) {
ch.n.pushEvent(&EventParseError{err, ch})
continue
}
return
}
evt := &EventFrame{fr, ch}
if ch.n.nodeStreamRequest != nil {
ch.n.nodeStreamRequest.onEventFrame(evt)
}
ch.n.pushEvent(evt)
}
}
func (ch *Channel) runWriter(writerTerminate chan struct{}, writerDone chan struct{}) {
defer close(writerDone)
for {
select {
case what := <-ch.chWrite:
switch wh := what.(type) {
case message.Message:
ch.frw.WriteMessage(wh) //nolint:errcheck
case frame.Frame:
ch.frw.WriteFrame(wh) //nolint:errcheck
}
case <-writerTerminate:
return
}
}
}
// 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
}
func (ch *Channel) write(what interface{}) {
select {
case ch.chWrite <- what:
case <-ch.ctx.Done():
default: // buffer is full
}
}