-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
220 lines (175 loc) · 4.09 KB
/
client.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package rtmp
import (
"crypto/tls"
"errors"
"github.com/elobuff/goamf"
"net"
"net/url"
"sync"
"sync/atomic"
"time"
)
var (
ErrResponseTimeout = errors.New("rtmp: response timeout")
)
type Client struct {
url string
connected bool
conn net.Conn
outBytes uint32
outMessages chan *Message
outWindowSize uint32
outChunkSize uint32
outChunkStreams map[uint32]*OutboundChunkStream
inBytes uint32
inMessages chan *Message
inNotify chan uint8
inWindowSize uint32
inChunkSize uint32
inChunkStreams map[uint32]*InboundChunkStream
responses map[uint32]*Response
responsesMutex sync.Mutex
lastTransactionId uint32
connectionId string
amfExternalHandlers map[string]amf.ExternalHandler
}
func NewClient(url string) (c *Client) {
c = &Client{
url: url,
amfExternalHandlers: make(map[string]amf.ExternalHandler),
}
c.Reset()
return
}
func (c *Client) IsAlive() bool {
if c.connected != true {
return false
}
return true
}
func (c *Client) Reset() {
c.connected = false
if c.conn != nil {
c.conn.Close()
}
if c.outMessages != nil {
close(c.outMessages)
}
if c.inMessages != nil {
close(c.inMessages)
}
c.outBytes = 0
c.outMessages = make(chan *Message)
c.outChunkSize = DEFAULT_CHUNK_SIZE
c.outWindowSize = DEFAULT_WINDOW_SIZE
c.outChunkStreams = make(map[uint32]*OutboundChunkStream)
c.inBytes = 0
c.inMessages = make(chan *Message)
c.inChunkSize = DEFAULT_CHUNK_SIZE
c.inWindowSize = DEFAULT_WINDOW_SIZE
c.inChunkStreams = make(map[uint32]*InboundChunkStream)
c.responses = make(map[uint32]*Response)
c.lastTransactionId = 0
c.connectionId = ""
}
func (c *Client) RegisterExternalHandler(name string, fn amf.ExternalHandler) {
c.amfExternalHandlers[name] = fn
}
func (c *Client) Disconnect() {
c.Reset()
log.Debug("disconnected from %s", c.url)
}
func (c *Client) Connect() (err error) {
log.Debug("connecting to %s", c.url)
url, err := url.Parse(c.url)
if err != nil {
return err
}
switch url.Scheme {
case "rtmp":
c.conn, err = net.DialTimeout("tcp", url.Host, 5*time.Second)
if err != nil {
return err
}
case "rtmps":
var nc net.Conn
nc, err = net.DialTimeout("tcp", url.Host, 5*time.Second)
if err != nil {
return err
}
config := &tls.Config{InsecureSkipVerify: true}
tc := tls.Client(nc, config)
err = tc.Handshake()
if err != nil {
return err
}
c.conn = tc
default:
return Error("Unsupported scheme: %s", url.Scheme)
}
log.Debug("handshaking with %s", c.url)
err = c.handshake()
if err != nil {
return Error("client connect: could not complete handshake: %s", err)
}
log.Debug("sending connect command to %s", c.url)
go c.receiveLoop()
go c.sendLoop()
go c.routeLoop()
var id string
id, err = c.connect()
if err != nil {
return Error("client connect: could not complete connect: %s", err)
}
c.connected = true
c.connectionId = id
log.Debug("connected to %s (%s)", c.url, c.connectionId)
return
}
func (c *Client) NextTransactionId() uint32 {
return atomic.AddUint32(&c.lastTransactionId, 1)
}
func (c *Client) GetResponse(tid uint32) (response *Response, ready bool) {
c.responsesMutex.Lock()
defer c.responsesMutex.Unlock()
response = c.responses[tid]
if response != nil {
ready = true
delete(c.responses, tid)
}
return
}
func (c *Client) SendMessage(msg *Message) {
c.outMessages <- msg
}
func (c *Client) Call(msg *Message, t uint32) (response *Response, err error) {
c.SendMessage(msg)
tid := msg.TransactionId
ticker := time.NewTicker(time.Duration(5) * time.Millisecond)
defer ticker.Stop()
timeout := time.After(time.Duration(t) * time.Second)
for {
select {
case <-ticker.C:
res, ready := c.GetResponse(tid)
if ready {
return res, nil
}
case <-timeout:
return response, ErrResponseTimeout
}
}
return
}
func (c *Client) Read(p []byte) (n int, err error) {
n, err = c.conn.Read(p)
c.inBytes += uint32(n)
log.Trace("read %d", n)
return n, err
}
func (c *Client) Write(p []byte) (n int, err error) {
n, err = c.conn.Write(p)
c.outBytes += uint32(n)
log.Trace("write %d", n)
return n, err
}