-
Notifications
You must be signed in to change notification settings - Fork 4
/
conn.go
80 lines (64 loc) · 1.46 KB
/
conn.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package xk6_dns
import (
"net"
"sync/atomic"
"time"
)
type k6UDPConn struct {
*net.UDPConn
rxBytes int64
txBytes int64
}
func NewK6UDPConn(addr string) (*k6UDPConn, error) {
raddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
c, err := net.DialUDP("udp", nil, raddr)
if err != nil {
return nil, err
}
return &k6UDPConn{UDPConn: c}, nil
}
func (k *k6UDPConn) GetRXBytes() int64 {
return k.rxBytes
}
func (k *k6UDPConn) GetTXBytes() int64 {
return k.txBytes
}
func (k *k6UDPConn) Read(b []byte) (int, error) {
n, err := k.UDPConn.Read(b)
if n > 0 {
atomic.AddInt64(&k.rxBytes, int64(n))
}
return n, err
}
func (k *k6UDPConn) Write(b []byte) (int, error) {
n, err := k.UDPConn.Write(b)
if n > 0 {
atomic.AddInt64(&k.txBytes, int64(n))
}
return n, err
}
func (k *k6UDPConn) ReadFrom(p []byte) (n int, addr net.Addr, err error) {
return k.UDPConn.ReadFrom(p)
}
func (k *k6UDPConn) WriteTo(p []byte, addr net.Addr) (n int, err error) {
return k.UDPConn.WriteTo(p, addr)
}
func (k *k6UDPConn) Close() error {
return k.UDPConn.Close()
}
func (k *k6UDPConn) LocalAddr() net.Addr {
return k.UDPConn.LocalAddr()
}
func (k *k6UDPConn) SetDeadline(t time.Time) error {
return k.UDPConn.SetDeadline(t)
}
func (k *k6UDPConn) SetReadDeadline(t time.Time) error {
return k.UDPConn.SetDeadline(t)
}
func (k *k6UDPConn) SetWriteDeadline(t time.Time) error {
return k.UDPConn.SetWriteDeadline(t)
}
var _ net.PacketConn = &k6UDPConn{}