Split GenericSocket and LinkInterface

Keep socket and link logic separated. Allows for setting MTU
specifically for the link.

- UDP: hardcoded 1000
- TCP: ignored (.max)
This commit is contained in:
Davide De Rosa 2018-08-24 12:31:08 +02:00
parent 684b3b6c3d
commit 6208fe8e21
4 changed files with 45 additions and 19 deletions

View File

@ -9,7 +9,7 @@
import Foundation
protocol LinkProducer {
func link() -> LinkInterface
func link(withMTU mtu: Int) -> LinkInterface
}
protocol GenericSocketDelegate: class {

View File

@ -12,16 +12,13 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
class NETCPInterface: NSObject, GenericSocket, LinkInterface {
class NETCPInterface: NSObject, GenericSocket {
private static var linkContext = 0
private let impl: NWTCPConnection
private let maxPacketSize: Int
init(impl: NWTCPConnection, maxPacketSize: Int? = nil) {
init(impl: NWTCPConnection) {
self.impl = impl
self.maxPacketSize = maxPacketSize ?? (512 * 1024)
isActive = false
isShutdown = false
}
@ -79,8 +76,8 @@ class NETCPInterface: NSObject, GenericSocket, LinkInterface {
return NETCPInterface(impl: NWTCPConnection(upgradeFor: impl))
}
func link() -> LinkInterface {
return self
func link(withMTU mtu: Int) -> LinkInterface {
return NETCPLinkInterface(impl: impl)
}
// MARK: Connection KVO (any queue)
@ -148,12 +145,28 @@ class NETCPInterface: NSObject, GenericSocket, LinkInterface {
break
}
}
}
class NETCPLinkInterface: LinkInterface {
private let impl: NWTCPConnection
private let maxPacketSize: Int
init(impl: NWTCPConnection, maxPacketSize: Int? = nil) {
self.impl = impl
self.mtu = .max
self.maxPacketSize = maxPacketSize ?? (512 * 1024)
}
// MARK: LinkInterface
let isReliable: Bool = true
let mtu: Int = .max
var remoteAddress: String? {
return (impl.remoteAddress as? NWHostEndpoint)?.hostname
}
let mtu: Int
var packetBufferSize: Int {
return maxPacketSize

View File

@ -12,16 +12,13 @@ import SwiftyBeaver
private let log = SwiftyBeaver.self
class NEUDPInterface: NSObject, GenericSocket, LinkInterface {
class NEUDPInterface: NSObject, GenericSocket {
private static var linkContext = 0
private let impl: NWUDPSession
private let maxDatagrams: Int
init(impl: NWUDPSession, maxDatagrams: Int? = nil) {
init(impl: NWUDPSession) {
self.impl = impl
self.maxDatagrams = maxDatagrams ?? 200
isActive = false
isShutdown = false
@ -78,8 +75,8 @@ class NEUDPInterface: NSObject, GenericSocket, LinkInterface {
return NEUDPInterface(impl: NWUDPSession(upgradeFor: impl))
}
func link() -> LinkInterface {
return self
func link(withMTU mtu: Int) -> LinkInterface {
return NEUDPLinkInterface(impl: impl, mtu: mtu)
}
// MARK: Connection KVO (any queue)
@ -150,12 +147,28 @@ class NEUDPInterface: NSObject, GenericSocket, LinkInterface {
break
}
}
}
class NEUDPLinkInterface: LinkInterface {
private let impl: NWUDPSession
private let maxDatagrams: Int
init(impl: NWUDPSession, mtu: Int, maxDatagrams: Int? = nil) {
self.impl = impl
self.mtu = mtu
self.maxDatagrams = maxDatagrams ?? 200
}
// MARK: LinkInterface
let isReliable: Bool = false
let mtu: Int = 1000
var remoteAddress: String? {
return (impl.resolvedEndpoint as? NWHostEndpoint)?.hostname
}
let mtu: Int
var packetBufferSize: Int {
return maxDatagrams

View File

@ -335,10 +335,10 @@ extension TunnelKitProvider: GenericSocketDelegate {
return
}
if proxy.canRebindLink() {
proxy.rebindLink(socket.link())
proxy.rebindLink(socket.link(withMTU: 1000))
reasserting = false
} else {
proxy.setLink(socket.link())
proxy.setLink(socket.link(withMTU: 1000))
}
}