add udp-client

This commit is contained in:
Joel Wetzell
2025-11-19 21:42:43 -06:00
parent 0e903eba2f
commit c6fbf3e427
2 changed files with 92 additions and 7 deletions

85
udp-client.go Normal file
View File

@@ -0,0 +1,85 @@
package showbridge
import (
"context"
"fmt"
"log/slog"
"net"
)
type UDPClient struct {
config ModuleConfig
Host string
Port uint16
conn net.Conn
router *Router
}
func init() {
RegisterModule(ModuleRegistration{
Type: "net.udp.client",
New: func(config ModuleConfig) (Module, error) {
params := config.Params
host, ok := params["host"]
if !ok {
return nil, fmt.Errorf("udp client requires a host parameter")
}
hostString, ok := host.(string)
if !ok {
return nil, fmt.Errorf("udp client host must be uint16")
}
port, ok := params["port"]
if !ok {
return nil, fmt.Errorf("udp client requires a port parameter")
}
portNum, ok := port.(float64)
if !ok {
return nil, fmt.Errorf("udp client port must be uint16")
}
return &UDPClient{Host: hostString, Port: uint16(portNum), config: config}, nil
},
})
}
func (uc *UDPClient) Id() string {
return uc.config.Id
}
func (uc *UDPClient) Type() string {
return uc.config.Type
}
func (uc *UDPClient) RegisterRouter(router *Router) {
slog.Debug("registering router", "id", uc.config.Id)
uc.router = router
}
func (uc *UDPClient) Run(ctx context.Context) error {
client, err := net.Dial("udp", fmt.Sprintf(":%d", uc.Port))
if err != nil {
return err
}
uc.conn = client
<-ctx.Done()
return nil
}
func (uc *UDPClient) Output(payload any) error {
if uc.conn != nil {
payloadBytes, ok := payload.([]byte)
if !ok {
return fmt.Errorf("udp-client is only able to output bytes")
}
_, err := uc.conn.Write(payloadBytes)
return err
}
return nil
}