-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtcp.go
52 lines (41 loc) · 829 Bytes
/
tcp.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
package tcp
import (
"net"
"go.k6.io/k6/js/modules"
)
func init() {
modules.Register("k6/x/tcp", new(TCP))
}
type TCP struct{}
func (tcp *TCP) Connect(addr string) (net.Conn, error) {
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
return conn, nil
}
func (tcp *TCP) Write(conn net.Conn, data []byte) error {
_, err := conn.Write(data)
if err != nil {
return err
}
return nil
}
func (tcp *TCP) Read(conn net.Conn, size int) ([]byte, error) {
buf := make([]byte, size)
_, err := conn.Read(buf)
if err != nil {
return nil, err
}
return buf, nil
}
func (tcp *TCP) WriteLn(conn net.Conn, data []byte) error {
return tcp.Write(conn, append(data, []byte("\n")...))
}
func (tcp *TCP) Close(conn net.Conn) error {
err := conn.Close()
if err != nil {
return err
}
return nil
}