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 import Foundation
protocol LinkProducer { protocol LinkProducer {
func link() -> LinkInterface func link(withMTU mtu: Int) -> LinkInterface
} }
protocol GenericSocketDelegate: class { protocol GenericSocketDelegate: class {

View File

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

View File

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

View File

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