-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.go
136 lines (124 loc) · 2.6 KB
/
connection.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
package adbserver
import (
"dadb"
"fmt"
"io"
"net"
"strconv"
"strings"
)
type Connection struct {
address string
deviceQuery string
features map[string]struct{}
}
type stream struct {
io.Reader
io.Writer
io.Closer
}
func Connect(address string, deviceQuery string) (Connection, error) {
features, err := readFeatures(address, deviceQuery)
if err != nil {
return Connection{}, err
}
return Connection{
address: address,
deviceQuery: deviceQuery,
features: features,
}, nil
}
func (c Connection) Open(destination string) (dadb.Stream, error) {
conn, err := open(c.address, c.deviceQuery, destination)
if err != nil {
return nil, err
}
return stream{
Reader: conn,
Writer: conn,
Closer: conn,
}, nil
}
func (c Connection) SupportsFeature(feature string) bool {
_, ok := c.features[feature]
return ok
}
func readFeatures(address string, deviceQuery string) (map[string]struct{}, error) {
rw, err := open(address, deviceQuery, "host:features")
if err != nil {
return nil, err
}
bytes, err := io.ReadAll(rw)
if err != nil {
return nil, err
}
features := make(map[string]struct{})
for _, feature := range strings.Split(string(bytes), ",") {
features[feature] = struct{}{}
}
return features, nil
}
func open(address string, deviceQuery string, destination string) (net.Conn, error) {
// TODO: Ensure server is running
conn, err := net.Dial("tcp", address)
if err != nil {
return nil, err
}
err = send(conn, deviceQuery)
if err != nil {
return nil, err
}
err = send(conn, destination)
if err != nil {
return nil, err
}
return conn, nil
}
func send(rw io.ReadWriter, command string) error {
err := writeString(rw, command)
if err != nil {
return err
}
response, err := readStringLen(rw, 4)
if err != nil {
return err
}
if response != "OKAY" {
errMsg, err := readString(rw)
if err != nil {
return err
}
return fmt.Errorf("failed to send command (%s): %s", command, errMsg)
}
return nil
}
func writeString(w io.Writer, string string) error {
_, err := fmt.Fprintf(w, "%04x", len(string))
if err != nil {
return err
}
_, err = w.Write([]byte(string))
if err != nil {
return err
}
return nil
}
func readString(r io.Reader) (string, error) {
encodedLength, err := readStringLen(r, 4)
if err != nil {
return "", err
}
length, err := strconv.ParseInt(encodedLength, 16, 32)
if err != nil {
return "", err
}
return readStringLen(r, int(length))
}
func readStringLen(r io.Reader, len int) (string, error) {
bytes := make([]byte, len)
_, err := io.ReadFull(r, bytes)
if err != nil {
return "", err
}
return string(bytes), nil
}