-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
info.go
170 lines (133 loc) · 3.3 KB
/
info.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
package moqt
import (
"errors"
"io"
"log/slog"
"sync"
"time"
"github.com/OkutaniDaichi0106/gomoqt/internal/message"
"github.com/OkutaniDaichi0106/gomoqt/internal/transport"
)
// type InfoHandler interface {
// HandleInfo(Info)
// }
type InfoRequestHandler interface {
HandleInfoRequest(InfoRequest, *Info, ReceivedInfoRequest)
}
type InfoRequest struct {
TrackPath string
}
type Info struct {
TrackPriority TrackPriority
LatestGroupSequence GroupSequence
GroupOrder GroupOrder
GroupExpires time.Duration
}
func newReceivedInfoRequest(stream transport.Stream) (*ReceivedInfoRequest, error) {
req, err := readInfoRequest(stream)
if err != nil {
slog.Error("failed to get a info-request", slog.String("error", err.Error()))
return nil, err
}
return &ReceivedInfoRequest{
InfoRequest: req,
stream: stream,
}, nil
}
type ReceivedInfoRequest struct {
InfoRequest
stream transport.Stream
mu sync.Mutex
}
func (req *ReceivedInfoRequest) Inform(i Info) {
req.mu.Lock()
defer req.mu.Unlock()
im := message.InfoMessage{
GroupPriority: message.GroupPriority(i.TrackPriority),
LatestGroupSequence: message.GroupSequence(i.LatestGroupSequence),
GroupOrder: message.GroupOrder(i.GroupOrder),
GroupExpires: i.GroupExpires,
}
err := im.Encode(req.stream)
if err != nil {
slog.Error("failed to send an INFO message", slog.String("error", err.Error()))
req.CloseWithError(err)
return
}
slog.Info("answered an info")
req.Close()
}
func (req *ReceivedInfoRequest) CloseWithError(err error) error {
req.mu.Lock()
defer req.mu.Unlock()
if err == nil {
return req.Close()
}
req.mu.Lock()
defer req.mu.Unlock()
var code transport.StreamErrorCode
var strerr transport.StreamError
if errors.As(err, &strerr) {
code = strerr.StreamErrorCode()
} else {
inferr, ok := err.(InfoError)
if ok {
code = transport.StreamErrorCode(inferr.InfoErrorCode())
} else {
code = ErrInternalError.StreamErrorCode()
}
}
req.stream.CancelRead(code)
req.stream.CancelWrite(code)
slog.Info("rejected an info request")
return nil
}
func (req *ReceivedInfoRequest) Close() error {
req.mu.Lock()
defer req.mu.Unlock()
return req.stream.Close()
}
type receivedInfoRequestQueue struct {
queue []*ReceivedInfoRequest
mu sync.Mutex
ch chan struct{}
}
func (q *receivedInfoRequestQueue) Len() int {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.queue)
}
func (q *receivedInfoRequestQueue) Enqueue(req *ReceivedInfoRequest) {
q.mu.Lock()
defer q.mu.Unlock()
q.queue = append(q.queue, req)
}
func (q *receivedInfoRequestQueue) Dequeue() *ReceivedInfoRequest {
q.mu.Lock()
defer q.mu.Unlock()
if len(q.queue) == 0 {
return nil
}
req := q.queue[0]
q.queue = q.queue[1:]
return req
}
func (q *receivedInfoRequestQueue) Chan() <-chan struct{} {
return q.ch
}
func readInfo(r io.Reader) (Info, error) {
// Read an INFO message
var im message.InfoMessage
err := im.Decode(r)
if err != nil {
slog.Error("failed to read a INFO message", slog.String("error", err.Error()))
return Info{}, err
}
info := Info{
TrackPriority: TrackPriority(im.GroupPriority),
LatestGroupSequence: GroupSequence(im.LatestGroupSequence),
GroupOrder: GroupOrder(im.GroupOrder),
GroupExpires: im.GroupExpires,
}
return info, nil
}