refactor: add SendWithCheck for adb transport

This commit is contained in:
lilong.129
2023-06-23 11:56:34 +08:00
parent 10b4612afa
commit 65cfb86ac5
4 changed files with 19 additions and 22 deletions

View File

@@ -221,10 +221,7 @@ func (c Client) executeCommand(command string, onlyVerifyResponse ...bool) (resp
}
defer func() { _ = tp.Close() }()
if err = tp.Send(command); err != nil {
return "", err
}
if err = tp.VerifyResponse(); err != nil {
if err = tp.SendWithCheck(command); err != nil {
return "", err
}

View File

@@ -357,10 +357,7 @@ func (d *Device) createDeviceTransport() (tp transport, err error) {
return transport{}, err
}
if err = tp.Send(fmt.Sprintf("host:transport:%s", d.serial)); err != nil {
return transport{}, err
}
err = tp.VerifyResponse()
err = tp.SendWithCheck(fmt.Sprintf("host:transport:%s", d.serial))
return
}
@@ -400,11 +397,7 @@ func (d *Device) executeCommand(command string, onlyVerifyResponse ...bool) (raw
}
defer func() { _ = tp.Close() }()
if err = tp.Send(command); err != nil {
return nil, err
}
if err = tp.VerifyResponse(); err != nil {
if err = tp.SendWithCheck(command); err != nil {
return nil, err
}
@@ -528,13 +521,12 @@ func (d *Device) installViaABBExec(apk io.ReadSeeker) (raw []byte, err error) {
return nil, err
}
defer func() { _ = tp.Close() }()
if err = tp.Send(fmt.Sprintf("abb_exec:package\x00install\x00-t\x00-S\x00%d", filesize)); err != nil {
cmd := fmt.Sprintf("abb_exec:package\x00install\x00-t\x00-S\x00%d", filesize)
if err = tp.SendWithCheck(cmd); err != nil {
return nil, err
}
if err = tp.VerifyResponse(); err != nil {
return nil, err
}
_, err = apk.Seek(0, io.SeekStart)
if err != nil {
return nil, err

View File

@@ -17,6 +17,7 @@ type syncTransport struct {
readTimeout time.Duration
}
// newSyncTransport creates a new sync transport with existed tcp socket connection
func newSyncTransport(sock net.Conn, readTimeout time.Duration) syncTransport {
return syncTransport{sock: sock, readTimeout: readTimeout}
}

View File

@@ -27,11 +27,14 @@ type transport struct {
readTimeout time.Duration
}
// newTransport creates a new tcp socket connection
func newTransport(address string, readTimeout ...time.Duration) (tp transport, err error) {
if len(readTimeout) == 0 {
readTimeout = []time.Duration{DefaultAdbReadTimeout}
}
tp.readTimeout = readTimeout[0]
tp = transport{
readTimeout: readTimeout[0],
}
tp.sock, err = net.Dial("tcp", address)
if err == nil {
// dial success
@@ -139,11 +142,15 @@ func (t transport) Close() (err error) {
return t.sock.Close()
}
func (t transport) CreateSyncTransport() (sTp syncTransport, err error) {
if err = t.Send("sync:"); err != nil {
return syncTransport{}, err
func (t transport) SendWithCheck(command string) (err error) {
if err = t.Send(command); err != nil {
return err
}
if err = t.VerifyResponse(); err != nil {
return t.VerifyResponse()
}
func (t transport) CreateSyncTransport() (sTp syncTransport, err error) {
if err = t.SendWithCheck("sync:"); err != nil {
return syncTransport{}, err
}
sTp = newSyncTransport(t.sock, t.readTimeout)