PacketTunnelProvider: fix locking logic
This commit is contained in:
parent
77d96b21a3
commit
809cb2ef78
|
@ -192,22 +192,19 @@ class WireGuardContext {
|
|||
readPacketCondition.signal()
|
||||
}
|
||||
|
||||
func readPacket(isTunnelClosed: inout Bool) -> NEPacket? {
|
||||
if outboundPackets.isEmpty {
|
||||
let readPacketCondition = NSCondition()
|
||||
readPacketCondition.lock()
|
||||
var packetsObtained: [NEPacket]?
|
||||
packetFlow.readPacketObjects { (packets: [NEPacket]) in
|
||||
packetsObtained = packets
|
||||
func packetsRead(packets: [NEPacket]) {
|
||||
outboundPackets.append(contentsOf: packets)
|
||||
readPacketCondition.signal()
|
||||
}
|
||||
|
||||
func readPacket(isTunnelClosed: inout Bool) -> NEPacket? {
|
||||
if outboundPackets.isEmpty {
|
||||
readPacketCondition.lock()
|
||||
packetFlow.readPacketObjects(completionHandler: packetsRead)
|
||||
// Wait till the completion handler of packetFlow.readPacketObjects() finishes
|
||||
while packetsObtained == nil && !self.isTunnelClosed {
|
||||
while outboundPackets.isEmpty && !self.isTunnelClosed {
|
||||
readPacketCondition.wait()
|
||||
}
|
||||
if let packetsObtained = packetsObtained {
|
||||
outboundPackets = packetsObtained
|
||||
}
|
||||
readPacketCondition.unlock()
|
||||
}
|
||||
isTunnelClosed = self.isTunnelClosed
|
||||
|
|
Loading…
Reference in New Issue