/
handshaker.go
166 lines (144 loc) · 3.53 KB
/
handshaker.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
package handshake
import (
"context"
"io"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
const (
ServerRTMPVersion = 0x03
)
type Handshaker interface {
Handshake(ctx context.Context, r io.Reader, w io.Writer) error
}
type defaultHandshaker struct {
isServer bool
}
var DefaultServerHandshaker = NewDefaultHandshaker(true)
var DefaultClientHandshaker = NewDefaultHandshaker(false)
func NewDefaultHandshaker(
isServer bool,
) defaultHandshaker {
return defaultHandshaker{
isServer: isServer,
}
}
func (y defaultHandshaker) Handshake(ctx context.Context, r io.Reader, w io.Writer) error {
g, cctx := errgroup.WithContext(ctx)
var received1, sent1 Chunk1
g.Go(func() (err error) {
_, received1, err = y.Receive0And1(cctx, r)
return err
})
if y.isServer {
// The server MUST wait until C0 has been received before sending sent0 and
// sent1, and MAY wait until after C1 as well.
err := g.Wait()
if err != nil {
return err
}
g, cctx = errgroup.WithContext(ctx)
}
time := uint32(0)
g.Go(func() (err error) {
_, sent1, err = y.Send0And1(cctx, w, ServerRTMPVersion, time)
return err
})
err := g.Wait()
if err != nil {
return err
}
g, cctx = errgroup.WithContext(ctx)
g.Go(func() error {
received2, err := y.Receive2(cctx, r)
if err != nil {
return errors.Wrap(err, "failed to receive Chunk2")
}
if err := received2.Validate(sent1); err != nil {
return errors.Wrap(err, "invalid Chunk2")
}
return nil
})
g.Go(func() error {
time2 := maxUint32(time, received1.Time())
return y.Send2(cctx, w, received1, time2)
})
return g.Wait()
}
func (y defaultHandshaker) Send0And1(
ctx context.Context,
w io.Writer,
rtmpVersion uint8,
time uint32,
) (c0 Chunk0, c1 Chunk1, err error) {
// send 0
c0 = NewChunk0(rtmpVersion)
b, err := c0.MarshalBinary()
if err != nil {
return c0, c1, errors.Wrap(err, "failed to MarshalBinary Chunk0")
}
if _, err = w.Write(b); err != nil {
return c0, c1, errors.Wrap(err, "failed to send Chunk0")
}
// send 1
c1, err = GenerateChunk1(time)
if err != nil {
return c0, c1, errors.Wrap(err, "failed to init Chunk1")
}
b, err = c1.MarshalBinary()
if err != nil {
return c0, c1, errors.Wrap(err, "failed to MarshalBinary Chunk1")
}
if _, err = w.Write(b); err != nil {
return c0, c1, errors.Wrap(err, "failed to send Chunk1")
}
if w, ok := w.(flusher); ok {
if err = w.Flush(); err != nil {
return c0, c1, errors.Wrap(err, "failed to flush writer for Chunk0 and Chunk1")
}
}
return c0, c1, nil
}
func (y defaultHandshaker) Send2(
ctx context.Context,
w io.Writer,
received1 Chunk1,
time2 uint32,
) (err error) {
// send 2
c2 := GenerateChunk2(received1, time2)
b, err := c2.MarshalBinary()
if err != nil {
return errors.Wrap(err, "failed to MarshalBinary Chunk2")
}
if _, err := w.Write(b); err != nil {
return errors.Wrap(err, "failed to send Chunk2")
}
if w, ok := w.(flusher); ok {
if err := w.Flush(); err != nil {
return errors.Wrap(err, "failed to flush writer for Chunk2")
}
}
return nil
}
func (y defaultHandshaker) Receive0And1(
ctx context.Context,
r io.Reader,
) (c0 Chunk0, c1 Chunk1, err error) {
// receive 0
c0, err = ReadChunk0(r)
if err != nil {
return c0, c1, errors.Wrap(err, "failed to read Chunk0")
}
// receive 1
c1, err = ReadChunk1(r)
return c0, c1, errors.Wrap(err, "failed to read Chunk1")
}
func (y defaultHandshaker) Receive2(
ctx context.Context,
r io.Reader,
) (c2 Chunk2, err error) {
// receive 2
received2, err := ReadChunk2(r)
return received2, errors.Wrap(err, "failed to read Chunk2")
}