Skip to content

Commit 05cd394

Browse files
committed
Added a buffered reader for dtx connections
1 parent afe9077 commit 05cd394

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

ios/deviceconnection.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ios
22

33
import (
4+
"bufio"
45
"crypto/tls"
56
"encoding/binary"
67
"io"
@@ -11,6 +12,8 @@ import (
1112
log "github.com/sirupsen/logrus"
1213
)
1314

15+
const readerBufferSize = 40 * 1024
16+
1417
// DeviceConnectionInterface contains a physical network connection to a usbmuxd socket.
1518
type DeviceConnectionInterface interface {
1619
Close() error
@@ -31,11 +34,12 @@ type DeviceConnectionInterface interface {
3134
type DeviceConnection struct {
3235
c net.Conn
3336
unencryptedConn net.Conn
37+
bufferedReader io.Reader
3438
}
3539

3640
// Read reads incoming data from the connection to the device
3741
func (conn *DeviceConnection) Read(p []byte) (n int, err error) {
38-
return conn.c.Read(p)
42+
return conn.bufferedReader.Read(p)
3943
}
4044

4145
// Write writes data on the connection to the device
@@ -51,7 +55,8 @@ func NewDeviceConnection(socketToConnectTo string) (*DeviceConnection, error) {
5155

5256
// NewDeviceConnectionWithConn create a DeviceConnection with a already connected network conn.
5357
func NewDeviceConnectionWithConn(conn net.Conn) *DeviceConnection {
54-
return &DeviceConnection{c: conn}
58+
bufferedReader := bufio.NewReaderSize(conn, readerBufferSize)
59+
return &DeviceConnection{c: conn, bufferedReader: bufferedReader}
5560
}
5661

5762
// ConnectToSocketAddress connects to the USB multiplexer with a specified socket addres
@@ -66,6 +71,7 @@ func (conn *DeviceConnection) connectToSocketAddress(socketAddress string) error
6671
}
6772
log.Tracef("Opening connection: %v", &c)
6873
conn.c = c
74+
conn.bufferedReader = bufio.NewReaderSize(c, readerBufferSize)
6975
return nil
7076
}
7177

@@ -117,6 +123,7 @@ func (conn *DeviceConnection) DisableSessionSSL() {
117123
}
118124
// Use the underlying conn again to receive unencrypted bytes
119125
conn.c = conn.unencryptedConn
126+
conn.bufferedReader = bufio.NewReaderSize(conn.c, readerBufferSize)
120127
// tls.Conn.CloseWrite() sets the writeDeadline to now, which will cause
121128
// all writes to timeout immediately, for this hacky workaround
122129
// we need to undo that
@@ -155,6 +162,7 @@ func (conn *DeviceConnection) EnableSessionSslServerMode(pairRecord PairRecord)
155162

156163
conn.unencryptedConn = conn.c
157164
conn.c = net.Conn(tlsConn)
165+
conn.bufferedReader = bufio.NewReaderSize(conn.c, readerBufferSize)
158166
return nil
159167
}
160168

@@ -173,6 +181,7 @@ func (conn *DeviceConnection) EnableSessionSsl(pairRecord PairRecord) error {
173181
}
174182
conn.unencryptedConn = conn.c
175183
conn.c = net.Conn(tlsConn)
184+
conn.bufferedReader = bufio.NewReaderSize(conn.c, readerBufferSize)
176185
return nil
177186
}
178187

0 commit comments

Comments
 (0)