Use the exit handler

This commit is contained in:
Nonoo 2020-10-19 09:45:49 +02:00
parent daadeacdfa
commit 91166ca937
4 changed files with 14 additions and 14 deletions

View file

@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/binary"
"errors"
"time"
"github.com/nonoo/kappanhang/log"
@ -101,7 +102,7 @@ func (s *audioStream) start() {
case r = <-s.common.readChan:
s.handleRead(r)
case <-s.timeoutTimer.C:
log.Fatal("timeout")
exit(errors.New("timeout"))
case <-testSendTicker.C: // TODO: remove
b1 := make([]byte, 1364)
s.sendPart1(b1)

View file

@ -25,7 +25,7 @@ func (s *controlStream) sendPktAuth() {
var randID [2]byte
_, err := rand.Read(randID[:])
if err != nil {
log.Fatal(err)
exit(err)
}
s.common.send([]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, byte(s.authSendSeq), byte(s.authSendSeq >> 8),
byte(s.common.localSID >> 24), byte(s.common.localSID >> 16), byte(s.common.localSID >> 8), byte(s.common.localSID),
@ -232,11 +232,11 @@ func (s *controlStream) start() {
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
r := s.common.expect(96, []byte{0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00})
if bytes.Equal(r[48:52], []byte{0xff, 0xff, 0xff, 0xfe}) {
log.Fatal("invalid user/password")
exit(errors.New("invalid user/password"))
}
copy(s.authID[:], r[26:32])
log.Print("auth ok")
log.Print("auth ok, waiting a bit")
s.sendPktReauth(true)
time.AfterFunc(time.Second, func() {

View file

@ -4,6 +4,7 @@ import (
"bytes"
"crypto/rand"
"encoding/binary"
"errors"
"time"
"github.com/nonoo/kappanhang/log"
@ -72,7 +73,7 @@ func (p *pkt7Type) sendDo(s *streamCommon, replyID []byte, seq uint16) {
var randID [2]byte
_, err := rand.Read(randID[:])
if err != nil {
log.Fatal(err)
exit(err)
}
replyID[0] = randID[0]
replyID[1] = randID[1]
@ -111,7 +112,7 @@ func (p *pkt7Type) startPeriodicSend(s *streamCommon, firstSeqNo uint16) {
case <-p.sendTicker.C:
p.send(s)
case <-p.timeoutTimer.C:
log.Fatal(s.name + "/ping timeout")
exit(errors.New(s.name + "/ping timeout"))
}
}
}()

View file

@ -4,6 +4,7 @@ import (
"bytes"
"crypto/rand"
"encoding/binary"
"errors"
"fmt"
"net"
"time"
@ -28,7 +29,7 @@ type streamCommon struct {
func (s *streamCommon) send(d []byte) {
_, err := s.conn.Write(d)
if err != nil {
log.Fatal(err)
exit(err)
}
}
@ -73,7 +74,7 @@ func (s *streamCommon) tryReceivePacket(timeout time.Duration, packetLength, mat
func (s *streamCommon) expect(packetLength int, b []byte) []byte {
r := s.tryReceivePacket(expectTimeoutDuration, packetLength, 0, b)
if r == nil {
log.Fatal(s.name + "/expect timeout")
exit(errors.New(s.name + "/expect timeout"))
}
return r
}
@ -84,7 +85,7 @@ func (s *streamCommon) open(name string, portNumber int) {
log.Print(s.name+"/connecting to ", hostPort)
raddr, err := net.ResolveUDPAddr("udp", hostPort)
if err != nil {
log.Fatal(err)
exit(err)
}
// Use the same local and remote port. The radio does not handle different ports well.
@ -93,17 +94,16 @@ func (s *streamCommon) open(name string, portNumber int) {
}
s.conn, err = net.DialUDP("udp", &l, raddr)
if err != nil {
log.Fatal(err)
exit(err)
}
// Constructing the local session ID by combining the local IP address and port.
laddr := s.conn.LocalAddr().(*net.UDPAddr)
s.localSID = binary.BigEndian.Uint32(laddr.IP[len(laddr.IP)-4:])<<16 | uint32(laddr.Port&0xffff)
log.Debugf(s.name+"/using session id %.8x", s.localSID)
_, err = rand.Read(s.pkt7.randIDBytes[:])
if err != nil {
log.Fatal(err)
exit(err)
}
s.readChan = make(chan []byte)
@ -155,8 +155,6 @@ func (s *streamCommon) waitForPkt4Answer() {
r := s.expect(16, []byte{0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00})
s.remoteSID = binary.BigEndian.Uint32(r[8:12])
s.gotRemoteSID = true
log.Debugf(s.name+"/got remote session id %.8x", s.remoteSID)
}
func (s *streamCommon) sendPkt6() {