Implement Poke functionality, refine talking status and add local log events

This commit is contained in:
Jose Luis Montañes Ojados
2026-01-17 00:53:50 +01:00
parent be5e26486c
commit 3a57f41fc2
5 changed files with 125 additions and 20 deletions

View File

@@ -134,26 +134,41 @@ func (c *Client) Connect(address string) error {
defer ticker.Stop()
for {
// Recovery from panics in the main loop
func() {
defer func() {
if r := recover(); r != nil {
log.Printf("PANIC in Client loop: %v", r)
}
}()
select {
case <-c.done:
log.Println("Client loop stopped")
return
case pkt := <-pktChan:
if pkt == nil {
// Channel closed
return
}
if err := c.handlePacket(pkt); err != nil {
log.Printf("Error handling packet: %v", err)
}
case <-ticker.C:
if !c.Connected {
return // Don't send pings if not connected yet
}
// Send KeepAlive Ping (Encrypted, No NewProtocol)
if err := c.sendPing(); err != nil {
log.Printf("Error sending Ping: %v", err)
}
}
}()
// Check if we should exit after the inner function
select {
case <-c.done:
log.Println("Client loop stopped")
return nil
case pkt := <-pktChan:
if pkt == nil {
// Channel closed
return nil
}
if err := c.handlePacket(pkt); err != nil {
log.Printf("Error handling packet: %v", err)
}
case <-ticker.C:
if !c.Connected {
continue // Don't send pings if not connected yet
}
// Send KeepAlive Ping (Encrypted, No NewProtocol)
if err := c.sendPing(); err != nil {
log.Printf("Error sending Ping: %v", err)
}
default:
}
}
}