forked from zach-klippenstein/goadb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
174 lines (157 loc) · 3.53 KB
/
io.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package adb
import (
"encoding/binary"
"fmt"
"io"
"strconv"
"time"
"github.com/pkg/errors"
)
const (
statusOK = "OKAY"
statusFail = "FAIL"
)
// use this only for short writes
func sendMessage(w io.Writer, msg string) error {
n, err := w.Write([]byte(fmt.Sprintf("%04x%s", len(msg), msg)))
if err != nil {
return err
}
if n != len(msg)+4 {
return io.ErrShortWrite
}
return nil
}
func sendSyncMessage(w io.Writer, status, msg string) error {
if len(msg) > syncMaxChunkSize {
return errors.New("maximum message length exceded")
}
buf := make([]byte, len(status)+4+len(msg))
buf = append(buf, status...)
binary.LittleEndian.PutUint32(buf, uint32(len(msg)))
buf = append(buf, msg...)
n, err := w.Write(buf)
if err != nil {
return err
}
if n != len(msg)+4 {
return io.ErrShortWrite
}
return nil
}
// wantStatus errors when the connection responds with a different status
// than status. If 'FAIL' is reportet the returned error will carry the
// error given by the server.
// When acceptStatus is left empty defaults to statusOK
func wantStatus(r io.Reader, acceptStatus ...string) error {
buf := make([]byte, 4)
_, err := io.ReadFull(r, buf)
if err != nil {
return err
}
s := string(buf)
if s == statusFail {
_, err = io.ReadFull(r, buf)
if err != nil {
return err
}
length, err := strconv.ParseUint(string(buf), 16, 16)
if err != nil {
return errors.Wrapf(err, "imparseable length: %s", buf)
}
errMsg := make([]byte, length)
_, err = io.ReadFull(r, errMsg)
if err != nil {
return err
}
return errors.New(string(errMsg))
}
if len(acceptStatus) == 0 {
if s != statusOK {
return &UnexpectedStatusError{acceptStatus, s}
}
} else {
success := false
for _, status := range acceptStatus {
if s == status {
success = true
break
}
}
if !success {
return &UnexpectedStatusError{acceptStatus, s}
}
}
return nil
}
// readString reads a message from r and returns it as a string.
// It will report an error for any status code that is not listed
// in acceptStatus or if none supplied 'OKAY'.
func readBytes(r io.Reader, acceptStatus ...string) ([]byte, error) {
head := make([]byte, 8)
_, err := io.ReadFull(r, head)
if err != nil {
return nil, err
}
status := string(head[:4])
length, err := strconv.ParseUint(string(head[4:]), 16, 16)
if err != nil {
return nil, errors.Wrapf(err, "length could not be parsed %s", head[4:])
}
if status == statusFail {
buf := make([]byte, length)
_, err := io.ReadFull(r, buf)
if err != nil {
return nil, err
}
return nil, errors.New(string(buf))
}
if len(acceptStatus) == 0 {
if status != statusOK {
return nil, &UnexpectedStatusError{acceptStatus, status}
}
} else {
ok := false
for _, s := range acceptStatus {
if s == status {
ok = true
break
}
}
if !ok {
return nil, &UnexpectedStatusError{acceptStatus, status}
}
}
buf := make([]byte, length)
_, err = io.ReadFull(r, buf)
if err != nil {
return nil, err
}
return buf, nil
}
func requestResponseBytes(address, msg string) ([]byte, error) {
conn, err := dial(address)
if err != nil {
return nil, err
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(10 * time.Second))
err = sendMessage(conn, msg)
if err != nil {
return nil, err
}
return readBytes(conn)
}
func send(address, msg string) error {
conn, err := dial(address)
if err != nil {
return err
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(10 * time.Second))
err = sendMessage(conn, msg)
if err != nil {
return err
}
return wantStatus(conn)
}