-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_receive.go
114 lines (98 loc) · 2.93 KB
/
client_receive.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
package rtmp
import (
"bytes"
"io"
)
func (c *Client) receiveLoop() {
for {
// Read the next header from the connection
h, err := ReadHeader(c)
if err != nil {
if c.IsAlive() {
log.Warn("unable to receive next header while connected: %s", err)
c.Reset()
} else {
log.Debug("client receive: connection closed")
}
return
}
// Determine whether or not we already have a chunk stream
// allocated for this ID. If we don't, create one.
var cs *InboundChunkStream = c.inChunkStreams[h.ChunkStreamId]
if cs == nil {
cs = NewInboundChunkStream(h.ChunkStreamId)
c.inChunkStreams[h.ChunkStreamId] = cs
}
var ts uint32
var m *Message
if (cs.lastHeader == nil) && (h.Format != HEADER_FORMAT_FULL) {
log.Warn("unable to find previous header on chunk stream %d", h.ChunkStreamId)
c.Reset()
return
}
switch h.Format {
case HEADER_FORMAT_FULL:
// If it's an entirely new header, replace the reference in
// the chunk stream and set the working timestamp from
// the header.
cs.lastHeader = &h
ts = h.Timestamp
case HEADER_FORMAT_SAME_STREAM:
// If it's the same stream, use the last message stream id,
// but otherwise use values from the header.
h.MessageStreamId = cs.lastHeader.MessageStreamId
cs.lastHeader = &h
ts = cs.lastInAbsoluteTimestamp + h.Timestamp
case HEADER_FORMAT_SAME_LENGTH_AND_STREAM:
// If it's the same length and stream, copy values from the
// last header and replace it.
h.MessageStreamId = cs.lastHeader.MessageStreamId
h.MessageLength = cs.lastHeader.MessageLength
h.MessageTypeId = cs.lastHeader.MessageTypeId
cs.lastHeader = &h
ts = cs.lastInAbsoluteTimestamp + h.Timestamp
case HEADER_FORMAT_CONTINUATION:
// A full continuation of the previous stream. Copy all values.
h.MessageStreamId = cs.lastHeader.MessageStreamId
h.MessageLength = cs.lastHeader.MessageLength
h.MessageTypeId = cs.lastHeader.MessageTypeId
h.Timestamp = cs.lastHeader.Timestamp
ts = cs.lastInAbsoluteTimestamp + cs.lastHeader.Timestamp
// If there's a message already started, use it.
if cs.currentMessage != nil {
m = cs.currentMessage
}
}
if m == nil {
m = &Message{
Type: h.MessageTypeId,
ChunkStreamId: h.ChunkStreamId,
StreamId: h.MessageStreamId,
Timestamp: h.CalculateTimestamp(),
AbsoluteTimestamp: ts,
Length: h.MessageLength,
Buffer: new(bytes.Buffer),
}
}
cs.lastInAbsoluteTimestamp = ts
rs := m.RemainingBytes()
if rs > c.inChunkSize {
rs = c.inChunkSize
}
_, err = io.CopyN(m.Buffer, c, int64(rs))
if err != nil {
if c.connected {
log.Warn("unable to copy %d message bytes from buffer", rs)
c.Reset()
}
return
}
if m.RemainingBytes() == 0 {
cs.currentMessage = nil
log.Trace("receive sending message to router: %#v", m)
c.inMessages <- m
} else {
cs.currentMessage = m
}
}
}