-
Notifications
You must be signed in to change notification settings - Fork 38
/
transport.go
156 lines (136 loc) · 3.4 KB
/
transport.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
package gadb
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"strconv"
"time"
)
var ErrConnBroken = errors.New("socket connection broken")
var DefaultAdbReadTimeout time.Duration = 60 * time.Second
type transport struct {
sock net.Conn
readTimeout time.Duration
}
func newTransport(address string, readTimeout ...time.Duration) (tp transport, err error) {
if len(readTimeout) == 0 {
readTimeout = []time.Duration{DefaultAdbReadTimeout}
}
tp.readTimeout = readTimeout[0]
if tp.sock, err = net.Dial("tcp", address); err != nil {
err = fmt.Errorf("adb transport: %w", err)
}
return
}
func (t transport) Send(command string) (err error) {
msg := fmt.Sprintf("%04x%s", len(command), command)
debugLog(fmt.Sprintf("--> %s", command))
return _send(t.sock, []byte(msg))
}
func (t transport) VerifyResponse() (err error) {
var status string
if status, err = t.ReadStringN(4); err != nil {
return err
}
if status == "OKAY" {
debugLog(fmt.Sprintf("<-- %s", status))
return nil
}
var sError string
if sError, err = t.UnpackString(); err != nil {
return err
}
err = fmt.Errorf("command failed: %s", sError)
debugLog(fmt.Sprintf("<-- %s %s", status, sError))
return
}
func (t transport) ReadStringAll() (s string, err error) {
var raw []byte
raw, err = t.ReadBytesAll()
return string(raw), err
}
func (t transport) ReadBytesAll() (raw []byte, err error) {
raw, err = ioutil.ReadAll(t.sock)
debugLog(fmt.Sprintf("\r%s", raw))
return
}
func (t transport) UnpackString() (s string, err error) {
var raw []byte
raw, err = t.UnpackBytes()
return string(raw), err
}
func (t transport) UnpackBytes() (raw []byte, err error) {
var length string
if length, err = t.ReadStringN(4); err != nil {
return nil, err
}
var size int64
if size, err = strconv.ParseInt(length, 16, 64); err != nil {
return nil, err
}
raw, err = t.ReadBytesN(int(size))
debugLog(fmt.Sprintf("\r%s", raw))
return
}
func (t transport) ReadStringN(size int) (s string, err error) {
var raw []byte
if raw, err = t.ReadBytesN(size); err != nil {
return "", err
}
return string(raw), nil
}
func (t transport) ReadBytesN(size int) (raw []byte, err error) {
_ = t.sock.SetReadDeadline(time.Now().Add(t.readTimeout))
return _readN(t.sock, size)
}
func (t transport) Close() (err error) {
if t.sock == nil {
return nil
}
return t.sock.Close()
}
func (t transport) CreateSyncTransport() (sTp syncTransport, err error) {
if err = t.Send("sync:"); err != nil {
return syncTransport{}, err
}
if err = t.VerifyResponse(); err != nil {
return syncTransport{}, err
}
sTp = newSyncTransport(t.sock, t.readTimeout)
return
}
// CreateShellTransport returns a transport useful for the shell protocol.
func (t transport) CreateShellTransport() (sTp shellTransport, err error) {
sTp = newShellTransport(t.sock, t.readTimeout)
return
}
func _send(writer io.Writer, msg []byte) (err error) {
for totalSent := 0; totalSent < len(msg); {
var sent int
if sent, err = writer.Write(msg[totalSent:]); err != nil {
return err
}
if sent == 0 {
return ErrConnBroken
}
totalSent += sent
}
return
}
func _readN(reader io.Reader, size int) (raw []byte, err error) {
raw = make([]byte, 0, size)
for len(raw) < size {
buf := make([]byte, size-len(raw))
var n int
if n, err = io.ReadFull(reader, buf); err != nil {
return nil, err
}
if n == 0 {
return nil, ErrConnBroken
}
raw = append(raw, buf...)
}
return
}