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