1
1
package ios
2
2
3
3
import (
4
+ "bufio"
4
5
"crypto/tls"
5
6
"encoding/binary"
6
7
"io"
@@ -11,6 +12,8 @@ import (
11
12
log "github.com/sirupsen/logrus"
12
13
)
13
14
15
+ const readerBufferSize = 40 * 1024
16
+
14
17
// DeviceConnectionInterface contains a physical network connection to a usbmuxd socket.
15
18
type DeviceConnectionInterface interface {
16
19
Close () error
@@ -31,11 +34,12 @@ type DeviceConnectionInterface interface {
31
34
type DeviceConnection struct {
32
35
c net.Conn
33
36
unencryptedConn net.Conn
37
+ bufferedReader io.Reader
34
38
}
35
39
36
40
// Read reads incoming data from the connection to the device
37
41
func (conn * DeviceConnection ) Read (p []byte ) (n int , err error ) {
38
- return conn .c .Read (p )
42
+ return conn .bufferedReader .Read (p )
39
43
}
40
44
41
45
// Write writes data on the connection to the device
@@ -51,7 +55,8 @@ func NewDeviceConnection(socketToConnectTo string) (*DeviceConnection, error) {
51
55
52
56
// NewDeviceConnectionWithConn create a DeviceConnection with a already connected network conn.
53
57
func NewDeviceConnectionWithConn (conn net.Conn ) * DeviceConnection {
54
- return & DeviceConnection {c : conn }
58
+ bufferedReader := bufio .NewReaderSize (conn , readerBufferSize )
59
+ return & DeviceConnection {c : conn , bufferedReader : bufferedReader }
55
60
}
56
61
57
62
// ConnectToSocketAddress connects to the USB multiplexer with a specified socket addres
@@ -66,6 +71,7 @@ func (conn *DeviceConnection) connectToSocketAddress(socketAddress string) error
66
71
}
67
72
log .Tracef ("Opening connection: %v" , & c )
68
73
conn .c = c
74
+ conn .bufferedReader = bufio .NewReaderSize (c , readerBufferSize )
69
75
return nil
70
76
}
71
77
@@ -117,6 +123,7 @@ func (conn *DeviceConnection) DisableSessionSSL() {
117
123
}
118
124
// Use the underlying conn again to receive unencrypted bytes
119
125
conn .c = conn .unencryptedConn
126
+ conn .bufferedReader = bufio .NewReaderSize (conn .c , readerBufferSize )
120
127
// tls.Conn.CloseWrite() sets the writeDeadline to now, which will cause
121
128
// all writes to timeout immediately, for this hacky workaround
122
129
// we need to undo that
@@ -155,6 +162,7 @@ func (conn *DeviceConnection) EnableSessionSslServerMode(pairRecord PairRecord)
155
162
156
163
conn .unencryptedConn = conn .c
157
164
conn .c = net .Conn (tlsConn )
165
+ conn .bufferedReader = bufio .NewReaderSize (conn .c , readerBufferSize )
158
166
return nil
159
167
}
160
168
@@ -173,6 +181,7 @@ func (conn *DeviceConnection) EnableSessionSsl(pairRecord PairRecord) error {
173
181
}
174
182
conn .unencryptedConn = conn .c
175
183
conn .c = net .Conn (tlsConn )
184
+ conn .bufferedReader = bufio .NewReaderSize (conn .c , readerBufferSize )
176
185
return nil
177
186
}
178
187
0 commit comments