forked from willy77/go-pigpio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.go
More file actions
110 lines (91 loc) · 2.4 KB
/
Copy pathsocket.go
File metadata and controls
110 lines (91 loc) · 2.4 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package pigpio
import (
"bufio"
"fmt"
"net"
"sync"
)
type Socket struct {
Connection net.Conn
writer *bufio.Writer
reader *bufio.Reader
address *net.TCPAddr
host string
port int
rwMutex sync.Mutex
}
func (s *Socket) Address() *net.TCPAddr { return s.address }
func (s *Socket) Host() string { return s.host }
func (s *Socket) Port() int { return s.port }
// NewSocket
//
// Create a new socket connection to host:port
func NewSocket(host string, port int) (*Socket, error) {
var e error
var tcpAddr *net.TCPAddr
var con net.Conn
hostAddr := fmt.Sprintf("%s:%d", host, port)
tcpAddr, e = net.ResolveTCPAddr("tcp", hostAddr)
if e != nil {
return nil, newPiError(1, e, "Error resolve address")
}
con, e = net.DialTCP("tcp", nil, tcpAddr)
if e != nil {
return nil, newPiError(2, e, "Error connect to host")
}
return &Socket{
host: host,
port: port,
address: tcpAddr,
writer: bufio.NewWriter(con),
reader: bufio.NewReader(con),
Connection: con}, nil
}
func (s *Socket) Close() error {
if s.Connection != nil {
s.rwMutex.Lock()
defer s.rwMutex.Unlock()
return s.Connection.Close()
}
return nil
}
func (s *Socket) Read(len int) ([]byte, error) {
buf := make([]byte, len)
s.rwMutex.Lock()
r, e := s.reader.Read(buf)
s.rwMutex.Unlock()
if e != nil || r != len {
return nil, newPiError(4, e, "cmdGR(%d)", len)
}
return buf, nil
}
func (s *Socket) SendCommand(command Command, p1 int, p2 int, extData []byte) (int, error) {
bufSize := 16
if extData != nil {
bufSize += len(extData)
}
buf := make([]byte, 0)
buf = append(buf, convertToBytes(int(command), p1, p2)[:]...)
if extData != nil && len(extData) > 0 {
buf = append(buf, convertToBytes(len(extData))[:]...)
buf = append(buf, extData[:]...)
} else {
buf = append(buf, convertToBytes(0)[:]...)
}
s.rwMutex.Lock()
w, e := s.writer.Write(buf)
fe := s.writer.Flush()
s.rwMutex.Unlock()
if e != nil || w != len(buf) {
return -1, newPiError(3, e, "SendCommand(%d, %d, %d, %v)", int(command), p1, p2, extData)
}
if fe != nil {
return -1, newPiError(3, fe, "SendCommand.Flush(%d, %d, %d, %v)", int(command), p1, p2, extData)
}
response := make([]byte, 16)
_, e = s.reader.Read(response)
if e != nil {
return -1, newPiError(5, e, "SendCommand.Read(%d, %d, %d, %v)", int(command), p1, p2, extData)
}
return convertToInt32(response[12:]), nil
}