cleanup
This commit is contained in:
+9
-17
@@ -108,13 +108,13 @@ func Dial(host, uid, authKey, enr string, verbose bool) (*Conn, error) {
|
|||||||
verbose: verbose,
|
verbose: verbose,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
mainBuf: make(chan []byte, 64),
|
mainBuf: make(chan []byte, 64),
|
||||||
speakerBuf: make(chan []byte, 64),
|
speakerBuf: make(chan []byte, 64),
|
||||||
packetQueue: make(chan *Packet, 128),
|
packetQueue: make(chan *Packet, 128),
|
||||||
done: make(chan struct{}),
|
done: make(chan struct{}),
|
||||||
ioctrl: make(chan []byte, 16),
|
ioctrl: make(chan []byte, 16),
|
||||||
ackReceived: make(chan struct{}, 1),
|
ackReceived: make(chan struct{}, 1),
|
||||||
errors: make(chan error, 1),
|
errors: make(chan error, 1),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = c.discovery(); err != nil {
|
if err = c.discovery(); err != nil {
|
||||||
@@ -169,7 +169,6 @@ func (c *Conn) AVClientStart(timeout time.Duration) error {
|
|||||||
return io.EOF
|
return io.EOF
|
||||||
}
|
}
|
||||||
if len(data) >= 32 && binary.LittleEndian.Uint16(data[0:2]) == MagicAVLoginResp {
|
if len(data) >= 32 && binary.LittleEndian.Uint16(data[0:2]) == MagicAVLoginResp {
|
||||||
// Parse response inline
|
|
||||||
c.avLoginResp = &AVLoginResponse{
|
c.avLoginResp = &AVLoginResponse{
|
||||||
ServerType: binary.LittleEndian.Uint32(data[4:8]),
|
ServerType: binary.LittleEndian.Uint32(data[4:8]),
|
||||||
Resend: int32(data[29]),
|
Resend: int32(data[29]),
|
||||||
@@ -265,7 +264,6 @@ func (c *Conn) AVSendAudioData(codec uint16, payload []byte, timestampUS uint32,
|
|||||||
return fmt.Errorf("speaker channel not connected")
|
return fmt.Errorf("speaker channel not connected")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build frame with 36-byte header + audio + 16-byte FrameInfo (FrameInfo inside payload!)
|
|
||||||
frame := c.buildAudioFrame(payload, timestampUS, codec, sampleRate, channels)
|
frame := c.buildAudioFrame(payload, timestampUS, codec, sampleRate, channels)
|
||||||
|
|
||||||
if c.verbose {
|
if c.verbose {
|
||||||
@@ -290,7 +288,6 @@ func (c *Conn) SendIOCtrl(cmdID uint16, payload []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block until ACK received (like SDK)
|
|
||||||
select {
|
select {
|
||||||
case <-c.ackReceived:
|
case <-c.ackReceived:
|
||||||
if c.verbose {
|
if c.verbose {
|
||||||
@@ -350,14 +347,12 @@ func (c *Conn) SetDeadline(t time.Time) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) Close() error {
|
func (c *Conn) Close() error {
|
||||||
// Signal done to stop goroutines
|
|
||||||
select {
|
select {
|
||||||
case <-c.done:
|
case <-c.done:
|
||||||
default:
|
default:
|
||||||
close(c.done)
|
close(c.done)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close DTLS connections
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
if c.mainConn != nil {
|
if c.mainConn != nil {
|
||||||
c.mainConn.Close()
|
c.mainConn.Close()
|
||||||
@@ -370,8 +365,6 @@ func (c *Conn) Close() error {
|
|||||||
c.mu.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
c.cancel()
|
c.cancel()
|
||||||
|
|
||||||
// Wait for goroutines
|
|
||||||
c.wg.Wait()
|
c.wg.Wait()
|
||||||
|
|
||||||
close(c.ioctrl)
|
close(c.ioctrl)
|
||||||
@@ -544,7 +537,6 @@ func (c *Conn) iotcReader() {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Inline receive with timeout
|
|
||||||
c.udpConn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
c.udpConn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||||
n, addr, err := c.udpConn.ReadFromUDP(buf)
|
n, addr, err := c.udpConn.ReadFromUDP(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -763,7 +755,7 @@ func (c *Conn) handleSpeakerAVLogin() error {
|
|||||||
return fmt.Errorf("AV login too short: %d bytes", n)
|
return fmt.Errorf("AV login too short: %d bytes", n)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract checksum from incoming request (bytes 20-23) - MUST echo this back!
|
// Extract checksum from incoming request (bytes 20-23)
|
||||||
checksum := binary.LittleEndian.Uint32(buf[20:24])
|
checksum := binary.LittleEndian.Uint32(buf[20:24])
|
||||||
|
|
||||||
// Build AV Login response (60 bytes like SDK)
|
// Build AV Login response (60 bytes like SDK)
|
||||||
@@ -877,7 +869,7 @@ func (c *Conn) extractPayload(data []byte, channel byte) ([]byte, *FrameInfo) {
|
|||||||
headerSize = 36
|
headerSize = 36
|
||||||
frameInfoSize = FrameInfoSize
|
frameInfoSize = FrameInfoSize
|
||||||
default:
|
default:
|
||||||
// Unknown frame type - use 28-byte header as fallback (most common)
|
// Unknown frame type - use 28-byte header as fallback
|
||||||
headerSize = 28
|
headerSize = 28
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user