-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathMultipeerConnection.swift
More file actions
319 lines (240 loc) · 11.9 KB
/
MultipeerConnection.swift
File metadata and controls
319 lines (240 loc) · 11.9 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import Foundation
import MultipeerConnectivity
import os.log
/// The completion handler called when the remote peer responds to a manual invite initiated
/// by calling ``MultipeerTransceiver/invite(_:with:timeout:completion:)``.
public typealias InvitationCompletionHandler = (_ result: Result<Peer, Error>) -> Void
public struct MultipeerError: LocalizedError {
public var localizedDescription: String
}
final class MultipeerConnection: NSObject, MultipeerProtocol {
enum Mode: Int, CaseIterable {
case receiver
case transmitter
}
private let log = MultipeerKit.log(for: MultipeerConnection.self)
let modes: [Mode]
let configuration: MultipeerConfiguration
let me: MCPeerID
init(modes: [Mode] = Mode.allCases, configuration: MultipeerConfiguration = .default) {
self.modes = modes
self.configuration = configuration
self.me = MCPeerID.fetchOrCreate(with: configuration)
}
var didReceiveData: ((Data, Peer) -> Void)?
var didFindPeer: ((Peer) -> Void)?
var didLosePeer: ((Peer) -> Void)?
var didConnectToPeer: ((Peer) -> Void)?
var didDisconnectFromPeer: ((Peer) -> Void)?
var didStartReceivingResource: ((_ sender: Peer, _ resourceName: String, _ progress: Progress) -> Void)?
var didFinishReceivingResource: ((_ sender: Peer, _ resourceName: String, _ result: Result<URL, Error>) -> Void)?
private var discoveredPeers: [MCPeerID: Peer] = [:]
func resume() {
os_log("%{public}@", log: log, type: .debug, #function)
if modes.contains(.transmitter) {
// Ideally, we'd just keep using the same browser for the lifetime of the MultipeerConnection object.
// However, due to #12, we can't. The same process is done with the advertiser for consistency.
browser = makeBrowser()
browser.startBrowsingForPeers()
}
if modes.contains(.receiver) {
advertiser = makeAdvertiser()
advertiser.startAdvertisingPeer()
}
}
func stop() {
os_log("%{public}@", log: log, type: .debug, #function)
if modes.contains(.receiver) {
advertiser.stopAdvertisingPeer()
}
if modes.contains(.transmitter) {
browser.stopBrowsingForPeers()
}
}
private lazy var session: MCSession = {
let s = MCSession(
peer: me,
securityIdentity: configuration.security.identity,
encryptionPreference: configuration.security.encryptionPreference
)
s.delegate = self
return s
}()
private func makeBrowser() -> MCNearbyServiceBrowser {
let b = MCNearbyServiceBrowser(peer: me, serviceType: configuration.serviceType)
b.delegate = self
return b
}
private lazy var browser: MCNearbyServiceBrowser = { makeBrowser() }()
private func makeAdvertiser() -> MCNearbyServiceAdvertiser {
let a = MCNearbyServiceAdvertiser(peer: me, discoveryInfo: nil, serviceType: configuration.serviceType)
a.delegate = self
return a
}
private lazy var advertiser: MCNearbyServiceAdvertiser = { makeAdvertiser() }()
func broadcast(_ data: Data) throws {
guard !session.connectedPeers.isEmpty else {
os_log("Not broadcasting message: no connected peers", log: self.log, type: .error)
return
}
try session.send(data, toPeers: session.connectedPeers, with: .reliable)
}
func send(_ data: Data, to peers: [Peer]) throws {
let ids = peers.map { $0.underlyingPeer }
try session.send(data, toPeers: ids, with: .reliable)
}
@available(iOS 13.0, tvOS 13.0, macOS 10.15, *)
func send(_ resourceURL: URL, to peer: Peer) -> MultipeerTransceiver.ResourceUploadStream {
AsyncThrowingStream { [weak self] continuation in
guard let self else {
return continuation.finish(throwing: CancellationError())
}
guard resourceURL.isFileURL else {
return continuation.finish(throwing: MultipeerError(localizedDescription: "Resource must be a local file URL, remote URLs are not supported."))
}
let peerID = peer.underlyingPeer
/// Ensure resource names are unique even if the file name is the same, while keeping the file name to aid in debugging.
let resourceID = "\(UUID().uuidString)-\(resourceURL.lastPathComponent)"
let progress = session.sendResource(at: resourceURL, withName: resourceID, toPeer: peerID) { error in
if let error {
continuation.finish(throwing: error)
} else {
continuation.finish()
}
}
guard let progress else {
return continuation.finish(throwing: MultipeerError(localizedDescription: "Failed to start resource upload."))
}
let cancellable = progress.publisher(for: \.fractionCompleted).sink { progress in
continuation.yield(progress)
}
continuation.onTermination = { @Sendable _ in
cancellable.cancel()
}
}
}
private var invitationCompletionHandlers: [MCPeerID: InvitationCompletionHandler] = [:]
func invite(_ peer: Peer, with context: Data?, timeout: TimeInterval, completion: InvitationCompletionHandler?) {
invitationCompletionHandlers[peer.underlyingPeer] = completion
browser.invitePeer(peer.underlyingPeer, to: session, withContext: context, timeout: timeout)
}
func getLocalPeer() -> Peer? {
return try? Peer(peer: me, discoveryInfo: nil)
}
}
// MARK: - Session delegate
extension MultipeerConnection: MCSessionDelegate {
func session(_ session: MCSession, peer peerID: MCPeerID, didChange state: MCSessionState) {
os_log("%{public}@", log: log, type: .debug, #function)
DispatchQueue.main.async {
guard let peer = self.discoveredPeers[peerID] else { return }
let handler = self.invitationCompletionHandlers[peerID]
switch state {
case .connected:
handler?(.success(peer))
self.invitationCompletionHandlers[peerID] = nil
self.didConnectToPeer?(peer)
case .notConnected:
handler?(.failure(MultipeerError(localizedDescription: "Failed to connect to peer.")))
self.invitationCompletionHandlers[peerID] = nil
self.didDisconnectFromPeer?(peer)
case .connecting:
break
@unknown default:
break
}
}
}
func session(_ session: MCSession, didReceive data: Data, fromPeer peerID: MCPeerID) {
os_log("%{public}@", log: log, type: .debug, #function)
do {
let peer = try Peer(peer: peerID, discoveryInfo: nil)
didReceiveData?(data, peer)
} catch {
os_log("Received data, but cannot create peer for %@: %{public}@", log: log, type: .error, #function, peerID.displayName, String(describing: error))
}
}
func session(_ session: MCSession, didReceive stream: InputStream, withName streamName: String, fromPeer peerID: MCPeerID) {
os_log("%{public}@", log: log, type: .debug, #function)
}
func session(_ session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, with progress: Progress) {
os_log("%{public}@", log: log, type: .debug, #function)
do {
let peer = try Peer(peer: peerID, discoveryInfo: nil)
didStartReceivingResource?(peer, resourceName, progress)
} catch {
os_log("Started receiving resource, but cannot create peer for %@: %{public}@", log: log, type: .error, #function, peerID.displayName, String(describing: error))
}
}
func session(_ session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, at localURL: URL?, withError error: Error?) {
os_log("%{public}@", log: log, type: .debug, #function)
do {
let peer = try Peer(peer: peerID, discoveryInfo: nil)
let result: Result<URL, Error>
if let localURL {
result = .success(localURL)
} else {
result = .failure(error ?? MultipeerError(localizedDescription: "Unknown MultipeerConnectivity error."))
}
didFinishReceivingResource?(peer, resourceName, result)
} catch {
os_log("Finished receiving resource, but cannot create peer for %@: %{public}@", log: log, type: .error, #function, peerID.displayName, String(describing: error))
}
}
}
// MARK: - Browser delegate
extension MultipeerConnection: MCNearbyServiceBrowserDelegate {
func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
os_log("%{public}@", log: log, type: .debug, #function)
do {
let peer = try Peer(peer: peerID, discoveryInfo: info)
discoveredPeers[peerID] = peer
didFindPeer?(peer)
switch configuration.invitation {
case .automatic:
browser.invitePeer(peerID, to: session, withContext: nil, timeout: 10.0)
case .custom(let inviter):
guard let invite = try inviter(peer) else {
os_log("Custom invite not sent for peer %@", log: self.log, type: .error, String(describing: peer))
return
}
browser.invitePeer(
peerID,
to: session,
withContext: invite.context,
timeout: invite.timeout
)
case .none:
os_log("Auto-invite disabled", log: self.log, type: .debug)
return
}
} catch {
os_log("Failed to initialize peer based on peer ID %@: %{public}@", log: self.log, type: .error, String(describing: peerID), String(describing: error))
}
}
func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
os_log("%{public}@", log: log, type: .debug, #function)
guard let peer = discoveredPeers[peerID] else { return }
didLosePeer?(peer)
discoveredPeers[peerID] = nil
}
func browser(_ browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: Error) {
os_log("The multipeer connection failed to start browsing for peers. This could be due to missing keys in your app's Info.plist, check out the documentation at http://github.com/insidegui/MultipeerKit for more information. Error: %{public}@", log: log, type: .fault, String(describing: error))
}
}
// MARK: - Advertiser delegate
extension MultipeerConnection: MCNearbyServiceAdvertiserDelegate {
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {
os_log("%{public}@", log: log, type: .debug, #function)
DispatchQueue.main.async {
guard let peer = self.discoveredPeers[peerID] else { return }
self.configuration.security.invitationHandler(peer, context, { [weak self] decision in
guard let self = self else { return }
invitationHandler(decision, decision ? self.session : nil)
})
}
}
func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: Error) {
os_log("The multipeer connection failed to start advertising to peers. This could be due to missing keys in your app's Info.plist, check out the documentation at http://github.com/insidegui/MultipeerKit for more information. Error: %{public}@", log: log, type: .fault, String(describing: error))
}
}