-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
81 lines (72 loc) · 2 KB
/
Copy pathmain.go
File metadata and controls
81 lines (72 loc) · 2 KB
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
package main
import (
"context"
"crypto/tls"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"
"github.com/lmittmann/tint"
"github.com/quic-go/quic-go"
"github.com/floatdrop/moq-go/pkg/moqt/session"
"github.com/floatdrop/moq-go/pkg/moqt/session/quicconn"
)
func main() {
addr := flag.String("addr", "localhost:4433", "relay address")
flag.Parse()
if flag.NArg() < 1 {
fmt.Fprintln(os.Stderr, "usage: clock [-addr host:port] publish|subscribe")
os.Exit(1)
}
slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, &tint.Options{
Level: slog.LevelDebug,
TimeFormat: time.TimeOnly,
})))
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
go func() {
<-ctx.Done()
slog.Info("signal received, shutting down (Ctrl+C again to force-exit)")
stop()
}()
var err error
switch flag.Arg(0) {
case "publish":
err = publish(ctx, *addr)
case "subscribe":
err = subscribe(ctx, *addr)
default:
fmt.Fprintf(os.Stderr, "unknown mode %q (want publish or subscribe)\n", flag.Arg(0))
os.Exit(1)
}
if err != nil {
slog.Error("fatal", tint.Err(err))
os.Exit(1)
}
}
// dial establishes a QUIC connection and completes the MOQT client handshake.
func dial(ctx context.Context, addr string) (*session.Session, error) {
tlsCfg := &tls.Config{
InsecureSkipVerify: true, //nolint:gosec // G402: dev-only demo client; certs not verified by design.
NextProtos: []string{"moq-00"},
}
quicCfg := &quic.Config{
MaxIdleTimeout: 30 * time.Second,
KeepAlivePeriod: 5 * time.Second,
EnableDatagrams: true,
EnableStreamResetPartialDelivery: true, // §11.4.3 RESET_STREAM_AT
}
qconn, err := quic.DialAddr(ctx, addr, tlsCfg, quicCfg)
if err != nil {
return nil, fmt.Errorf("dial %s: %w", addr, err)
}
sess, err := session.Client(ctx, quicconn.New(qconn),
session.WithImplementation("clock/0.1"),
)
if err != nil {
return nil, fmt.Errorf("moqt handshake: %w", err)
}
return sess, nil
}