github.com/iDigitalFlame/xmt@v0.5.4/com/c_compat.go (about) 1 //go:build !go1.18 2 // +build !go1.18 3 4 // Copyright (C) 2020 - 2023 iDigitalFlame 5 // 6 // This program is free software: you can redistribute it and/or modify 7 // it under the terms of the GNU General Public License as published by 8 // the Free Software Foundation, either version 3 of the License, or 9 // any later version. 10 // 11 // This program is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // You should have received a copy of the GNU General Public License 17 // along with this program. If not, see <https://www.gnu.org/licenses/>. 18 // 19 20 package com 21 22 import ( 23 "net" 24 25 "github.com/iDigitalFlame/xmt/device" 26 ) 27 28 type udpAddr struct { 29 p uint16 30 device.Address 31 } 32 type udpSockInternal interface{} 33 34 func (u udpAddr) IsValid() bool { 35 return u.IsValid() 36 } 37 func (u udpAddr) String() string { 38 return u.Address.String() 39 } 40 func (u *udpCompat) ReadPacket(p []byte) (int, udpAddr, error) { 41 var ( 42 o udpAddr 43 n, a, err = u.udpSock.ReadFrom(p) 44 ) 45 if a != nil { 46 switch t := a.(type) { 47 case *net.IPAddr: 48 o.Set(t.IP) 49 case *net.UDPAddr: 50 o.Set(t.IP) 51 o.p = uint16(t.Port) 52 } 53 } 54 return n, o, err 55 } 56 func (u *udpCompat) WritePacket(p []byte, a udpAddr) (int, error) { 57 var o net.Addr 58 if a.p > 0 { 59 o = &net.UDPAddr{IP: a.IP(), Port: int(a.p)} 60 } else { 61 o = &net.IPAddr{IP: a.IP()} 62 } 63 n, err := u.udpSock.WriteTo(p, o) 64 o = nil 65 return n, err 66 }