-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (48 loc) · 1.11 KB
/
main.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
package udp
import (
"net"
"time"
"go.k6.io/k6/js/modules"
)
func init() {
modules.Register("k6/x/udp", new(UDP))
}
type UDP struct{}
func (udp *UDP) Connect(addr string) (net.Conn, error) {
conn, err := net.Dial("udp", addr)
if err != nil {
return nil, err
}
return conn, nil
}
func (udp *UDP) Write(conn net.Conn, data []byte, writeTimeout time.Duration) error {
if err := conn.SetWriteDeadline(time.Now().Add(writeTimeout * time.Second)); err != nil {
return err
}
_, err := conn.Write(data)
if err != nil {
return err
}
return nil
}
func (udp *UDP) Read(conn net.Conn, size int, readTimeout time.Duration) ([]byte, error) {
if err := conn.SetReadDeadline(time.Now().Add(60 * time.Second)); err != nil {
return nil, err
}
buf := make([]byte, size)
_, err := conn.Read(buf)
if err != nil {
return nil, err
}
return buf, nil
}
func (udp *UDP) WriteLn(conn net.Conn, data []byte, writeTimeout time.Duration) error {
return udp.Write(conn, append(data, '\n'), writeTimeout)
}
func (udp *UDP) Close(conn net.Conn) error {
err := conn.Close()
if err != nil {
return err
}
return nil
}