blob: f0886e5fb57f4f3afdd5b32e7da401ca1bdac0c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
//
// UDPConnection.swift
// MullvadRustRuntimeTests
//
// Created by pronebird on 27/06/2023.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import Foundation
import Network
/// Minimal implementation of UDP connection capable of sending data.
/// > Warning: Do not use this implementation in production code. See the warning in `start()`.
class UDPConnection {
private let dispatchQueue = DispatchQueue(label: "UDPConnection")
private let nwConnection: NWConnection
init(remote: IPAddress, port: UInt16) {
nwConnection = NWConnection(
host: NWEndpoint.Host("\(remote)"),
port: NWEndpoint.Port(integerLiteral: port),
using: .udp
)
}
deinit {
cancel()
}
/// Establishes the UDP connection.
///
/// > Warning: This implementation is **not safe to use in production**
/// It will cancel the `listener.stateUpdateHandler` after it becomes ready and ignore future updates.
///
/// Waits for the underlying connection to become ready before returning control to the caller, otherwise throws an
/// error if connection state indicates as such.
func start() async throws {
return try await withCheckedThrowingContinuation { continuation in
nwConnection.stateUpdateHandler = { state in
switch state {
case .ready:
continuation.resume(returning: ())
case let .failed(error):
continuation.resume(throwing: error)
case .cancelled:
continuation.resume(throwing: CancellationError())
default:
return
}
// Reset state update handler after resuming continuation.
self.nwConnection.stateUpdateHandler = nil
}
nwConnection.start(queue: dispatchQueue)
}
}
func cancel() {
nwConnection.cancel()
}
func sendData(_ data: Data) async throws {
return try await withCheckedThrowingContinuation { continuation in
nwConnection.send(content: data, completion: .contentProcessed { error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: ())
}
})
}
}
}
|