@@ -19,7 +19,7 @@ class ClientAppProxyConnection : Connection {
19
19
let appProxyFlow : NEAppProxyFlow
20
20
21
21
/// A dispatch queue used to regulate the sending of the connection's data through the tunnel connection.
22
- lazy var queue : dispatch_queue_t = dispatch_queue_create ( " ClientConnection Handle Data queue " , nil )
22
+ lazy var queue : DispatchQueue = DispatchQueue ( label : " ClientConnection Handle Data queue " , attributes : [ ] )
23
23
24
24
// MARK: Initializers
25
25
@@ -36,16 +36,16 @@ class ClientAppProxyConnection : Connection {
36
36
}
37
37
38
38
/// Send an "Open" message to the SimpleTunnel server, to begin the process of establishing a flow of data in the SimpleTunnel protocol.
39
- func open( extraProperties: [ String : AnyObject ] ) {
39
+ func open( _ extraProperties: [ String : AnyObject ] ) {
40
40
guard let clientTunnel = tunnel as? ClientTunnel else {
41
41
// Close the NEAppProxyFlow.
42
- let error : SimpleTunnelError = . BadConnection
42
+ let error : SimpleTunnelError = . badConnection
43
43
appProxyFlow. closeReadWithError ( error as NSError )
44
44
appProxyFlow. closeWriteWithError ( error as NSError )
45
45
return
46
46
}
47
47
48
- let properties = createMessagePropertiesForConnection ( identifier, commandType: . Open , extraProperties: extraProperties)
48
+ let properties = createMessagePropertiesForConnection ( identifier, commandType: . open , extraProperties: extraProperties)
49
49
50
50
clientTunnel. sendMessage ( properties) { error in
51
51
if let error = error {
@@ -57,37 +57,37 @@ class ClientAppProxyConnection : Connection {
57
57
}
58
58
59
59
/// Handle the result of sending a data message to the SimpleTunnel server.
60
- func handleSendResult( error: NSError ? ) {
60
+ func handleSendResult( _ error: NSError ? ) {
61
61
}
62
62
63
63
/// Handle errors that occur on the connection.
64
- func handleErrorCondition( flowError: NEAppProxyFlowError ? = nil , notifyServer: Bool = true ) {
64
+ func handleErrorCondition( _ flowError: NEAppProxyErrorDomain ? = nil , notifyServer: Bool = true ) {
65
65
66
66
guard !isClosedCompletely else { return }
67
67
68
- tunnel? . sendCloseType ( . All , forConnection: identifier)
68
+ tunnel? . sendCloseType ( . all , forConnection: identifier)
69
69
70
- closeConnection ( . All )
70
+ closeConnection ( . all )
71
71
}
72
72
73
73
/// Send a "Data" message to the SimpleTunnel server.
74
- func sendDataMessage( data: NSData , extraProperties: [ String : AnyObject ] = [ : ] ) {
75
- dispatch_async ( queue) {
74
+ func sendDataMessage( _ data: Data , extraProperties: [ String : AnyObject ] = [ : ] ) {
75
+ queue. async {
76
76
77
77
guard let clientTunnel = self . tunnel as? ClientTunnel else { return }
78
78
79
79
// Suspend further writes to the tunnel until this write operation is completed.
80
- dispatch_suspend ( self . queue)
80
+ self . queue. suspend ( )
81
81
82
82
var dataProperties = extraProperties
83
- dataProperties [ TunnelMessageKey . Data. rawValue] = data
83
+ dataProperties [ TunnelMessageKey . Data. rawValue] = data as AnyObject ?
84
84
85
- let properties = createMessagePropertiesForConnection ( self . identifier, commandType: . Data , extraProperties: dataProperties)
85
+ let properties = createMessagePropertiesForConnection ( self . identifier, commandType: . data , extraProperties: dataProperties)
86
86
87
87
clientTunnel. sendMessage ( properties) { error in
88
88
89
89
// Resume the queue to allow subsequent writes.
90
- dispatch_resume ( self . queue)
90
+ self . queue. resume ( )
91
91
92
92
// This will schedule another read operation on the NEAppProxyFlow.
93
93
self . handleSendResult ( error)
@@ -98,30 +98,30 @@ class ClientAppProxyConnection : Connection {
98
98
// MARK: Connection
99
99
100
100
/// Handle the "Open Completed" message received from the SimpleTunnel server for this connection.
101
- override func handleOpenCompleted( resultCode: TunnelConnectionOpenResult , properties: [ NSObject : AnyObject ] ) {
102
- guard resultCode == . Success else {
101
+ override func handleOpenCompleted( _ resultCode: TunnelConnectionOpenResult , properties: [ NSObject : AnyObject ] ) {
102
+ guard resultCode == . success else {
103
103
simpleTunnelLog ( " Failed to open \( identifier) , result = \( resultCode) " )
104
- handleErrorCondition ( . PeerReset , notifyServer: false )
104
+ handleErrorCondition ( . peerReset , notifyServer: false )
105
105
return
106
106
}
107
107
108
108
guard let localAddress = ( tunnel as? ClientTunnel ) ? . connection!. localAddress as? NWHostEndpoint else {
109
109
simpleTunnelLog ( " Failed to get localAddress. " )
110
- handleErrorCondition ( . Internal )
110
+ handleErrorCondition ( . internal )
111
111
return
112
112
}
113
113
114
114
// Now that the SimpleTunnel connection is open, indicate that we are ready to handle data on the NEAppProxyFlow.
115
- appProxyFlow. openWithLocalEndpoint ( localAddress) { error in
116
- self . handleSendResult ( error)
115
+ appProxyFlow. open ( withLocalEndpoint : localAddress) { error in
116
+ self . handleSendResult ( error as NSError ? )
117
117
}
118
118
}
119
119
120
- override func closeConnection( direction: TunnelConnectionCloseDirection ) {
120
+ override func closeConnection( _ direction: TunnelConnectionCloseDirection ) {
121
121
self . closeConnection ( direction, flowError: nil )
122
122
}
123
123
124
- func closeConnection( direction: TunnelConnectionCloseDirection , flowError: NEAppProxyFlowError ? ) {
124
+ func closeConnection( _ direction: TunnelConnectionCloseDirection , flowError: NEAppProxyErrorDomain ? ) {
125
125
super. closeConnection ( direction)
126
126
127
127
var error : NSError ?
@@ -159,32 +159,32 @@ class ClientAppProxyTCPConnection : ClientAppProxyConnection {
159
159
/// Send an "Open" message to the SimpleTunnel server, to begin the process of establishing a flow of data in the SimpleTunnel protocol.
160
160
override func open( ) {
161
161
open ( [
162
- TunnelMessageKey . TunnelType. rawValue: TunnelLayer . App . rawValue,
163
- TunnelMessageKey . Host. rawValue: ( TCPFlow . remoteEndpoint as! NWHostEndpoint ) . hostname,
164
- TunnelMessageKey . Port. rawValue: Int ( ( TCPFlow . remoteEndpoint as! NWHostEndpoint ) . port) !,
165
- TunnelMessageKey . AppProxyFlowType. rawValue: AppProxyFlowKind . TCP . rawValue
162
+ TunnelMessageKey . TunnelType. rawValue: TunnelLayer . app . rawValue as AnyObject ,
163
+ TunnelMessageKey . Host. rawValue: ( TCPFlow . remoteEndpoint as! NWHostEndpoint ) . hostname as AnyObject ,
164
+ TunnelMessageKey . Port. rawValue: Int ( ( TCPFlow . remoteEndpoint as! NWHostEndpoint ) . port) ! as AnyObject ,
165
+ TunnelMessageKey . AppProxyFlowType. rawValue: AppProxyFlowKind . tcp . rawValue as AnyObject
166
166
] )
167
167
}
168
168
169
169
/// Handle the result of sending a "Data" message to the SimpleTunnel server.
170
- override func handleSendResult( error: NSError ? ) {
170
+ override func handleSendResult( _ error: NSError ? ) {
171
171
if let sendError = error {
172
172
simpleTunnelLog ( " Failed to send Data Message to the Tunnel Server. error = \( sendError) " )
173
- handleErrorCondition ( . HostUnreachable )
173
+ handleErrorCondition ( . hostUnreachable )
174
174
return
175
175
}
176
176
177
177
// Read another chunk of data from the source application.
178
- TCPFlow . readDataWithCompletionHandler { data, readError in
179
- guard let readData = data where readError == nil else {
178
+ TCPFlow . readData { data, readError in
179
+ guard let readData = data , readError == nil else {
180
180
simpleTunnelLog ( " Failed to read data from the TCP flow. error = \( readError) " )
181
- self . handleErrorCondition ( . PeerReset )
181
+ self . handleErrorCondition ( . peerReset )
182
182
return
183
183
}
184
184
185
- guard readData. length > 0 else {
185
+ guard readData. count > 0 else {
186
186
simpleTunnelLog ( " \( self . identifier) : received EOF on the TCP flow. Closing the flow... " )
187
- self . tunnel? . sendCloseType ( . Write , forConnection: self . identifier)
187
+ self . tunnel? . sendCloseType ( . write , forConnection: self . identifier)
188
188
self . TCPFlow. closeReadWithError ( nil )
189
189
return
190
190
}
@@ -194,11 +194,11 @@ class ClientAppProxyTCPConnection : ClientAppProxyConnection {
194
194
}
195
195
196
196
/// Send data received from the SimpleTunnel server to the destination application, using the NEAppProxyTCPFlow object.
197
- override func sendData( data: NSData ) {
198
- TCPFlow . writeData ( data) { error in
197
+ override func sendData( _ data: Data ) {
198
+ TCPFlow . write ( data) { error in
199
199
if let writeError = error {
200
200
simpleTunnelLog ( " Failed to write data to the TCP flow. error = \( writeError) " )
201
- self . tunnel? . sendCloseType ( . Read , forConnection: self . identifier)
201
+ self . tunnel? . sendCloseType ( . read , forConnection: self . identifier)
202
202
self . TCPFlow. closeWriteWithError ( nil )
203
203
}
204
204
}
@@ -229,17 +229,17 @@ class ClientAppProxyUDPConnection : ClientAppProxyConnection {
229
229
/// Send an "Open" message to the SimpleTunnel server, to begin the process of establishing a flow of data in the SimpleTunnel protocol.
230
230
override func open( ) {
231
231
open ( [
232
- TunnelMessageKey . TunnelType. rawValue: TunnelLayer . App . rawValue,
233
- TunnelMessageKey . AppProxyFlowType. rawValue: AppProxyFlowKind . UDP . rawValue
232
+ TunnelMessageKey . TunnelType. rawValue: TunnelLayer . app . rawValue as AnyObject ,
233
+ TunnelMessageKey . AppProxyFlowType. rawValue: AppProxyFlowKind . udp . rawValue as AnyObject
234
234
] )
235
235
}
236
236
237
237
/// Handle the result of sending a "Data" message to the SimpleTunnel server.
238
- override func handleSendResult( error: NSError ? ) {
238
+ override func handleSendResult( _ error: NSError ? ) {
239
239
240
240
if let sendError = error {
241
241
simpleTunnelLog ( " Failed to send message to Tunnel Server. error = \( sendError) " )
242
- handleErrorCondition ( . HostUnreachable )
242
+ handleErrorCondition ( . hostUnreachable )
243
243
return
244
244
}
245
245
@@ -251,50 +251,50 @@ class ClientAppProxyUDPConnection : ClientAppProxyConnection {
251
251
guard datagramsOutstanding == 0 else { return }
252
252
253
253
// Read a new set of datagrams from the source application.
254
- UDPFlow . readDatagramsWithCompletionHandler { datagrams, remoteEndPoints, readError in
254
+ UDPFlow . readDatagrams { datagrams, remoteEndPoints, readError in
255
255
256
256
guard let readDatagrams = datagrams,
257
- readEndpoints = remoteEndPoints
258
- where readError == nil else
257
+ let readEndpoints = remoteEndPoints
258
+ , readError == nil else
259
259
{
260
260
simpleTunnelLog ( " Failed to read data from the UDP flow. error = \( readError) " )
261
- self . handleErrorCondition ( . PeerReset )
261
+ self . handleErrorCondition ( . peerReset )
262
262
return
263
263
}
264
264
265
265
guard !readDatagrams. isEmpty && readEndpoints. count == readDatagrams. count else {
266
266
simpleTunnelLog ( " \( self . identifier) : Received EOF on the UDP flow. Closing the flow... " )
267
- self . tunnel? . sendCloseType ( . Write , forConnection: self . identifier)
267
+ self . tunnel? . sendCloseType ( . write , forConnection: self . identifier)
268
268
self . UDPFlow. closeReadWithError ( nil )
269
269
return
270
270
}
271
271
272
272
self . datagramsOutstanding = readDatagrams. count
273
273
274
- for (index, datagram) in readDatagrams. enumerate ( ) {
274
+ for (index, datagram) in readDatagrams. enumerated ( ) {
275
275
guard let endpoint = readEndpoints [ index] as? NWHostEndpoint else { continue }
276
276
277
- simpleTunnelLog ( " ( \( self . identifier) ): Sending a \( datagram. length ) -byte datagram to \( endpoint. hostname) : \( endpoint. port) " )
277
+ simpleTunnelLog ( " ( \( self . identifier) ): Sending a \( datagram. count ) -byte datagram to \( endpoint. hostname) : \( endpoint. port) " )
278
278
279
279
// Send a data message to the SimpleTunnel server.
280
280
self . sendDataMessage ( datagram, extraProperties: [
281
- TunnelMessageKey . Host. rawValue: endpoint. hostname,
282
- TunnelMessageKey . Port. rawValue: Int ( endpoint. port) !
281
+ TunnelMessageKey . Host. rawValue: endpoint. hostname as AnyObject ,
282
+ TunnelMessageKey . Port. rawValue: Int ( endpoint. port) ! as AnyObject
283
283
] )
284
284
}
285
285
}
286
286
}
287
287
288
288
/// Send a datagram received from the SimpleTunnel server to the destination application.
289
- override func sendDataWithEndPoint( data: NSData , host: String , port: Int ) {
289
+ override func sendDataWithEndPoint( _ data: Data , host: String , port: Int ) {
290
290
let datagrams = [ data ]
291
291
let endpoints = [ NWHostEndpoint ( hostname: host, port: String ( port) ) ]
292
292
293
293
// Send the datagram to the destination application.
294
- UDPFlow . writeDatagrams ( datagrams, sentByEndpoints : endpoints) { error in
294
+ UDPFlow . writeDatagrams ( datagrams, sentBy : endpoints) { error in
295
295
if let error = error {
296
296
simpleTunnelLog ( " Failed to write datagrams to the UDP Flow: \( error) " )
297
- self . tunnel? . sendCloseType ( . Read , forConnection: self . identifier)
297
+ self . tunnel? . sendCloseType ( . read , forConnection: self . identifier)
298
298
self . UDPFlow. closeWriteWithError ( nil )
299
299
}
300
300
}
0 commit comments