Add read loop for UDP interface to handle incoming packets asynchronously.

This commit is contained in:
2025-10-07 21:30:27 -05:00
parent 7d7a022736
commit b601ae1c51

View File

@@ -182,9 +182,37 @@ func (ui *UDPInterface) Start() error {
}
ui.conn = conn
ui.Online = true
// Start the read loop in a goroutine
go ui.readLoop()
return nil
}
func (ui *UDPInterface) readLoop() {
buffer := make([]byte, common.DEFAULT_MTU)
for ui.IsOnline() && !ui.IsDetached() {
n, remoteAddr, err := ui.conn.ReadFromUDP(buffer)
if err != nil {
if ui.IsOnline() {
log.Printf("Error reading from UDP interface %s: %v", ui.Name, err)
}
return
}
ui.mutex.Lock()
if ui.targetAddr == nil {
log.Printf("[DEBUG-7] UDP interface %s discovered peer %s", ui.Name, remoteAddr)
ui.targetAddr = remoteAddr
}
ui.mutex.Unlock()
if ui.packetCallback != nil {
ui.packetCallback(buffer[:n], ui)
}
}
}
/*
func (ui *UDPInterface) readLoop() {
buffer := make([]byte, ui.MTU)