mirror of
https://github.com/tinygo-org/net.git
synced 2026-08-14 08:23:39 +00:00
Added missing TCPAddr and UDPAddr implementations to the net package
This commit is contained in:
committed by
deadprogram
parent
d6afd8a0f4
commit
b46e2ec2ac
+55
@@ -0,0 +1,55 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"internal/itoa"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
// UDPAddr represents the address of a UDP end point.
|
||||
type UDPAddr struct {
|
||||
IP IP
|
||||
Port int
|
||||
Zone string // IPv6 scoped addressing zone
|
||||
}
|
||||
|
||||
// AddrPort returns the UDPAddr a as a netip.AddrPort.
|
||||
//
|
||||
// If a.Port does not fit in a uint16, it's silently truncated.
|
||||
//
|
||||
// If a is nil, a zero value is returned.
|
||||
func (a *UDPAddr) AddrPort() netip.AddrPort {
|
||||
if a == nil {
|
||||
return netip.AddrPort{}
|
||||
}
|
||||
na, _ := netip.AddrFromSlice(a.IP)
|
||||
na = na.WithZone(a.Zone)
|
||||
return netip.AddrPortFrom(na, uint16(a.Port))
|
||||
}
|
||||
|
||||
// Network returns the address's network name, "udp".
|
||||
func (a *UDPAddr) Network() string { return "udp" }
|
||||
|
||||
func (a *UDPAddr) String() string {
|
||||
if a == nil {
|
||||
return "<nil>"
|
||||
}
|
||||
ip := ipEmptyString(a.IP)
|
||||
if a.Zone != "" {
|
||||
return JoinHostPort(ip+"%"+a.Zone, itoa.Itoa(a.Port))
|
||||
}
|
||||
return JoinHostPort(ip, itoa.Itoa(a.Port))
|
||||
}
|
||||
|
||||
func (a *UDPAddr) isWildcard() bool {
|
||||
if a == nil || a.IP == nil {
|
||||
return true
|
||||
}
|
||||
return a.IP.IsUnspecified()
|
||||
}
|
||||
|
||||
func (a *UDPAddr) opAddr() Addr {
|
||||
if a == nil {
|
||||
return nil
|
||||
}
|
||||
return a
|
||||
}
|
||||
Reference in New Issue
Block a user