we no longer need to pass in the kind

remove option function WithKind
This commit is contained in:
onyx-and-iris 2026-02-07 13:58:37 +00:00
parent 8a452c83b9
commit 3c47d12719
7 changed files with 47 additions and 56 deletions

View File

@ -121,7 +121,6 @@ func connect(config Config) (*xair.X32Client, error) {
client, err := xair.NewX32Client( client, err := xair.NewX32Client(
config.Host, config.Host,
config.Port, config.Port,
xair.WithKind("x32"),
xair.WithTimeout(config.Timeout), xair.WithTimeout(config.Timeout),
) )
if err != nil { if err != nil {

View File

@ -119,7 +119,6 @@ func connect(config Config) (*xair.XAirClient, error) {
client, err := xair.NewXAirClient( client, err := xair.NewXAirClient(
config.Host, config.Host,
config.Port, config.Port,
xair.WithKind("xair"),
xair.WithTimeout(config.Timeout), xair.WithTimeout(config.Timeout),
) )
if err != nil { if err != nil {

View File

@ -18,9 +18,9 @@ var x32AddressMap = map[string]string{
"snapshot": "/-snap", "snapshot": "/-snap",
} }
func addressMapForMixerKind(kind MixerKind) map[string]string { func addressMapFromMixerKind(kind mixerKind) map[string]string {
switch kind { switch kind {
case KindX32: case kindX32:
return x32AddressMap return x32AddressMap
default: default:
return xairAddressMap return xairAddressMap

View File

@ -2,7 +2,6 @@ package xair
import ( import (
"fmt" "fmt"
"net"
"time" "time"
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
@ -11,7 +10,7 @@ import (
) )
type Client struct { type Client struct {
engine *engine
} }
type XAirClient struct { type XAirClient struct {
@ -34,50 +33,15 @@ type X32Client struct {
Snapshot *Snapshot Snapshot *Snapshot
} }
func createEngine(mixerIP string, mixerPort int, opts ...Option) (*engine, error) {
localAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", 0))
if err != nil {
return nil, fmt.Errorf("failed to resolve local address: %v", err)
}
conn, err := net.ListenUDP("udp", localAddr)
if err != nil {
return nil, fmt.Errorf("failed to create UDP connection: %v", err)
}
mixerAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", mixerIP, mixerPort))
if err != nil {
conn.Close()
return nil, fmt.Errorf("failed to resolve mixer address: %v", err)
}
log.Debugf("Local UDP connection: %s ", conn.LocalAddr().String())
e := &engine{
timeout: 100 * time.Millisecond,
conn: conn,
mixerAddr: mixerAddr,
parser: newParser(),
done: make(chan bool),
respChan: make(chan *osc.Message, 100),
}
for _, opt := range opts {
opt(e)
}
return e, nil
}
// NewX32Client creates a new X32Client instance // NewX32Client creates a new X32Client instance
func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, error) { func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, error) {
e, err := createEngine(mixerIP, mixerPort, opts...) e, err := newEngine(mixerIP, mixerPort, kindX32, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
c := &X32Client{ c := &X32Client{
Client: Client{*e}, Client: Client{e},
} }
c.Main = newMainStereo(&c.Client) c.Main = newMainStereo(&c.Client)
c.MainMono = newMainMono(&c.Client) c.MainMono = newMainMono(&c.Client)
@ -92,13 +56,13 @@ func NewX32Client(mixerIP string, mixerPort int, opts ...Option) (*X32Client, er
// NewXAirClient creates a new XAirClient instance // NewXAirClient creates a new XAirClient instance
func NewXAirClient(mixerIP string, mixerPort int, opts ...Option) (*XAirClient, error) { func NewXAirClient(mixerIP string, mixerPort int, opts ...Option) (*XAirClient, error) {
e, err := createEngine(mixerIP, mixerPort, opts...) e, err := newEngine(mixerIP, mixerPort, kindXAir, opts...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
c := &XAirClient{ c := &XAirClient{
Client: Client{*e}, Client: Client{e},
} }
c.Main = newMainStereo(&c.Client) c.Main = newMainStereo(&c.Client)
c.Strip = newStrip(&c.Client) c.Strip = newStrip(&c.Client)

View File

@ -14,7 +14,7 @@ type parser interface {
} }
type engine struct { type engine struct {
Kind MixerKind Kind mixerKind
timeout time.Duration timeout time.Duration
conn *net.UDPConn conn *net.UDPConn
mixerAddr *net.UDPAddr mixerAddr *net.UDPAddr
@ -26,6 +26,42 @@ type engine struct {
respChan chan *osc.Message respChan chan *osc.Message
} }
func newEngine(mixerIP string, mixerPort int, kind mixerKind, opts ...Option) (*engine, error) {
localAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%d", 0))
if err != nil {
return nil, fmt.Errorf("failed to resolve local address: %v", err)
}
conn, err := net.ListenUDP("udp", localAddr)
if err != nil {
return nil, fmt.Errorf("failed to create UDP connection: %v", err)
}
mixerAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", mixerIP, mixerPort))
if err != nil {
conn.Close()
return nil, fmt.Errorf("failed to resolve mixer address: %v", err)
}
log.Debugf("Local UDP connection: %s ", conn.LocalAddr().String())
e := &engine{
timeout: 100 * time.Millisecond,
conn: conn,
mixerAddr: mixerAddr,
parser: newParser(),
addressMap: addressMapFromMixerKind(kind),
done: make(chan bool),
respChan: make(chan *osc.Message, 100),
}
for _, opt := range opts {
opt(e)
}
return e, nil
}
// receiveLoop handles incoming OSC messages // receiveLoop handles incoming OSC messages
func (e *engine) receiveLoop() { func (e *engine) receiveLoop() {
buffer := make([]byte, 4096) buffer := make([]byte, 4096)

View File

@ -1,8 +1,8 @@
package xair package xair
type MixerKind string type mixerKind string
const ( const (
KindXAir MixerKind = "xair" kindXAir mixerKind = "xair"
KindX32 MixerKind = "x32" kindX32 mixerKind = "x32"
) )

View File

@ -4,13 +4,6 @@ import "time"
type Option func(*engine) type Option func(*engine)
func WithKind(kind string) Option {
return func(e *engine) {
e.Kind = MixerKind(kind)
e.addressMap = addressMapForMixerKind(e.Kind)
}
}
func WithTimeout(timeout time.Duration) Option { func WithTimeout(timeout time.Duration) Option {
return func(e *engine) { return func(e *engine) {
e.timeout = timeout e.timeout = timeout