gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/ipc/cgo/shm-server/addr.go (about) 1 package shmServer 2 3 import ( 4 "fmt" 5 "net" 6 "strconv" 7 "strings" 8 ) 9 10 var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff} 11 12 type AddrId struct { 13 IP [16]byte 14 Port int 15 } 16 17 func (a *AddrId) Unmarshal(ip net.IP, port int) { 18 if len(ip) == net.IPv4len { 19 copy(a.IP[:], v4InV6Prefix) 20 copy(a.IP[12:], ip) 21 } 22 a.Port = port 23 } 24 25 func addrToString(addr *net.UDPAddr) string { 26 var ips string 27 if ipv4 := addr.IP.To4(); ipv4 != nil { 28 ips = strings.ReplaceAll(ipv4.String(), ".", "-") 29 } else if len(addr.IP) == net.IPv6len { 30 ips = strings.ReplaceAll(addr.IP.String(), ":", "-") 31 } 32 if len(ips) > 0 { 33 return fmt.Sprintf("%s_%d", ips, addr.Port) 34 } else { 35 return strconv.Itoa(addr.Port) 36 } 37 }