tunnelkit/TunnelKit/Sources/Protocols/OpenVPN/AppExtension/NEUDPLink.swift

83 lines
2.3 KiB
Swift
Raw Normal View History

//
// NEUDPLink.swift
// TunnelKit
//
// Created by Davide De Rosa on 5/23/19.
2020-12-27 16:29:39 +00:00
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of TunnelKit.
//
// TunnelKit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TunnelKit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
//
import Foundation
import NetworkExtension
class NEUDPLink: LinkInterface {
private let impl: NWUDPSession
private let maxDatagrams: Int
init(impl: NWUDPSession, maxDatagrams: Int? = nil) {
self.impl = impl
self.maxDatagrams = maxDatagrams ?? 200
}
// MARK: LinkInterface
let isReliable: Bool = false
var remoteAddress: String? {
return (impl.resolvedEndpoint as? NWHostEndpoint)?.hostname
}
var packetBufferSize: Int {
return maxDatagrams
}
func setReadHandler(queue: DispatchQueue, _ handler: @escaping ([Data]?, Error?) -> Void) {
// WARNING: runs in Network.framework queue
impl.setReadHandler({ [weak self] (packets, error) in
guard let _ = self else {
return
}
queue.sync {
handler(packets, error)
}
}, maxDatagrams: maxDatagrams)
}
func writePacket(_ packet: Data, completionHandler: ((Error?) -> Void)?) {
impl.writeDatagram(packet) { (error) in
completionHandler?(error)
}
}
func writePackets(_ packets: [Data], completionHandler: ((Error?) -> Void)?) {
impl.writeMultipleDatagrams(packets) { (error) in
completionHandler?(error)
}
}
}
2019-05-23 19:49:20 +00:00
/// :nodoc:
extension NEUDPSocket: LinkProducer {
public func link() -> LinkInterface {
return NEUDPLink(impl: impl)
}
}