Add decoding of amf external via handler

This commit is contained in:
Jason Coene
2013-06-13 11:38:21 -05:00
parent 73ef0b8508
commit 71d01458a6
3 changed files with 15 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ package rtmp
import (
"crypto/tls"
"github.com/elobuff/goamf"
"net"
"net/url"
"sync"
@@ -33,6 +34,8 @@ type Client struct {
lastTransactionId uint32
connectionId string
amfExternalHandlers map[string]amf.ExternalHandler
}
func NewClient(url string) (c *Client) {
@@ -78,11 +81,16 @@ func (c *Client) Reset() {
c.inChunkSize = DEFAULT_CHUNK_SIZE
c.inWindowSize = DEFAULT_WINDOW_SIZE
c.inChunkStreams = make(map[uint32]*InboundChunkStream)
c.amfExternalHandlers = make(map[string]amf.ExternalHandler)
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.Info("disconnected from %s", c.url)

View File

@@ -23,7 +23,7 @@ func (c *Client) routeLoop() {
}
func (c *Client) routeCommandMessage(msg *Message) {
response, err := msg.DecodeResponse()
response, err := msg.DecodeResponse(c)
if err != nil {
log.Error("unable to decode message type %d on stream %d into command, discarding: %s", msg.Type, msg.ChunkStreamId, err)
return

View File

@@ -24,10 +24,14 @@ func (m *Message) RemainingBytes() uint32 {
return m.Length - uint32(m.Buffer.Len())
}
func (m *Message) DecodeResponse() (response *Response, err error) {
dec := new(amf.Decoder)
func (m *Message) DecodeResponse(c *Client) (response *Response, err error) {
dec := amf.NewDecoder()
response = new(Response)
for nam, fn := range c.amfExternalHandlers {
dec.RegisterExternalHandler(nam, fn)
}
if m.ChunkStreamId != CHUNK_STREAM_ID_COMMAND {
return response, Error("message is not a command message")
}