-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_stream.go
109 lines (83 loc) · 1.86 KB
/
data_stream.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
package moqt
import (
"log/slog"
"sync"
"github.com/OkutaniDaichi0106/gomoqt/internal/message"
"github.com/OkutaniDaichi0106/gomoqt/internal/transport"
)
type DataSendStream interface {
transport.SendStream
Group
}
var _ DataSendStream = (*dataSendStream)(nil)
type dataSendStream struct {
transport.SendStream
SentGroup
}
func (stream dataSendStream) Write(buf []byte) (int, error) {
fm := message.FrameMessage{
Payload: buf,
}
err := fm.Encode(stream.SendStream)
if err != nil {
return 0, err
}
return len(buf), nil
}
type DataReceiveStream interface {
transport.ReceiveStream
Group
}
func newDataReceiveStream(stream transport.ReceiveStream) (DataReceiveStream, error) {
group, err := readGroup(stream)
if err != nil {
slog.Error("failed to get a group", slog.String("error", err.Error()))
return nil, err
}
return &dataReceiveStream{
ReceiveStream: stream,
ReceivedGroup: group,
}, nil
}
var _ DataReceiveStream = (*dataReceiveStream)(nil)
type dataReceiveStream struct {
transport.ReceiveStream
ReceivedGroup
}
func (stream dataReceiveStream) Read(buf []byte) (int, error) {
var fm message.FrameMessage
err := fm.Decode(stream.ReceiveStream)
if err != nil {
return 0, err
}
n := copy(buf, fm.Payload)
return n, nil
}
type dataReceiveStreamQueue struct {
queue []DataReceiveStream
ch chan struct{}
mu sync.Mutex
}
func (q *dataReceiveStreamQueue) Len() int {
q.mu.Lock()
defer q.mu.Unlock()
return len(q.queue)
}
func (q *dataReceiveStreamQueue) Chan() <-chan struct{} {
return q.ch
}
func (q *dataReceiveStreamQueue) Enqueue(stream DataReceiveStream) {
q.mu.Lock()
defer q.mu.Unlock()
q.queue = append(q.queue, stream)
}
func (q *dataReceiveStreamQueue) Dequeue() DataReceiveStream {
q.mu.Lock()
defer q.mu.Unlock()
if len(q.queue) == 0 {
return nil
}
next := q.queue[0]
q.queue = q.queue[1:]
return next
}