-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
104 lines (96 loc) · 2.61 KB
/
main.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
package main
import (
"context"
"crypto/tls"
"flag"
"io"
"log"
"github.com/mengelbart/moqtransport"
"github.com/mengelbart/moqtransport/quicmoq"
"github.com/mengelbart/moqtransport/webtransportmoq"
"github.com/quic-go/quic-go"
"github.com/quic-go/webtransport-go"
)
func main() {
addr := flag.String("addr", "localhost:8080", "address to connect to")
wt := flag.Bool("webtransport", false, "Use webtransport instead of QUIC")
namespace := flag.String("namespace", "clock", "Namespace to subscribe to")
trackname := flag.String("trackname", "second", "Track to subscribe to")
flag.Parse()
if err := run(context.Background(), *addr, *wt, *namespace, *trackname); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context, addr string, wt bool, namespace, trackname string) error {
var conn moqtransport.Connection
var err error
if wt {
conn, err = dialWebTransport(ctx, addr)
} else {
conn, err = dialQUIC(ctx, addr)
}
if err != nil {
return err
}
announcementWaitCh := make(chan *moqtransport.Announcement)
session := &moqtransport.Session{
Conn: conn,
EnableDatagrams: true,
LocalRole: moqtransport.RoleSubscriber,
AnnouncementHandler: moqtransport.AnnouncementHandlerFunc(func(s *moqtransport.Session, a *moqtransport.Announcement, arw moqtransport.AnnouncementResponseWriter) {
if a.Namespace() == "clock" {
arw.Accept()
announcementWaitCh <- a
return
}
arw.Reject(0, "invalid namespace")
}),
}
if err = session.RunClient(); err != nil {
return err
}
defer session.Close()
a := <-announcementWaitCh
log.Printf("got Announcement: %v\n", a)
log.Println("subscribing")
rs, err := session.Subscribe(context.Background(), 0, 0, namespace, trackname, "")
if err != nil {
return err
}
log.Println("got subscription")
for {
o, err := rs.ReadObject(ctx)
if err != nil {
if err == io.EOF {
log.Printf("got last object")
return nil
}
return err
}
log.Printf("got object: %v\n", string(o.Payload))
}
}
func dialQUIC(ctx context.Context, addr string) (moqtransport.Connection, error) {
conn, err := quic.DialAddr(ctx, addr, &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{"moq-00"},
}, &quic.Config{
EnableDatagrams: true,
})
if err != nil {
return nil, err
}
return quicmoq.New(conn), nil
}
func dialWebTransport(ctx context.Context, addr string) (moqtransport.Connection, error) {
dialer := webtransport.Dialer{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
_, session, err := dialer.Dial(ctx, addr, nil)
if err != nil {
return nil, err
}
return webtransportmoq.New(session), nil
}