Add Send method to TCPClientInterface and better logging in UDPInterface

This commit is contained in:
2025-09-07 02:21:48 -05:00
parent 069d4163eb
commit 2ffd12b3e1
2 changed files with 35 additions and 1 deletions

View File

@@ -238,6 +238,23 @@ func (tc *TCPClientInterface) handlePacket(data []byte) {
}
}
// Send implements the interface Send method for TCP interface
func (tc *TCPClientInterface) Send(data []byte, address string) error {
log.Printf("[DEBUG-7] TCP interface %s: Sending %d bytes", tc.Name, len(data))
if !tc.IsEnabled() || !tc.IsOnline() {
return fmt.Errorf("TCP interface %s is not online", tc.Name)
}
// For TCP interface, we need to prepend a packet type byte for announce packets
// RNS TCP protocol expects: [packet_type][data]
frame := make([]byte, 0, len(data)+1)
frame = append(frame, 0x01) // Announce packet type
frame = append(frame, data...)
return tc.ProcessOutgoing(frame)
}
func (tc *TCPClientInterface) ProcessOutgoing(data []byte) error {
if !tc.Online {
return fmt.Errorf("interface offline")
@@ -257,8 +274,12 @@ func (tc *TCPClientInterface) ProcessOutgoing(data []byte) error {
// Update TX stats before sending
tc.UpdateStats(uint64(len(frame)), false)
log.Printf("[DEBUG-7] TCP interface %s: Writing %d bytes to network", tc.Name, len(frame))
_, err := tc.conn.Write(frame)
if err != nil {
log.Printf("[DEBUG-1] TCP interface %s: Write failed: %v", tc.Name, err)
}
return err
}

View File

@@ -2,6 +2,7 @@ package interfaces
import (
"fmt"
"log"
"net"
"sync"
@@ -75,6 +76,8 @@ func (ui *UDPInterface) Detach() {
}
func (ui *UDPInterface) Send(data []byte, addr string) error {
log.Printf("[DEBUG-7] UDP interface %s: Sending %d bytes", ui.Name, len(data))
if !ui.IsEnabled() {
return fmt.Errorf("interface not enabled")
}
@@ -83,7 +86,17 @@ func (ui *UDPInterface) Send(data []byte, addr string) error {
return fmt.Errorf("no target address configured")
}
// Update TX stats before sending
ui.mutex.Lock()
ui.TxBytes += uint64(len(data))
ui.mutex.Unlock()
_, err := ui.conn.WriteTo(data, ui.targetAddr)
if err != nil {
log.Printf("[DEBUG-1] UDP interface %s: Write failed: %v", ui.Name, err)
} else {
log.Printf("[DEBUG-7] UDP interface %s: Sent %d bytes successfully", ui.Name, len(data))
}
return err
}