Skip to content

Commit

Permalink
feat: [NewConnect] add timeout setting (Zero means no limit. The defa…
Browse files Browse the repository at this point in the history
…ult timeout is 30s)
  • Loading branch information
electricbubble committed Apr 17, 2021
1 parent b3919be commit f6c6a23
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 22 deletions.
4 changes: 2 additions & 2 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion idevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 10 additions & 19 deletions pkg/libimobiledevice/usbmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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,
}
}

Expand Down

0 comments on commit f6c6a23

Please sign in to comment.