mirror of
https://github.com/onyx-and-iris/vbantxt.git
synced 2026-04-07 01:33:30 +00:00
rename client struct to udpConn
This commit is contained in:
48
udpconn.go
Normal file
48
udpconn.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package vbantxt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// udpConn represents the UDP client
|
||||
type udpConn struct {
|
||||
conn *net.UDPConn
|
||||
}
|
||||
|
||||
// newUDPConn returns a UDP client
|
||||
func newUDPConn(host string, port int) (udpConn, error) {
|
||||
udpAddr, err := net.ResolveUDPAddr("udp4", fmt.Sprintf("%s:%d", host, port))
|
||||
if err != nil {
|
||||
return udpConn{}, err
|
||||
}
|
||||
conn, err := net.DialUDP("udp4", nil, udpAddr)
|
||||
if err != nil {
|
||||
return udpConn{}, err
|
||||
}
|
||||
log.Infof("Outgoing address %s", conn.RemoteAddr())
|
||||
|
||||
return udpConn{conn: conn}, nil
|
||||
}
|
||||
|
||||
// Write implements the io.WriteCloser interface
|
||||
func (u udpConn) Write(buf []byte) (int, error) {
|
||||
n, err := u.conn.Write(buf)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
log.Debugf("Sending '%s' to: %s", string(buf), u.conn.RemoteAddr())
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
// Close implements the io.WriteCloser interface
|
||||
func (u udpConn) Close() error {
|
||||
err := u.conn.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user