mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
reuse the same UDP client for every output
This commit is contained in:
@@ -8,11 +8,10 @@ import (
|
|||||||
|
|
||||||
type UDPClient struct {
|
type UDPClient struct {
|
||||||
config ModuleConfig
|
config ModuleConfig
|
||||||
Host string
|
Addr *net.UDPAddr
|
||||||
Port uint16
|
Port uint16
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
router *Router
|
router *Router
|
||||||
addr *net.UDPAddr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -43,7 +42,12 @@ func init() {
|
|||||||
return nil, fmt.Errorf("net.udp.client port must be a number")
|
return nil, fmt.Errorf("net.udp.client port must be a number")
|
||||||
}
|
}
|
||||||
|
|
||||||
return &UDPClient{Host: hostString, Port: uint16(portNum), config: config}, nil
|
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", hostString, uint16(portNum)))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &UDPClient{Addr: addr, config: config}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -62,12 +66,12 @@ func (uc *UDPClient) RegisterRouter(router *Router) {
|
|||||||
|
|
||||||
func (uc *UDPClient) Run() error {
|
func (uc *UDPClient) Run() error {
|
||||||
|
|
||||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", uc.Host, uc.Port))
|
client, err := net.DialUDP("udp", nil, uc.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
uc.addr = addr
|
uc.conn = client
|
||||||
|
|
||||||
<-uc.router.Context.Done()
|
<-uc.router.Context.Done()
|
||||||
slog.Debug("router context done in module", "id", uc.config.Id)
|
slog.Debug("router context done in module", "id", uc.config.Id)
|
||||||
@@ -83,19 +87,14 @@ func (uc *UDPClient) Output(payload any) error {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("net.udp.client is only able to output bytes")
|
return fmt.Errorf("net.udp.client is only able to output bytes")
|
||||||
}
|
}
|
||||||
|
if uc.conn != nil {
|
||||||
|
_, err := uc.conn.Write(payloadBytes)
|
||||||
|
|
||||||
// TODO(jwetzell): reuse connection or setup new one when necessary
|
|
||||||
client, err := net.DialUDP("udp", nil, uc.addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
uc.conn = client
|
return fmt.Errorf("net.udp.client client is not setup")
|
||||||
|
|
||||||
_, err = uc.conn.Write(payloadBytes)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user