-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
52 lines (46 loc) · 1.1 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
package pubsub
import (
"io"
"iter"
"sync"
"github.com/antoniomika/syncmap"
)
func NewClient(ID string, rw io.ReadWriter, direction ChannelDirection, blockWrite, replay, keepAlive bool) *Client {
return &Client{
ID: ID,
ReadWriter: rw,
Direction: direction,
Channels: syncmap.New[string, *Channel](),
Done: make(chan struct{}),
Data: make(chan ChannelMessage),
Replay: replay,
BlockWrite: blockWrite,
KeepAlive: keepAlive,
}
}
/*
Client is the container for holding state between multiple devices. A
client has a direction (input, output, inputout) as well as a way to
send data to all the associated channels.
*/
type Client struct {
ID string
ReadWriter io.ReadWriter
Channels *syncmap.Map[string, *Channel]
Direction ChannelDirection
Done chan struct{}
Data chan ChannelMessage
Replay bool
BlockWrite bool
KeepAlive bool
once sync.Once
onceData sync.Once
}
func (c *Client) GetChannels() iter.Seq2[string, *Channel] {
return c.Channels.Range
}
func (c *Client) Cleanup() {
c.once.Do(func() {
close(c.Done)
})
}