forked from yutopp/go-rtmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
203 lines (159 loc) · 4.07 KB
/
conn.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// Copyright (c) 2018- yutopp (yutopp@gmail.com)
//
// 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 (
"bufio"
"context"
"io"
"io/ioutil"
"sync"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/yutopp/go-rtmp/message"
)
type Conn struct {
rwc io.ReadWriteCloser
bufr *bufio.Reader
bufw *bufio.Writer
streamer *ChunkStreamer
streams *streams
handler Handler
config *ConnConfig
logger logrus.FieldLogger
ignoredMessages uint32
m sync.Mutex
isClosed bool
}
type ConnConfig struct {
Handler Handler
SkipHandshakeVerification bool
IgnoreMessagesOnNotExistStream bool
IgnoreMessagesOnNotExistStreamThreshold uint32
ReaderBufferSize int
WriterBufferSize int
ControlState StreamControlStateConfig
Logger logrus.FieldLogger
RPreset ResponsePreset
}
func (cb *ConnConfig) normalize() *ConnConfig {
c := ConnConfig(*cb)
if c.Handler == nil {
c.Handler = &DefaultHandler{}
}
if c.ReaderBufferSize == 0 {
c.ReaderBufferSize = 4 * 1024 // 4KB (Default)
}
if c.WriterBufferSize == 0 {
c.WriterBufferSize = 4 * 1024 // 4KB (Default)
}
c.ControlState = *c.ControlState.normalize()
if c.Logger == nil {
l := logrus.New()
l.Out = ioutil.Discard
c.Logger = l
}
return &c
}
func newConn(rwc io.ReadWriteCloser, config *ConnConfig) *Conn {
if config == nil {
config = &ConnConfig{}
}
config = config.normalize()
conn := &Conn{
rwc: rwc,
bufr: bufio.NewReaderSize(rwc, config.ReaderBufferSize),
bufw: bufio.NewWriterSize(rwc, config.WriterBufferSize),
handler: config.Handler,
config: config,
logger: config.Logger,
}
conn.streamer = NewChunkStreamer(conn.bufr, conn.bufw, &conn.config.ControlState)
conn.streamer.logger = conn.logger
conn.streams = newStreams(conn)
return conn
}
func (c *Conn) Close() error {
c.m.Lock()
defer c.m.Unlock()
if c.isClosed {
return nil
}
c.isClosed = true
if c.handler != nil {
c.handler.OnClose()
}
var result error
if c.streamer != nil {
c.streamer.waitWriters()
if err := c.streamer.Close(); err != nil {
result = multierror.Append(result, err)
}
}
if err := c.rwc.Close(); err != nil {
result = multierror.Append(result, err)
}
return result
}
func (c *Conn) Write(ctx context.Context, chunkStreamID int, timestamp uint32, cmsg *ChunkMessage) error {
return c.streamer.Write(ctx, chunkStreamID, timestamp, cmsg)
}
func (c *Conn) handleMessageLoop() (err error) {
defer func() {
if r := recover(); r != nil {
errTmp, ok := r.(error)
if !ok {
errTmp = errors.Errorf("Panic: %+v", r)
}
err = errors.WithStack(errTmp)
}
}()
return c.runHandleMessageLoop()
}
func (c *Conn) runHandleMessageLoop() error {
var cmsg ChunkMessage
for {
select {
case <-c.streamer.Done():
return c.streamer.Err()
default:
chunkStreamID, timestamp, err := c.streamer.Read(&cmsg)
if err != nil {
return err
}
if err := c.handleMessage(chunkStreamID, timestamp, &cmsg); err != nil {
return err // Shutdown the connection
}
}
}
}
func (c *Conn) handleMessage(chunkStreamID int, timestamp uint32, cmsg *ChunkMessage) error {
stream, err := c.streams.At(cmsg.StreamID)
if err != nil {
if c.config.IgnoreMessagesOnNotExistStream {
c.logger.Warnf("Messages are received on not exist streams: StreamID = %d, MessageType = %T",
cmsg.StreamID,
cmsg.Message,
)
if c.ignoredMessages < c.config.IgnoreMessagesOnNotExistStreamThreshold {
c.ignoredMessages++
return nil
}
}
return errors.Errorf("Specified stream is not created yet: StreamID = %d", cmsg.StreamID)
}
if err := stream.handle(chunkStreamID, timestamp, cmsg.Message); err != nil {
switch err := err.(type) {
case *message.UnknownDataBodyDecodeError, *message.UnknownCommandBodyDecodeError:
// Ignore unknown messsage body
c.logger.Warnf("Ignored unknown message body: Err = %+v", err)
return nil
}
return err
}
return nil
}