From f6c6a232b5867c81c78269f5fcfbf6e903f43c4a Mon Sep 17 00:00:00 2001 From: electricbubble Date: Sun, 18 Apr 2021 00:18:39 +0800 Subject: [PATCH] feat: [NewConnect] add timeout setting (Zero means no limit. The default timeout is 30s) --- device.go | 4 ++-- idevice.go | 2 +- pkg/libimobiledevice/usbmux.go | 29 ++++++++++------------------- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/device.go b/device.go index 3cf6035..28df8ed 100644 --- a/device.go +++ b/device.go @@ -42,8 +42,8 @@ func (d *device) Properties() DeviceProperties { return *d.properties } -func (d *device) NewConnect(port int) (InnerConn, error) { - newClient, err := libimobiledevice.NewUsbmuxClient() +func (d *device) NewConnect(port int, timeout ...time.Duration) (InnerConn, error) { + newClient, err := libimobiledevice.NewUsbmuxClient(timeout...) if err != nil { return nil, err } diff --git a/idevice.go b/idevice.go index bbd7d77..331324a 100644 --- a/idevice.go +++ b/idevice.go @@ -19,7 +19,7 @@ type Usbmux interface { type Device interface { Properties() DeviceProperties - NewConnect(port int) (InnerConn, error) + NewConnect(port int, timeout ...time.Duration) (InnerConn, error) ReadPairRecord() (pairRecord *PairRecord, err error) SavePairRecord(pairRecord *PairRecord) (err error) DeletePairRecord() (err error) diff --git a/pkg/libimobiledevice/usbmux.go b/pkg/libimobiledevice/usbmux.go index 0ccf5aa..06b32a1 100644 --- a/pkg/libimobiledevice/usbmux.go +++ b/pkg/libimobiledevice/usbmux.go @@ -112,18 +112,17 @@ type DeviceProperties struct { NetworkAddress []byte `plist:"NetworkAddress"` } -func NewUsbmuxClient(opts ...InnerConnOption) (c *UsbmuxClient, err error) { +func NewUsbmuxClient(timeout ...time.Duration) (c *UsbmuxClient, err error) { + if len(timeout) == 0 { + timeout = []time.Duration{DefaultDeadlineTimeout} + } c = &UsbmuxClient{version: ProtoVersionPlist} var conn net.Conn - if conn, err = rawDial(); err != nil { + if conn, err = rawDial(timeout[0]); err != nil { return nil, fmt.Errorf("usbmux connect: %w", err) } - innerConn := newInnerConn(conn) - for _, opt := range opts { - opt(innerConn) - } - c.innerConn = innerConn + c.innerConn = newInnerConn(conn, timeout[0]) return } @@ -260,9 +259,9 @@ func (c *UsbmuxClient) InnerConn() InnerConn { return c.innerConn } -func rawDial() (net.Conn, error) { +func rawDial(timeout time.Duration) (net.Conn, error) { dialer := net.Dialer{ - Timeout: DefaultDeadlineTimeout, + Timeout: timeout, } var network, address string @@ -278,14 +277,6 @@ func rawDial() (net.Conn, error) { return dialer.Dial(network, address) } -type InnerConnOption func(innerConn InnerConn) - -func TimeoutOption(duration time.Duration) InnerConnOption { - return func(innerConn InnerConn) { - innerConn.Timeout(duration) - } -} - type InnerConn interface { Write(data []byte) (err error) Read(length int) (data []byte, err error) @@ -296,10 +287,10 @@ type InnerConn interface { Timeout(time.Duration) } -func newInnerConn(conn net.Conn) InnerConn { +func newInnerConn(conn net.Conn, timeout time.Duration) InnerConn { return &safeConn{ conn: conn, - timeout: DefaultDeadlineTimeout, + timeout: timeout, } }