From 65cfb86ac50971cd7c164ee5a567f198bebacccc Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Fri, 23 Jun 2023 11:56:34 +0800 Subject: [PATCH] refactor: add SendWithCheck for adb transport --- hrp/pkg/gadb/client.go | 5 +---- hrp/pkg/gadb/device.go | 18 +++++------------- hrp/pkg/gadb/sync_transport.go | 1 + hrp/pkg/gadb/transport.go | 17 ++++++++++++----- 4 files changed, 19 insertions(+), 22 deletions(-) diff --git a/hrp/pkg/gadb/client.go b/hrp/pkg/gadb/client.go index 79e69d91..1c867dd1 100644 --- a/hrp/pkg/gadb/client.go +++ b/hrp/pkg/gadb/client.go @@ -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 } diff --git a/hrp/pkg/gadb/device.go b/hrp/pkg/gadb/device.go index 58175669..515553b1 100644 --- a/hrp/pkg/gadb/device.go +++ b/hrp/pkg/gadb/device.go @@ -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 diff --git a/hrp/pkg/gadb/sync_transport.go b/hrp/pkg/gadb/sync_transport.go index ff7346a4..0a37bc29 100644 --- a/hrp/pkg/gadb/sync_transport.go +++ b/hrp/pkg/gadb/sync_transport.go @@ -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} } diff --git a/hrp/pkg/gadb/transport.go b/hrp/pkg/gadb/transport.go index d9891069..f667dd4a 100644 --- a/hrp/pkg/gadb/transport.go +++ b/hrp/pkg/gadb/transport.go @@ -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)