begin adding udp conn (#69)

* begin adding udp conn

* udp tests passing

* bugfixes for udp Send,Abort,Encapsulate methods

* document UDP methods
This commit is contained in:
Pat Whittingslow
2026-04-10 00:34:37 -03:00
committed by GitHub
parent 568ef2854f
commit 0382410f02
6 changed files with 731 additions and 6 deletions
+44 -1
View File
@@ -9,11 +9,13 @@ import (
"github.com/soypat/lneto"
"github.com/soypat/lneto/tcp"
"github.com/soypat/lneto/udp"
)
// Socket types
const (
sockSTREAM = 0x1
sockDGRAM = 0x2
)
type StackGoConfig struct {
@@ -81,7 +83,37 @@ func (s StackGo) SocketNetip(ctx context.Context, network string, family, sotype
}
switch network {
case "udp", "udp4":
return nil, lneto.ErrUnsupported
if sotype != sockDGRAM {
return nil, lneto.ErrUnsupported
}
if !raddr.IsValid() || raddr.Addr() == netip.IPv4Unspecified() {
return nil, lneto.ErrZeroDestination
}
var conn udp.Conn
err = conn.Configure(udp.ConnConfig{
TxBuf: make([]byte, s.plcfg.TxBufSize),
RxBuf: make([]byte, s.plcfg.RxBufSize),
TxQueueSize: s.plcfg.QueueSize,
RxQueueSize: s.plcfg.QueueSize,
})
if err != nil {
return nil, err
}
raddr4 := raddr.Addr().As4()
err = conn.Open(laddr.Port(), raddr.Port(), raddr4[:])
if err != nil {
return nil, err
}
err = s.blk.async.RegisterUDP(&conn, raddr4[:], raddr.Port())
if err != nil {
return nil, err
}
uc := udpconn{
Conn: &conn,
localAddr: net.UDPAddrFromAddrPort(laddr),
raddr: net.UDPAddrFromAddrPort(raddr),
}
return uc, nil
case "tcp", "tcp4":
if sotype != sockSTREAM {
return nil, lneto.ErrUnsupported
@@ -206,3 +238,14 @@ func (c tcpconn) RemoteAddr() net.Addr {
Port: int(c.Conn.RemotePort()),
}
}
type udpconn struct {
*udp.Conn
localAddr net.Addr
raddr net.Addr
}
var _ net.Conn = udpconn{}
func (c udpconn) LocalAddr() net.Addr { return c.localAddr }
func (c udpconn) RemoteAddr() net.Addr { return c.raddr }