-
Notifications
You must be signed in to change notification settings - Fork 2
/
socket.go
50 lines (39 loc) · 760 Bytes
/
socket.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
package wpaclient
import (
"fmt"
"io"
"net"
)
type socket struct {
c net.Conn
f string
}
func (s *socket) send(b []byte) error {
n, err := s.c.Write(b)
if err != nil {
return fmt.Errorf("write to socket failed: %w", err)
}
if n != len(b) {
return io.ErrShortWrite
}
return nil
}
func (s *socket) receive() ([]byte, error) {
b := make([]byte, 4095)
n, err := s.c.Read(b[:])
if err != nil {
return nil, fmt.Errorf("read from socket failed: %w", err)
}
return b[:n], nil
}
func (s *socket) execute(cmd []byte) ([]byte, error) {
err := s.send(cmd)
if err != nil {
return nil, fmt.Errorf("send failed: %w", err)
}
buf, err := s.receive()
if err != nil {
return nil, fmt.Errorf("read failed: %w", err)
}
return buf, nil
}