forked from mengelbart/moqtransport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
104 lines (93 loc) · 2.48 KB
/
server.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 moqtransport
import (
"context"
"crypto/tls"
"errors"
"log"
"github.com/danielpfeifer02/quic-go-prio-packs"
)
type PeerHandlerFunc func(*Peer)
func (h PeerHandlerFunc) Handle(p *Peer) {
h(p)
}
type PeerHandler interface {
Handle(*Peer)
}
type Server struct {
Handler PeerHandler
TLSConfig *tls.Config
}
type listener interface {
Accept(context.Context) (connection, error)
}
type quicListener struct {
ql *quic.Listener
}
func (l *quicListener) Accept(ctx context.Context) (connection, error) {
c, err := l.ql.Accept(ctx)
if err != nil {
return nil, err
}
qc := &quicConn{
conn: c,
}
return qc, nil
}
func (s *Server) ListenQUIC(ctx context.Context, addr string) error {
ql, err := quic.ListenAddr(addr, s.TLSConfig, &quic.Config{
GetConfigForClient: nil,
Versions: nil,
HandshakeIdleTimeout: 0,
MaxIdleTimeout: 1<<63 - 1,
TokenStore: nil,
InitialStreamReceiveWindow: 1 << 30, // TODONOW: what should this be?
MaxStreamReceiveWindow: 1 << 30, // TODONOW: what should this be?
InitialConnectionReceiveWindow: 1 << 30, // TODONOW: what should this be?
MaxConnectionReceiveWindow: 1 << 30, // TODONOW: what should this be?
AllowConnectionWindowIncrease: nil,
MaxIncomingStreams: 0,
MaxIncomingUniStreams: 0,
KeepAlivePeriod: 0,
DisablePathMTUDiscovery: false,
Allow0RTT: false,
EnableDatagrams: true,
Tracer: nil,
})
if err != nil {
return err
}
l := &quicListener{
ql: ql,
}
return s.Listen(ctx, l)
}
func (s *Server) Listen(ctx context.Context, l listener) error {
_, enableDatagrams := l.(*quicListener)
for {
conn, err := l.Accept(context.TODO())
if err != nil {
return err
}
peer, err := newServerPeer(ctx, conn)
if err != nil {
log.Printf("failed to create new server peer: %v", err)
switch {
case errors.Is(err, errUnsupportedVersion):
conn.CloseWithError(SessionTerminatedErrorCode, err.Error())
case errors.Is(err, errMissingRoleParameter):
conn.CloseWithError(SessionTerminatedErrorCode, err.Error())
default:
conn.CloseWithError(GenericErrorCode, "internal server error")
}
continue
}
// TODO: This should probably be a map keyed by the MoQ-URI the request
// is targeting
go func() {
peer.run(ctx, enableDatagrams)
}()
if s.Handler != nil {
s.Handler.Handle(peer)
}
}
}