-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpconn.go
48 lines (40 loc) · 960 Bytes
/
udpconn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
}