mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
add udp-client
This commit is contained in:
14
config.json
14
config.json
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"modules": [
|
"modules": [
|
||||||
{
|
{
|
||||||
"id": "tcp1",
|
"id": "tcp",
|
||||||
"type": "net.tcp.server",
|
"type": "net.tcp.server",
|
||||||
"params": {
|
"params": {
|
||||||
"port": 8000,
|
"port": 8000,
|
||||||
@@ -9,18 +9,18 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "tcp2",
|
"id": "udp",
|
||||||
"type": "net.tcp.server",
|
"type": "net.udp.client",
|
||||||
"params": {
|
"params": {
|
||||||
"port": 8001,
|
"host": "127.0.0.1",
|
||||||
"framing": "LF"
|
"port": 8888
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"routes": [
|
"routes": [
|
||||||
{
|
{
|
||||||
"input": "tcp1",
|
"input": "tcp",
|
||||||
"output": "tcp2"
|
"output": "udp"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
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