diff --git a/discovery.go b/discovery.go index 745df31..12e52d5 100644 --- a/discovery.go +++ b/discovery.go @@ -2,20 +2,20 @@ package artnet import ( "net" - "sort" + "slices" "sync" "time" ) type Node struct { - IP net.IP - Port uint16 - MAC net.HardwareAddr - ShortName string - LongName string - Inputs []Universe - Outputs []Universe - LastSeen time.Time + IP net.IP + Port uint16 + MAC net.HardwareAddr + ShortName string + LongName string + Inputs []Universe + Outputs []Universe + LastSeen time.Time } type Discovery struct { @@ -180,16 +180,13 @@ func (d *Discovery) sendPollReplies(dst *net.UDPAddr, universes []Universe, isIn for k := range groups { keys = append(keys, k) } - sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] }) + slices.Sort(keys) for _, key := range keys { univs := groups[key] - sort.Slice(univs, func(i, j int) bool { return univs[i] < univs[j] }) + slices.Sort(univs) for i := 0; i < len(univs); i += 4 { - end := i + 4 - if end > len(univs) { - end = len(univs) - } + end := min(i+4, len(univs)) chunk := univs[i:end] pkt := BuildPollReplyPacket(d.localIP, d.localMAC, d.shortName, d.longName, chunk, isInput) d.receiver.SendTo(pkt, dst) @@ -204,11 +201,8 @@ func (d *Discovery) GetNodesForUniverse(universe Universe) []*Node { var result []*Node for _, node := range d.nodes { - for _, u := range node.Outputs { - if u == universe { - result = append(result, node) - break - } + if slices.Contains(node.Outputs, universe) { + result = append(result, node) } } return result @@ -226,10 +220,5 @@ func (d *Discovery) GetAllNodes() []*Node { } func containsUniverse(slice []Universe, val Universe) bool { - for _, v := range slice { - if v == val { - return true - } - } - return false + return slices.Contains(slice, val) } diff --git a/fuzz_test.go b/fuzz_test.go index 707f3f2..abb937c 100644 --- a/fuzz_test.go +++ b/fuzz_test.go @@ -77,20 +77,14 @@ func FuzzDMXRoundtrip(f *testing.F) { if dmx.Universe != universe { t.Fatalf("universe mismatch: sent %v, got %v", universe, dmx.Universe) } - expectedLen := len(dmxInput) - if expectedLen > 512 { - expectedLen = 512 - } + expectedLen := min(len(dmxInput), 512) if expectedLen%2 != 0 { expectedLen++ } if int(dmx.Length) != expectedLen { t.Fatalf("length mismatch: expected %d, got %d", expectedLen, dmx.Length) } - compareLen := len(dmxInput) - if compareLen > 512 { - compareLen = 512 - } + compareLen := min(len(dmxInput), 512) if !bytes.Equal(dmx.Data[:compareLen], dmxInput[:compareLen]) { t.Fatal("dmx data mismatch") } diff --git a/poller.go b/poller.go index 390a69c..889bb36 100644 --- a/poller.go +++ b/poller.go @@ -94,7 +94,7 @@ func InterfaceBroadcast(iface net.Interface) *net.UDPAddr { } bcast := make(net.IP, 4) - for i := 0; i < 4; i++ { + for i := range 4 { bcast[i] = ip4[i] | ^ipnet.Mask[i] } return &net.UDPAddr{IP: bcast, Port: Port} diff --git a/protocol.go b/protocol.go index 78bc54f..2c7f2ce 100644 --- a/protocol.go +++ b/protocol.go @@ -13,15 +13,15 @@ const ( Port = 6454 ProtocolVersion = 14 - OpPoll uint16 = 0x2000 - OpPollReply uint16 = 0x2100 - OpDmx uint16 = 0x5000 - OpSync uint16 = 0x5200 - OpAddress uint16 = 0x6000 - OpInput uint16 = 0x7000 - OpTodData uint16 = 0x8100 + OpPoll uint16 = 0x2000 + OpPollReply uint16 = 0x2100 + OpDmx uint16 = 0x5000 + OpSync uint16 = 0x5200 + OpAddress uint16 = 0x6000 + OpInput uint16 = 0x7000 + OpTodData uint16 = 0x8100 OpTodControl uint16 = 0x8200 - OpRdm uint16 = 0x8300 + OpRdm uint16 = 0x8300 PortTypeOutput uint8 = 0x80 PortTypeInput uint8 = 0x40 @@ -51,10 +51,10 @@ func NewUniverse(netVal, subnet, universe uint8) Universe { return Universe((uint16(netVal&0x7F) << 8) | (uint16(subnet&0x0F) << 4) | uint16(universe&0x0F)) } -func (u Universe) Net() uint8 { return uint8((u >> 8) & 0x7F) } -func (u Universe) SubNet() uint8 { return uint8((u >> 4) & 0x0F) } +func (u Universe) Net() uint8 { return uint8((u >> 8) & 0x7F) } +func (u Universe) SubNet() uint8 { return uint8((u >> 4) & 0x0F) } func (u Universe) Universe() uint8 { return uint8(u & 0x0F) } -func (u Universe) String() string { return fmt.Sprintf("%d.%d.%d", u.Net(), u.SubNet(), u.Universe()) } +func (u Universe) String() string { return fmt.Sprintf("%d.%d.%d", u.Net(), u.SubNet(), u.Universe()) } type DMXPacket struct { ProtocolVersion uint16 @@ -119,10 +119,7 @@ func (p *PollReplyPacket) GetLongName() string { } func (p *PollReplyPacket) NumPorts() int { - n := int(p.NumPortsLo) - if n > 4 { - n = 4 - } + n := min(int(p.NumPortsLo), 4) return n } @@ -148,7 +145,7 @@ func (p *PollReplyPacket) OutputUniverses() []Universe { return result } -func ParsePacket(data []byte) (uint16, interface{}, error) { +func ParsePacket(data []byte) (uint16, any, error) { if len(data) < 10 { return 0, nil, ErrPacketTooShort } @@ -187,10 +184,7 @@ func parseDMXPacket(data []byte) (*DMXPacket, error) { Length: binary.BigEndian.Uint16(data[16:18]), } - dataLen := int(pkt.Length) - if dataLen > 512 { - dataLen = 512 - } + dataLen := min(int(pkt.Length), 512) if len(data) >= 18+dataLen { copy(pkt.Data[:], data[18:18+dataLen]) } @@ -248,10 +242,7 @@ func parsePollReplyPacket(data []byte) (*PollReplyPacket, error) { } func BuildDMXPacket(universe Universe, sequence uint8, data []byte) []byte { - dataLen := len(data) - if dataLen > 512 { - dataLen = 512 - } + dataLen := min(len(data), 512) if dataLen%2 != 0 { dataLen++ } @@ -295,10 +286,7 @@ func BuildPollReplyPacket(ip [4]byte, mac [6]byte, shortName, longName string, u copy(buf[26:44], shortName) copy(buf[44:108], longName) - numPorts := len(universes) - if numPorts > 4 { - numPorts = 4 - } + numPorts := min(len(universes), 4) buf[173] = byte(numPorts) for i := 0; i < numPorts; i++ {