mirror of
https://github.com/onyx-and-iris/vbantxt.git
synced 2024-11-21 17:30:49 +00:00
rename client struct to udpConn
This commit is contained in:
parent
c063feb919
commit
d72c6a2d17
@ -16,7 +16,7 @@ func WithRateLimit(ratelimit time.Duration) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithBPSOpt is a functional option to set the bps index for {VbanTx}.{Packet}.bpsIndex
|
// WithBPSOpt is a functional option to set the bps index for {VbanTxt}.packet
|
||||||
func WithBPSOpt(bps int) Option {
|
func WithBPSOpt(bps int) Option {
|
||||||
return func(vt *VbanTxt) {
|
return func(vt *VbanTxt) {
|
||||||
bpsIndex := indexOf(BpsOpts, bps)
|
bpsIndex := indexOf(BpsOpts, bps)
|
||||||
@ -28,7 +28,7 @@ func WithBPSOpt(bps int) Option {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithChannel is a functional option to set the bps index for {VbanTx}.{Packet}.channel
|
// WithChannel is a functional option to set the channel for {VbanTxt}.packet
|
||||||
func WithChannel(channel int) Option {
|
func WithChannel(channel int) Option {
|
||||||
return func(vt *VbanTxt) {
|
return func(vt *VbanTxt) {
|
||||||
vt.packet.channel = channel
|
vt.packet.channel = channel
|
||||||
|
@ -7,40 +7,40 @@ import (
|
|||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// client represents the UDP client
|
// udpConn represents the UDP client
|
||||||
type client struct {
|
type udpConn struct {
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewClient returns a UDP client
|
// newUDPConn returns a UDP client
|
||||||
func newClient(host string, port int) (client, error) {
|
func newUDPConn(host string, port int) (udpConn, error) {
|
||||||
udpAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", host, port))
|
udpAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", host, port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return client{}, err
|
return udpConn{}, err
|
||||||
}
|
}
|
||||||
conn, err := net.DialUDP("udp4", nil, udpAddr)
|
conn, err := net.DialUDP("udp4", nil, udpAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return client{}, err
|
return udpConn{}, err
|
||||||
}
|
}
|
||||||
log.Infof("Outgoing address %s", conn.RemoteAddr())
|
log.Infof("Outgoing address %s", conn.RemoteAddr())
|
||||||
|
|
||||||
return client{conn: conn}, nil
|
return udpConn{conn: conn}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write implements the io.WriteCloser interface
|
// Write implements the io.WriteCloser interface
|
||||||
func (c client) Write(buf []byte) (int, error) {
|
func (u udpConn) Write(buf []byte) (int, error) {
|
||||||
n, err := c.conn.Write(buf)
|
n, err := u.conn.Write(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
log.Debugf("Sending '%s' to: %s", string(buf), c.conn.RemoteAddr())
|
log.Debugf("Sending '%s' to: %s", string(buf), u.conn.RemoteAddr())
|
||||||
|
|
||||||
return n, nil
|
return n, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close implements the io.WriteCloser interface
|
// Close implements the io.WriteCloser interface
|
||||||
func (c client) Close() error {
|
func (u udpConn) Close() error {
|
||||||
err := c.conn.Close()
|
err := u.conn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
10
vbantxt.go
10
vbantxt.go
@ -8,7 +8,7 @@ import (
|
|||||||
|
|
||||||
// VbanTxt is used to send VBAN-TXT requests to a distant Voicemeeter/Matrix.
|
// VbanTxt is used to send VBAN-TXT requests to a distant Voicemeeter/Matrix.
|
||||||
type VbanTxt struct {
|
type VbanTxt struct {
|
||||||
client io.WriteCloser
|
udpConn io.WriteCloser
|
||||||
packet packet
|
packet packet
|
||||||
ratelimit time.Duration
|
ratelimit time.Duration
|
||||||
}
|
}
|
||||||
@ -16,13 +16,13 @@ type VbanTxt struct {
|
|||||||
// New constructs a fully formed VbanTxt instance. This is the package's entry point.
|
// New constructs a fully formed VbanTxt instance. This is the package's entry point.
|
||||||
// It sets default values for it's fields and then runs the option functions.
|
// It sets default values for it's fields and then runs the option functions.
|
||||||
func New(host string, port int, streamname string, options ...Option) (*VbanTxt, error) {
|
func New(host string, port int, streamname string, options ...Option) (*VbanTxt, error) {
|
||||||
client, err := newClient(host, port)
|
udpConn, err := newUDPConn(host, port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating UDP client for (%s:%d): %w", host, port, err)
|
return nil, fmt.Errorf("error creating UDP client for (%s:%d): %w", host, port, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
vt := &VbanTxt{
|
vt := &VbanTxt{
|
||||||
client: client,
|
udpConn: udpConn,
|
||||||
packet: newPacket(streamname),
|
packet: newPacket(streamname),
|
||||||
ratelimit: time.Duration(20) * time.Millisecond,
|
ratelimit: time.Duration(20) * time.Millisecond,
|
||||||
}
|
}
|
||||||
@ -37,7 +37,7 @@ func New(host string, port int, streamname string, options ...Option) (*VbanTxt,
|
|||||||
// Send is resonsible for firing each VBAN-TXT request.
|
// Send is resonsible for firing each VBAN-TXT request.
|
||||||
// It waits for {vt.ratelimit} time before returning.
|
// It waits for {vt.ratelimit} time before returning.
|
||||||
func (vt VbanTxt) Send(cmd string) error {
|
func (vt VbanTxt) Send(cmd string) error {
|
||||||
_, err := vt.client.Write(append(vt.packet.header(), []byte(cmd)...))
|
_, err := vt.udpConn.Write(append(vt.packet.header(), []byte(cmd)...))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error sending command (%s): %w", cmd, err)
|
return fmt.Errorf("error sending command (%s): %w", cmd, err)
|
||||||
}
|
}
|
||||||
@ -51,7 +51,7 @@ func (vt VbanTxt) Send(cmd string) error {
|
|||||||
|
|
||||||
// Close is responsible for closing the UDP Client connection
|
// Close is responsible for closing the UDP Client connection
|
||||||
func (vt VbanTxt) Close() error {
|
func (vt VbanTxt) Close() error {
|
||||||
err := vt.client.Close()
|
err := vt.udpConn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error attempting to close UDP Client: %w", err)
|
return fmt.Errorf("error attempting to close UDP Client: %w", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user