Skip to content

Commit

Permalink
Adds the ability to set the listen-on address
Browse files Browse the repository at this point in the history
Adds raw access to the node's underlying socket

Signed-off-by: Brian Murray <brian@bmurray.ca>
  • Loading branch information
bmurray committed Feb 1, 2024
1 parent 797445e commit 2e849cf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
20 changes: 15 additions & 5 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ type Node struct {
broadcastAddr net.UDPAddr

// conn is the UDP connection this node will listen on
conn *net.UDPConn
localAddr net.UDPAddr
sendCh chan netPayload
recvCh chan netPayload
conn *net.UDPConn
localAddr net.UDPAddr
listenAddr net.UDPAddr
sendCh chan netPayload
recvCh chan netPayload

// shutdownCh will be closed on shutdown of the node
shutdownCh chan struct{}
Expand Down Expand Up @@ -57,6 +58,7 @@ func NewNode(name string, style code.StyleCode, ip net.IP, log Logger, opts ...N
Name: name,
Type: style,
},
listenAddr: net.UDPAddr{Port: packet.ArtNetPort},
broadcastAddr: defaultBroadcastAddr,
conn: nil,
shutdown: true,
Expand All @@ -67,6 +69,10 @@ func NewNode(name string, style code.StyleCode, ip net.IP, log Logger, opts ...N
n.SetOption(opt)
}

for _, opt := range opts {
n.SetOption(opt)
}

// initialize required node callbacks
n.callbacks = map[code.OpCode]NodeCallbackFn{
code.OpPoll: n.handlePacketPoll,
Expand All @@ -87,6 +93,10 @@ func NewNode(name string, style code.StyleCode, ip net.IP, log Logger, opts ...N
return n
}

func (n *Node) Connection() *net.UDPConn {
return n.conn
}

// Stop will stop all running routines and close the network connection
func (n *Node) Stop() {
n.shutdownLock.Lock()
Expand Down Expand Up @@ -117,7 +127,7 @@ func (n *Node) Start() error {
n.shutdownCh = make(chan struct{})
n.shutdown = false

c, err := net.ListenPacket("udp4", fmt.Sprintf(":%d", packet.ArtNetPort))
c, err := net.ListenPacket("udp4", n.listenAddr.String())
if err != nil {
n.shutdownErr = fmt.Errorf("error net.ListenPacket: %s", err)
n.log.With(Fields{"error": err}).Error("error net.ListenPacket")
Expand Down
36 changes: 35 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package artnet

import "net"
import (
"net"

"github.com/jsimonetti/go-artnet/packet"
)

// Option is a functional option handler for Controller.
type Option func(*Controller) error
Expand All @@ -26,6 +30,20 @@ func BroadcastAddr(addr net.UDPAddr) Option {
}
}

// ListenAddr sets the listen address and port to use; defaults to :6454 if unset
func ListenAddress(addr net.UDPAddr) Option {
return func(c *Controller) error {
return NodeListenAddress(addr)(c.cNode)
}
}

// ListenIP sets the listen IP to use; defaults to :6454 if unset
func ListenIP(ip net.IP) Option {
return func(c *Controller) error {
return NodeListenIP(ip)(c.cNode)
}
}

// NodeOption is a functional option handler for Node.
type NodeOption func(*Node) error

Expand All @@ -41,3 +59,19 @@ func NodeBroadcastAddress(addr net.UDPAddr) NodeOption {
return nil
}
}

// NodeListenAddress sets the listen address and port to use; defaults to :6454 if unset
func NodeListenAddress(addr net.UDPAddr) NodeOption {
return func(n *Node) error {
n.listenAddr = addr
return nil
}
}

// NodeListenIP sets the listen IP to use; defaults to :6454 if unset
func NodeListenIP(ip net.IP) NodeOption {
return func(n *Node) error {
n.listenAddr = net.UDPAddr{IP: ip, Port: packet.ArtNetPort}
return nil
}
}

0 comments on commit 2e849cf

Please sign in to comment.