Removed unused code for unwritable links, and collapsed unhandled cases in switch statments.

This commit is contained in:
scruzin 2019-01-13 09:47:04 +10:30
parent 8898ee3add
commit 40ce3357eb
1 changed files with 27 additions and 103 deletions

View File

@ -241,20 +241,19 @@ func connectStream(s *Session) error {
} }
// handlePacket handles a packet that the client has received. // handlePacket handles a packet that the client has received.
// NB: cases have been commented out that are not currently used by AusOcean // NB: Unsupported packet types are logged fatally.
func handlePacket(s *Session, pkt *packet) error { func handlePacket(s *Session, pkt *packet) error {
if pkt.bodySize < 4 {
return errInvalidBody
}
switch pkt.packetType { switch pkt.packetType {
case packetTypeChunkSize: case packetTypeChunkSize:
if pkt.bodySize >= 4 { s.inChunkSize = int32(amf.DecodeInt32(pkt.body[:4]))
s.inChunkSize = int32(amf.DecodeInt32(pkt.body[:4]))
}
case packetTypeBytesReadReport: case packetTypeBytesReadReport:
s.serverBW = int32(amf.DecodeInt32(pkt.body[:4])) s.serverBW = int32(amf.DecodeInt32(pkt.body[:4]))
case packetTypeControl:
s.log(FatalLevel, pkg+"unsupported packet type packetTypeControl")
case packetTypeServerBW: case packetTypeServerBW:
s.serverBW = int32(amf.DecodeInt32(pkt.body[:4])) s.serverBW = int32(amf.DecodeInt32(pkt.body[:4]))
@ -266,18 +265,6 @@ func handlePacket(s *Session, pkt *packet) error {
s.clientBW2 = 0xff s.clientBW2 = 0xff
} }
case packetTypeAudio:
s.log(FatalLevel, pkg+"unsupported packet type packetTypeAudio")
case packetTypeVideo:
s.log(FatalLevel, pkg+"unsupported packet type packetTypeVideo")
case packetTypeFlexMessage:
s.log(FatalLevel, pkg+"unsupported packet type packetTypeFlexMessage")
case packetTypeInfo:
s.log(FatalLevel, pkg+"unsupported packet type packetTypeInfo")
case packetTypeInvoke: case packetTypeInvoke:
err := handleInvoke(s, pkt.body[:pkt.bodySize]) err := handleInvoke(s, pkt.body[:pkt.bodySize])
if err != nil { if err != nil {
@ -286,8 +273,8 @@ func handlePacket(s *Session, pkt *packet) error {
return err return err
} }
case packetTypeFlashVideo: case packetTypeControl, packetTypeAudio, packetTypeVideo, packetTypeFlashVideo, packetTypeFlexMessage, packetTypeInfo:
s.log(FatalLevel, pkg+"unsupported packet type packetType_FLASHVideo") s.log(FatalLevel, pkg+"unsupported packet type "+strconv.Itoa(int(pkt.packetType)))
default: default:
s.log(WarnLevel, pkg+"unknown packet type", "type", pkt.packetType) s.log(WarnLevel, pkg+"unknown packet type", "type", pkt.packetType)
@ -315,49 +302,21 @@ func sendConnectPacket(s *Session) error {
if err != nil { if err != nil {
return err return err
} }
enc[0] = amf.TypeObject enc[0] = amf.TypeObject
enc = enc[1:] enc = enc[1:]
enc, err = amf.EncodeNamedString(enc, avApp, s.link.app) enc, err = amf.EncodeNamedString(enc, avApp, s.link.app)
if err != nil { if err != nil {
return err return err
} }
if s.link.protocol&featureWrite != 0 { enc, err = amf.EncodeNamedString(enc, avType, avNonprivate)
enc, err = amf.EncodeNamedString(enc, avType, avNonprivate) if err != nil {
if err != nil { return err
return err
}
} }
if s.link.url != "" { enc, err = amf.EncodeNamedString(enc, avTcUrl, s.link.url)
enc, err = amf.EncodeNamedString(enc, avTcUrl, s.link.url) if err != nil {
if err != nil { return err
return err
}
} }
if s.link.protocol&featureWrite == 0 {
enc, err = amf.EncodeNamedBoolean(enc, avFpad, false)
if err != nil {
return err
}
enc, err = amf.EncodeNamedNumber(enc, avCapabilities, 15)
if err != nil {
return err
}
enc, err = amf.EncodeNamedNumber(enc, avAudioCodecs, s.audioCodecs)
if err != nil {
return err
}
enc, err = amf.EncodeNamedNumber(enc, avVideoCodecs, s.videoCodecs)
if err != nil {
return err
}
enc, err = amf.EncodeNamedNumber(enc, avVideoFunction, 1)
if err != nil {
return err
}
}
// terminate the AMF object
enc, err = amf.EncodeInt24(enc, amf.TypeObjectEnd) enc, err = amf.EncodeInt24(enc, amf.TypeObjectEnd)
if err != nil { if err != nil {
return err return err
@ -707,8 +666,8 @@ func handleInvoke(s *Session, body []byte) error {
return err return err
} }
case avPlay, avPublish: default:
s.log(FatalLevel, pkg+"unsupported method avPlay/avPublish") s.log(FatalLevel, pkg+"unexpected method invoked"+methodInvoked)
} }
case avOnBWDone: case avOnBWDone:
@ -719,21 +678,6 @@ func handleInvoke(s *Session, body []byte) error {
} }
} }
case avOnFCUnsubscribe, avOnFCSubscribe:
s.log(FatalLevel, pkg+"unsupported method avOnFCUnsubscribe/avOonfcsubscribe")
case avPing:
s.log(FatalLevel, pkg+"unsupported method avPing")
case av_onbwcheck:
s.log(FatalLevel, pkg+"unsupported method av_onbwcheck")
case av_onbwdone:
s.log(FatalLevel, pkg+"unsupported method av_onbwdone")
case avClose:
s.log(FatalLevel, pkg+"unsupported method avClose")
case avOnStatus: case avOnStatus:
obj2, err := obj.ObjectProperty("", 3) obj2, err := obj.ObjectProperty("", 3)
if err != nil { if err != nil {
@ -749,40 +693,20 @@ func handleInvoke(s *Session, body []byte) error {
} }
s.log(DebugLevel, pkg+"onStatus", "code", code, "level", level) s.log(DebugLevel, pkg+"onStatus", "code", code, "level", level)
switch code { if code != avNetStreamPublish_Start {
case avNetStreamFailed, avNetStreamPlayFailed, s.log(ErrorLevel, pkg+"unexpected response "+code)
avNetStreamPlayStreamNotFound, avNetConnectionConnectInvalidApp: return errUnimplemented
s.log(FatalLevel, pkg+"unsupported method avNetStream/avNetStreamPlayFailed/avNetstream_play_streamnotfound/av_netConnection_Connect_invalidApp") }
s.log(DebugLevel, pkg+"playing")
case avNetStreamPlayStart, avNetStreamPlayPublishNotify: s.isPlaying = true
s.log(FatalLevel, pkg+"unsupported method avNetStreamPlayStart/avNetStreamPlayPublishNotify") for i, m := range s.methodCalls {
if m.name == avPublish {
case avNetStreamPublish_Start: s.methodCalls = eraseMethod(s.methodCalls, i)
s.log(DebugLevel, pkg+"playing")
s.isPlaying = true
for i, m := range s.methodCalls {
if m.name == avPublish {
s.methodCalls = eraseMethod(s.methodCalls, i)
break
}
} }
// ToDo: handle case when avPublish method not found
case avNetStreamPlayComplete, avNetStreamPlayStop, avNetStreamPlayUnpublishNotify:
s.log(FatalLevel, pkg+"unsupported method avNetStreamPlayComplete/avNetStreamPlayStop/avNetStreamPlayUnpublishNotify")
case avNetStreamSeekNotify:
s.log(FatalLevel, pkg+"unsupported method avNetstream_seek_notify")
case avNetStreamPauseNotify:
s.log(FatalLevel, pkg+"unsupported method avNetStreamPauseNotify")
} }
case avPlaylist_ready:
s.log(FatalLevel, pkg+"unsupported method avPlaylist_ready")
default: default:
s.log(FatalLevel, pkg+"unknown method "+meth) s.log(FatalLevel, pkg+"unsuppoted method "+meth)
} }
return nil return nil
} }