Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.

Commit b3ee436

Browse files
committed
Merge pull request #24 from WatchBeam/feature/set-buffer-length
control: add temporary Event type
2 parents 41e4e25 + fe12b7c commit b3ee436

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

control/control.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var (
99
&SetChunkSize{},
1010
&AbortMessage{},
1111
&Acknowledgement{},
12+
&Event{},
1213
&WindowAckSize{},
1314
&SetPeerBandwidth{},
1415
}

control/control_event.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package control
2+
3+
import (
4+
"encoding/binary"
5+
"io"
6+
"io/ioutil"
7+
)
8+
9+
// EventType is a const type that wraps the uint16 base type to represent a
10+
// particular sub-type of the Event message sent over the control sequence chunk
11+
// stream.
12+
type EventType uint16
13+
14+
const (
15+
SetBufferLength EventType = 3
16+
)
17+
18+
// Event encapsulates any event that is sent over the control stream.
19+
//
20+
// NOTE: this is a temporary type, eventually it will be replaced with
21+
// individual types implementing both Control and Event interfaces. The current
22+
// structure of the parser/identifier does not easily allow for this, so some
23+
// more heavy lifting will have to occur before this gets further attention.
24+
type Event struct {
25+
// Type is the event type of the event.
26+
Type EventType
27+
// Body is the body payload of the event.
28+
Body []byte
29+
}
30+
31+
var _ Control = new(Event)
32+
33+
// Read implements the Event.Read function, returning any errors that it
34+
// encounters, or nil if the read was successful.
35+
func (e *Event) Read(r io.Reader) error {
36+
var tid uint16
37+
if err := binary.Read(r, binary.BigEndian, &tid); err != nil {
38+
return err
39+
}
40+
41+
e.Type = EventType(tid)
42+
43+
body, err := ioutil.ReadAll(r)
44+
if err != nil {
45+
return err
46+
}
47+
48+
e.Body = body
49+
50+
return nil
51+
}
52+
53+
// Write implements the Event.Write function, returning any errors that it
54+
// encounters, or nil if the write was successful.
55+
func (e *Event) Write(w io.Writer) error {
56+
if err := binary.Write(
57+
w, binary.BigEndian, uint16(e.Type),
58+
); err != nil {
59+
return err
60+
}
61+
62+
if _, err := w.Write(e.Body); err != nil {
63+
return err
64+
}
65+
66+
return nil
67+
}
68+
69+
// TypeId implements Event.TypeId and returns the TypeId of an Event control
70+
// sequence.
71+
func (e *Event) TypeId() byte { return 0x04 }

control/control_event_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package control_test
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/WatchBeam/rtmp/control"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestEventReadsFromBuffer(t *testing.T) {
12+
e := new(control.Event)
13+
14+
err := e.Read(bytes.NewReader([]byte{
15+
0x00, 0x03, 0x01, 0x02, 0x03,
16+
}))
17+
18+
assert.Nil(t, err)
19+
assert.Equal(t, control.SetBufferLength, e.Type)
20+
assert.Equal(t, []byte{1, 2, 3}, e.Body)
21+
}
22+
23+
func TestEventWritesToBuffer(t *testing.T) {
24+
buf := new(bytes.Buffer)
25+
e := &control.Event{
26+
Type: control.SetBufferLength,
27+
Body: []byte{4, 5, 6},
28+
}
29+
30+
err := e.Write(buf)
31+
32+
assert.Nil(t, err)
33+
assert.Equal(t, []byte{0x00, 0x03}, buf.Bytes()[:2])
34+
assert.Equal(t, []byte{0x04, 0x05, 0x06}, buf.Bytes()[2:])
35+
}

0 commit comments

Comments
 (0)