mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add udp-client
This commit is contained in:
85
udp-client.go
Normal file
85
udp-client.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user