Check audio/serial stream request for timeout

This commit is contained in:
Nonoo 2020-10-18 11:15:31 +02:00
parent df47a24a6d
commit 385d96c7d1
2 changed files with 28 additions and 13 deletions

View file

@ -4,19 +4,20 @@ import (
"bytes"
"crypto/rand"
"encoding/binary"
"os"
"errors"
"time"
"github.com/nonoo/kappanhang/log"
)
type controlStream struct {
common streamCommon
authSendSeq uint16
authInnerSendSeq uint16
authID [6]byte
randIDByteForPktSeven [1]byte
expectedPkt7ReplySeq uint16
common streamCommon
authSendSeq uint16
authInnerSendSeq uint16
authID [6]byte
randIDByteForPktSeven [1]byte
expectedPkt7ReplySeq uint16
requestSerialAndAudioTimeout *time.Timer
}
func (s *controlStream) sendPkt7(replyID []byte, seq uint16) {
@ -150,6 +151,10 @@ func (s *controlStream) sendRequestSerialAndAudio() {
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})
s.authSendSeq++
s.authInnerSendSeq++
s.requestSerialAndAudioTimeout = time.AfterFunc(3*time.Second, func() {
exit(errors.New("serial and audio request timeout"))
})
}
func (s *controlStream) handleRead(r []byte) {
@ -199,9 +204,7 @@ func (s *controlStream) handleRead(r []byte) {
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
log.Error("reauth failed")
s.SendDisconnect()
os.Exit(1)
exit(errors.New("reauth failed"))
}
case 144:
if bytes.Equal(r[:6], []byte{0x90, 0x00, 0x00, 0x00, 0x00, 0x00}) && r[96] == 1 {
@ -225,6 +228,8 @@ func (s *controlStream) handleRead(r []byte) {
// 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x03, 0x03,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
log.Print("serial and audio request success")
s.requestSerialAndAudioTimeout.Stop()
s.requestSerialAndAudioTimeout = nil
go streams.audio.Start()
}
}
@ -277,7 +282,6 @@ func (s *controlStream) Start() {
copy(s.authID[:], r[26:32])
log.Print("auth ok")
s.sendPktReauth(true)
time.AfterFunc(time.Second*2, s.sendRequestSerialAndAudio)
_, err := rand.Read(s.randIDByteForPktSeven[:])
if err != nil {
@ -291,6 +295,8 @@ func (s *controlStream) Start() {
reauthTicker := time.NewTicker(60 * time.Second)
statusLogTicker := time.NewTicker(10 * time.Second)
time.AfterFunc(time.Second*2, s.sendRequestSerialAndAudio)
for {
select {
case r = <-readChan:

13
main.go
View file

@ -13,14 +13,23 @@ var streams struct {
audio audioStream
}
func exit(err error) {
streams.control.SendDisconnect()
if err == nil {
os.Exit(0)
} else {
log.Error(err.Error())
os.Exit(1)
}
}
func setupCloseHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
log.Print("disconnecting")
streams.control.SendDisconnect()
os.Exit(0)
exit(nil)
}()
}