Add Send method to TCPClientInterface and better logging in UDPInterface
This commit is contained in:
@@ -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 {
|
func (tc *TCPClientInterface) ProcessOutgoing(data []byte) error {
|
||||||
if !tc.Online {
|
if !tc.Online {
|
||||||
return fmt.Errorf("interface offline")
|
return fmt.Errorf("interface offline")
|
||||||
@@ -258,7 +275,11 @@ func (tc *TCPClientInterface) ProcessOutgoing(data []byte) error {
|
|||||||
// Update TX stats before sending
|
// Update TX stats before sending
|
||||||
tc.UpdateStats(uint64(len(frame)), false)
|
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)
|
_, err := tc.conn.Write(frame)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("[DEBUG-1] TCP interface %s: Write failed: %v", tc.Name, err)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package interfaces
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -75,6 +76,8 @@ func (ui *UDPInterface) Detach() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ui *UDPInterface) Send(data []byte, addr string) error {
|
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() {
|
if !ui.IsEnabled() {
|
||||||
return fmt.Errorf("interface not enabled")
|
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")
|
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)
|
_, 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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user