// Listen monitors all of the ingoing and outgoing chnanels on the NetConn type // and makes sure that things are in order. // - It decodes chunks when they are received into Receivables, passing them // along the In() channel, or writing an error to Errs() if a parse error was // encountered. // // Listen terminates when the closer channel can be read (accomplished by // calling Close()). // // Listen runs within its own goroutine. func (n *NetConn) Listen() { for { select { case c := <-n.chunkStream: buf := bytes.NewBuffer(c.Data) name, err := amf0.Decode(buf) if err != nil { n.errs <- err continue } nameStr, ok := name.(*amf0.String) if !ok { n.errs <- fmt.Errorf("rtmp/conn: wrong type for AMF header: %T (expected amf0.String)", name) continue } if r, err := n.parser.Parse(nameStr, buf); err != nil { n.errs <- err } else { n.in <- r } case <-n.closer: return } } }
func TestErrorfulDecode(t *testing.T) { b := []byte{0x02} typ, err := amf0.Decode(bytes.NewReader(b)) assert.Nil(t, typ) assert.Equal(t, io.EOF, err) }
func TestUnidentifiableDecode(t *testing.T) { b := []byte{0xff} typ, err := amf0.Decode(bytes.NewReader(b)) assert.Nil(t, typ) assert.Equal(t, "amf0: unknown packet identifier for 0xff", err.Error()) }
func TestSuccesfulDecode(t *testing.T) { b := []byte{0x01, 0xf} typ, err := amf0.Decode(bytes.NewReader(b)) assert.Nil(t, err) assert.IsType(t, new(amf0.Bool), typ) assert.True(t, bool(*(typ.(*amf0.Bool)))) }