-
Notifications
You must be signed in to change notification settings - Fork 85
/
conn_state.go
150 lines (110 loc) · 3.19 KB
/
conn_state.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
//
// Copyright (c) 2018- yutopp ([email protected])
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
//
package rtmp
import (
"math"
"github.com/pkg/errors"
"github.com/yutopp/go-rtmp/message"
)
const DefaultChunkSize = 128
const MaxChunkSize = 0xffffff // 5.4.1
type StreamControlState struct {
chunkSize uint32
ackWindowSize int32
bandwidthWindowSize int32
bandwidthLimitType message.LimitType
config *StreamControlStateConfig
}
type StreamControlStateConfig struct {
DefaultChunkSize uint32
MaxChunkSize uint32
MaxChunkStreams int
DefaultAckWindowSize int32
MaxAckWindowSize int32
DefaultBandwidthWindowSize int32
DefaultBandwidthLimitType message.LimitType
MaxBandwidthWindowSize int32
MaxMessageSize uint32
MaxMessageStreams int
}
func (cb *StreamControlStateConfig) normalize() *StreamControlStateConfig {
c := StreamControlStateConfig(*cb)
// chunks
if c.DefaultChunkSize == 0 {
c.DefaultChunkSize = DefaultChunkSize
}
if c.MaxChunkSize == 0 {
c.MaxChunkSize = MaxChunkSize
}
if c.MaxChunkStreams == 0 {
c.MaxChunkStreams = math.MaxUint32
}
// ack
if c.DefaultAckWindowSize == 0 {
c.DefaultAckWindowSize = math.MaxInt32
}
if c.MaxAckWindowSize == 0 {
c.MaxAckWindowSize = math.MaxInt32
}
// bandwidth
if c.DefaultBandwidthWindowSize == 0 {
c.DefaultBandwidthWindowSize = math.MaxInt32
}
if c.MaxBandwidthWindowSize == 0 {
c.MaxBandwidthWindowSize = math.MaxInt32
}
// message
if c.MaxMessageStreams == 0 {
c.MaxMessageStreams = math.MaxUint32
}
if c.MaxMessageSize == 0 {
c.MaxMessageSize = MaxChunkSize // as same as chunk size
}
return &c
}
var defaultStreamControlStateConfig = (&StreamControlStateConfig{}).normalize()
func NewStreamControlState(config *StreamControlStateConfig) *StreamControlState {
if config == nil {
config = defaultStreamControlStateConfig
}
return &StreamControlState{
chunkSize: config.DefaultChunkSize,
ackWindowSize: config.DefaultAckWindowSize,
bandwidthWindowSize: config.DefaultBandwidthWindowSize,
bandwidthLimitType: config.DefaultBandwidthLimitType,
config: config,
}
}
func (s *StreamControlState) ChunkSize() uint32 {
return s.chunkSize
}
func (s *StreamControlState) SetChunkSize(chunkSize uint32) error {
if chunkSize > MaxChunkSize {
chunkSize = MaxChunkSize
}
if chunkSize > s.config.MaxChunkSize {
return errors.Errorf("Exceeded configured max chunk size: Limit = %d, Value = %d", s.config.MaxChunkSize, chunkSize)
}
s.chunkSize = chunkSize
return nil
}
func (s *StreamControlState) AckWindowSize() int32 {
return s.ackWindowSize
}
func (s *StreamControlState) SetAckWindowSize(ackWindowSize int32) error {
if ackWindowSize > s.config.MaxAckWindowSize {
return errors.Errorf("Exceeded configured max ack window size: Limit = %d, Value = %d", s.config.MaxAckWindowSize, ackWindowSize)
}
s.ackWindowSize = ackWindowSize
return nil
}
func (s *StreamControlState) BandwidthWindowSize() int32 {
return s.bandwidthWindowSize
}
func (s *StreamControlState) BandwidthLimitType() message.LimitType {
return s.bandwidthLimitType
}